3 The file is used to let user choose to create module in an existing package
4 or to create a new package first.
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
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.
16 package org
.tianocore
.frameworkwizard
;
18 import java
.awt
.event
.ActionEvent
;
20 import java
.util
.Vector
;
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
;
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
;
50 public class SelectModuleBelong
extends IDialog
{
56 private static final long serialVersionUID
= 4171355136991448972L;
58 private JPanel jContentPane
= null;
60 private JComboBox jComboBoxExistingPackage
= null;
62 private JButton jButtonOk
= null;
64 private JButton jButtonCancel
= null;
66 private Vector
<PackageIdentification
> packages
= null;
68 private JLabel jLabelPackage
= null;
70 private JLabel jLabelFilePath
= null;
72 private JTextField jTextFieldFilePath
= null;
74 private JButton jButtonBrowse
= null;
76 private JLabel jLabelName
= null;
78 private JTextField jTextFieldName
= null;
80 private JLabel jLabelGuid
= null;
82 private JTextField jTextFieldGuid
= null;
84 private JButton jButtonGen
= null;
86 private JLabel jLabelVersion
= null;
88 private JTextField jTextFieldVersion
= null;
90 private StarLabel jStarLabel1
= null;
92 private StarLabel jStarLabel2
= null;
94 private StarLabel jStarLabel3
= null;
96 private StarLabel jStarLabel4
= null;
98 private StarLabel jStarLabel5
= null;
100 private WorkspaceTools wt
= new WorkspaceTools();
102 private ModuleIdentification mid
= null;
104 private PackageIdentification pid
= null;
106 private PlatformIdentification fid
= null;
108 private int mode
= -1;
111 * This method initializes jComboBoxExistingPackage
113 * @return javax.swing.JComboBox
115 private JComboBox
getJComboBoxExistingPackage() {
116 if (jComboBoxExistingPackage
== null) {
117 jComboBoxExistingPackage
= new JComboBox();
118 jComboBoxExistingPackage
.setBounds(new java
.awt
.Rectangle(140, 10, 340, 20));
120 return jComboBoxExistingPackage
;
124 * This method initializes jButtonNext
126 * @return javax.swing.JButton
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);
139 * This method initializes jButtonCancel
141 * @return javax.swing.JButton
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);
150 return jButtonCancel
;
154 * This method initializes jTextFieldFilePath
156 * @return javax.swing.JTextField
158 private JTextField
getJTextFieldFilePath() {
159 if (jTextFieldFilePath
== null) {
160 jTextFieldFilePath
= new JTextField();
161 jTextFieldFilePath
.setBounds(new java
.awt
.Rectangle(140, 35, 250, 20));
163 return jTextFieldFilePath
;
167 * This method initializes jButtonBrowse
169 * @return javax.swing.JButton
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);
178 return jButtonBrowse
;
182 * This method initializes jTextFieldModuleName
184 * @return javax.swing.JTextField
186 private JTextField
getJTextFieldName() {
187 if (jTextFieldName
== null) {
188 jTextFieldName
= new JTextField();
189 jTextFieldName
.setBounds(new java
.awt
.Rectangle(140, 60, 340, 20));
191 return jTextFieldName
;
195 * This method initializes jTextFieldGuid
197 * @return javax.swing.JTextField
199 private JTextField
getJTextFieldGuid() {
200 if (jTextFieldGuid
== null) {
201 jTextFieldGuid
= new JTextField();
202 jTextFieldGuid
.setBounds(new java
.awt
.Rectangle(140, 85, 250, 20));
204 return jTextFieldGuid
;
208 * This method initializes jButtonGen
210 * @return javax.swing.JButton
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);
223 * This method initializes jTextFieldVersion
225 * @return javax.swing.JTextField
227 private JTextField
getJTextFieldVersion() {
228 if (jTextFieldVersion
== null) {
229 jTextFieldVersion
= new JTextField();
230 jTextFieldVersion
.setBounds(new java
.awt
.Rectangle(140, 110, 340, 20));
232 return jTextFieldVersion
;
238 public static void main(String
[] args
) {
239 SelectModuleBelong smb
= new SelectModuleBelong();
240 smb
.setVisible(true);
244 * This is the default constructor
246 public SelectModuleBelong() {
252 * This is the default constructor
254 public SelectModuleBelong(IFrame parentFrame
, boolean modal
, int fileType
) {
255 super(parentFrame
, modal
);
256 this.mode
= fileType
;
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);
267 * This method initializes this
271 private void init() {
272 this.setSize(500, 200);
273 this.setContentPane(getJContentPane());
274 this.setTitle("New Module");
279 * This method initializes jContentPane
281 * @return javax.swing.JPanel
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);
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);
338 Query all existing packages and fill them into combox
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());
349 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
351 * Override actionPerformed to listen all actions
353 public void actionPerformed(ActionEvent arg0
) {
354 if (arg0
.getSource() == jButtonCancel
) {
355 this.setVisible(false);
356 this.returnType
= DataType
.RETURN_TYPE_CANCEL
;
359 if (arg0
.getSource() == jButtonOk
) {
365 this.setVisible(false);
366 this.returnType
= DataType
.RETURN_TYPE_OK
;
369 if (arg0
.getSource() == this.jButtonGen
) {
370 this.jTextFieldGuid
.setText(Tools
.generateUuidString());
373 if (arg0
.getSource() == this.jButtonBrowse
) {
374 JFileChooser fc
= new JFileChooser();
375 fc
.setAcceptAllFileFilterUsed(false);
377 if (mode
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
378 fc
.setCurrentDirectory(new File(packages
.elementAt(this.jComboBoxExistingPackage
.getSelectedIndex())
380 fc
.addChoosableFileFilter(new IFileFilter(DataType
.MODULE_SURFACE_AREA_EXT
));
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
));
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
));
390 int result
= fc
.showSaveDialog(new JPanel());
391 if (result
== JFileChooser
.APPROVE_OPTION
) {
392 this.jTextFieldFilePath
.setText(Tools
.addPathExt(fc
.getSelectedFile().getPath(), mode
));
398 Data validation for all fields
400 @retval true - All datas are valid
401 @retval false - At least one data is invalid
404 public boolean check() {
406 // Check if all required fields are not empty
408 if (isEmpty(this.jTextFieldFilePath
.getText())) {
409 Log
.err("File Path couldn't be empty");
412 if (isEmpty(this.jTextFieldName
.getText())) {
413 Log
.err("Name couldn't be empty");
416 if (isEmpty(this.jTextFieldGuid
.getText())) {
417 Log
.err("Guid Value couldn't be empty");
420 if (isEmpty(this.jTextFieldVersion
.getText())) {
421 Log
.err("Version couldn't be empty");
426 // Check if all fields have correct data types
428 if (!DataValidation
.isBaseName(this.jTextFieldName
.getText())) {
429 Log
.err("Incorrect data type for Base Name");
432 if (!DataValidation
.isGuid((this.jTextFieldGuid
).getText())) {
433 Log
.err("Incorrect data type for Guid");
437 if (mode
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
439 // Check if the module is already existed in current package
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
);
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");
455 if (mode
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
457 // Check if the package is already existed in database
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");
471 if (mode
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
473 // Check if the platform is already existed in database
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");
495 if (mode
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
498 if (mode
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
501 if (mode
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
507 Save all components of Msa Header
510 private void saveModule() {
511 ModuleSurfaceAreaDocument
.ModuleSurfaceArea msa
= null;
512 String path
= Tools
.convertPathToCurrentOsType(this.jTextFieldFilePath
.getText());
518 MsaHeaderDocument
.MsaHeader msaHeader
= null;
520 msa
= ModuleSurfaceAreaDocument
.ModuleSurfaceArea
.Factory
.newInstance();
521 msaHeader
= MsaHeaderDocument
.MsaHeader
.Factory
.newInstance();
523 msaHeader
.setModuleName(this.jTextFieldName
.getText());
524 msaHeader
.setGuidValue(this.jTextFieldGuid
.getText());
525 msaHeader
.setVersion(this.jTextFieldVersion
.getText());
527 msa
.setMsaHeader(msaHeader
);
528 } catch (Exception e
) {
529 Log
.err("Save ModuleSurfaceArea Document", e
.getMessage());
537 SaveFile
.saveMsaFile(path
, msa
);
539 } catch (Exception e
) {
540 Log
.err("Save Module to file system", e
.getMessage());
545 // Save to identification
547 mid
= new ModuleIdentification(this.jTextFieldName
.getText(), this.jTextFieldGuid
.getText(),
548 this.jTextFieldVersion
.getText(), path
);
549 mid
.setPackageId(packages
.elementAt(this.jComboBoxExistingPackage
.getSelectedIndex()));
553 Save all components of Spd Header
556 private void savePackage() {
557 PackageSurfaceArea spd
= null;
558 String path
= Tools
.convertPathToCurrentOsType(this.jTextFieldFilePath
.getText());
564 SpdHeaderDocument
.SpdHeader spdHeader
= null;
566 spd
= PackageSurfaceArea
.Factory
.newInstance();
567 spdHeader
= SpdHeaderDocument
.SpdHeader
.Factory
.newInstance();
569 spdHeader
.setPackageName(this.jTextFieldName
.getText());
570 spdHeader
.setGuidValue(this.jTextFieldGuid
.getText());
571 spdHeader
.setVersion(this.jTextFieldVersion
.getText());
573 spd
.setSpdHeader(spdHeader
);
574 } catch (Exception e
) {
575 Log
.err("Save PackageSurfaceArea Document", e
.getMessage());
583 SaveFile
.saveSpdFile(path
, spd
);
585 } catch (Exception e
) {
586 Log
.err("Save Package to file system", e
.getMessage());
591 // Save to identification
593 pid
= new PackageIdentification(this.jTextFieldName
.getText(), this.jTextFieldGuid
.getText(),
594 this.jTextFieldVersion
.getText(), path
);
598 Save all components of Fpd Header
601 private void savePlatform() {
602 PlatformSurfaceArea fpd
= null;
603 String path
= Tools
.convertPathToCurrentOsType(this.jTextFieldFilePath
.getText());
609 PlatformHeader fpdHeader
= null;
611 fpd
= PlatformSurfaceArea
.Factory
.newInstance();
612 fpdHeader
= PlatformHeader
.Factory
.newInstance();
614 fpdHeader
.setPlatformName(this.jTextFieldName
.getText());
615 fpdHeader
.setGuidValue(this.jTextFieldGuid
.getText());
616 fpdHeader
.setVersion(this.jTextFieldVersion
.getText());
618 fpd
.setPlatformHeader(fpdHeader
);
619 } catch (Exception e
) {
620 Log
.err("Save FrameworkPlatformDescription Document", e
.getMessage());
628 SaveFile
.saveFpdFile(path
, fpd
);
630 } catch (Exception e
) {
631 Log
.err("Save Platform to file system", e
.getMessage());
636 // Save to identification
638 fid
= new PlatformIdentification(this.jTextFieldName
.getText(), this.jTextFieldGuid
.getText(),
639 this.jTextFieldVersion
.getText(), path
);
642 public ModuleIdentification
getMid() {
646 public void setMid(ModuleIdentification mid
) {
650 public PlatformIdentification
getFid() {
654 public void setFid(PlatformIdentification fid
) {
658 public PackageIdentification
getPid() {
662 public void setPid(PackageIdentification pid
) {