]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleProtocols.java
1. Wrap text by word when showing a message box
[mirror_edk2.git] / Tools / 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 /**
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, 80, 20));
110 jButtonAdd.setText("Add");
111 jButtonAdd.addActionListener(this);
112 jButtonAdd.setPreferredSize(new java.awt.Dimension(80, 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, 80, 20));
127 jButtonRemove.setText("Remove");
128 jButtonRemove.addActionListener(this);
129 jButtonRemove.setPreferredSize(new java.awt.Dimension(80, 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, 80, 20));
144 jButtonUpdate.setPreferredSize(new java.awt.Dimension(80, 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("Protocols");
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(Protocols inProtocols) {
225 init();
226 this.protocols = inProtocols;
227
228 if (this.protocols != null) {
229 if (this.protocols.getProtocolList().size() > 0) {
230 for (int index = 0; index < this.protocols.getProtocolList().size(); index++) {
231 String arg0 = protocols.getProtocolList().get(index).getProtocolCName();
232 String arg1 = ed.getVProtocolType().get(0);
233 String arg2 = null;
234 if (protocols.getProtocolList().get(index).getUsage() != null) {
235 arg2 = protocols.getProtocolList().get(index).getUsage().toString();
236 }
237
238 String arg3 = protocols.getProtocolList().get(index).getFeatureFlag();
239 Vector<String> arg4 = Tools.convertListToVector(protocols.getProtocolList().get(index)
240 .getSupArchList());
241 String arg5 = protocols.getProtocolList().get(index).getHelpText();
242 id = new ProtocolsIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
243 vid.addProtocols(id);
244 }
245 }
246 if (this.protocols.getProtocolNotifyList().size() > 0) {
247 for (int index = 0; index < this.protocols.getProtocolNotifyList().size(); index++) {
248 String arg0 = protocols.getProtocolNotifyList().get(index).getProtocolNotifyCName();
249 String arg1 = ed.getVProtocolType().get(1);
250 String arg2 = null;
251 if (protocols.getProtocolNotifyList().get(index).getUsage() != null) {
252 arg2 = protocols.getProtocolNotifyList().get(index).getUsage().toString();
253 }
254
255 String arg3 = protocols.getProtocolNotifyList().get(index).getFeatureFlag();
256 Vector<String> arg4 = Tools.convertListToVector(protocols.getProtocolNotifyList().get(index)
257 .getSupArchList());
258 String arg5 = protocols.getProtocolNotifyList().get(index).getHelpText();
259 id = new ProtocolsIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
260 vid.addProtocols(id);
261 }
262 }
263 }
264 showTable();
265 }
266
267 /**
268 This is the default constructor
269
270 **/
271 public ModuleProtocols() {
272 super();
273 init();
274 this.setVisible(true);
275 }
276
277 /**
278 This is the override edit constructor
279
280 @param inProtocol The input data of ProtocolsDocument.Protocols
281
282 **/
283 public ModuleProtocols(OpeningModuleType inOmt) {
284 super();
285 this.omt = inOmt;
286 this.msa = omt.getXmlMsa();
287 init(msa.getProtocols());
288 this.setVisible(true);
289 }
290
291 /**
292 This method initializes jContentPane
293
294 @return javax.swing.JPanel jContentPane
295
296 **/
297 private JPanel getJContentPane() {
298 if (jContentPane == null) {
299 jContentPane = new JPanel();
300 jContentPane.setLayout(null);
301 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
302
303 jContentPane.add(getJButtonAdd(), null);
304 jContentPane.add(getJButtonRemove(), null);
305 jContentPane.add(getJButtonUpdate(), null);
306 jContentPane.add(getJScrollPaneTable(), null);
307 }
308 return jContentPane;
309 }
310
311 private void showEdit(int index) {
312 ProtocolsDlg dlg = new ProtocolsDlg(vid.getProtocols(index), new IFrame());
313 int result = dlg.showDialog();
314 if (result == DataType.RETURN_TYPE_OK) {
315 if (index == -1) {
316 this.vid.addProtocols(dlg.getId());
317 } else {
318 this.vid.setProtocols(dlg.getId(), index);
319 }
320 this.showTable();
321 this.save();
322 dlg.dispose();
323 }
324 if (result == DataType.RETURN_TYPE_CANCEL) {
325 dlg.dispose();
326 }
327 }
328
329 /**
330 Clear all table rows
331
332 **/
333 private void clearAll() {
334 if (model != null) {
335 for (int index = model.getRowCount() - 1; index >= 0; index--) {
336 model.removeRow(index);
337 }
338 }
339 }
340
341 /**
342 Read content of vector and put then into table
343
344 **/
345 private void showTable() {
346 clearAll();
347
348 if (vid.size() > 0) {
349 for (int index = 0; index < vid.size(); index++) {
350 model.addRow(vid.toStringVector(index));
351 }
352 }
353 this.jTable.repaint();
354 this.jTable.updateUI();
355 }
356
357 /* (non-Javadoc)
358 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
359 *
360 * Override actionPerformed to listen all actions
361 *
362 */
363 public void actionPerformed(ActionEvent arg0) {
364 if (arg0.getSource() == jButtonAdd) {
365 showEdit(-1);
366 }
367 if (arg0.getSource() == jButtonUpdate) {
368 if (this.selectedRow < 0) {
369 Log.wrn("Update Protocols", "Please select one record first.");
370 return;
371 }
372 showEdit(selectedRow);
373 }
374
375 if (arg0.getSource() == jButtonRemove) {
376 if (jTable.isEditing()) {
377 jTable.getCellEditor().stopCellEditing();
378 }
379 if (selectedRow > -1) {
380 this.model.removeRow(selectedRow);
381 this.vid.removeProtocols(selectedRow);
382 selectedRow = -1;
383 this.save();
384 }
385 }
386 }
387
388 /**
389 Save all components of Protocols
390 if exists protocols, set the value directly
391 if not exists protocols, new an instance first
392
393 **/
394 public void save() {
395 try {
396 int count = this.vid.size();
397
398 this.protocols = Protocols.Factory.newInstance();
399 if (count > 0) {
400 for (int index = 0; index < count; index++) {
401 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(0))) {
402 Protocol p = Protocol.Factory.newInstance();
403 if (!isEmpty(vid.getProtocols(index).getName())) {
404 p.setProtocolCName(vid.getProtocols(index).getName());
405 }
406 if (!isEmpty(vid.getProtocols(index).getUsage())) {
407 p.setUsage(ProtocolUsage.Enum.forString(vid.getProtocols(index).getUsage()));
408 }
409 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {
410 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());
411 }
412 if (vid.getProtocols(index).getSupArchList() != null
413 && vid.getProtocols(index).getSupArchList().size() > 0) {
414 p.setSupArchList(vid.getProtocols(index).getSupArchList());
415 }
416 if (!isEmpty(vid.getProtocols(index).getHelp())) {
417 p.setHelpText(vid.getProtocols(index).getHelp());
418 }
419 this.protocols.addNewProtocol();
420 this.protocols.setProtocolArray(protocols.getProtocolList().size() - 1, p);
421 }
422 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(1))) {
423 ProtocolNotify p = ProtocolNotify.Factory.newInstance();
424 if (!isEmpty(vid.getProtocols(index).getName())) {
425 p.setProtocolNotifyCName(vid.getProtocols(index).getName());
426 }
427 if (!isEmpty(vid.getProtocols(index).getUsage())) {
428 p.setUsage(ProtocolNotifyUsage.Enum.forString(vid.getProtocols(index).getUsage()));
429 }
430 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {
431 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());
432 }
433 if (vid.getProtocols(index).getSupArchList() != null
434 && vid.getProtocols(index).getSupArchList().size() > 0) {
435 p.setSupArchList(vid.getProtocols(index).getSupArchList());
436 }
437 if (!isEmpty(vid.getProtocols(index).getHelp())) {
438 p.setHelpText(vid.getProtocols(index).getHelp());
439 }
440 this.protocols.addNewProtocolNotify();
441 this.protocols.setProtocolNotifyArray(protocols.getProtocolNotifyList().size() - 1, p);
442 }
443 }
444 }
445
446 this.msa.setProtocols(protocols);
447 this.omt.setSaved(false);
448 } catch (Exception e) {
449 Log.wrn("Update Protocols", e.getMessage());
450 Log.err("Update Protocols", e.getMessage());
451 }
452 }
453
454 /* (non-Javadoc)
455 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
456 *
457 */
458 public void valueChanged(ListSelectionEvent arg0) {
459 if (arg0.getValueIsAdjusting()) {
460 return;
461 }
462 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
463 if (lsm.isSelectionEmpty()) {
464 return;
465 } else {
466 selectedRow = lsm.getMinSelectionIndex();
467 }
468 }
469
470 /* (non-Javadoc)
471 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
472 *
473 */
474 public void mouseClicked(MouseEvent arg0) {
475 if (arg0.getClickCount() == 2) {
476 if (this.selectedRow < 0) {
477 return;
478 } else {
479 showEdit(selectedRow);
480 }
481 }
482 }
483
484 /* (non-Javadoc)
485 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
486 *
487 * Override componentResized to resize all components when frame's size is changed
488 */
489 public void componentResized(ComponentEvent arg0) {
490 int intCurrentWidth = this.getJContentPane().getWidth();
491 int intCurrentHeight = this.getJContentPane().getHeight();
492 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
493 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
494
495 resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight);
496 relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
497 DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
498 relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
499 DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
500 relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
501 DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
502 }
503 }