]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/SelectModuleBelong.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / SelectModuleBelong.java
1 /** @file
2
3 The file is used to let user choose to create module in an existing package
4 or to create a new package first.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16 package org.tianocore.frameworkwizard;
17
18 import java.awt.event.ActionEvent;
19 import java.io.File;
20 import java.util.Vector;
21
22 import javax.swing.JButton;
23 import javax.swing.JComboBox;
24 import javax.swing.JFileChooser;
25 import javax.swing.JLabel;
26 import javax.swing.JPanel;
27 import javax.swing.JTextField;
28
29 import org.tianocore.ModuleSurfaceAreaDocument;
30 import org.tianocore.MsaHeaderDocument;
31 import org.tianocore.SpdHeaderDocument;
32 import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
33 import org.tianocore.PlatformHeaderDocument.PlatformHeader;
34 import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;
35 import org.tianocore.frameworkwizard.common.DataType;
36 import org.tianocore.frameworkwizard.common.DataValidation;
37 import org.tianocore.frameworkwizard.common.IFileFilter;
38 import org.tianocore.frameworkwizard.common.Log;
39 import org.tianocore.frameworkwizard.common.SaveFile;
40 import org.tianocore.frameworkwizard.common.Tools;
41 import org.tianocore.frameworkwizard.common.ui.IDialog;
42 import org.tianocore.frameworkwizard.common.ui.IFrame;
43 import org.tianocore.frameworkwizard.common.ui.StarLabel;
44 import org.tianocore.frameworkwizard.module.Identification.ModuleIdentification;
45 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
46 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
47 import org.tianocore.frameworkwizard.workspace.Workspace;
48 import org.tianocore.frameworkwizard.workspace.WorkspaceTools;
49
50 public class SelectModuleBelong extends IDialog {
51
52 /**
53 Define class members
54
55 **/
56 private static final long serialVersionUID = 4171355136991448972L;
57
58 private JPanel jContentPane = null;
59
60 private JComboBox jComboBoxExistingPackage = null;
61
62 private JButton jButtonOk = null;
63
64 private JButton jButtonCancel = null;
65
66 private Vector<PackageIdentification> packages = null;
67
68 private JLabel jLabelPackage = null;
69
70 private JLabel jLabelFilePath = null;
71
72 private JTextField jTextFieldFilePath = null;
73
74 private JButton jButtonBrowse = null;
75
76 private JLabel jLabelName = null;
77
78 private JTextField jTextFieldName = null;
79
80 private JLabel jLabelGuid = null;
81
82 private JTextField jTextFieldGuid = null;
83
84 private JButton jButtonGen = null;
85
86 private JLabel jLabelVersion = null;
87
88 private JTextField jTextFieldVersion = null;
89
90 private StarLabel jStarLabel1 = null;
91
92 private StarLabel jStarLabel2 = null;
93
94 private StarLabel jStarLabel3 = null;
95
96 private StarLabel jStarLabel4 = null;
97
98 private StarLabel jStarLabel5 = null;
99
100 private WorkspaceTools wt = new WorkspaceTools();
101
102 private ModuleIdentification mid = null;
103
104 private PackageIdentification pid = null;
105
106 private PlatformIdentification fid = null;
107
108 private int mode = -1;
109
110 /**
111 * This method initializes jComboBoxExistingPackage
112 *
113 * @return javax.swing.JComboBox
114 */
115 private JComboBox getJComboBoxExistingPackage() {
116 if (jComboBoxExistingPackage == null) {
117 jComboBoxExistingPackage = new JComboBox();
118 jComboBoxExistingPackage.setBounds(new java.awt.Rectangle(140, 10, 340, 20));
119 }
120 return jComboBoxExistingPackage;
121 }
122
123 /**
124 * This method initializes jButtonNext
125 *
126 * @return javax.swing.JButton
127 */
128 private JButton getJButtonOk() {
129 if (jButtonOk == null) {
130 jButtonOk = new JButton();
131 jButtonOk.setBounds(new java.awt.Rectangle(310, 140, 80, 20));
132 jButtonOk.setText("Ok");
133 jButtonOk.addActionListener(this);
134 }
135 return jButtonOk;
136 }
137
138 /**
139 * This method initializes jButtonCancel
140 *
141 * @return javax.swing.JButton
142 */
143 private JButton getJButtonCancel() {
144 if (jButtonCancel == null) {
145 jButtonCancel = new JButton();
146 jButtonCancel.setBounds(new java.awt.Rectangle(395, 140, 80, 20));
147 jButtonCancel.setText("Cancel");
148 jButtonCancel.addActionListener(this);
149 }
150 return jButtonCancel;
151 }
152
153 /**
154 * This method initializes jTextFieldFilePath
155 *
156 * @return javax.swing.JTextField
157 */
158 private JTextField getJTextFieldFilePath() {
159 if (jTextFieldFilePath == null) {
160 jTextFieldFilePath = new JTextField();
161 jTextFieldFilePath.setBounds(new java.awt.Rectangle(140, 35, 250, 20));
162 }
163 return jTextFieldFilePath;
164 }
165
166 /**
167 * This method initializes jButtonBrowse
168 *
169 * @return javax.swing.JButton
170 */
171 private JButton getJButtonBrowse() {
172 if (jButtonBrowse == null) {
173 jButtonBrowse = new JButton();
174 jButtonBrowse.setBounds(new java.awt.Rectangle(395, 35, 85, 20));
175 jButtonBrowse.setText("Browse");
176 jButtonBrowse.addActionListener(this);
177 }
178 return jButtonBrowse;
179 }
180
181 /**
182 * This method initializes jTextFieldModuleName
183 *
184 * @return javax.swing.JTextField
185 */
186 private JTextField getJTextFieldName() {
187 if (jTextFieldName == null) {
188 jTextFieldName = new JTextField();
189 jTextFieldName.setBounds(new java.awt.Rectangle(140, 60, 340, 20));
190 }
191 return jTextFieldName;
192 }
193
194 /**
195 * This method initializes jTextFieldGuid
196 *
197 * @return javax.swing.JTextField
198 */
199 private JTextField getJTextFieldGuid() {
200 if (jTextFieldGuid == null) {
201 jTextFieldGuid = new JTextField();
202 jTextFieldGuid.setBounds(new java.awt.Rectangle(140, 85, 250, 20));
203 }
204 return jTextFieldGuid;
205 }
206
207 /**
208 * This method initializes jButtonGen
209 *
210 * @return javax.swing.JButton
211 */
212 private JButton getJButtonGen() {
213 if (jButtonGen == null) {
214 jButtonGen = new JButton();
215 jButtonGen.setBounds(new java.awt.Rectangle(395, 85, 85, 20));
216 jButtonGen.setText("GEN");
217 jButtonGen.addActionListener(this);
218 }
219 return jButtonGen;
220 }
221
222 /**
223 * This method initializes jTextFieldVersion
224 *
225 * @return javax.swing.JTextField
226 */
227 private JTextField getJTextFieldVersion() {
228 if (jTextFieldVersion == null) {
229 jTextFieldVersion = new JTextField();
230 jTextFieldVersion.setBounds(new java.awt.Rectangle(140, 110, 340, 20));
231 }
232 return jTextFieldVersion;
233 }
234
235 /**
236 * @param args
237 */
238 public static void main(String[] args) {
239 SelectModuleBelong smb = new SelectModuleBelong();
240 smb.setVisible(true);
241 }
242
243 /**
244 * This is the default constructor
245 */
246 public SelectModuleBelong() {
247 super();
248 init();
249 }
250
251 /**
252 * This is the default constructor
253 */
254 public SelectModuleBelong(IFrame parentFrame, boolean modal, int fileType) {
255 super(parentFrame, modal);
256 this.mode = fileType;
257 init();
258 initExistingPackage();
259 if (mode != DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
260 this.jStarLabel1.setVisible(false);
261 this.jLabelPackage.setVisible(false);
262 this.jComboBoxExistingPackage.setVisible(false);
263 }
264 }
265
266 /**
267 * This method initializes this
268 *
269 * @return void
270 */
271 private void init() {
272 this.setSize(500, 200);
273 this.setContentPane(getJContentPane());
274 this.setTitle("New Module");
275 this.centerWindow();
276 }
277
278 /**
279 * This method initializes jContentPane
280 *
281 * @return javax.swing.JPanel
282 */
283 private JPanel getJContentPane() {
284 if (jContentPane == null) {
285 jLabelVersion = new JLabel();
286 jLabelVersion.setBounds(new java.awt.Rectangle(15, 110, 120, 20));
287 jLabelVersion.setText("Version");
288 jLabelGuid = new JLabel();
289 jLabelGuid.setBounds(new java.awt.Rectangle(15, 85, 120, 20));
290 jLabelGuid.setText("Guid");
291 jLabelName = new JLabel();
292 jLabelName.setBounds(new java.awt.Rectangle(15, 60, 120, 20));
293 jLabelName.setText("Name");
294 jLabelFilePath = new JLabel();
295 jLabelFilePath.setBounds(new java.awt.Rectangle(15, 35, 120, 20));
296 jLabelFilePath.setText("File Path");
297 jLabelPackage = new JLabel();
298 jLabelPackage.setBounds(new java.awt.Rectangle(15, 10, 120, 20));
299 jLabelPackage.setText("Choose Package");
300 jContentPane = new JPanel();
301 jContentPane.setLayout(null);
302 jContentPane.setSize(new java.awt.Dimension(490, 175));
303 jContentPane.add(getJComboBoxExistingPackage(), null);
304 jContentPane.add(getJButtonOk(), null);
305 jContentPane.add(getJButtonCancel(), null);
306 jContentPane.add(jLabelPackage, null);
307 jContentPane.add(jLabelFilePath, null);
308 jContentPane.add(getJTextFieldFilePath(), null);
309 jContentPane.add(getJButtonBrowse(), null);
310 jContentPane.add(jLabelName, null);
311 jContentPane.add(getJTextFieldName(), null);
312 jContentPane.add(jLabelGuid, null);
313 jContentPane.add(getJTextFieldGuid(), null);
314 jContentPane.add(getJButtonGen(), null);
315 jContentPane.add(jLabelVersion, null);
316 jContentPane.add(getJTextFieldVersion(), null);
317
318 jStarLabel1 = new StarLabel();
319 jStarLabel1.setLocation(new java.awt.Point(0, 10));
320 jStarLabel2 = new StarLabel();
321 jStarLabel2.setLocation(new java.awt.Point(0, 35));
322 jStarLabel3 = new StarLabel();
323 jStarLabel3.setLocation(new java.awt.Point(0, 60));
324 jStarLabel4 = new StarLabel();
325 jStarLabel4.setLocation(new java.awt.Point(0, 85));
326 jStarLabel5 = new StarLabel();
327 jStarLabel5.setLocation(new java.awt.Point(0, 110));
328 jContentPane.add(jStarLabel1, null);
329 jContentPane.add(jStarLabel2, null);
330 jContentPane.add(jStarLabel3, null);
331 jContentPane.add(jStarLabel4, null);
332 jContentPane.add(jStarLabel5, null);
333 }
334 return jContentPane;
335 }
336
337 /**
338 Query all existing packages and fill them into combox
339
340 **/
341 private void initExistingPackage() {
342 packages = wt.getAllPackages();
343 for (int index = 0; index < packages.size(); index++) {
344 this.jComboBoxExistingPackage.addItem(packages.elementAt(index).getName());
345 }
346 }
347
348 /* (non-Javadoc)
349 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
350 *
351 * Override actionPerformed to listen all actions
352 */
353 public void actionPerformed(ActionEvent arg0) {
354 if (arg0.getSource() == jButtonCancel) {
355 this.setVisible(false);
356 this.returnType = DataType.RETURN_TYPE_CANCEL;
357 }
358
359 if (arg0.getSource() == jButtonOk) {
360 if (this.check()) {
361 this.save();
362 } else {
363 return;
364 }
365 this.setVisible(false);
366 this.returnType = DataType.RETURN_TYPE_OK;
367 }
368
369 if (arg0.getSource() == this.jButtonGen) {
370 this.jTextFieldGuid.setText(Tools.generateUuidString());
371 }
372
373 if (arg0.getSource() == this.jButtonBrowse) {
374 JFileChooser fc = new JFileChooser();
375 fc.setAcceptAllFileFilterUsed(false);
376
377 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
378 fc.setCurrentDirectory(new File(packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex())
379 .getPath()));
380 fc.addChoosableFileFilter(new IFileFilter(DataType.MODULE_SURFACE_AREA_EXT));
381 }
382 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
383 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
384 fc.addChoosableFileFilter(new IFileFilter(DataType.PACKAGE_SURFACE_AREA_EXT));
385 }
386 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
387 fc.setCurrentDirectory(new File(Workspace.getCurrentWorkspace()));
388 fc.addChoosableFileFilter(new IFileFilter(DataType.PLATFORM_SURFACE_AREA_EXT));
389 }
390 int result = fc.showSaveDialog(new JPanel());
391 if (result == JFileChooser.APPROVE_OPTION) {
392 this.jTextFieldFilePath.setText(Tools.addPathExt(fc.getSelectedFile().getPath(), mode));
393 }
394 }
395 }
396
397 /**
398 Data validation for all fields
399
400 @retval true - All datas are valid
401 @retval false - At least one data is invalid
402
403 **/
404 public boolean check() {
405 //
406 // Check if all required fields are not empty
407 //
408 if (isEmpty(this.jTextFieldFilePath.getText())) {
409 Log.err("File Path couldn't be empty");
410 return false;
411 }
412 if (isEmpty(this.jTextFieldName.getText())) {
413 Log.err("Name couldn't be empty");
414 return false;
415 }
416 if (isEmpty(this.jTextFieldGuid.getText())) {
417 Log.err("Guid Value couldn't be empty");
418 return false;
419 }
420 if (isEmpty(this.jTextFieldVersion.getText())) {
421 Log.err("Version couldn't be empty");
422 return false;
423 }
424
425 //
426 // Check if all fields have correct data types
427 //
428 if (!DataValidation.isBaseName(this.jTextFieldName.getText())) {
429 Log.err("Incorrect data type for Base Name");
430 return false;
431 }
432 if (!DataValidation.isGuid((this.jTextFieldGuid).getText())) {
433 Log.err("Incorrect data type for Guid");
434 return false;
435 }
436
437 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
438 //
439 // Check if the module is already existed in current package
440 //
441 String packagePath = packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()).getPath();
442 String modulePath = Tools.convertPathToCurrentOsType(this.jTextFieldFilePath.getText());
443 Vector<String> msaFile = wt.getAllModulesOfPackage(packagePath);
444 packagePath = packagePath.substring(0, packagePath.lastIndexOf(DataType.FILE_SEPARATOR));
445 String path = Tools.getRelativePath(modulePath, packagePath);
446
447 for (int index = 0; index < msaFile.size(); index++) {
448 if (msaFile.elementAt(index).equals(path)) {
449 Log.err("This module is already existing in selected package");
450 return false;
451 }
452 }
453 }
454
455 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
456 //
457 // Check if the package is already existed in database
458 //
459 String path = Tools.convertPathToCurrentOsType(this.jTextFieldFilePath.getText());
460 Vector<PackageIdentification> vPackageList = wt.getAllPackages();
461 if (vPackageList != null && vPackageList.size() > 0) {
462 for (int index = 0; index < vPackageList.size(); index++) {
463 if (vPackageList.get(index).getPath().equals(path)) {
464 Log.err("This package is already existing in database");
465 return false;
466 }
467 }
468 }
469 }
470
471 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
472 //
473 // Check if the platform is already existed in database
474 //
475 String path = Tools.convertPathToCurrentOsType(this.jTextFieldFilePath.getText());
476 Vector<PlatformIdentification> vPlatfromList = wt.getAllPlatforms();
477 if (vPlatfromList != null && vPlatfromList.size() > 0) {
478 for (int index = 0; index < vPlatfromList.size(); index++) {
479 if (vPlatfromList.get(index).getPath().equals(path)) {
480 Log.err("This platform is already existing in database");
481 return false;
482 }
483 }
484 }
485 }
486
487 return true;
488 }
489
490 /**
491 Save file
492
493 **/
494 public void save() {
495 if (mode == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
496 this.saveModule();
497 }
498 if (mode == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
499 this.savePackage();
500 }
501 if (mode == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
502 this.savePlatform();
503 }
504 }
505
506 /**
507 Save all components of Msa Header
508
509 **/
510 private void saveModule() {
511 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = null;
512 String path = Tools.convertPathToCurrentOsType(this.jTextFieldFilePath.getText());
513
514 //
515 // Save to memory
516 //
517 try {
518 MsaHeaderDocument.MsaHeader msaHeader = null;
519
520 msa = ModuleSurfaceAreaDocument.ModuleSurfaceArea.Factory.newInstance();
521 msaHeader = MsaHeaderDocument.MsaHeader.Factory.newInstance();
522
523 msaHeader.setModuleName(this.jTextFieldName.getText());
524 msaHeader.setGuidValue(this.jTextFieldGuid.getText());
525 msaHeader.setVersion(this.jTextFieldVersion.getText());
526
527 msa.setMsaHeader(msaHeader);
528 } catch (Exception e) {
529 Log.err("Save ModuleSurfaceArea Document", e.getMessage());
530 return;
531 }
532
533 //
534 // Save to real file
535 //
536 try {
537 SaveFile.saveMsaFile(path, msa);
538
539 } catch (Exception e) {
540 Log.err("Save Module to file system", e.getMessage());
541 return;
542 }
543
544 //
545 // Save to identification
546 //
547 mid = new ModuleIdentification(this.jTextFieldName.getText(), this.jTextFieldGuid.getText(),
548 this.jTextFieldVersion.getText(), path);
549 mid.setPackageId(packages.elementAt(this.jComboBoxExistingPackage.getSelectedIndex()));
550 }
551
552 /**
553 Save all components of Spd Header
554
555 **/
556 private void savePackage() {
557 PackageSurfaceArea spd = null;
558 String path = Tools.convertPathToCurrentOsType(this.jTextFieldFilePath.getText());
559
560 //
561 // Save to memory
562 //
563 try {
564 SpdHeaderDocument.SpdHeader spdHeader = null;
565
566 spd = PackageSurfaceArea.Factory.newInstance();
567 spdHeader = SpdHeaderDocument.SpdHeader.Factory.newInstance();
568
569 spdHeader.setPackageName(this.jTextFieldName.getText());
570 spdHeader.setGuidValue(this.jTextFieldGuid.getText());
571 spdHeader.setVersion(this.jTextFieldVersion.getText());
572
573 spd.setSpdHeader(spdHeader);
574 } catch (Exception e) {
575 Log.err("Save PackageSurfaceArea Document", e.getMessage());
576 return;
577 }
578
579 //
580 // Save to real file
581 //
582 try {
583 SaveFile.saveSpdFile(path, spd);
584
585 } catch (Exception e) {
586 Log.err("Save Package to file system", e.getMessage());
587 return;
588 }
589
590 //
591 // Save to identification
592 //
593 pid = new PackageIdentification(this.jTextFieldName.getText(), this.jTextFieldGuid.getText(),
594 this.jTextFieldVersion.getText(), path);
595 }
596
597 /**
598 Save all components of Fpd Header
599
600 **/
601 private void savePlatform() {
602 PlatformSurfaceArea fpd = null;
603 String path = Tools.convertPathToCurrentOsType(this.jTextFieldFilePath.getText());
604
605 //
606 // Save to memory
607 //
608 try {
609 PlatformHeader fpdHeader = null;
610
611 fpd = PlatformSurfaceArea.Factory.newInstance();
612 fpdHeader = PlatformHeader.Factory.newInstance();
613
614 fpdHeader.setPlatformName(this.jTextFieldName.getText());
615 fpdHeader.setGuidValue(this.jTextFieldGuid.getText());
616 fpdHeader.setVersion(this.jTextFieldVersion.getText());
617
618 fpd.setPlatformHeader(fpdHeader);
619 } catch (Exception e) {
620 Log.err("Save FrameworkPlatformDescription Document", e.getMessage());
621 return;
622 }
623
624 //
625 // Save to real file
626 //
627 try {
628 SaveFile.saveFpdFile(path, fpd);
629
630 } catch (Exception e) {
631 Log.err("Save Platform to file system", e.getMessage());
632 return;
633 }
634
635 //
636 // Save to identification
637 //
638 fid = new PlatformIdentification(this.jTextFieldName.getText(), this.jTextFieldGuid.getText(),
639 this.jTextFieldVersion.getText(), path);
640 }
641
642 public ModuleIdentification getMid() {
643 return mid;
644 }
645
646 public void setMid(ModuleIdentification mid) {
647 this.mid = mid;
648 }
649
650 public PlatformIdentification getFid() {
651 return fid;
652 }
653
654 public void setFid(PlatformIdentification fid) {
655 this.fid = fid;
656 }
657
658 public PackageIdentification getPid() {
659 return pid;
660 }
661
662 public void setPid(PackageIdentification pid) {
663 this.pid = pid;
664 }
665 }