1. Fix EDKT457 Multiple FPD, MSA or SPD files in a directory is prohibited
[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 // Check if the package can be cloned
386 //
387 PackageSurfaceArea spd = GlobalData.openingPackageList
388 .getPackageSurfaceAreaFromId(GlobalData.openingPackageList
389 .getIdByPath(this.oldId
390 .getPath()));
391 if (spd != null) {
392 if (spd.getPackageDefinitions() != null) {
393 if (!spd.getPackageDefinitions().getRePackage()) {
394 Log.wrn("Clone Package", "This package can't repackaged and cloned");
395 this.jTextFieldBaseName.setEnabled(false);
396 this.jTextFieldFilePath.setEnabled(false);
397 this.jTextFieldGuid.setEnabled(false);
398 this.jTextFieldVersion.setEnabled(false);
399 this.jButtonGenerateGuid.setEnabled(false);
400 this.jButtonOk.setEnabled(false);
401 }
402 }
403 }
404 }
405 //
406 // For PLATFORM_SURFACE_AREA
407 //
408 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
409 this.jTextFieldType.setText(DataType.PLATFORM_SURFACE_AREA);
410 this.jTextFieldSource.setText(oldId.getPath());
411 this.jLabelBelong.setEnabled(false);
412 this.jComboBoxExistingPackage.setEnabled(false);
413 this.jTextFieldFilePath
414 .setToolTipText("<html>Select the platform's relative path and filename. For example:<br>C:\\MyWorkspace\\EdkNt32Pkg\\Nt32.fpd</html>");
415 this.jLabelDestinationFile.setText("New Platform Path and Filename");
416 }
417 //
418 // For WORKSPACE
419 //
420 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
421 this.jTextFieldType.setText(DataType.WORKSPACE);
422 this.jTextFieldSource.setText(Workspace.getCurrentWorkspace());
423 this.jLabelBelong.setEnabled(false);
424 this.jComboBoxExistingPackage.setEnabled(false);
425 this.jLabelBaseName.setEnabled(false);
426 this.jTextFieldBaseName.setEditable(false);
427 this.jLabelGuid.setEnabled(false);
428 this.jTextFieldGuid.setEnabled(false);
429 this.jButtonGenerateGuid.setEnabled(false);
430 this.jLabelVersion.setEnabled(false);
431 this.jTextFieldVersion.setEnabled(false);
432 this.jTextFieldFilePath
433 .setToolTipText("<html>Input the workspace path, for example:<br>C:\\MyWorkspace</html>");
434 this.jLabelDestinationFile.setText("New Workspace Path");
435 }
436 }
437
438 /**
439 This method initializes jContentPane
440
441 @return javax.swing.JPanel
442
443 **/
444 private JPanel getJContentPane() {
445 if (jContentPane == null) {
446 jLabelBelong = new JLabel();
447 jLabelBelong.setBounds(new java.awt.Rectangle(15, 60, 190, 20));
448 jLabelBelong.setText("Clone Package");
449 jLabelVersion = new JLabel();
450 jLabelVersion.setBounds(new java.awt.Rectangle(15, 160, 190, 20));
451 jLabelVersion.setText("Version");
452 jLabelGuid = new JLabel();
453 jLabelGuid.setBounds(new java.awt.Rectangle(15, 135, 190, 20));
454 jLabelGuid.setText("Guid");
455 jLabelBaseName = new JLabel();
456 jLabelBaseName.setBounds(new java.awt.Rectangle(15, 110, 190, 20));
457 jLabelBaseName.setText("Base Name");
458 jLabelDestinationFile = new JLabel();
459 jLabelDestinationFile.setBounds(new java.awt.Rectangle(15, 85, 190, 20));
460 jLabelDestinationFile.setText("Destination File Name");
461 jLabelSource = new JLabel();
462 jLabelSource.setBounds(new java.awt.Rectangle(15, 35, 190, 20));
463 jLabelSource.setText("Source");
464 jLabelType = new JLabel();
465 jLabelType.setBounds(new java.awt.Rectangle(15, 10, 190, 20));
466 jLabelType.setText("Type");
467 jContentPane = new JPanel();
468 jContentPane.setLayout(null);
469 jContentPane.setSize(new java.awt.Dimension(540, 227));
470 jContentPane.add(jLabelType, null);
471 jContentPane.add(getJTextFieldType(), null);
472 jContentPane.add(jLabelSource, null);
473 jContentPane.add(getJTextFieldSource(), null);
474 jContentPane.add(jLabelDestinationFile, null);
475 jContentPane.add(getJTextFieldFilePath(), null);
476 jContentPane.add(jLabelBaseName, null);
477 jContentPane.add(getJTextFieldBaseName(), null);
478 jContentPane.add(jLabelGuid, null);
479 jContentPane.add(getJTextFieldGuid(), null);
480 jContentPane.add(jLabelVersion, null);
481 jContentPane.add(getJTextFieldVersion(), null);
482 jContentPane.add(getJButtonOk(), null);
483 jContentPane.add(getJButtonCancel(), null);
484 jContentPane.add(getJButtonBrowse(), null);
485 jContentPane.add(getJButtonGenerateGuid(), null);
486 jContentPane.add(jLabelBelong, null);
487 jContentPane.add(getJComboBoxExistingPackage(), null);
488 }
489 return jContentPane;
490 }
491
492 /* (non-Javadoc)
493 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
494 *
495 * Override actionPerformed to listen all actions
496 */
497 public void actionPerformed(ActionEvent arg0) {
498 if (arg0.getSource() == jButtonCancel) {
499 this.setVisible(false);
500 this.returnType = DataType.RETURN_TYPE_CANCEL;
501 }
502
503 if (arg0.getSource() == jButtonOk) {
504 if (this.check()) {
505 try {
506 //
507 // Save to file
508 //
509 this.save();
510 } catch (IOException e) {
511 Log.wrn("Clone", e.getMessage());
512 Log.err("Clone", e.getMessage());
513 return;
514 } catch (XmlException e) {
515 Log.wrn("Clone", e.getMessage());
516 Log.err("Clone", e.getMessage());
517 return;
518 } catch (Exception e) {
519 Log.wrn("Clone", e.getMessage());
520 Log.err("Clone", e.getMessage());
521 return;
522 }
523 } else {
524 return;
525 }
526 this.setVisible(false);
527 }
528
529 if (arg0.getSource() == this.jButtonGenerateGuid) {
530 this.jTextFieldGuid.setText(Tools.generateUuidString());
531 }
532
533 //
534 // Use different file ext for different clone target type
535 //
536 if (arg0.getSource() == this.jButtonBrowse) {
537 JFileChooser fc = new JFileChooser();
538 fc.setAcceptAllFileFilterUsed(false);
539
540 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
541 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
542 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
543 }
544 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
545 fc.setCurrentDirectory(new File(packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex())
546 .getPath()));
547 fc.addChoosableFileFilter(new IFileFilter(DataType.MODULE_SURFACE_AREA_EXT));
548 }
549 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
550 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
551 fc.addChoosableFileFilter(new IFileFilter(DataType.PACKAGE_SURFACE_AREA_EXT));
552 }
553 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
554 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
555 fc.addChoosableFileFilter(new IFileFilter(DataType.PLATFORM_SURFACE_AREA_EXT));
556 }
557 int result = fc.showSaveDialog(new JPanel());
558 if (result == JFileChooser.APPROVE_OPTION) {
559 this.jTextFieldFilePath.setText(Tools.addPathExt(fc.getSelectedFile().getPath(), mode));
560 }
561 }
562 }
563
564 /**
565 Check name, guid and version.
566 If all of them are valid, save information to new id
567
568 @retval true All name, guid and version are valid
569 @retval false Any one of name, guid and version is invalid
570
571 **/
572 private boolean checkId(int mode) {
573 String name = this.jTextFieldBaseName.getText();
574 String guid = this.jTextFieldGuid.getText();
575 String version = this.jTextFieldVersion.getText();
576
577 //
578 // Check Basename
579 //
580 if (isEmpty(name)) {
581 Log.wrn("Clone", "The Name is required!");
582 return false;
583 }
584 if (!DataValidation.isBaseName(name)) {
585 Log
586 .wrn("Clone",
587 "<html>Incorrect data type for the Name, it must<br>be a single word, starting with an alpha character.</html>");
588 return false;
589 }
590
591 //
592 // Check Guid
593 //
594 if (isEmpty(guid)) {
595 Log.wrn("Clone", "A Guid is required!!");
596 return false;
597 }
598 if (!DataValidation.isGuid(guid)) {
599 Log
600 .wrn(
601 "Clone",
602 "<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>");
603 return false;
604 }
605
606 //
607 // Check Version
608 //
609 if (isEmpty(version)) {
610 Log.wrn("Clone", "A Version must be entered!");
611 return false;
612 }
613 if (!DataValidation.isVersion(version)) {
614 Log
615 .wrn(
616 "Clone",
617 "<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>");
618 return false;
619 }
620
621 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
622 String packageGuid = packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()).getGuid();
623 String packageVersion = packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()).getVersion();
624 if (GlobalData.findModuleIdByGuidVersion(guid, version, packageGuid, packageVersion) != null) {
625 Log.wrn("Clone", "A module with same Guid and same Version already exists, please selece a new Guid or Version!");
626 return false;
627 }
628 }
629
630 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
631 if (GlobalData.findPackageIdByGuidVersion(guid, version) != null) {
632 Log.wrn("Clone", "A package with same Guid and same Version already exists, please selece a new Guid or Version!");
633 return false;
634 }
635 }
636
637 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
638 if (GlobalData.findPlatformIdByGuidVersion(guid, version) != null) {
639 Log.wrn("Clone", "A platform with same Guid and same Version already exists, please selece a new Guid or Version!");
640 return false;
641 }
642 }
643
644 //
645 // Save information to id
646 //
647 newId.setName(this.jTextFieldBaseName.getText());
648 newId.setGuid(this.jTextFieldGuid.getText());
649 newId.setVersion(this.jTextFieldVersion.getText());
650 newId.setPath(this.jTextFieldFilePath.getText());
651
652 return true;
653 }
654
655 /**
656 Check before save
657
658 @retval true All check points are passed
659 @retval false Any one of check points is failed
660
661 **/
662 private boolean check() {
663 String src = this.oldId.getPath();
664 String trg = this.jTextFieldFilePath.getText();
665 File srcFile = new File(src);
666 File trgFile = new File(trg);
667
668 //
669 // Common Check
670 //
671 if (!srcFile.exists()) {
672 Log.wrn("Clone", "The source file does not exist!");
673 return false;
674 }
675 if (isEmpty(trg)) {
676 Log.wrn("Clone", "The destination file path must be entered!");
677 return false;
678 }
679 if (src.equals(trg)) {
680 Log.wrn("Clone", "The source and destination can not be same!");
681 return false;
682 }
683 if (trgFile.exists()) {
684 Log.wrn("Clone", "The destination already exists!");
685 return false;
686 }
687
688 //
689 // Check for workspace
690 //
691 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
692 if (trg.indexOf(src + DataType.FILE_SEPARATOR) == 0) {
693 Log.wrn("Clone", "The new workspace can not be located within the current workspace!");
694 return false;
695 }
696 }
697
698 //
699 // Check for Module
700 //
701 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
702 trg = this.getModulePath();
703 if (Tools.getFilePathOnly(src).equals(Tools.getFilePathOnly(trg))) {
704 Log.wrn("Clone", "The source and destination paths for cloning a module must be different!");
705 return false;
706 }
707 trgFile = new File(trg);
708 if (trgFile.exists()) {
709 Log.wrn("Clone", "The target module already exists!");
710 return false;
711 }
712 return checkId(mode);
713 }
714
715 //
716 // Check for Package
717 //
718 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
719 if (trg.indexOf(DataType.DOS_FILE_SEPARATOR) == -1 && trg.indexOf(DataType.UNIX_FILE_SEPARATOR) == -1) {
720 Log.wrn("Clone", "The package name must include a path!");
721 return false;
722 }
723 trg = this.getPackagePath();
724 if (Tools.getFilePathOnly(src).equals(Tools.getFilePathOnly(trg))) {
725 Log.wrn("Clone", "The source and destination paths for cloning a package must be different!");
726 return false;
727 }
728 trgFile = new File(trg);
729 if (trgFile.exists()) {
730 Log.wrn("Clone", "The target package already exists!");
731 return false;
732 }
733 return checkId(mode);
734 }
735
736 //
737 // Check for Platform
738 //
739 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
740 if (trg.indexOf(Workspace.getCurrentWorkspace()) != 0) {
741 Log.wrn("Clone", "The platform clone must be located in the current workspace!");
742 return false;
743 }
744 if (Tools.getFilePathOnly(src).equals(Tools.getFilePathOnly(trg))) {
745 Log.wrn("Clone", "The source and destination paths for cloning a platform must be different!");
746 return false;
747 }
748 trgFile = new File(trg);
749 if (trgFile.exists()) {
750 Log.wrn("Clone", "The target platform already exists.");
751 return false;
752 }
753 return checkId(mode);
754 }
755
756 return true;
757 }
758
759 /**
760 Save clone target to new location
761
762 @throws IOException
763 @throws XmlException
764 @throws Exception
765
766 **/
767 private void save() throws IOException, XmlException, Exception {
768 String src = this.oldId.getPath();
769 String trg = this.jTextFieldFilePath.getText();
770 Vector<String> vFiles = new Vector<String>();
771
772 //
773 // Clone Workspace
774 //
775 if (mode == DataType.RETURN_TYPE_WORKSPACE) {
776 FileOperation.copyFolder(src, trg);
777 this.returnType = DataType.RETURN_TYPE_WORKSPACE;
778 }
779
780 //
781 // Clone Module Surface Area
782 //
783 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
784 //
785 // Get target path from source path
786 //
787 trg = getModulePath();
788 newId.setPath(trg);
789 vFiles = wt.getAllFilesPathOfModule(src);
790
791 String oldPackagePath = GlobalData.openingModuleList.getIdByPath(src).getPackageId().getPath();
792 String newPackagePath = packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()).getPath();
793
794 //
795 // First copy all files to new directory
796 //
797 FileOperation.copyFile(src, trg);
798 for (int index = 1; index < vFiles.size(); index++) {
799 String oldFile = vFiles.get(index);
800 String newFile = "";
801 if (oldFile.indexOf(Tools.getFilePathOnly(src)) > -1) {
802 //
803 // The file is not include header
804 //
805 newFile = oldFile.replace(Tools.getFilePathOnly(src), Tools.getFilePathOnly(trg));
806 } else if (oldFile.indexOf(Tools.getFilePathOnly(oldPackagePath)) > -1) {
807 //
808 // The file is include header
809 //
810 newFile = oldFile.replace(Tools.getFilePathOnly(oldPackagePath),
811 Tools.getFilePathOnly(newPackagePath));
812 }
813
814 FileOperation.copyFile(oldFile, newFile);
815 }
816
817 //
818 // Create new msa file
819 //
820 ModuleSurfaceArea msa = null;
821 msa = OpenFile.openMsaFile(src);
822
823 //
824 // Update to memory
825 //
826 msa.getMsaHeader().setModuleName(newId.getName());
827 msa.getMsaHeader().setGuidValue(newId.getGuid());
828 msa.getMsaHeader().setVersion(newId.getVersion());
829
830 //
831 // Update <Cloned> Section
832 //
833 updateModuleClonedId(msa, oldId);
834
835 //
836 // Save to file
837 //
838 SaveFile.saveMsaFile(trg, msa);
839
840 //
841 // Update to platformId
842 //
843 this.setMid(new ModuleIdentification(newId,
844 packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex())));
845
846 //
847 // Open belonging package
848 //
849 PackageSurfaceArea psa = PackageSurfaceArea.Factory.newInstance();
850 psa = OpenFile.openSpdFile(mid.getPackageId().getPath());
851
852 //
853 // Update the db file
854 //
855 wt.addModuleToPackage(mid, psa);
856
857 //
858 // Update GlobalData
859 //
860 GlobalData.vModuleList.addElement(mid);
861 GlobalData.openingModuleList.insertToOpeningModuleList(mid, msa);
862
863 this.returnType = DataType.RETURN_TYPE_MODULE_SURFACE_AREA;
864 }
865
866 //
867 // Clone Package Surface Area
868 //
869 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
870 //
871 // Get target path from source path
872 //
873 trg = this.getPackagePath();
874 newId.setPath(trg);
875 vFiles = wt.getAllFilesPathOfPakcage(src);
876
877 //
878 // First copy all files to new directory
879 //
880 FileOperation.copyFile(src, trg);
881 for (int index = 1; index < vFiles.size(); index++) {
882 String oldFile = vFiles.get(index);
883 String newFile = vFiles.get(index).replace(Tools.getFilePathOnly(src), Tools.getFilePathOnly(trg));
884 FileOperation.copyFile(oldFile, newFile);
885 }
886
887 //
888 // Create new spd file
889 //
890 PackageSurfaceArea spd = null;
891 spd = OpenFile.openSpdFile(src);
892
893 //
894 // Update to memory
895 //
896 spd.getSpdHeader().setPackageName(newId.getName());
897 spd.getSpdHeader().setGuidValue(newId.getGuid());
898 spd.getSpdHeader().setVersion(newId.getVersion());
899
900 //
901 // Update <Cloned> Section
902 //
903 updatePackageClonedId(spd, oldId);
904
905 //
906 // Save to file
907 //
908 SaveFile.saveSpdFile(trg, spd);
909
910 //
911 // Update to platformId
912 //
913 this.setPid(new PackageIdentification(newId));
914
915 //
916 // Update the db file
917 //
918 wt.addPackageToDatabase(pid);
919
920 //
921 // Update GlobalData
922 //
923 GlobalData.vPackageList.addElement(pid);
924 GlobalData.openingPackageList.insertToOpeningPackageList(pid, spd);
925
926 //
927 // Add all cloned modules
928 //
929 Vector<String> modulePaths = GlobalData.getAllModulesOfPackage(pid.getPath());
930 String modulePath = null;
931 ModuleSurfaceArea msa = null;
932
933 for (int indexJ = 0; indexJ < modulePaths.size(); indexJ++) {
934 try {
935 modulePath = modulePaths.get(indexJ);
936 msa = OpenFile.openMsaFile(modulePath);
937 } catch (IOException e) {
938 Log.err("Open Module Surface Area " + modulePath, e.getMessage());
939 continue;
940 } catch (XmlException e) {
941 Log.err("Open Module Surface Area " + modulePath, e.getMessage());
942 continue;
943 } catch (Exception e) {
944 Log.err("Open Module Surface Area " + modulePath, "Invalid file type");
945 continue;
946 }
947 Identification id = Tools.getId(modulePath, msa);
948 mid = new ModuleIdentification(id, pid);
949 GlobalData.vModuleList.addElement(mid);
950 GlobalData.openingModuleList.insertToOpeningModuleList(mid, msa);
951 }
952
953 this.returnType = DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA;
954 }
955
956 //
957 // Clone Platform Surface Area
958 //
959 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
960 PlatformSurfaceArea fpd = null;
961 fpd = OpenFile.openFpdFile(src);
962
963 //
964 // Update to memory
965 //
966 fpd.getPlatformHeader().setPlatformName(newId.getName());
967 fpd.getPlatformHeader().setGuidValue(newId.getGuid());
968 fpd.getPlatformHeader().setVersion(newId.getVersion());
969
970 //
971 // Update Cloned From element
972 //
973 updatePlatformClonedId(fpd, oldId);
974
975 //
976 // Save to file
977 //
978 SaveFile.saveFpdFile(trg, fpd);
979
980 //
981 // Update to platformId
982 //
983 this.setFid(new PlatformIdentification(newId));
984
985 //
986 // Update the db file
987 //
988 wt.addPlatformToDatabase(fid);
989
990 //
991 // Update GlobalData
992 //
993 GlobalData.vPlatformList.addElement(fid);
994 GlobalData.openingPlatformList.insertToOpeningPlatformList(fid, fpd);
995
996 this.returnType = DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA;
997 }
998 vFiles = null;
999 }
1000
1001 /**
1002 Get the path of selected package
1003
1004 @return String The path of selected package
1005
1006 **/
1007 private String getSelectPackagePath() {
1008 return Tools.getFilePathOnly(packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()).getPath());
1009 }
1010
1011 /**
1012 Get the path of source module
1013 Since the path of source module is relative, make it up to full path.
1014
1015 @return String The full path of source module
1016
1017 **/
1018 private String getModulePath() {
1019 String trg = this.jTextFieldFilePath.getText();
1020 trg = Tools.addPathExt(trg, mode);
1021 trg = Tools.addFileSeparator(getSelectPackagePath()) + trg;
1022 Tools.convertPathToCurrentOsType(trg);
1023 return trg;
1024 }
1025
1026 /**
1027 Get the path of source package
1028 Since the path of source package is relative, make it up to full path.
1029
1030 @return String The full path of source package
1031
1032 **/
1033 private String getPackagePath() {
1034 String trg = this.jTextFieldFilePath.getText();
1035 trg = Tools.addPathExt(trg, mode);
1036 trg = Tools.addFileSeparator(Workspace.getCurrentWorkspace()) + trg;
1037 trg = Tools.convertPathToCurrentOsType(trg);
1038 return trg;
1039 }
1040
1041 /**
1042 Set msa file's <Cloned> section via given identification
1043
1044 @param msa ModuleSurfaceArea for clone target
1045 @param id Identification of clone source
1046
1047 **/
1048 private void updateModuleClonedId(ModuleSurfaceArea msa, Identification id) {
1049 //
1050 // Get PlatformDefinitions First
1051 //
1052 ModuleDefinitions pd = null;
1053 if (msa.getModuleDefinitions() == null) {
1054 pd = ModuleDefinitions.Factory.newInstance();
1055 msa.addNewModuleDefinitions();
1056 } else {
1057 pd = msa.getModuleDefinitions();
1058 }
1059
1060 //
1061 // Get ClonedFrom then
1062 //
1063 ModuleDefinitions.ClonedFrom cf = null;
1064 BigInteger count = new BigInteger("-1");
1065 if (pd.getClonedFrom() == null) {
1066 cf = ModuleDefinitions.ClonedFrom.Factory.newInstance();
1067 } else {
1068 cf = pd.getClonedFrom();
1069 if (cf != null) {
1070 for (int index = 0; index < cf.getClonedList().size(); index++) {
1071 if (cf.getClonedList().get(index).getId() != null) {
1072 count = count.max(cf.getClonedList().get(index).getId());
1073 }
1074 }
1075 }
1076 }
1077
1078 //
1079 // Set new Cloned item
1080 //
1081 ModuleDefinitions.ClonedFrom.Cloned c = ModuleDefinitions.ClonedFrom.Cloned.Factory.newInstance();
1082 c.setModuleGuid(id.getGuid());
1083 c.setModuleVersion(id.getVersion());
1084 c.setPackageGuid(wt.getPackageIdByModuleId(oldId).getGuid());
1085 c.setPackageVersion(wt.getPackageIdByModuleId(oldId).getVersion());
1086 c.setId(count.add(new BigInteger("1")));
1087 String guid = wt.getModuleFarGuid(oldId);
1088 if (guid != null && !guid.equals("")) {
1089 c.setFarGuid(guid);
1090 }
1091
1092 cf.addNewCloned();
1093 cf.setClonedArray(cf.getClonedList().size() - 1, c);
1094 pd.addNewClonedFrom();
1095 pd.setClonedFrom(cf);
1096 msa.setModuleDefinitions(pd);
1097 }
1098
1099 /**
1100 Set spd file's <Cloned> section via given identification
1101
1102 @param spd PackageSurfaceArea for clone target
1103 @param id Identification of clone source
1104
1105 **/
1106 private void updatePackageClonedId(PackageSurfaceArea spd, Identification id) {
1107 //
1108 // Get PlatformDefinitions First
1109 //
1110 PackageDefinitions pd = null;
1111 if (spd.getPackageDefinitions() == null) {
1112 pd = PackageDefinitions.Factory.newInstance();
1113 spd.addNewPackageDefinitions();
1114 } else {
1115 pd = spd.getPackageDefinitions();
1116 }
1117
1118 //
1119 // Get ClonedFrom then
1120 //
1121 PackageDefinitions.ClonedFrom cf = null;
1122 BigInteger count = new BigInteger("-1");
1123 if (pd.getClonedFrom() == null) {
1124 cf = PackageDefinitions.ClonedFrom.Factory.newInstance();
1125 } else {
1126 cf = pd.getClonedFrom();
1127 if (cf != null) {
1128 for (int index = 0; index < cf.getClonedList().size(); index++) {
1129 if (cf.getClonedList().get(index).getId() != null) {
1130 count = count.max(cf.getClonedList().get(index).getId());
1131 }
1132 }
1133 }
1134 }
1135
1136 //
1137 // Set new Cloned item
1138 //
1139 PackageDefinitions.ClonedFrom.Cloned c = PackageDefinitions.ClonedFrom.Cloned.Factory.newInstance();
1140 c.setPackageGuid(id.getGuid());
1141 c.setPackageVersion(id.getVersion());
1142 c.setId(count.add(new BigInteger("1")));
1143 String guid = wt.getPackageFarGuid(oldId);
1144 if (guid != null && !guid.equals("")) {
1145 c.setFarGuid(guid);
1146 }
1147
1148 cf.addNewCloned();
1149 cf.setClonedArray(cf.getClonedList().size() - 1, c);
1150 pd.addNewClonedFrom();
1151 pd.setClonedFrom(cf);
1152 spd.setPackageDefinitions(pd);
1153 }
1154
1155 /**
1156 Set fpd file's <Cloned> section via given identification
1157
1158 @param fpd PlatformSurfaceArea for clone target
1159 @param id Identification of clone source
1160
1161 **/
1162 private void updatePlatformClonedId(PlatformSurfaceArea fpd, Identification id) {
1163 //
1164 // Get PlatformDefinitions First
1165 //
1166 PlatformDefinitions pd = null;
1167 if (fpd.getPlatformDefinitions() == null) {
1168 pd = PlatformDefinitions.Factory.newInstance();
1169 fpd.addNewPlatformDefinitions();
1170 } else {
1171 pd = fpd.getPlatformDefinitions();
1172 }
1173
1174 //
1175 // Get ClonedFrom then
1176 //
1177 PlatformDefinitions.ClonedFrom cf = null;
1178 BigInteger count = new BigInteger("-1");
1179 if (pd.getClonedFrom() == null) {
1180 cf = PlatformDefinitions.ClonedFrom.Factory.newInstance();
1181 } else {
1182 cf = pd.getClonedFrom();
1183 if (cf != null) {
1184 for (int index = 0; index < cf.getClonedList().size(); index++) {
1185 if (cf.getClonedList().get(index).getId() != null) {
1186 count = count.max(cf.getClonedList().get(index).getId());
1187 }
1188 }
1189 }
1190 }
1191
1192 //
1193 // Set new Cloned item
1194 //
1195 PlatformDefinitions.ClonedFrom.Cloned c = PlatformDefinitions.ClonedFrom.Cloned.Factory.newInstance();
1196 c.setPlatformGuid(id.getGuid());
1197 c.setPlatformVersion(id.getVersion());
1198 c.setId(count.add(new BigInteger("1")));
1199 String guid = wt.getPlatformFarGuid(oldId);
1200 if (guid != null && !guid.equals("")) {
1201 c.setFarGuid(guid);
1202 }
1203
1204 cf.addNewCloned();
1205 cf.setClonedArray(cf.getClonedList().size() - 1, c);
1206 pd.addNewClonedFrom();
1207 pd.setClonedFrom(cf);
1208 fpd.setPlatformDefinitions(pd);
1209 }
1210
1211 /**
1212 Get PlatformIdentification
1213
1214 @return PlatformIdentification
1215
1216 **/
1217 public PlatformIdentification getFid() {
1218 return fid;
1219 }
1220
1221 /**
1222 Set PlatformIdentification
1223
1224 @param fid PlatformIdentification
1225
1226 **/
1227 public void setFid(PlatformIdentification fid) {
1228 this.fid = fid;
1229 }
1230
1231 /**
1232 Get ModuleIdentification
1233
1234 @return ModuleIdentification
1235
1236 **/
1237 public ModuleIdentification getMid() {
1238 return mid;
1239 }
1240
1241 /**
1242 Set ModuleIdentification
1243
1244 @param mid ModuleIdentification
1245
1246 **/
1247 public void setMid(ModuleIdentification mid) {
1248 this.mid = mid;
1249 }
1250
1251 /**
1252 Get PackageIdentification
1253
1254 @return PackageIdentification
1255
1256 **/
1257 public PackageIdentification getPid() {
1258 return pid;
1259 }
1260
1261 /**
1262 Set PackageIdentification
1263
1264 @param pid PackageIdentification
1265
1266 **/
1267 public void setPid(PackageIdentification pid) {
1268 this.pid = pid;
1269 }
1270 }