]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleProtocols.java
1. Finish Enhancement EDKT345: Support deleting multiple rows together when remove...
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / module / ui / ModuleProtocols.java
1 /** @file
2
3 The file is used to create, update Protocol 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
16 package org.tianocore.frameworkwizard.module.ui;
17
18 import java.awt.Dimension;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ComponentEvent;
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.ProtocolNotifyUsage;
32 import org.tianocore.ProtocolUsage;
33 import org.tianocore.ProtocolsDocument;
34 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
35 import org.tianocore.ProtocolsDocument.Protocols;
36 import org.tianocore.ProtocolsDocument.Protocols.Protocol;
37 import org.tianocore.ProtocolsDocument.Protocols.ProtocolNotify;
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.Protocols.ProtocolsIdentification;
47 import org.tianocore.frameworkwizard.module.Identifications.Protocols.ProtocolsVector;
48 import org.tianocore.frameworkwizard.module.ui.dialog.ProtocolsDlg;
49
50 /**
51 The class is used to create, update Protocol of MSA/MBD file
52 It extends IInternalFrame
53
54
55
56 **/
57 public class ModuleProtocols extends IInternalFrame {
58
59 ///
60 /// Define class Serial Version UID
61 ///
62 private static final long serialVersionUID = -9084913640747858848L;
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 ProtocolsDocument.Protocols protocols = null;
89
90 private ProtocolsIdentification id = null;
91
92 private ProtocolsVector vid = new ProtocolsVector();
93
94 private EnumerationData ed = new EnumerationData();
95
96 private IDefaultTableModel model = null;
97
98 private int selectedRow = -1;
99
100 private IFrame parentFrame = null;
101
102 /**
103 This method initializes jButtonAdd
104
105 @return javax.swing.JButton jButtonAdd
106
107 **/
108 private JButton getJButtonAdd() {
109 if (jButtonAdd == null) {
110 jButtonAdd = new JButton();
111 jButtonAdd.setBounds(new java.awt.Rectangle(230, 220, 90, 20));
112 jButtonAdd.setText("Add");
113 jButtonAdd.addActionListener(this);
114 jButtonAdd.setPreferredSize(new java.awt.Dimension(90, 20));
115 }
116 return jButtonAdd;
117 }
118
119 /**
120 This method initializes jButtonRemove
121
122 @return javax.swing.JButton jButtonRemove
123
124 **/
125 private JButton getJButtonRemove() {
126 if (jButtonRemove == null) {
127 jButtonRemove = new JButton();
128 jButtonRemove.setBounds(new java.awt.Rectangle(400, 220, 90, 20));
129 jButtonRemove.setText("Remove");
130 jButtonRemove.addActionListener(this);
131 jButtonRemove.setPreferredSize(new java.awt.Dimension(90, 20));
132 }
133 return jButtonRemove;
134 }
135
136 /**
137 This method initializes jButtonUpdate
138
139 @return javax.swing.JButton jButtonUpdate
140
141 **/
142 private JButton getJButtonUpdate() {
143 if (jButtonUpdate == null) {
144 jButtonUpdate = new JButton();
145 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 220, 90, 20));
146 jButtonUpdate.setPreferredSize(new java.awt.Dimension(90, 20));
147 jButtonUpdate.setText("Edit");
148 jButtonUpdate.addActionListener(this);
149 }
150 return jButtonUpdate;
151 }
152
153 /**
154 This method initializes jScrollPane
155
156 @return javax.swing.JScrollPane
157 */
158 private JScrollPane getJScrollPane() {
159 if (jScrollPane == null) {
160 jScrollPane = new JScrollPane();
161 jScrollPane.setViewportView(getJContentPane());
162 }
163 return jScrollPane;
164 }
165
166 /**
167 This method initializes jScrollPaneTable
168
169 @return javax.swing.JScrollPane
170 **/
171 private JScrollPane getJScrollPaneTable() {
172 if (jScrollPaneTable == null) {
173 jScrollPaneTable = new JScrollPane();
174 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
175 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
176 jScrollPaneTable.setViewportView(getJTable());
177 }
178 return jScrollPaneTable;
179 }
180
181 /**
182 This method initializes jTable
183
184 @return javax.swing.JTable
185 **/
186 private JTable getJTable() {
187 if (jTable == null) {
188 jTable = new JTable();
189 model = new IDefaultTableModel();
190 jTable = new JTable(model);
191 jTable.setRowHeight(20);
192
193 model.addColumn("Guid C_Name");
194 model.addColumn("Type");
195 model.addColumn("Usage");
196
197 jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
198 jTable.getSelectionModel().addListSelectionListener(this);
199 jTable.getModel().addTableModelListener(this);
200 jTable.addMouseListener(this);
201 }
202 return jTable;
203 }
204
205 public static void main(String[] args) {
206
207 }
208
209 /**
210 This method initializes this
211
212 **/
213 private void init() {
214 this.setSize(500, 515);
215 this.setContentPane(getJScrollPane());
216 this.setTitle("Protocols");
217 }
218
219 /**
220 This method initializes this
221 Fill values to all fields if these values are not empty
222
223 @param inPackageDependencies
224
225 **/
226 private void init(Protocols inProtocols) {
227 init();
228 this.protocols = inProtocols;
229
230 if (this.protocols != null) {
231 if (this.protocols.getProtocolList().size() > 0) {
232 for (int index = 0; index < this.protocols.getProtocolList().size(); index++) {
233 String arg0 = protocols.getProtocolList().get(index).getProtocolCName();
234 String arg1 = ed.getVProtocolType().get(0);
235 String arg2 = null;
236 if (protocols.getProtocolList().get(index).getUsage() != null) {
237 arg2 = protocols.getProtocolList().get(index).getUsage().toString();
238 }
239
240 String arg3 = protocols.getProtocolList().get(index).getFeatureFlag();
241 Vector<String> arg4 = Tools.convertListToVector(protocols.getProtocolList().get(index)
242 .getSupArchList());
243 String arg5 = protocols.getProtocolList().get(index).getHelpText();
244 id = new ProtocolsIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
245 vid.addProtocols(id);
246 }
247 }
248 if (this.protocols.getProtocolNotifyList().size() > 0) {
249 for (int index = 0; index < this.protocols.getProtocolNotifyList().size(); index++) {
250 String arg0 = protocols.getProtocolNotifyList().get(index).getProtocolNotifyCName();
251 String arg1 = ed.getVProtocolType().get(1);
252 String arg2 = null;
253 if (protocols.getProtocolNotifyList().get(index).getUsage() != null) {
254 arg2 = protocols.getProtocolNotifyList().get(index).getUsage().toString();
255 }
256
257 String arg3 = protocols.getProtocolNotifyList().get(index).getFeatureFlag();
258 Vector<String> arg4 = Tools.convertListToVector(protocols.getProtocolNotifyList().get(index)
259 .getSupArchList());
260 String arg5 = protocols.getProtocolNotifyList().get(index).getHelpText();
261 id = new ProtocolsIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
262 vid.addProtocols(id);
263 }
264 }
265 }
266 showTable();
267 }
268
269 /**
270 This is the default constructor
271
272 **/
273 public ModuleProtocols() {
274 super();
275 init();
276 this.setVisible(true);
277 }
278
279 /**
280 This is the override edit constructor
281
282 @param inProtocol The input data of ProtocolsDocument.Protocols
283
284 **/
285 public ModuleProtocols(OpeningModuleType inOmt, IFrame iFrame) {
286 super();
287 this.omt = inOmt;
288 this.msa = omt.getXmlMsa();
289 this.parentFrame = iFrame;
290 init(msa.getProtocols());
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 ProtocolsDlg dlg = new ProtocolsDlg(vid.getProtocols(index), this.parentFrame, omt.getId());
316 int result = dlg.showDialog();
317 if (result == DataType.RETURN_TYPE_OK) {
318 if (index == -1) {
319 this.vid.addProtocols(dlg.getId());
320 } else {
321 this.vid.setProtocols(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.wrn("Update Protocols", "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
383 int selectedRows[] = this.jTable.getSelectedRows();
384
385 if (selectedRows != null) {
386 for (int index = selectedRows.length - 1; index > -1; index--) {
387 this.model.removeRow(selectedRows[index]);
388 this.vid.removeProtocols(selectedRows[index]);
389 }
390 selectedRow = -1;
391 this.save();
392 }
393 }
394 }
395
396 /**
397 Save all components of Protocols
398 if exists protocols, set the value directly
399 if not exists protocols, new an instance first
400
401 **/
402 public void save() {
403 try {
404 int count = this.vid.size();
405
406 this.protocols = Protocols.Factory.newInstance();
407 if (count > 0) {
408 for (int index = 0; index < count; index++) {
409 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(0))) {
410 Protocol p = Protocol.Factory.newInstance();
411 if (!isEmpty(vid.getProtocols(index).getName())) {
412 p.setProtocolCName(vid.getProtocols(index).getName());
413 }
414 if (!isEmpty(vid.getProtocols(index).getUsage())) {
415 p.setUsage(ProtocolUsage.Enum.forString(vid.getProtocols(index).getUsage()));
416 }
417 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {
418 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());
419 }
420 if (vid.getProtocols(index).getSupArchList() != null
421 && vid.getProtocols(index).getSupArchList().size() > 0) {
422 p.setSupArchList(vid.getProtocols(index).getSupArchList());
423 }
424 if (!isEmpty(vid.getProtocols(index).getHelp())) {
425 p.setHelpText(vid.getProtocols(index).getHelp());
426 }
427 this.protocols.addNewProtocol();
428 this.protocols.setProtocolArray(protocols.getProtocolList().size() - 1, p);
429 }
430 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(1))) {
431 ProtocolNotify p = ProtocolNotify.Factory.newInstance();
432 if (!isEmpty(vid.getProtocols(index).getName())) {
433 p.setProtocolNotifyCName(vid.getProtocols(index).getName());
434 }
435 if (!isEmpty(vid.getProtocols(index).getUsage())) {
436 p.setUsage(ProtocolNotifyUsage.Enum.forString(vid.getProtocols(index).getUsage()));
437 }
438 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {
439 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());
440 }
441 if (vid.getProtocols(index).getSupArchList() != null
442 && vid.getProtocols(index).getSupArchList().size() > 0) {
443 p.setSupArchList(vid.getProtocols(index).getSupArchList());
444 }
445 if (!isEmpty(vid.getProtocols(index).getHelp())) {
446 p.setHelpText(vid.getProtocols(index).getHelp());
447 }
448 this.protocols.addNewProtocolNotify();
449 this.protocols.setProtocolNotifyArray(protocols.getProtocolNotifyList().size() - 1, p);
450 }
451 }
452 }
453
454 this.msa.setProtocols(protocols);
455 this.omt.setSaved(false);
456 } catch (Exception e) {
457 Log.wrn("Update Protocols", e.getMessage());
458 Log.err("Update Protocols", e.getMessage());
459 }
460 }
461
462 /* (non-Javadoc)
463 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
464 *
465 */
466 public void valueChanged(ListSelectionEvent arg0) {
467 if (arg0.getValueIsAdjusting()) {
468 return;
469 }
470 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
471 if (lsm.isSelectionEmpty()) {
472 return;
473 } else {
474 selectedRow = lsm.getMinSelectionIndex();
475 }
476 }
477
478 /* (non-Javadoc)
479 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
480 *
481 */
482 public void mouseClicked(MouseEvent arg0) {
483 if (arg0.getClickCount() == 2) {
484 if (this.selectedRow < 0) {
485 return;
486 } else {
487 showEdit(selectedRow);
488 }
489 }
490 }
491
492 /* (non-Javadoc)
493 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
494 *
495 * Override componentResized to resize all components when frame's size is changed
496 */
497 public void componentResized(ComponentEvent arg0) {
498 int intCurrentWidth = this.getJContentPane().getWidth();
499 int intCurrentHeight = this.getJContentPane().getHeight();
500 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
501 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
502
503 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
504 intPreferredHeight);
505 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
506 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
507 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
508 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
509 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
510 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
511 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
512 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
513 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
514 }
515 }