Remove empty FfsFileNameGuid element under ModuleSaBuildOptions if user input empty...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / platform / ui / FpdFrameworkModules.java
1 package org.tianocore.frameworkwizard.platform.ui;
2
3 import java.awt.BorderLayout;
4
5 import javax.swing.JFrame;
6 import javax.swing.JOptionPane;
7 import javax.swing.JPanel;
8 import javax.swing.JSplitPane;
9 import javax.swing.JLabel;
10 import javax.swing.JScrollPane;
11 import javax.swing.JTable;
12 import javax.swing.JButton;
13 import javax.swing.ListSelectionModel;
14 import javax.swing.table.DefaultTableModel;
15
16 import org.tianocore.PlatformSurfaceAreaDocument;
17 import org.tianocore.frameworkwizard.common.Identifications.OpeningPlatformType;
18 import org.tianocore.frameworkwizard.common.ui.IInternalFrame;
19 import org.tianocore.frameworkwizard.platform.ui.global.GlobalData;
20 import org.tianocore.frameworkwizard.platform.ui.id.ModuleIdentification;
21 import org.tianocore.frameworkwizard.platform.ui.id.PackageIdentification;
22
23 import java.awt.FlowLayout;
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.Vector;
31
32 public class FpdFrameworkModules extends IInternalFrame {
33
34 /**
35 *
36 */
37 private static final long serialVersionUID = 1L;
38 static JFrame frame;
39 private JSplitPane jSplitPane = null;
40 private JPanel jPanel = null;
41 private JPanel jPanel1 = null;
42 private JLabel jLabel = null;
43 private JScrollPane jScrollPane = null;
44 private JTable jTable = null;
45 private JPanel jPanel2 = null;
46 private JButton jButton = null;
47 private JLabel jLabel1 = null;
48 private JPanel jPanel3 = null;
49 private JScrollPane jScrollPane1 = null;
50 private JTable jTable1 = null;
51 private JButton jButton1 = null;
52 private JButton jButton2 = null;
53 private NonEditableTableModel model = null;
54 private NonEditableTableModel model1 = null;
55
56 private FpdModuleSA settingDlg = null;
57
58 private FpdFileContents ffc = null;
59 private OpeningPlatformType docConsole = null;
60 private Map<String, ArrayList<String>> fpdMsa = null;
61
62 private ArrayList<ModuleIdentification> miList = null;
63
64 /**
65 * This method initializes jSplitPane
66 *
67 * @return javax.swing.JSplitPane
68 */
69 private JSplitPane getJSplitPane() {
70 if (jSplitPane == null) {
71 jSplitPane = new JSplitPane();
72 jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
73 jSplitPane.setDividerLocation(250);
74 jSplitPane.setBottomComponent(getJPanel1());
75 jSplitPane.setTopComponent(getJPanel());
76 }
77 return jSplitPane;
78 }
79
80 /**
81 * This method initializes jPanel
82 *
83 * @return javax.swing.JPanel
84 */
85 private JPanel getJPanel() {
86 if (jPanel == null) {
87 jLabel = new JLabel();
88 jLabel.setText("Modules in Workspace");
89 jPanel = new JPanel();
90 jPanel.setLayout(new BorderLayout());
91 jPanel.add(jLabel, java.awt.BorderLayout.NORTH);
92 jPanel.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
93 jPanel.add(getJPanel2(), java.awt.BorderLayout.SOUTH);
94 }
95 return jPanel;
96 }
97
98 /**
99 * This method initializes jPanel1
100 *
101 * @return javax.swing.JPanel
102 */
103 private JPanel getJPanel1() {
104 if (jPanel1 == null) {
105 jLabel1 = new JLabel();
106 jLabel1.setText("Modules Added");
107 jPanel1 = new JPanel();
108 jPanel1.setLayout(new BorderLayout());
109 jPanel1.add(jLabel1, java.awt.BorderLayout.NORTH);
110 jPanel1.add(getJPanel3(), java.awt.BorderLayout.SOUTH);
111 jPanel1.add(getJScrollPane1(), java.awt.BorderLayout.CENTER);
112 }
113 return jPanel1;
114 }
115
116 /**
117 * This method initializes jScrollPane
118 *
119 * @return javax.swing.JScrollPane
120 */
121 private JScrollPane getJScrollPane() {
122 if (jScrollPane == null) {
123 jScrollPane = new JScrollPane();
124 jScrollPane.setPreferredSize(new java.awt.Dimension(600,200));
125 jScrollPane.setViewportView(getJTable());
126 }
127 return jScrollPane;
128 }
129
130 /**
131 * This method initializes jTable
132 *
133 * @return javax.swing.JTable
134 */
135 private JTable getJTable() {
136 if (jTable == null) {
137 model = new NonEditableTableModel();
138 jTable = new JTable(model);
139 jTable.setRowHeight(20);
140 model.addColumn("ModuleName");
141 model.addColumn("ModuleVersion");
142 model.addColumn("PackageName");
143 model.addColumn("PackageVersion");
144 model.addColumn("Path");
145
146 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
147 }
148 return jTable;
149 }
150
151 /**
152 * This method initializes jPanel2
153 *
154 * @return javax.swing.JPanel
155 */
156 private JPanel getJPanel2() {
157 if (jPanel2 == null) {
158 FlowLayout flowLayout = new FlowLayout();
159 flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
160 jPanel2 = new JPanel();
161 jPanel2.setLayout(flowLayout);
162 jPanel2.add(getJButton(), null);
163 }
164 return jPanel2;
165 }
166
167 /**
168 * This method initializes jButton
169 *
170 * @return javax.swing.JButton
171 */
172 private JButton getJButton() {
173 if (jButton == null) {
174 jButton = new JButton();
175 jButton.setPreferredSize(new java.awt.Dimension(130,20));
176 jButton.setText("Add a Module");
177 jButton.addActionListener(new java.awt.event.ActionListener() {
178 public void actionPerformed(java.awt.event.ActionEvent e) {
179 int selectedRow = jTable.getSelectedRow();
180 if (selectedRow < 0){
181 return;
182 }
183
184 String path = model.getValueAt(selectedRow, 4)+"";
185 ModuleIdentification mi = miList.get(selectedRow);
186 Vector<String> vArchs = null;
187 try {
188 vArchs = GlobalData.getModuleSupArchs(mi);
189 }
190 catch (Exception exp) {
191 JOptionPane.showMessageDialog(frame, exp.getMessage());
192 }
193
194 if (vArchs == null) {
195 JOptionPane.showMessageDialog(frame, "No supported Archs specified in MSA file.");
196 return;
197 }
198
199 String archsAdded = "";
200 String mg = mi.getGuid();
201 String mv = mi.getVersion();
202 String pg = mi.getPackage().getGuid();
203 String pv = mi.getPackage().getVersion();
204
205 ArrayList<String> al = fpdMsa.get(mg + mv + pg + pv);
206 if (al == null) {
207 al = new ArrayList<String>();
208 fpdMsa.put(mg + mv + pg + pv, al);
209 }
210 for (int i = 0; i < al.size(); ++i) {
211 vArchs.remove(al.get(i));
212 }
213 //
214 // Archs this Module supported have already been added.
215 //
216 if (vArchs.size() == 0) {
217 JOptionPane.showMessageDialog(frame, "This Module Already Added.");
218 return;
219 }
220 //ToDo put Arch instead of null
221 for (int i = 0; i < vArchs.size(); ++i) {
222 String arch = vArchs.get(i);
223 al.add(arch);
224 archsAdded += arch + " ";
225 String[] row = {"", mv, "", pv, arch, path};
226
227 if (mi != null) {
228 row[0] = mi.getName();
229 row[2] = mi.getPackage().getName();
230
231 }
232 model1.addRow(row);
233
234 docConsole.setSaved(false);
235 try{
236 //ToDo : specify archs need to add.
237 ffc.addFrameworkModulesPcdBuildDefs(mi, arch, null);
238 }
239 catch (Exception exception) {
240 JOptionPane.showMessageDialog(frame, "PCD Insertion Fail. " + exception.getMessage());
241 }
242 }
243
244
245 JOptionPane.showMessageDialog(frame, "This Module with Arch "+ archsAdded +" Added Successfully.");
246 jTable1.changeSelection(model1.getRowCount()-1, 0, false, false);
247 }
248 });
249 }
250 return jButton;
251 }
252
253 /**
254 * This method initializes jPanel3
255 *
256 * @return javax.swing.JPanel
257 */
258 private JPanel getJPanel3() {
259 if (jPanel3 == null) {
260 FlowLayout flowLayout1 = new FlowLayout();
261 flowLayout1.setAlignment(java.awt.FlowLayout.RIGHT);
262 jPanel3 = new JPanel();
263 jPanel3.setLayout(flowLayout1);
264 jPanel3.add(getJButton1(), null);
265 jPanel3.add(getJButton2(), null);
266 }
267 return jPanel3;
268 }
269
270 /**
271 * This method initializes jScrollPane1
272 *
273 * @return javax.swing.JScrollPane
274 */
275 private JScrollPane getJScrollPane1() {
276 if (jScrollPane1 == null) {
277 jScrollPane1 = new JScrollPane();
278 jScrollPane1.setPreferredSize(new java.awt.Dimension(453,200));
279 jScrollPane1.setViewportView(getJTable1());
280 }
281 return jScrollPane1;
282 }
283
284 /**
285 * This method initializes jTable1
286 *
287 * @return javax.swing.JTable
288 */
289 private JTable getJTable1() {
290 if (jTable1 == null) {
291 model1 = new NonEditableTableModel();
292 jTable1 = new JTable(model1);
293 jTable1.setRowHeight(20);
294 model1.addColumn("ModuleName");
295 model1.addColumn("ModuleVersion");
296 model1.addColumn("PackageName");
297 model1.addColumn("PackageVersion");
298 model1.addColumn("SupportedArch");
299 model1.addColumn("Path");
300
301 jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
302 }
303 return jTable1;
304 }
305
306 /**
307 * This method initializes jButton1
308 *
309 * @return javax.swing.JButton
310 */
311 private JButton getJButton1() {
312 if (jButton1 == null) {
313 jButton1 = new JButton();
314 jButton1.setPreferredSize(new java.awt.Dimension(130,20));
315 jButton1.setText("Settings");
316 jButton1.addActionListener(new java.awt.event.ActionListener() {
317 public void actionPerformed(java.awt.event.ActionEvent e) {
318 int selectedRow = jTable1.getSelectedRow();
319 if (selectedRow < 0){
320 return;
321 }
322 if (settingDlg == null) {
323 settingDlg = new FpdModuleSA(ffc);
324 }
325
326 String[] sa = new String[5];
327 ffc.getFrameworkModuleInfo(selectedRow, sa);
328 String mg = sa[0];
329 String mv = sa[1];
330 String pg = sa[2];
331 String pv = sa[3];
332 String arch = sa[4];
333 settingDlg.setKey(mg + " " + mv + " " + pg + " " + pv + " " + arch, selectedRow, docConsole);
334 settingDlg.setVisible(true);
335 }
336 });
337 }
338 return jButton1;
339 }
340
341 /**
342 * This method initializes jButton2
343 *
344 * @return javax.swing.JButton
345 */
346 private JButton getJButton2() {
347 if (jButton2 == null) {
348 jButton2 = new JButton();
349 jButton2.setPreferredSize(new java.awt.Dimension(130,20));
350 jButton2.setText("Remove Module");
351 jButton2.addActionListener(new java.awt.event.ActionListener() {
352 public void actionPerformed(java.awt.event.ActionEvent e) {
353 int selectedRow = jTable1.getSelectedRow();
354 if (selectedRow < 0){
355 return;
356 }
357 String[] sa = new String[5];
358 ffc.getFrameworkModuleInfo(selectedRow, sa);
359 String mg = sa[0];
360 String mv = sa[1];
361 String pg = sa[2];
362 String pv = sa[3];
363 String arch = sa[4];
364 ModuleIdentification mi = getModuleId(sa[0] + " " + sa[1] + " " + sa[2] + " " + sa[3] + " " + sa[4]);
365 mv = mi.getVersion();
366 pv = mi.getPackage().getVersion();
367 model1.removeRow(selectedRow);
368 if (arch == null) {
369 // if no arch specified in ModuleSA
370 fpdMsa.remove(mg+mv+pg+pv);
371 }
372 else {
373 ArrayList<String> al = fpdMsa.get(mg+mv+pg+pv);
374 al.remove(arch);
375 if (al.size() == 0) {
376 fpdMsa.remove(mg+mv+pg+pv);
377 }
378 }
379
380
381 docConsole.setSaved(false);
382 ffc.removeModuleSA(selectedRow);
383 }
384 });
385 }
386 return jButton2;
387 }
388
389 /**
390 * @param args
391 */
392 public static void main(String[] args) {
393 // TODO Auto-generated method stub
394 new FpdFrameworkModules().setVisible(true);
395 }
396
397 /**
398 * This is the default constructor
399 */
400 public FpdFrameworkModules() {
401 super();
402 initialize();
403 }
404
405 public FpdFrameworkModules(PlatformSurfaceAreaDocument.PlatformSurfaceArea fpd){
406 this();
407 init(fpd);
408
409 }
410
411 public FpdFrameworkModules(OpeningPlatformType opt) {
412 this(opt.getXmlFpd());
413 docConsole = opt;
414 }
415
416 private void init(PlatformSurfaceAreaDocument.PlatformSurfaceArea fpd) {
417 try {
418 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db", System.getenv("WORKSPACE"));
419 }
420 catch(Exception e){
421 JOptionPane.showMessageDialog(frame, "Error occurred when getting module data.");
422 }
423
424 if (ffc == null){
425 ffc = new FpdFileContents(fpd);
426 ffc.initDynPcdMap();
427 }
428
429 if (fpdMsa == null) {
430 fpdMsa = new HashMap<String, ArrayList<String>>();
431 }
432
433 if (ffc.getFrameworkModulesCount() > 0) {
434 String[][] saa = new String[ffc.getFrameworkModulesCount()][5];
435 ffc.getFrameworkModulesInfo(saa);
436 for (int i = 0; i < saa.length; ++i) {
437 ModuleIdentification mi = getModuleId(saa[i][0]+ " "+saa[i][1]+" "+saa[i][2]+" "+saa[i][3]);
438 String[] row = {"", "", "", "", "", ""};
439 if (mi != null) {
440 row[0] = mi.getName();
441 row[1] = mi.getVersion();
442 row[2] = mi.getPackage().getName();
443 row[3] = mi.getPackage().getVersion();
444 row[4] = saa[i][4];
445 try{
446 row[5] = GlobalData.getMsaFile(mi).getPath().substring(System.getenv("WORKSPACE").length() + 1);
447 }
448 catch (Exception e) {
449 JOptionPane.showMessageDialog(frame, "ShowFPDModules:" + e.getMessage());
450 }
451 }
452 model1.addRow(row);
453 ArrayList<String> al = fpdMsa.get(saa[i][0]+row[1]+saa[i][2]+row[3]);
454 if (al == null) {
455 al = new ArrayList<String>();
456 fpdMsa.put(saa[i][0]+row[1]+saa[i][2]+row[3], al);
457 }
458 al.add(saa[i][4]);
459
460 }
461 }
462
463 showAllModules();
464
465 }
466
467 private void showAllModules() {
468
469 if (miList == null) {
470 miList = new ArrayList<ModuleIdentification>();
471 }
472 Set<PackageIdentification> spi = GlobalData.getPackageList();
473 Iterator ispi = spi.iterator();
474
475 while(ispi.hasNext()) {
476 PackageIdentification pi = (PackageIdentification)ispi.next();
477 String[] s = {"", "", "", "", ""};
478
479 Set<ModuleIdentification> smi = GlobalData.getModules(pi);
480 Iterator ismi = smi.iterator();
481 while(ismi.hasNext()) {
482 ModuleIdentification mi = (ModuleIdentification)ismi.next();
483 s[0] = mi.getName();
484 s[1] = mi.getVersion();
485 s[2] = pi.getName();
486 s[3] = pi.getVersion();
487 try {
488 s[4] = GlobalData.getMsaFile(mi).getPath().substring(System.getenv("WORKSPACE").length() + 1);
489 }
490 catch (Exception e) {
491 JOptionPane.showMessageDialog(frame, "ShowAllModules:" + e.getMessage());
492 }
493 model.addRow(s);
494 miList.add(mi);
495 }
496 }
497 }
498 /**
499 * This method initializes this
500 *
501 * @return void
502 */
503 private void initialize() {
504 this.setSize(633, 533);
505 this.setTitle("Framework Modules");
506 this.setContentPane(getJSplitPane());
507 this.setVisible(true);
508
509 }
510
511 private ModuleIdentification getModuleId(String key){
512 //
513 // Get ModuleGuid, ModuleVersion, PackageGuid, PackageVersion, Arch into string array.
514 //
515 String[] keyPart = key.split(" ");
516 Set<PackageIdentification> spi = GlobalData.getPackageList();
517 Iterator ispi = spi.iterator();
518
519 while(ispi.hasNext()) {
520 PackageIdentification pi = (PackageIdentification)ispi.next();
521 if ( !pi.getGuid().equals(keyPart[2])){
522
523 continue;
524 }
525 if (keyPart[3] != null && keyPart[3].length() > 0 && !keyPart[3].equals("null")){
526 if(!pi.getVersion().equals(keyPart[3])){
527 continue;
528 }
529 }
530 Set<ModuleIdentification> smi = GlobalData.getModules(pi);
531 Iterator ismi = smi.iterator();
532 while(ismi.hasNext()) {
533 ModuleIdentification mi = (ModuleIdentification)ismi.next();
534 if (mi.getGuid().equals(keyPart[0])){
535 if (keyPart[1] != null && keyPart[1].length() > 0 && !keyPart[1].equals("null")){
536 if(!mi.getVersion().equals(keyPart[1])){
537 continue;
538 }
539 }
540
541 return mi;
542 }
543 }
544 }
545 return null;
546 }
547
548 } // @jve:decl-index=0:visual-constraint="10,10"
549
550 class NonEditableTableModel extends DefaultTableModel {
551 /**
552 *
553 */
554 private static final long serialVersionUID = 1L;
555
556 public boolean isCellEditable(int row, int col) {
557 return false;
558 }
559 }