1. Fix EDKT273 "Externs are not being grouped together in MSA Files"
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / module / ui / ModuleExterns.java
1 /** @file
2
3 The file is used to create, update DataHub of MSA/MBD file
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.module.ui;
16
17 import java.awt.Dimension;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ComponentEvent;
20 import java.awt.event.ItemListener;
21 import java.awt.event.MouseEvent;
22 import java.util.Vector;
23
24 import javax.swing.JButton;
25 import javax.swing.JPanel;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTable;
28 import javax.swing.ListSelectionModel;
29 import javax.swing.event.ListSelectionEvent;
30
31 import org.tianocore.ExternsDocument;
32 import org.tianocore.ExternsDocument.Externs;
33 import org.tianocore.ExternsDocument.Externs.Extern;
34 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
35 import org.tianocore.frameworkwizard.common.DataType;
36 import org.tianocore.frameworkwizard.common.EnumerationData;
37 import org.tianocore.frameworkwizard.common.IDefaultTableModel;
38 import org.tianocore.frameworkwizard.common.Log;
39 import org.tianocore.frameworkwizard.common.Tools;
40 import org.tianocore.frameworkwizard.common.Identifications.OpeningModuleType;
41 import org.tianocore.frameworkwizard.common.ui.IFrame;
42 import org.tianocore.frameworkwizard.common.ui.IInternalFrame;
43 import org.tianocore.frameworkwizard.module.Identifications.Externs.ExternsIdentification;
44 import org.tianocore.frameworkwizard.module.Identifications.Externs.ExternsVector;
45 import org.tianocore.frameworkwizard.module.ui.dialog.ExternsDlg;
46
47 /**
48 The class is used to create, update DataHub of MSA/MBD file
49 It extends IInternalFrame
50
51
52
53 **/
54 public class ModuleExterns extends IInternalFrame implements ItemListener {
55
56 ///
57 /// Define class Serial Version UID
58 ///
59 private static final long serialVersionUID = -7382008402932047191L;
60
61 //
62 //Define class members
63 //
64 private JPanel jContentPane = null;
65
66 private JButton jButtonAdd = null;
67
68 private JButton jButtonRemove = null;
69
70 private JButton jButtonUpdate = null;
71
72 private JScrollPane jScrollPane = null;
73
74 private JScrollPane jScrollPaneTable = null;
75
76 private JTable jTable = null;
77
78 //
79 // Not used by UI
80 //
81 private OpeningModuleType omt = null;
82
83 private ModuleSurfaceArea msa = null;
84
85 private ExternsDocument.Externs externs = null;
86
87 private ExternsIdentification id = null;
88
89 private ExternsVector vid = new ExternsVector();
90
91 private IDefaultTableModel model = null;
92
93 private int selectedRow = -1;
94
95 /**
96 This method initializes jButtonAdd
97
98 @return javax.swing.JButton jButtonAdd
99
100 **/
101 private JButton getJButtonAdd() {
102 if (jButtonAdd == null) {
103 jButtonAdd = new JButton();
104 jButtonAdd.setBounds(new java.awt.Rectangle(230, 195, 80, 20));
105 jButtonAdd.setText("Add");
106 jButtonAdd.addActionListener(this);
107 jButtonAdd.setPreferredSize(new java.awt.Dimension(80, 20));
108 }
109 return jButtonAdd;
110 }
111
112 /**
113 This method initializes jButtonRemove
114
115 @return javax.swing.JButton jButtonRemove
116
117 **/
118 private JButton getJButtonRemove() {
119 if (jButtonRemove == null) {
120 jButtonRemove = new JButton();
121 jButtonRemove.setBounds(new java.awt.Rectangle(400, 195, 80, 20));
122 jButtonRemove.setText("Remove");
123 jButtonRemove.addActionListener(this);
124 jButtonRemove.setPreferredSize(new java.awt.Dimension(80, 20));
125 }
126 return jButtonRemove;
127 }
128
129 /**
130 This method initializes jButtonUpdate
131
132 @return javax.swing.JButton jButtonUpdate
133
134 **/
135 private JButton getJButtonUpdate() {
136 if (jButtonUpdate == null) {
137 jButtonUpdate = new JButton();
138 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 195, 80, 20));
139 jButtonUpdate.setPreferredSize(new java.awt.Dimension(80, 20));
140 jButtonUpdate.setText("Edit");
141 jButtonUpdate.addActionListener(this);
142 }
143 return jButtonUpdate;
144 }
145
146 /**
147 This method initializes jScrollPane
148
149 @return javax.swing.JScrollPane
150 */
151 private JScrollPane getJScrollPane() {
152 if (jScrollPane == null) {
153 jScrollPane = new JScrollPane();
154 jScrollPane.setViewportView(getJContentPane());
155 }
156 return jScrollPane;
157 }
158
159 /**
160 This method initializes jScrollPaneTable
161
162 @return javax.swing.JScrollPane
163 **/
164 private JScrollPane getJScrollPaneTable() {
165 if (jScrollPaneTable == null) {
166 jScrollPaneTable = new JScrollPane();
167 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
168 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
169 jScrollPaneTable.setViewportView(getJTable());
170 }
171 return jScrollPaneTable;
172 }
173
174 /**
175 This method initializes jTable
176
177 @return javax.swing.JTable
178 **/
179 private JTable getJTable() {
180 if (jTable == null) {
181 jTable = new JTable();
182 model = new IDefaultTableModel();
183 jTable = new JTable(model);
184 jTable.setRowHeight(20);
185
186 model.addColumn("Type");
187 model.addColumn("Name");
188 model.addColumn("Value");
189
190 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
191 jTable.getSelectionModel().addListSelectionListener(this);
192 jTable.getModel().addTableModelListener(this);
193 jTable.addMouseListener(this);
194 }
195 return jTable;
196 }
197
198 /**
199 This method initializes this
200
201 **/
202 private void init() {
203 this.setSize(500, 515);
204 this.setContentPane(getJScrollPane());
205 this.setTitle("Externs");
206 }
207
208 /**
209 This method initializes this
210 Fill values to all fields if these values are not empty
211
212 @param inPackageDependencies
213
214 **/
215 private void init(Externs inExterns) {
216 init();
217 this.externs = inExterns;
218
219 if (this.externs != null) {
220 //
221 // Get specification
222 //
223 if (this.externs.getSpecificationList().size() > 0) {
224 for (int index = 0; index < this.externs.getSpecificationList().size(); index++) {
225 String arg0 = externs.getSpecificationList().get(index);
226 String arg1 = EnumerationData.EXTERNS_SPECIFICATION;
227
228 id = new ExternsIdentification(arg0, arg1);
229 vid.addExterns(id);
230 }
231 }
232
233 //
234 // Get Externs list
235 //
236 if (this.externs.getExternList().size() > 0) {
237 for (int index = 0; index < this.externs.getExternList().size(); index++) {
238 //
239 // Get common data
240 //
241 String featureFlag = externs.getExternList().get(index).getFeatureFlag();
242 Vector<String> arch = Tools
243 .convertListToVector(externs.getExternList().get(index).getSupArchList());
244
245 //
246 // Get Image data
247 //
248 if (this.externs.getExternList().get(index).getModuleEntryPoint() != null
249 || this.externs.getExternList().get(index).getModuleUnloadImage() != null) {
250 String moduleEntryPoint = this.externs.getExternList().get(index).getModuleEntryPoint();
251 String moduleUnloadImage = this.externs.getExternList().get(index).getModuleUnloadImage();
252 String type = EnumerationData.EXTERNS_IMAGE;
253 id = new ExternsIdentification(moduleEntryPoint, moduleUnloadImage, type, featureFlag, arch);
254 }
255
256 //
257 // Get Library Data
258 //
259 if (this.externs.getExternList().get(index).getConstructor() != null
260 || this.externs.getExternList().get(index).getDestructor() != null) {
261 String constructor = this.externs.getExternList().get(index).getConstructor();
262 String destructor = this.externs.getExternList().get(index).getDestructor();
263 String type = EnumerationData.EXTERNS_LIBRARY;
264 id = new ExternsIdentification(constructor, destructor, type, featureFlag, arch);
265 }
266
267 //
268 // Get Driver Data
269 //
270 if (this.externs.getExternList().get(index).getDriverBinding() != null) {
271 String driverBinding = this.externs.getExternList().get(index).getDriverBinding();
272 String componentName = this.externs.getExternList().get(index).getComponentName();
273 String driverConfig = this.externs.getExternList().get(index).getDriverConfig();
274 String driverDiag = this.externs.getExternList().get(index).getDriverDiag();
275 String type = EnumerationData.EXTERNS_DRIVER;
276 id = new ExternsIdentification(driverBinding, componentName, driverConfig, driverDiag, type,
277 featureFlag, arch);
278 }
279
280 //
281 // Get Call Back Data
282 //
283 if (this.externs.getExternList().get(index).getSetVirtualAddressMapCallBack() != null
284 || this.externs.getExternList().get(index).getExitBootServicesCallBack() != null) {
285 String virtualAddressMap = this.externs.getExternList().get(index)
286 .getSetVirtualAddressMapCallBack();
287 String exitBootServices = this.externs.getExternList().get(index).getExitBootServicesCallBack();
288 String type = EnumerationData.EXTERNS_CALL_BACK;
289 id = new ExternsIdentification(virtualAddressMap, exitBootServices, type, featureFlag, arch);
290 }
291
292 //
293 // Add id to vector
294 //
295 vid.addExterns(id);
296 }
297 }
298 }
299 showTable();
300 }
301
302 /**
303 This is the default constructor
304
305 **/
306 public ModuleExterns() {
307 super();
308 init();
309 this.setVisible(true);
310 }
311
312 /**
313 This is the override edit constructor
314
315 @param inExterns The input data of ExternsDocument.Externs
316
317 **/
318 public ModuleExterns(OpeningModuleType inOmt) {
319 super();
320 this.omt = inOmt;
321 this.msa = omt.getXmlMsa();
322 init(msa.getExterns());
323 this.setVisible(true);
324 }
325
326 /**
327 This method initializes jContentPane
328
329 @return javax.swing.JPanel jContentPane
330
331 **/
332 private JPanel getJContentPane() {
333 if (jContentPane == null) {
334 jContentPane = new JPanel();
335 jContentPane.setLayout(null);
336 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
337
338 jContentPane.add(getJButtonAdd(), null);
339 jContentPane.add(getJButtonRemove(), null);
340 jContentPane.add(getJButtonUpdate(), null);
341 jContentPane.add(getJScrollPaneTable(), null);
342 }
343 return jContentPane;
344 }
345
346 private void showEdit(int index) {
347 ExternsDlg dlg = new ExternsDlg(vid.getExterns(index), new IFrame());
348 int result = dlg.showDialog();
349 if (result == DataType.RETURN_TYPE_OK) {
350 if (index == -1) {
351 this.vid.addExterns(dlg.getId());
352 } else {
353 this.vid.setExterns(dlg.getId(), index);
354 }
355 this.showTable();
356 this.save();
357 dlg.dispose();
358 }
359 if (result == DataType.RETURN_TYPE_CANCEL) {
360 dlg.dispose();
361 }
362 }
363
364 /**
365 Clear all table rows
366
367 **/
368 private void clearAll() {
369 if (model != null) {
370 for (int index = model.getRowCount() - 1; index >= 0; index--) {
371 model.removeRow(index);
372 }
373 }
374 }
375
376 /**
377 Read content of vector and put then into table
378
379 **/
380 private void showTable() {
381 clearAll();
382
383 if (vid.size() > 0) {
384 for (int index = 0; index < vid.size(); index++) {
385 int line = 1;
386
387 //
388 // For Specification
389 //
390 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_SPECIFICATION)) {
391 line = 1;
392 }
393
394 //
395 // For Image
396 //
397 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_IMAGE)) {
398 line = 2;
399 }
400
401 //
402 // For Library
403 //
404 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_LIBRARY)) {
405 line = 2;
406 }
407
408 //
409 // For Driver
410 //
411 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_DRIVER)) {
412 line = 4;
413 }
414
415 //
416 // For Call Back
417 //
418 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_CALL_BACK)) {
419 line = 2;
420 }
421
422 model.addRow(vid.toStringVector(index));
423 jTable.setRowHeight(index, line * 18);
424 }
425 }
426 this.jTable.repaint();
427 this.jTable.updateUI();
428 }
429
430 /* (non-Javadoc)
431 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
432 *
433 * Override actionPerformed to listen all actions
434 *
435 */
436 public void actionPerformed(ActionEvent arg0) {
437 if (arg0.getSource() == jButtonAdd) {
438 showEdit(-1);
439 }
440 if (arg0.getSource() == jButtonUpdate) {
441 if (this.selectedRow < 0) {
442 Log.wrn("Update Externs", "Please select one record first.");
443 return;
444 }
445 showEdit(selectedRow);
446 }
447
448 if (arg0.getSource() == jButtonRemove) {
449 if (jTable.isEditing()) {
450 jTable.getCellEditor().stopCellEditing();
451 }
452 if (selectedRow > -1) {
453 this.model.removeRow(selectedRow);
454 this.vid.removeExterns(selectedRow);
455 selectedRow = -1;
456 this.save();
457 }
458 }
459 }
460
461 /**
462 Save all components of Externs
463 if exists externs, set the value directly
464 if not exists externs, new an instance first
465
466 **/
467 public void save() {
468 try {
469 int count = this.vid.size();
470
471 //
472 // Save pcd and flash map information first
473 //
474 Externs ex = Externs.Factory.newInstance();
475 if (this.externs.getPcdIsDriver() != null) {
476 ex.setPcdIsDriver(this.externs.getPcdIsDriver());
477 }
478 if (this.externs.getTianoR8FlashMapH()) {
479 ex.setTianoR8FlashMapH(this.externs.getTianoR8FlashMapH());
480 }
481 this.externs = ex;
482
483 //
484 // Save externs
485 //
486 if (count > 0) {
487 for (int index = 0; index < count; index++) {
488 //
489 // Save specfication
490 //
491 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_SPECIFICATION)) {
492 if (!isEmpty(vid.getExterns(index).getName0())) {
493 this.externs.addNewSpecification();
494 this.externs.setSpecificationArray(externs.getSpecificationList().size() - 1,
495 vid.getExterns(index).getName0());
496 continue;
497 }
498 }
499
500 Extern e = Extern.Factory.newInstance();
501 //
502 // Save image
503 //
504 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_IMAGE)) {
505 if (!isEmpty(vid.getExterns(index).getName0()) || !isEmpty(vid.getExterns(index).getName1())) {
506 e = Extern.Factory.newInstance();
507 if (!isEmpty(vid.getExterns(index).getName0())) {
508 e.setModuleEntryPoint(vid.getExterns(index).getName0());
509 }
510 if (!isEmpty(vid.getExterns(index).getName1())) {
511 e.setModuleUnloadImage(vid.getExterns(index).getName1());
512 }
513 }
514 }
515
516 //
517 // Save library
518 //
519 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_LIBRARY)) {
520 if (!isEmpty(vid.getExterns(index).getName0()) || !isEmpty(vid.getExterns(index).getName1())) {
521 e = Extern.Factory.newInstance();
522 if (!isEmpty(vid.getExterns(index).getName0())) {
523 e.setConstructor(vid.getExterns(index).getName0());
524 }
525 if (!isEmpty(vid.getExterns(index).getName1())) {
526 e.setDestructor(vid.getExterns(index).getName1());
527 }
528 }
529 }
530
531 //
532 // Save call back
533 //
534 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_CALL_BACK)) {
535 if (!isEmpty(vid.getExterns(index).getName0()) || !isEmpty(vid.getExterns(index).getName1())) {
536 e = Extern.Factory.newInstance();
537 if (!isEmpty(vid.getExterns(index).getName0())) {
538 e.setSetVirtualAddressMapCallBack(vid.getExterns(index).getName0());
539 }
540 if (!isEmpty(vid.getExterns(index).getName1())) {
541 e.setExitBootServicesCallBack(vid.getExterns(index).getName1());
542 }
543 }
544 }
545
546 //
547 // Save driver
548 //
549 if (vid.getExterns(index).getType().equals(EnumerationData.EXTERNS_DRIVER)) {
550 if (!isEmpty(vid.getExterns(index).getName0()) || !isEmpty(vid.getExterns(index).getName1())
551 || !isEmpty(vid.getExterns(index).getName2()) || !isEmpty(vid.getExterns(index).getName3())) {
552 e = Extern.Factory.newInstance();
553 if (!isEmpty(vid.getExterns(index).getName0())) {
554 e.setDriverBinding(vid.getExterns(index).getName0());
555 }
556 if (!isEmpty(vid.getExterns(index).getName1())) {
557 e.setComponentName(vid.getExterns(index).getName1());
558 }
559 if (!isEmpty(vid.getExterns(index).getName2())) {
560 e.setDriverConfig(vid.getExterns(index).getName2());
561 }
562 if (!isEmpty(vid.getExterns(index).getName3())) {
563 e.setDriverDiag(vid.getExterns(index).getName3());
564 }
565 }
566 }
567
568 //
569 // Save common data
570 //
571 if (!isEmpty(vid.getExterns(index).getFeatureFlag())) {
572 e.setFeatureFlag(vid.getExterns(index).getFeatureFlag());
573 }
574 if (vid.getExterns(index).getSupArchList() != null
575 && vid.getExterns(index).getSupArchList().size() > 0) {
576 e.setSupArchList(vid.getExterns(index).getSupArchList());
577 }
578
579 this.externs.addNewExtern();
580 this.externs.setExternArray(this.externs.getExternList().size() - 1, e);
581 }
582 }
583
584 this.msa.setExterns(externs);
585 this.omt.setSaved(false);
586 } catch (Exception e) {
587 Log.wrn("Update Externs", e.getMessage());
588 Log.err("Update Externs", e.getMessage());
589 }
590 }
591
592 /* (non-Javadoc)
593 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
594 *
595 */
596 public void valueChanged(ListSelectionEvent arg0) {
597 if (arg0.getValueIsAdjusting()) {
598 return;
599 }
600 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
601 if (lsm.isSelectionEmpty()) {
602 return;
603 } else {
604 selectedRow = lsm.getMinSelectionIndex();
605 }
606 }
607
608 /* (non-Javadoc)
609 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
610 *
611 */
612 public void mouseClicked(MouseEvent arg0) {
613 if (arg0.getClickCount() == 2) {
614 if (this.selectedRow < 0) {
615 return;
616 } else {
617 showEdit(selectedRow);
618 }
619 }
620 }
621
622 /* (non-Javadoc)
623 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
624 *
625 * Override componentResized to resize all components when frame's size is changed
626 */
627 public void componentResized(ComponentEvent arg0) {
628 int intCurrentWidth = this.getJContentPane().getWidth();
629 int intCurrentHeight = this.getJContentPane().getHeight();
630 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
631 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
632
633 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
634 intPreferredHeight);
635 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
636 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
637 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
638 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
639 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
640 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
641 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
642 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
643 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
644 }
645 }