]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/Clone.java
6c9e25a31a61f6ef7f9b2871b0434507baf21330
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / Clone.java
1 /** @file
2
3 The file is used to clone workspace, module, package and platform
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15 package org.tianocore.frameworkwizard;
16
17 import java.awt.event.ActionEvent;
18 import java.io.File;
19 import java.io.IOException;
20 import java.math.BigInteger;
21 import java.util.Vector;
22
23 import javax.swing.JFileChooser;
24 import javax.swing.JPanel;
25 import javax.swing.JLabel;
26 import javax.swing.JTextField;
27 import javax.swing.JButton;
28
29 import org.apache.xmlbeans.XmlException;
30 import org.tianocore.ModuleDefinitionsDocument.ModuleDefinitions;
31 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
32 import org.tianocore.PackageDefinitionsDocument.PackageDefinitions;
33 import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
34 import org.tianocore.PlatformDefinitionsDocument.PlatformDefinitions;
35 import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;
36 import org.tianocore.frameworkwizard.common.DataType;
37 import org.tianocore.frameworkwizard.common.DataValidation;
38 import org.tianocore.frameworkwizard.common.FileOperation;
39 import org.tianocore.frameworkwizard.common.GlobalData;
40 import org.tianocore.frameworkwizard.common.IFileFilter;
41 import org.tianocore.frameworkwizard.common.Log;
42 import org.tianocore.frameworkwizard.common.OpenFile;
43 import org.tianocore.frameworkwizard.common.SaveFile;
44 import org.tianocore.frameworkwizard.common.Tools;
45 import org.tianocore.frameworkwizard.common.Identifications.Identification;
46 import org.tianocore.frameworkwizard.common.ui.IDialog;
47 import org.tianocore.frameworkwizard.common.ui.IFrame;
48 import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;
49 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
50 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
51 import org.tianocore.frameworkwizard.workspace.Workspace;
52 import org.tianocore.frameworkwizard.workspace.WorkspaceTools;
53 import javax.swing.JComboBox;
54
55 /**
56 The class is used to provide functions to clone workspace, module, package and platform
57 It extends IDialog
58
59 **/
60 public class Clone extends IDialog {
61
62 ///
63 /// Define Class Serial Version UID
64 ///
65 private static final long serialVersionUID = -5469299324965727137L;
66
67 ///
68 /// Define Class Members
69 ///
70 private JPanel jContentPane = null;
71
72 private JLabel jLabelType = null;
73
74 private JTextField jTextFieldType = null;
75
76 private JLabel jLabelSource = null;
77
78 private JTextField jTextFieldSource = null;
79
80 private JButton jButtonBrowse = null;
81
82 private JLabel jLabelDestinationFile = null;
83
84 private JTextField jTextFieldFilePath = null;
85
86 private JLabel jLabelBaseName = null;
87
88 private JTextField jTextFieldBaseName = null;
89
90 private JLabel jLabelGuid = null;
91
92 private JTextField jTextFieldGuid = null;
93
94 private JLabel jLabelVersion = null;
95
96 private JTextField jTextFieldVersion = null;
97
98 private JButton jButtonOk = null;
99
100 private JButton jButtonCancel = null;
101
102 private JButton jButtonGenerateGuid = null;
103
104 private JLabel jLabelBelong = null;
105
106 private JComboBox jComboBoxExistingPackage = null;
107
108 ///
109 /// Define members not for UI
110 ///
111
112 private int mode = -1;
113
114 private Vector<PackageIdentification> packages = null;
115
116 private WorkspaceTools wt = new WorkspaceTools();
117
118 private Identification oldId = null;
119
120 private Identification newId = null;
121
122 private ModuleIdentification mid = null;
123
124 private PackageIdentification pid = null;
125
126 private PlatformIdentification fid = null;
127
128 /**
129 This method initializes jTextFieldType
130
131 @return javax.swing.JTextField
132
133 **/
134 private JTextField getJTextFieldType() {
135 if (jTextFieldType == null) {
136 jTextFieldType = new JTextField();
137 jTextFieldType.setBounds(new java.awt.Rectangle(210, 10, 320, 20));
138 jTextFieldType.setEditable(false);
139 }
140 return jTextFieldType;
141 }
142
143 /**
144 This method initializes jTextFieldSource
145
146 @return javax.swing.JTextField
147
148 **/
149 private JTextField getJTextFieldSource() {
150 if (jTextFieldSource == null) {
151 jTextFieldSource = new JTextField();
152 jTextFieldSource.setBounds(new java.awt.Rectangle(210, 35, 320, 20));
153 jTextFieldSource.setEditable(false);
154 }
155 return jTextFieldSource;
156 }
157
158 /**
159 This method initializes jButtonBrowse
160
161 @return javax.swing.JButton
162
163 **/
164 private JButton getJButtonBrowse() {
165 if (jButtonBrowse == null) {
166 jButtonBrowse = new JButton();
167 jButtonBrowse.setBounds(new java.awt.Rectangle(445, 85, 85, 20));
168 jButtonBrowse.setText("Browse");
169 jButtonBrowse.addActionListener(this);
170 }
171 return jButtonBrowse;
172 }
173
174 /**
175 This method initializes jTextFieldDestinationFile
176
177 @return javax.swing.JTextField
178
179 **/
180 private JTextField getJTextFieldFilePath() {
181 if (jTextFieldFilePath == null) {
182 jTextFieldFilePath = new JTextField();
183 jTextFieldFilePath.setBounds(new java.awt.Rectangle(210, 85, 230, 20));
184 }
185 return jTextFieldFilePath;
186 }
187
188 /**
189 This method initializes jTextFieldBaseName
190
191 @return javax.swing.JTextField
192
193 **/
194 private JTextField getJTextFieldBaseName() {
195 if (jTextFieldBaseName == null) {
196 jTextFieldBaseName = new JTextField();
197 jTextFieldBaseName.setBounds(new java.awt.Rectangle(210, 110, 320, 20));
198 }
199 return jTextFieldBaseName;
200 }
201
202 /**
203 This method initializes jTextFieldGuid
204
205 @return javax.swing.JTextField
206
207 **/
208 private JTextField getJTextFieldGuid() {
209 if (jTextFieldGuid == null) {
210 jTextFieldGuid = new JTextField();
211 jTextFieldGuid.setBounds(new java.awt.Rectangle(210, 135, 230, 20));
212 }
213 return jTextFieldGuid;
214 }
215
216 /**
217 This method initializes jTextFieldVersion
218
219 @return javax.swing.JTextField
220
221 **/
222 private JTextField getJTextFieldVersion() {
223 if (jTextFieldVersion == null) {
224 jTextFieldVersion = new JTextField();
225 jTextFieldVersion.setBounds(new java.awt.Rectangle(210, 160, 320, 20));
226 }
227 return jTextFieldVersion;
228 }
229
230 /**
231 This method initializes jButtonOk
232
233 @return javax.swing.JButton
234
235 **/
236 private JButton getJButtonOk() {
237 if (jButtonOk == null) {
238 jButtonOk = new JButton();
239 jButtonOk.setBounds(new java.awt.Rectangle(285, 200, 90, 20));
240 jButtonOk.setText("Ok");
241 jButtonOk.addActionListener(this);
242 }
243 return jButtonOk;
244 }
245
246 /**
247 This method initializes jButtonCancel
248
249 @return javax.swing.JButton
250
251 **/
252 private JButton getJButtonCancel() {
253 if (jButtonCancel == null) {
254 jButtonCancel = new JButton();
255 jButtonCancel.setBounds(new java.awt.Rectangle(405, 200, 90, 20));
256 jButtonCancel.setText("Cancel");
257 jButtonCancel.addActionListener(this);
258 }
259 return jButtonCancel;
260 }
261
262 /**
263 This method initializes jButtonGenerateGuid
264
265 @return javax.swing.JButton
266
267 **/
268 private JButton getJButtonGenerateGuid() {
269 if (jButtonGenerateGuid == null) {
270 jButtonGenerateGuid = new JButton();
271 jButtonGenerateGuid.setBounds(new java.awt.Rectangle(445, 135, 85, 20));
272 jButtonGenerateGuid.setText("Gen");
273 jButtonGenerateGuid.addActionListener(this);
274 }
275 return jButtonGenerateGuid;
276 }
277
278 /**
279 This method initializes jComboBoxExistingPackage
280
281 @return javax.swing.JComboBox
282
283 **/
284 private JComboBox getJComboBoxExistingPackage() {
285 if (jComboBoxExistingPackage == null) {
286 jComboBoxExistingPackage = new JComboBox();
287 jComboBoxExistingPackage.setBounds(new java.awt.Rectangle(210, 60, 320, 20));
288 }
289 return jComboBoxExistingPackage;
290 }
291
292 /**
293 This is the default constructor
294
295 **/
296 public Clone() {
297 super();
298 init();
299 }
300
301 /**
302 This is the override constructor
303
304 @param parentFrame The parent frame which starts this frame
305 @param modal To identify the frame's modal
306 @param fileType To identify the clone target type
307 @param identification The clone target's identification
308
309 **/
310 public Clone(IFrame parentFrame, boolean modal, int fileType, Identification identification) {
311 super(parentFrame, modal);
312 this.mode = fileType;
313 if (identification != null) {
314 this.oldId = new Identification(identification.getName(), identification.getGuid(),
315 identification.getVersion(), identification.getPath());
316 this.newId = new Identification(identification.getName(), identification.getGuid(),
317 identification.getVersion(), identification.getPath());
318 }
319 init(mode);
320 }
321
322 /**
323 Query all existing packages and fill them into combox
324
325 **/
326 private void initExistingPackage() {
327 packages = wt.getAllPackages();
328 for (int index = 0; index < packages.size(); index++) {
329 this.jComboBoxExistingPackage.addItem(packages.elementAt(index).getName());
330 }
331 }
332
333 /**
334 This method initializes this
335
336 **/
337 private void init() {
338 this.setSize(550, 260);
339 this.setContentPane(getJContentPane());
340 this.setTitle("Clone");
341 this.centerWindow();
342 }
343
344 /**
345 This method initializes this with given clone target type.
346 Customize the frame interface via different clone target type.
347
348 @param mode To identify the clone target type
349
350 **/
351 private void init(int mode) {
352 init();
353 //
354 // For MODULE_SURFACE_AREA
355 //
356 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
357 this.jTextFieldType.setText(DataType.MODULE_SURFACE_AREA);
358 String s = oldId.getPath();
359 s = Tools.getRelativePath(s, Tools.getFilePathOnly(wt.getPackageIdByModuleId(oldId).getPath()));
360 this.jTextFieldSource.setText(Tools.convertPathToCurrentOsType(s));
361 initExistingPackage();
362 this.jButtonBrowse.setVisible(false);
363 this.jTextFieldFilePath
364 .setToolTipText("<html>Input the module's relative path and filename, for example:<br>Application\\HelloWorld\\HelloWorld.msa</html>");
365 this.jTextFieldFilePath.setSize(320, this.jTextFieldFilePath.getSize().height);
366 this.jLabelDestinationFile.setText("New Module Path and Filename");
367 }
368 //
369 // For PACKAGE_SURFACE_AREA
370 //
371 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
372 this.jTextFieldType.setText(DataType.PACKAGE_SURFACE_AREA);
373 String s = oldId.getPath();
374 s = Tools.getRelativePath(oldId.getPath(), Workspace.getCurrentWorkspace());
375 this.jTextFieldSource.setText(Tools.convertPathToCurrentOsType(s));
376 this.jLabelBelong.setEnabled(false);
377 this.jComboBoxExistingPackage.setEnabled(false);
378 this.jButtonBrowse.setVisible(false);
379 this.jTextFieldFilePath
380 .setToolTipText("<html>Input the package's relative path and file name, for example:<br>MdePkg\\MdePkg.spd</html>");
381 this.jTextFieldFilePath.setSize(320, this.jTextFieldFilePath.getSize().height);
382 this.jLabelDestinationFile.setText("New Package Path and Filename");
383 }
384 //
385 // For PLATFORM_SURFACE_AREA
386 //
387 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
388 this.jTextFieldType.setText(DataType.PLATFORM_SURFACE_AREA);
389 this.jTextFieldSource.setText(oldId.getPath());
390 this.jLabelBelong.setEnabled(false);
391 this.jComboBoxExistingPackage.setEnabled(false);
392 this.jTextFieldFilePath
393 .setToolTipText("<html>Select the platform's relative path and filename. For example:<br>C:\\MyWorkspace\\EdkNt32Pkg\\Nt32.fpd</html>");
394 this.jLabelDestinationFile.setText("New Platform Path and Filename");
395 }
396 //
397 // For WORKSPACE
398 //
399 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
400 this.jTextFieldType.setText(DataType.WORKSPACE);
401 this.jTextFieldSource.setText(Workspace.getCurrentWorkspace());
402 this.jLabelBelong.setEnabled(false);
403 this.jComboBoxExistingPackage.setEnabled(false);
404 this.jLabelBaseName.setEnabled(false);
405 this.jTextFieldBaseName.setEditable(false);
406 this.jLabelGuid.setEnabled(false);
407 this.jTextFieldGuid.setEnabled(false);
408 this.jButtonGenerateGuid.setEnabled(false);
409 this.jLabelVersion.setEnabled(false);
410 this.jTextFieldVersion.setEnabled(false);
411 this.jTextFieldFilePath
412 .setToolTipText("<html>Input the workspace path, for example:<br>C:\\MyWorkspace</html>");
413 this.jLabelDestinationFile.setText("New Workspace Path");
414 }
415 }
416
417 /**
418 This method initializes jContentPane
419
420 @return javax.swing.JPanel
421
422 **/
423 private JPanel getJContentPane() {
424 if (jContentPane == null) {
425 jLabelBelong = new JLabel();
426 jLabelBelong.setBounds(new java.awt.Rectangle(15, 60, 190, 20));
427 jLabelBelong.setText("Clone Package");
428 jLabelVersion = new JLabel();
429 jLabelVersion.setBounds(new java.awt.Rectangle(15, 160, 190, 20));
430 jLabelVersion.setText("Version");
431 jLabelGuid = new JLabel();
432 jLabelGuid.setBounds(new java.awt.Rectangle(15, 135, 190, 20));
433 jLabelGuid.setText("Guid");
434 jLabelBaseName = new JLabel();
435 jLabelBaseName.setBounds(new java.awt.Rectangle(15, 110, 190, 20));
436 jLabelBaseName.setText("Base Name");
437 jLabelDestinationFile = new JLabel();
438 jLabelDestinationFile.setBounds(new java.awt.Rectangle(15, 85, 190, 20));
439 jLabelDestinationFile.setText("Destination File Name");
440 jLabelSource = new JLabel();
441 jLabelSource.setBounds(new java.awt.Rectangle(15, 35, 190, 20));
442 jLabelSource.setText("Source");
443 jLabelType = new JLabel();
444 jLabelType.setBounds(new java.awt.Rectangle(15, 10, 190, 20));
445 jLabelType.setText("Type");
446 jContentPane = new JPanel();
447 jContentPane.setLayout(null);
448 jContentPane.setSize(new java.awt.Dimension(540, 227));
449 jContentPane.add(jLabelType, null);
450 jContentPane.add(getJTextFieldType(), null);
451 jContentPane.add(jLabelSource, null);
452 jContentPane.add(getJTextFieldSource(), null);
453 jContentPane.add(jLabelDestinationFile, null);
454 jContentPane.add(getJTextFieldFilePath(), null);
455 jContentPane.add(jLabelBaseName, null);
456 jContentPane.add(getJTextFieldBaseName(), null);
457 jContentPane.add(jLabelGuid, null);
458 jContentPane.add(getJTextFieldGuid(), null);
459 jContentPane.add(jLabelVersion, null);
460 jContentPane.add(getJTextFieldVersion(), null);
461 jContentPane.add(getJButtonOk(), null);
462 jContentPane.add(getJButtonCancel(), null);
463 jContentPane.add(getJButtonBrowse(), null);
464 jContentPane.add(getJButtonGenerateGuid(), null);
465 jContentPane.add(jLabelBelong, null);
466 jContentPane.add(getJComboBoxExistingPackage(), null);
467 }
468 return jContentPane;
469 }
470
471 /* (non-Javadoc)
472 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
473 *
474 * Override actionPerformed to listen all actions
475 */
476 public void actionPerformed(ActionEvent arg0) {
477 if (arg0.getSource() == jButtonCancel) {
478 this.setVisible(false);
479 this.returnType = DataType.RETURN_TYPE_CANCEL;
480 }
481
482 if (arg0.getSource() == jButtonOk) {
483 if (this.check()) {
484 try {
485 //
486 // Save to file
487 //
488 this.save();
489 } catch (IOException e) {
490 Log.wrn("Clone", e.getMessage());
491 Log.err("Clone", e.getMessage());
492 return;
493 } catch (XmlException e) {
494 Log.wrn("Clone", e.getMessage());
495 Log.err("Clone", e.getMessage());
496 return;
497 } catch (Exception e) {
498 Log.wrn("Clone", e.getMessage());
499 Log.err("Clone", e.getMessage());
500 return;
501 }
502 } else {
503 return;
504 }
505 this.setVisible(false);
506 }
507
508 if (arg0.getSource() == this.jButtonGenerateGuid) {
509 this.jTextFieldGuid.setText(Tools.generateUuidString());
510 }
511
512 //
513 // Use different file ext for different clone target type
514 //
515 if (arg0.getSource() == this.jButtonBrowse) {
516 JFileChooser fc = new JFileChooser();
517 fc.setAcceptAllFileFilterUsed(false);
518
519 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
520 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
521 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
522 }
523 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
524 fc.setCurrentDirectory(new File(packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex())
525 .getPath()));
526 fc.addChoosableFileFilter(new IFileFilter(DataType.MODULE_SURFACE_AREA_EXT));
527 }
528 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
529 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
530 fc.addChoosableFileFilter(new IFileFilter(DataType.PACKAGE_SURFACE_AREA_EXT));
531 }
532 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
533 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
534 fc.addChoosableFileFilter(new IFileFilter(DataType.PLATFORM_SURFACE_AREA_EXT));
535 }
536 int result = fc.showSaveDialog(new JPanel());
537 if (result == JFileChooser.APPROVE_OPTION) {
538 this.jTextFieldFilePath.setText(Tools.addPathExt(fc.getSelectedFile().getPath(), mode));
539 }
540 }
541 }
542
543 /**
544 Check name, guid and version.
545 If all of them are valid, save information to new id
546
547 @retval true All name, guid and version are valid
548 @retval false Any one of name, guid and version is invalid
549
550 **/
551 private boolean checkId() {
552 //
553 // Check Basename
554 //
555 if (isEmpty(this.jTextFieldBaseName.getText())) {
556 Log.wrn("Clone", "The Name is required!");
557 return false;
558 }
559 if (!DataValidation.isBaseName(this.jTextFieldBaseName.getText())) {
560 Log
561 .wrn("Clone",
562 "<html>Incorrect data type for the Name, it must<br>be a single word, starting with an alpha character.</html>");
563 return false;
564 }
565
566 //
567 // Check Guid
568 //
569 if (isEmpty(this.jTextFieldGuid.getText())) {
570 Log.wrn("Clone", "A Guid is required!!");
571 return false;
572 }
573 if (!DataValidation.isGuid(this.jTextFieldGuid.getText())) {
574 Log
575 .wrn(
576 "Clone",
577 "<html>Incorrect data type for Guid, which must<br>be in registry format (8-4-4-4-12) for example:<br>d3adb123-eef1-466d-39ac-02febcaf5997</html>");
578 return false;
579 }
580
581 //
582 // Check Version
583 //
584 if (isEmpty(this.jTextFieldVersion.getText())) {
585 Log.wrn("Clone", "A Version must be entered!");
586 return false;
587 }
588 if (!DataValidation.isVersion(this.jTextFieldVersion.getText())) {
589 Log
590 .wrn(
591 "Clone",
592 "<html>Incorrect data type for Version, which must<br>be one or more digits, optionally followed by sequence<br>of one or more dot, one or more digits; examples:<br>1.0 1.0.1 12.25.256</html>");
593 return false;
594 }
595
596 //
597 // Save information to id
598 //
599 newId.setName(this.jTextFieldBaseName.getText());
600 newId.setGuid(this.jTextFieldGuid.getText());
601 newId.setVersion(this.jTextFieldVersion.getText());
602 newId.setPath(this.jTextFieldFilePath.getText());
603
604 return true;
605 }
606
607 /**
608 Check before save
609
610 @retval true All check points are passed
611 @retval false Any one of check points is failed
612
613 **/
614 private boolean check() {
615 String src = this.oldId.getPath();
616 String trg = this.jTextFieldFilePath.getText();
617 File srcFile = new File(src);
618 File trgFile = new File(trg);
619
620 //
621 // Common Check
622 //
623 if (!srcFile.exists()) {
624 Log.wrn("Clone", "The source file does not exist!");
625 return false;
626 }
627 if (isEmpty(trg)) {
628 Log.wrn("Clone", "The destination file path must be entered!");
629 return false;
630 }
631 if (src.equals(trg)) {
632 Log.wrn("Clone", "The source and destination can not be same!");
633 return false;
634 }
635 if (trgFile.exists()) {
636 Log.wrn("Clone", "The destination already exists!");
637 return false;
638 }
639
640 //
641 // Check for workspace
642 //
643 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
644 if (trg.indexOf(src + DataType.FILE_SEPARATOR) == 0) {
645 Log.wrn("Clone", "The new workspace can not be located within the current workspace!");
646 return false;
647 }
648 }
649
650 //
651 // Check for Module
652 //
653 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
654 trg = this.getModulePath();
655 if (src.equals(trg)) {
656 Log.wrn("Clone", "The source and destination paths for cloning a module must be different!");
657 return false;
658 }
659 trgFile = new File(trg);
660 if (trgFile.exists()) {
661 Log.wrn("Clone", "The target module already exists!");
662 return false;
663 }
664 return checkId();
665 }
666
667 //
668 // Check for Package
669 //
670 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
671 if (trg.indexOf(DataType.DOS_FILE_SEPARATOR) == -1 && trg.indexOf(DataType.UNIX_FILE_SEPARATOR) == -1) {
672 Log.wrn("Clone", "The package name must include a path!");
673 return false;
674 }
675 trg = this.getPackagePath();
676 if (Tools.getFilePathOnly(src).equals(Tools.getFilePathOnly(trg))) {
677 Log.wrn("Clone", "The source and destination paths for cloning a package must be different!");
678 return false;
679 }
680 trgFile = new File(trg);
681 if (trgFile.exists()) {
682 Log.wrn("Clone", "The target package already exists!");
683 return false;
684 }
685 return checkId();
686 }
687
688 //
689 // Check for Platform
690 //
691 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
692 if (trg.indexOf(Workspace.getCurrentWorkspace()) != 0) {
693 Log.wrn("Clone", "The platform clone must be located in the current workspace!");
694 return false;
695 }
696 trgFile = new File(trg);
697 if (trgFile.exists()) {
698 Log.wrn("Clone", "The target platform already exists.");
699 return false;
700 }
701 return checkId();
702 }
703
704 return true;
705 }
706
707 /**
708 Save clone target to new location
709
710 @throws IOException
711 @throws XmlException
712 @throws Exception
713
714 **/
715 private void save() throws IOException, XmlException, Exception {
716 String src = this.oldId.getPath();
717 String trg = this.jTextFieldFilePath.getText();
718 Vector<String> vFiles = new Vector<String>();
719
720 //
721 // Clone Workspace
722 //
723 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
724 FileOperation.copyFolder(src, trg);
725 this.returnType = DataType.RETURN_TYPE_WORKSPACE;
726 }
727
728 //
729 // Clone Module Surface Area
730 //
731 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
732 //
733 // Get target path from source path
734 //
735 trg = getModulePath();
736 newId.setPath(trg);
737 vFiles = wt.getAllFilesPathOfModule(src);
738
739 String oldPackagePath = GlobalData.openingModuleList.getIdByPath(src).getPackageId().getPath();
740 String newPackagePath = packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()).getPath();
741
742 //
743 // First copy all files to new directory
744 //
745 FileOperation.copyFile(src, trg);
746 for (int index = 1; index < vFiles.size(); index++) {
747 String oldFile = vFiles.get(index);
748 String newFile = "";
749 if (oldFile.indexOf(Tools.getFilePathOnly(src)) > -1) {
750 //
751 // The file is not include header
752 //
753 newFile = oldFile.replace(Tools.getFilePathOnly(src), Tools.getFilePathOnly(trg));
754 } else if (oldFile.indexOf(Tools.getFilePathOnly(oldPackagePath)) > -1) {
755 //
756 // The file is include header
757 //
758 newFile = oldFile.replace(Tools.getFilePathOnly(oldPackagePath),
759 Tools.getFilePathOnly(newPackagePath));
760 }
761
762 FileOperation.copyFile(oldFile, newFile);
763 }
764
765 //
766 // Create new msa file
767 //
768 ModuleSurfaceArea msa = null;
769 msa = OpenFile.openMsaFile(src);
770
771 //
772 // Update to memory
773 //
774 msa.getMsaHeader().setModuleName(newId.getName());
775 msa.getMsaHeader().setGuidValue(newId.getGuid());
776 msa.getMsaHeader().setVersion(newId.getVersion());
777
778 //
779 // Update <Cloned> Section
780 //
781 updateModuleClonedId(msa, oldId);
782
783 //
784 // Save to file
785 //
786 SaveFile.saveMsaFile(trg, msa);
787
788 //
789 // Update to platformId
790 //
791 this.setMid(new ModuleIdentification(newId,
792 packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex())));
793
794 //
795 // Open belonging package
796 //
797 PackageSurfaceArea psa = PackageSurfaceArea.Factory.newInstance();
798 psa = OpenFile.openSpdFile(mid.getPackageId().getPath());
799
800 //
801 // Update the db file
802 //
803 wt.addModuleToPackage(mid, psa);
804
805 //
806 // Update GlobalData
807 //
808 GlobalData.vModuleList.addElement(mid);
809 GlobalData.openingModuleList.insertToOpeningModuleList(mid, msa);
810
811 this.returnType = DataType.RETURN_TYPE_MODULE_SURFACE_AREA;
812 }
813
814 //
815 // Clone Package Surface Area
816 //
817 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
818 //
819 // Get target path from source path
820 //
821 trg = this.getPackagePath();
822 newId.setPath(trg);
823 vFiles = wt.getAllFilesPathOfPakcage(src);
824
825 //
826 // First copy all files to new directory
827 //
828 FileOperation.copyFile(src, trg);
829 for (int index = 1; index < vFiles.size(); index++) {
830 String oldFile = vFiles.get(index);
831 String newFile = vFiles.get(index).replace(Tools.getFilePathOnly(src), Tools.getFilePathOnly(trg));
832 FileOperation.copyFile(oldFile, newFile);
833 }
834
835 //
836 // Create new spd file
837 //
838 PackageSurfaceArea spd = null;
839 spd = OpenFile.openSpdFile(src);
840
841 //
842 // Update to memory
843 //
844 spd.getSpdHeader().setPackageName(newId.getName());
845 spd.getSpdHeader().setGuidValue(newId.getGuid());
846 spd.getSpdHeader().setVersion(newId.getVersion());
847
848 //
849 // Update <Cloned> Section
850 //
851 updatePackageClonedId(spd, oldId);
852
853 //
854 // Save to file
855 //
856 SaveFile.saveSpdFile(trg, spd);
857
858 //
859 // Update to platformId
860 //
861 this.setPid(new PackageIdentification(newId));
862
863 //
864 // Update the db file
865 //
866 wt.addPackageToDatabase(pid);
867
868 //
869 // Update GlobalData
870 //
871 GlobalData.vPackageList.addElement(pid);
872 GlobalData.openingPackageList.insertToOpeningPackageList(pid, spd);
873
874 //
875 // Add all cloned modules
876 //
877 Vector<String> modulePaths = GlobalData.getAllModulesOfPackage(pid.getPath());
878 String modulePath = null;
879 ModuleSurfaceArea msa = null;
880
881 for (int indexJ = 0; indexJ < modulePaths.size(); indexJ++) {
882 try {
883 modulePath = modulePaths.get(indexJ);
884 msa = OpenFile.openMsaFile(modulePath);
885 } catch (IOException e) {
886 Log.err("Open Module Surface Area " + modulePath, e.getMessage());
887 continue;
888 } catch (XmlException e) {
889 Log.err("Open Module Surface Area " + modulePath, e.getMessage());
890 continue;
891 } catch (Exception e) {
892 Log.err("Open Module Surface Area " + modulePath, "Invalid file type");
893 continue;
894 }
895 Identification id = Tools.getId(modulePath, msa);
896 mid = new ModuleIdentification(id, pid);
897 GlobalData.vModuleList.addElement(mid);
898 GlobalData.openingModuleList.insertToOpeningModuleList(mid, msa);
899 }
900
901 this.returnType = DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA;
902 }
903
904 //
905 // Clone Platform Surface Area
906 //
907 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
908 PlatformSurfaceArea fpd = null;
909 fpd = OpenFile.openFpdFile(src);
910
911 //
912 // Update to memory
913 //
914 fpd.getPlatformHeader().setPlatformName(newId.getName());
915 fpd.getPlatformHeader().setGuidValue(newId.getGuid());
916 fpd.getPlatformHeader().setVersion(newId.getVersion());
917
918 //
919 // Update Cloned From element
920 //
921 updatePlatformClonedId(fpd, oldId);
922
923 //
924 // Save to file
925 //
926 SaveFile.saveFpdFile(trg, fpd);
927
928 //
929 // Update to platformId
930 //
931 this.setFid(new PlatformIdentification(newId));
932
933 //
934 // Update the db file
935 //
936 wt.addPlatformToDatabase(fid);
937
938 //
939 // Update GlobalData
940 //
941 GlobalData.vPlatformList.addElement(fid);
942 GlobalData.openingPlatformList.insertToOpeningPlatformList(fid, fpd);
943
944 this.returnType = DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA;
945 }
946 vFiles = null;
947 }
948
949 /**
950 Get the path of selected package
951
952 @return String The path of selected package
953
954 **/
955 private String getSelectPackagePath() {
956 return Tools.getFilePathOnly(packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()).getPath());
957 }
958
959 /**
960 Get the path of source module
961 Since the path of source module is relative, make it up to full path.
962
963 @return String The full path of source module
964
965 **/
966 private String getModulePath() {
967 String trg = this.jTextFieldFilePath.getText();
968 trg = Tools.addPathExt(trg, mode);
969 trg = Tools.addFileSeparator(getSelectPackagePath()) + trg;
970 Tools.convertPathToCurrentOsType(trg);
971 return trg;
972 }
973
974 /**
975 Get the path of source package
976 Since the path of source package is relative, make it up to full path.
977
978 @return String The full path of source package
979
980 **/
981 private String getPackagePath() {
982 String trg = this.jTextFieldFilePath.getText();
983 trg = Tools.addPathExt(trg, mode);
984 trg = Tools.addFileSeparator(Workspace.getCurrentWorkspace()) + trg;
985 trg = Tools.convertPathToCurrentOsType(trg);
986 return trg;
987 }
988
989 /**
990 Set msa file's <Cloned> section via given identification
991
992 @param msa ModuleSurfaceArea for clone target
993 @param id Identification of clone source
994
995 **/
996 private void updateModuleClonedId(ModuleSurfaceArea msa, Identification id) {
997 //
998 // Get PlatformDefinitions First
999 //
1000 ModuleDefinitions pd = null;
1001 if (msa.getModuleDefinitions() == null) {
1002 pd = ModuleDefinitions.Factory.newInstance();
1003 msa.addNewModuleDefinitions();
1004 } else {
1005 pd = msa.getModuleDefinitions();
1006 }
1007
1008 //
1009 // Get ClonedFrom then
1010 //
1011 ModuleDefinitions.ClonedFrom cf = null;
1012 BigInteger count = new BigInteger("-1");
1013 if (pd.getClonedFrom() == null) {
1014 cf = ModuleDefinitions.ClonedFrom.Factory.newInstance();
1015 } else {
1016 cf = pd.getClonedFrom();
1017 if (cf != null) {
1018 for (int index = 0; index < cf.getClonedList().size(); index++) {
1019 if (cf.getClonedList().get(index).getId() != null) {
1020 count = count.max(cf.getClonedList().get(index).getId());
1021 }
1022 }
1023 }
1024 }
1025
1026 //
1027 // Set new Cloned item
1028 //
1029 ModuleDefinitions.ClonedFrom.Cloned c = ModuleDefinitions.ClonedFrom.Cloned.Factory.newInstance();
1030 c.setModuleGuid(id.getGuid());
1031 c.setModuleVersion(id.getVersion());
1032 c.setPackageGuid(wt.getPackageIdByModuleId(oldId).getGuid());
1033 c.setPackageVersion(wt.getPackageIdByModuleId(oldId).getVersion());
1034 c.setId(count.add(new BigInteger("1")));
1035 String guid = wt.getModuleFarGuid(oldId);
1036 if (guid != null && !guid.equals("")) {
1037 c.setFarGuid(guid);
1038 }
1039
1040 cf.addNewCloned();
1041 cf.setClonedArray(cf.getClonedList().size() - 1, c);
1042 pd.addNewClonedFrom();
1043 pd.setClonedFrom(cf);
1044 msa.setModuleDefinitions(pd);
1045 }
1046
1047 /**
1048 Set spd file's <Cloned> section via given identification
1049
1050 @param spd PackageSurfaceArea for clone target
1051 @param id Identification of clone source
1052
1053 **/
1054 private void updatePackageClonedId(PackageSurfaceArea spd, Identification id) {
1055 //
1056 // Get PlatformDefinitions First
1057 //
1058 PackageDefinitions pd = null;
1059 if (spd.getPackageDefinitions() == null) {
1060 pd = PackageDefinitions.Factory.newInstance();
1061 spd.addNewPackageDefinitions();
1062 } else {
1063 pd = spd.getPackageDefinitions();
1064 }
1065
1066 //
1067 // Get ClonedFrom then
1068 //
1069 PackageDefinitions.ClonedFrom cf = null;
1070 BigInteger count = new BigInteger("-1");
1071 if (pd.getClonedFrom() == null) {
1072 cf = PackageDefinitions.ClonedFrom.Factory.newInstance();
1073 } else {
1074 cf = pd.getClonedFrom();
1075 if (cf != null) {
1076 for (int index = 0; index < cf.getClonedList().size(); index++) {
1077 if (cf.getClonedList().get(index).getId() != null) {
1078 count = count.max(cf.getClonedList().get(index).getId());
1079 }
1080 }
1081 }
1082 }
1083
1084 //
1085 // Set new Cloned item
1086 //
1087 PackageDefinitions.ClonedFrom.Cloned c = PackageDefinitions.ClonedFrom.Cloned.Factory.newInstance();
1088 c.setPackageGuid(id.getGuid());
1089 c.setPackageVersion(id.getVersion());
1090 c.setId(count.add(new BigInteger("1")));
1091 String guid = wt.getPackageFarGuid(oldId);
1092 if (guid != null && !guid.equals("")) {
1093 c.setFarGuid(guid);
1094 }
1095
1096 cf.addNewCloned();
1097 cf.setClonedArray(cf.getClonedList().size() - 1, c);
1098 pd.addNewClonedFrom();
1099 pd.setClonedFrom(cf);
1100 spd.setPackageDefinitions(pd);
1101 }
1102
1103 /**
1104 Set fpd file's <Cloned> section via given identification
1105
1106 @param fpd PlatformSurfaceArea for clone target
1107 @param id Identification of clone source
1108
1109 **/
1110 private void updatePlatformClonedId(PlatformSurfaceArea fpd, Identification id) {
1111 //
1112 // Get PlatformDefinitions First
1113 //
1114 PlatformDefinitions pd = null;
1115 if (fpd.getPlatformDefinitions() == null) {
1116 pd = PlatformDefinitions.Factory.newInstance();
1117 fpd.addNewPlatformDefinitions();
1118 } else {
1119 pd = fpd.getPlatformDefinitions();
1120 }
1121
1122 //
1123 // Get ClonedFrom then
1124 //
1125 PlatformDefinitions.ClonedFrom cf = null;
1126 BigInteger count = new BigInteger("-1");
1127 if (pd.getClonedFrom() == null) {
1128 cf = PlatformDefinitions.ClonedFrom.Factory.newInstance();
1129 } else {
1130 cf = pd.getClonedFrom();
1131 if (cf != null) {
1132 for (int index = 0; index < cf.getClonedList().size(); index++) {
1133 if (cf.getClonedList().get(index).getId() != null) {
1134 count = count.max(cf.getClonedList().get(index).getId());
1135 }
1136 }
1137 }
1138 }
1139
1140 //
1141 // Set new Cloned item
1142 //
1143 PlatformDefinitions.ClonedFrom.Cloned c = PlatformDefinitions.ClonedFrom.Cloned.Factory.newInstance();
1144 c.setPlatformGuid(id.getGuid());
1145 c.setPlatformVersion(id.getVersion());
1146 c.setId(count.add(new BigInteger("1")));
1147 String guid = wt.getPlatformFarGuid(oldId);
1148 if (guid != null && !guid.equals("")) {
1149 c.setFarGuid(guid);
1150 }
1151
1152 cf.addNewCloned();
1153 cf.setClonedArray(cf.getClonedList().size() - 1, c);
1154 pd.addNewClonedFrom();
1155 pd.setClonedFrom(cf);
1156 fpd.setPlatformDefinitions(pd);
1157 }
1158
1159 /**
1160 Get PlatformIdentification
1161
1162 @return PlatformIdentification
1163
1164 **/
1165 public PlatformIdentification getFid() {
1166 return fid;
1167 }
1168
1169 /**
1170 Set PlatformIdentification
1171
1172 @param fid PlatformIdentification
1173
1174 **/
1175 public void setFid(PlatformIdentification fid) {
1176 this.fid = fid;
1177 }
1178
1179 /**
1180 Get ModuleIdentification
1181
1182 @return ModuleIdentification
1183
1184 **/
1185 public ModuleIdentification getMid() {
1186 return mid;
1187 }
1188
1189 /**
1190 Set ModuleIdentification
1191
1192 @param mid ModuleIdentification
1193
1194 **/
1195 public void setMid(ModuleIdentification mid) {
1196 this.mid = mid;
1197 }
1198
1199 /**
1200 Get PackageIdentification
1201
1202 @return PackageIdentification
1203
1204 **/
1205 public PackageIdentification getPid() {
1206 return pid;
1207 }
1208
1209 /**
1210 Set PackageIdentification
1211
1212 @param pid PackageIdentification
1213
1214 **/
1215 public void setPid(PackageIdentification pid) {
1216 this.pid = pid;
1217 }
1218 }