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