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