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