1. Fix EDKT418: Some buttons are not displayed correctly in Linux and MAC OS X operat...
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / module / ui / ModulePpis.java
1 /** @file
2
3 The file is used to create, update Ppi 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.MouseEvent;
21 import java.util.Vector;
22
23 import javax.swing.JButton;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.JTable;
27 import javax.swing.ListSelectionModel;
28 import javax.swing.event.ListSelectionEvent;
29
30 import org.tianocore.PPIsDocument;
31 import org.tianocore.PpiNotifyUsage;
32 import org.tianocore.PpiUsage;
33 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
34 import org.tianocore.PPIsDocument.PPIs;
35 import org.tianocore.PPIsDocument.PPIs.Ppi;
36 import org.tianocore.PPIsDocument.PPIs.PpiNotify;
37 import org.tianocore.frameworkwizard.common.DataType;
38 import org.tianocore.frameworkwizard.common.EnumerationData;
39 import org.tianocore.frameworkwizard.common.IDefaultTableModel;
40 import org.tianocore.frameworkwizard.common.Log;
41 import org.tianocore.frameworkwizard.common.Tools;
42 import org.tianocore.frameworkwizard.common.Identifications.OpeningModuleType;
43 import org.tianocore.frameworkwizard.common.ui.IFrame;
44 import org.tianocore.frameworkwizard.common.ui.IInternalFrame;
45 import org.tianocore.frameworkwizard.module.Identifications.Ppis.PpisIdentification;
46 import org.tianocore.frameworkwizard.module.Identifications.Ppis.PpisVector;
47 import org.tianocore.frameworkwizard.module.ui.dialog.PpisDlg;
48
49 /**
50 The class is used to create, update Ppi of MSA/MBD file
51 It extends IInternalFrame
52
53 **/
54 public class ModulePpis extends IInternalFrame {
55
56 ///
57 /// Define class Serial Version UID
58 ///
59 private static final long serialVersionUID = -4284901202357037724L;
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
82 private OpeningModuleType omt = null;
83
84 private ModuleSurfaceArea msa = null;
85
86 private PPIsDocument.PPIs ppis = null;
87
88 private PpisIdentification id = null;
89
90 private PpisVector vid = new PpisVector();
91
92 private EnumerationData ed = new EnumerationData();
93
94 private IDefaultTableModel model = null;
95
96 private int selectedRow = -1;
97
98 /**
99 This method initializes jButtonAdd
100
101 @return javax.swing.JButton jButtonAdd
102
103 **/
104 private JButton getJButtonAdd() {
105 if (jButtonAdd == null) {
106 jButtonAdd = new JButton();
107 jButtonAdd.setBounds(new java.awt.Rectangle(230, 220, 90, 20));
108 jButtonAdd.setText("Add");
109 jButtonAdd.addActionListener(this);
110 jButtonAdd.setPreferredSize(new java.awt.Dimension(90, 20));
111 }
112 return jButtonAdd;
113 }
114
115 /**
116 This method initializes jButtonRemove
117
118 @return javax.swing.JButton jButtonRemove
119
120 **/
121 private JButton getJButtonRemove() {
122 if (jButtonRemove == null) {
123 jButtonRemove = new JButton();
124 jButtonRemove.setBounds(new java.awt.Rectangle(400, 220, 90, 20));
125 jButtonRemove.setText("Remove");
126 jButtonRemove.addActionListener(this);
127 jButtonRemove.setPreferredSize(new java.awt.Dimension(90, 20));
128 }
129 return jButtonRemove;
130 }
131
132 /**
133 This method initializes jButtonUpdate
134
135 @return javax.swing.JButton jButtonUpdate
136
137 **/
138 private JButton getJButtonUpdate() {
139 if (jButtonUpdate == null) {
140 jButtonUpdate = new JButton();
141 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 220, 90, 20));
142 jButtonUpdate.setPreferredSize(new java.awt.Dimension(90, 20));
143 jButtonUpdate.setText("Edit");
144 jButtonUpdate.addActionListener(this);
145 }
146 return jButtonUpdate;
147 }
148
149 /**
150 This method initializes jScrollPane
151
152 @return javax.swing.JScrollPane
153 */
154 private JScrollPane getJScrollPane() {
155 if (jScrollPane == null) {
156 jScrollPane = new JScrollPane();
157 jScrollPane.setViewportView(getJContentPane());
158 }
159 return jScrollPane;
160 }
161
162 /**
163 This method initializes jScrollPaneTable
164
165 @return javax.swing.JScrollPane
166 **/
167 private JScrollPane getJScrollPaneTable() {
168 if (jScrollPaneTable == null) {
169 jScrollPaneTable = new JScrollPane();
170 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
171 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
172 jScrollPaneTable.setViewportView(getJTable());
173 }
174 return jScrollPaneTable;
175 }
176
177 /**
178 This method initializes jTable
179
180 @return javax.swing.JTable
181 **/
182 private JTable getJTable() {
183 if (jTable == null) {
184 jTable = new JTable();
185 model = new IDefaultTableModel();
186 jTable = new JTable(model);
187 jTable.setRowHeight(20);
188
189 model.addColumn("Guid C_Name");
190 model.addColumn("Type");
191 model.addColumn("Usage");
192
193 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
194 jTable.getSelectionModel().addListSelectionListener(this);
195 jTable.getModel().addTableModelListener(this);
196 jTable.addMouseListener(this);
197 }
198 return jTable;
199 }
200
201 public static void main(String[] args) {
202
203 }
204
205 /**
206 This method initializes this
207
208 **/
209 private void init() {
210 this.setSize(500, 515);
211 this.setContentPane(getJScrollPane());
212 this.setTitle("Ppis");
213 }
214
215 /**
216 This method initializes this
217 Fill values to all fields if these values are not empty
218
219 @param inPackageDependencies
220
221 **/
222 private void init(PPIs inPpis) {
223 init();
224 this.ppis = inPpis;
225
226 if (this.ppis != null) {
227 if (this.ppis.getPpiList().size() > 0) {
228 for (int index = 0; index < this.ppis.getPpiList().size(); index++) {
229 String arg0 = ppis.getPpiList().get(index).getPpiCName();
230 String arg1 = ed.getVPpiType().get(0);
231 String arg2 = null;
232 if (ppis.getPpiList().get(index).getUsage() != null) {
233 arg2 = ppis.getPpiList().get(index).getUsage().toString();
234 }
235
236 String arg3 = ppis.getPpiList().get(index).getFeatureFlag();
237 Vector<String> arg4 = Tools.convertListToVector(ppis.getPpiList().get(index).getSupArchList());
238 String arg5 = ppis.getPpiList().get(index).getHelpText();
239
240 id = new PpisIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
241 vid.addPpis(id);
242 }
243 }
244 if (this.ppis.getPpiNotifyList().size() > 0) {
245 for (int index = 0; index < this.ppis.getPpiNotifyList().size(); index++) {
246 String arg0 = ppis.getPpiNotifyList().get(index).getPpiNotifyCName();
247 String arg1 = ed.getVPpiType().get(1);
248 String arg2 = null;
249 if (ppis.getPpiNotifyList().get(index).getUsage() != null) {
250 arg2 = ppis.getPpiNotifyList().get(index).getUsage().toString();
251 }
252
253 String arg3 = ppis.getPpiNotifyList().get(index).getFeatureFlag();
254 Vector<String> arg4 = Tools
255 .convertListToVector(ppis.getPpiNotifyList().get(index).getSupArchList());
256 String arg5 = ppis.getPpiNotifyList().get(index).getHelpText();
257
258 id = new PpisIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
259 vid.addPpis(id);
260 }
261 }
262 }
263 showTable();
264 }
265
266 /**
267 This is the default constructor
268
269 **/
270 public ModulePpis() {
271 super();
272 init();
273 this.setVisible(true);
274 }
275
276 /**
277 This is the override edit constructor
278
279 @param inPpis The input data of PPIsDocument.PPIs
280
281 **/
282 public ModulePpis(OpeningModuleType inOmt) {
283 super();
284 this.omt = inOmt;
285 this.msa = omt.getXmlMsa();
286 init(msa.getPPIs());
287 this.setVisible(true);
288 }
289
290 /**
291 This method initializes jContentPane
292
293 @return javax.swing.JPanel jContentPane
294
295 **/
296 private JPanel getJContentPane() {
297 if (jContentPane == null) {
298 jContentPane = new JPanel();
299 jContentPane.setLayout(null);
300 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
301
302 jContentPane.add(getJButtonAdd(), null);
303 jContentPane.add(getJButtonRemove(), null);
304 jContentPane.add(getJButtonUpdate(), null);
305 jContentPane.add(getJScrollPaneTable(), null);
306 }
307 return jContentPane;
308 }
309
310 private void showEdit(int index) {
311 PpisDlg dlg = new PpisDlg(vid.getPpis(index), new IFrame(), omt.getId());
312 int result = dlg.showDialog();
313 if (result == DataType.RETURN_TYPE_OK) {
314 if (index == -1) {
315 this.vid.addPpis(dlg.getId());
316 } else {
317 this.vid.setPpis(dlg.getId(), index);
318 }
319 this.showTable();
320 this.save();
321 dlg.dispose();
322 }
323 if (result == DataType.RETURN_TYPE_CANCEL) {
324 dlg.dispose();
325 }
326 }
327
328 /**
329 Clear all table rows
330
331 **/
332 private void clearAll() {
333 if (model != null) {
334 for (int index = model.getRowCount() - 1; index >= 0; index--) {
335 model.removeRow(index);
336 }
337 }
338 }
339
340 /**
341 Read content of vector and put then into table
342
343 **/
344 private void showTable() {
345 clearAll();
346
347 if (vid.size() > 0) {
348 for (int index = 0; index < vid.size(); index++) {
349 model.addRow(vid.toStringVector(index));
350 }
351 }
352 this.jTable.repaint();
353 this.jTable.updateUI();
354 }
355
356 /* (non-Javadoc)
357 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
358 *
359 * Override actionPerformed to listen all actions
360 *
361 */
362 public void actionPerformed(ActionEvent arg0) {
363 if (arg0.getSource() == jButtonAdd) {
364 showEdit(-1);
365 }
366 if (arg0.getSource() == jButtonUpdate) {
367 if (this.selectedRow < 0) {
368 Log.wrn("Update Ppis", "Please select one record first.");
369 return;
370 }
371 showEdit(selectedRow);
372 }
373
374 if (arg0.getSource() == jButtonRemove) {
375 if (jTable.isEditing()) {
376 jTable.getCellEditor().stopCellEditing();
377 }
378 if (selectedRow > -1) {
379 this.model.removeRow(selectedRow);
380 this.vid.removePpis(selectedRow);
381 selectedRow = -1;
382 this.save();
383 }
384 }
385 }
386
387 /**
388 Save all components of PPIs
389 if exists ppis, set the value directly
390 if not exists ppis, new an instance first
391
392 **/
393 public void save() {
394 try {
395 int count = this.vid.size();
396
397 this.ppis = PPIs.Factory.newInstance();
398 if (count > 0) {
399 for (int index = 0; index < count; index++) {
400 if (vid.getPpis(index).getType().equals(ed.getVPpiType().get(0))) {
401 Ppi p = Ppi.Factory.newInstance();
402 if (!isEmpty(vid.getPpis(index).getName())) {
403 p.setPpiCName(vid.getPpis(index).getName());
404 }
405 if (!isEmpty(vid.getPpis(index).getUsage())) {
406 p.setUsage(PpiUsage.Enum.forString(vid.getPpis(index).getUsage()));
407 }
408 if (!isEmpty(vid.getPpis(index).getFeatureFlag())) {
409 p.setFeatureFlag(vid.getPpis(index).getFeatureFlag());
410 }
411 if (vid.getPpis(index).getSupArchList() != null
412 && vid.getPpis(index).getSupArchList().size() > 0) {
413 p.setSupArchList(vid.getPpis(index).getSupArchList());
414 }
415 if (!isEmpty(vid.getPpis(index).getHelp())) {
416 p.setHelpText(vid.getPpis(index).getHelp());
417 }
418 this.ppis.addNewPpi();
419 this.ppis.setPpiArray(ppis.getPpiList().size() - 1, p);
420 }
421 if (vid.getPpis(index).getType().equals(ed.getVPpiType().get(1))) {
422 PpiNotify p = PpiNotify.Factory.newInstance();
423 if (!isEmpty(vid.getPpis(index).getName())) {
424 p.setPpiNotifyCName(vid.getPpis(index).getName());
425 }
426 if (!isEmpty(vid.getPpis(index).getUsage())) {
427 p.setUsage(PpiNotifyUsage.Enum.forString(vid.getPpis(index).getUsage()));
428 }
429 if (!isEmpty(vid.getPpis(index).getFeatureFlag())) {
430 p.setFeatureFlag(vid.getPpis(index).getFeatureFlag());
431 }
432 if (vid.getPpis(index).getSupArchList() != null
433 && vid.getPpis(index).getSupArchList().size() > 0) {
434 p.setSupArchList(vid.getPpis(index).getSupArchList());
435 }
436 if (!isEmpty(vid.getPpis(index).getHelp())) {
437 p.setHelpText(vid.getPpis(index).getHelp());
438 }
439 this.ppis.addNewPpiNotify();
440 this.ppis.setPpiNotifyArray(ppis.getPpiNotifyList().size() - 1, p);
441 }
442 }
443 }
444
445 this.msa.setPPIs(ppis);
446 this.omt.setSaved(false);
447 } catch (Exception e) {
448 Log.wrn("Update Ppis", e.getMessage());
449 Log.err("Update Ppis", e.getMessage());
450 }
451 }
452
453 /* (non-Javadoc)
454 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
455 *
456 */
457 public void valueChanged(ListSelectionEvent arg0) {
458 if (arg0.getValueIsAdjusting()) {
459 return;
460 }
461 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
462 if (lsm.isSelectionEmpty()) {
463 return;
464 } else {
465 selectedRow = lsm.getMinSelectionIndex();
466 }
467 }
468
469 /* (non-Javadoc)
470 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
471 *
472 */
473 public void mouseClicked(MouseEvent arg0) {
474 if (arg0.getClickCount() == 2) {
475 if (this.selectedRow < 0) {
476 return;
477 } else {
478 showEdit(selectedRow);
479 }
480 }
481 }
482
483 /* (non-Javadoc)
484 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
485 *
486 * Override componentResized to resize all components when frame's size is changed
487 */
488 public void componentResized(ComponentEvent arg0) {
489 int intCurrentWidth = this.getJContentPane().getWidth();
490 int intCurrentHeight = this.getJContentPane().getHeight();
491 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
492 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
493
494 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
495 intPreferredHeight);
496 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
497 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
498 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
499 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
500 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
501 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
502 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
503 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
504 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
505 }
506 }