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