]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleProtocols.java
1. Fix EDKT336: Back to focused dialog window when shift to wizard
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / module / ui / ModuleProtocols.java
CommitLineData
a13899c5 1/** @file\r
2 \r
3 The file is used to create, update Protocol of MSA/MBD file\r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15\r
16package org.tianocore.frameworkwizard.module.ui;\r
17\r
06a19cee 18import java.awt.Dimension;\r
a13899c5 19import java.awt.event.ActionEvent;\r
20import java.awt.event.ComponentEvent;\r
06a19cee 21import java.awt.event.MouseEvent;\r
a13899c5 22import java.util.Vector;\r
23\r
24import javax.swing.JButton;\r
a13899c5 25import javax.swing.JPanel;\r
26import javax.swing.JScrollPane;\r
06a19cee 27import javax.swing.JTable;\r
28import javax.swing.ListSelectionModel;\r
29import javax.swing.event.ListSelectionEvent;\r
a13899c5 30\r
31import org.tianocore.ProtocolNotifyUsage;\r
32import org.tianocore.ProtocolUsage;\r
33import org.tianocore.ProtocolsDocument;\r
34import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\r
35import org.tianocore.ProtocolsDocument.Protocols;\r
36import org.tianocore.ProtocolsDocument.Protocols.Protocol;\r
37import org.tianocore.ProtocolsDocument.Protocols.ProtocolNotify;\r
38import org.tianocore.frameworkwizard.common.DataType;\r
a13899c5 39import org.tianocore.frameworkwizard.common.EnumerationData;\r
06a19cee 40import org.tianocore.frameworkwizard.common.IDefaultTableModel;\r
a13899c5 41import org.tianocore.frameworkwizard.common.Log;\r
a13899c5 42import org.tianocore.frameworkwizard.common.Tools;\r
79cb6fdb 43import org.tianocore.frameworkwizard.common.Identifications.OpeningModuleType;\r
06a19cee 44import org.tianocore.frameworkwizard.common.ui.IFrame;\r
a13899c5 45import org.tianocore.frameworkwizard.common.ui.IInternalFrame;\r
79cb6fdb 46import org.tianocore.frameworkwizard.module.Identifications.Protocols.ProtocolsIdentification;\r
47import org.tianocore.frameworkwizard.module.Identifications.Protocols.ProtocolsVector;\r
06a19cee 48import org.tianocore.frameworkwizard.module.ui.dialog.ProtocolsDlg;\r
a13899c5 49\r
50/**\r
51 The class is used to create, update Protocol of MSA/MBD file\r
52 It extends IInternalFrame\r
53 \r
54\r
55\r
56 **/\r
57public class ModuleProtocols extends IInternalFrame {\r
58\r
59 ///\r
60 /// Define class Serial Version UID\r
61 ///\r
62 private static final long serialVersionUID = -9084913640747858848L;\r
63\r
64 //\r
65 //Define class members\r
66 //\r
67 private JPanel jContentPane = null;\r
68\r
a13899c5 69 private JButton jButtonAdd = null;\r
70\r
71 private JButton jButtonRemove = null;\r
72\r
73 private JButton jButtonUpdate = null;\r
74\r
75 private JScrollPane jScrollPane = null;\r
76\r
06a19cee 77 private JScrollPane jScrollPaneTable = null;\r
a13899c5 78\r
06a19cee 79 private JTable jTable = null;\r
a13899c5 80\r
81 //\r
82 // Not used by UI\r
83 //\r
a13899c5 84 private OpeningModuleType omt = null;\r
85\r
86 private ModuleSurfaceArea msa = null;\r
87\r
88 private ProtocolsDocument.Protocols protocols = null;\r
89\r
90 private ProtocolsIdentification id = null;\r
91\r
92 private ProtocolsVector vid = new ProtocolsVector();\r
93\r
a13899c5 94 private EnumerationData ed = new EnumerationData();\r
95\r
06a19cee 96 private IDefaultTableModel model = null;\r
a13899c5 97\r
06a19cee 98 private int selectedRow = -1;\r
a9e882c9 99 \r
100 private IFrame parentFrame = null;\r
a13899c5 101\r
102 /**\r
103 This method initializes jButtonAdd \r
104 \r
105 @return javax.swing.JButton jButtonAdd\r
106 \r
107 **/\r
108 private JButton getJButtonAdd() {\r
109 if (jButtonAdd == null) {\r
110 jButtonAdd = new JButton();\r
3767c118 111 jButtonAdd.setBounds(new java.awt.Rectangle(230, 220, 90, 20));\r
a13899c5 112 jButtonAdd.setText("Add");\r
113 jButtonAdd.addActionListener(this);\r
3767c118 114 jButtonAdd.setPreferredSize(new java.awt.Dimension(90, 20));\r
a13899c5 115 }\r
116 return jButtonAdd;\r
117 }\r
118\r
119 /**\r
120 This method initializes jButtonRemove \r
121 \r
122 @return javax.swing.JButton jButtonRemove\r
123 \r
124 **/\r
125 private JButton getJButtonRemove() {\r
126 if (jButtonRemove == null) {\r
127 jButtonRemove = new JButton();\r
3767c118 128 jButtonRemove.setBounds(new java.awt.Rectangle(400, 220, 90, 20));\r
a13899c5 129 jButtonRemove.setText("Remove");\r
130 jButtonRemove.addActionListener(this);\r
3767c118 131 jButtonRemove.setPreferredSize(new java.awt.Dimension(90, 20));\r
a13899c5 132 }\r
133 return jButtonRemove;\r
134 }\r
135\r
136 /**\r
137 This method initializes jButtonUpdate \r
138 \r
139 @return javax.swing.JButton jButtonUpdate\r
140 \r
141 **/\r
142 private JButton getJButtonUpdate() {\r
143 if (jButtonUpdate == null) {\r
144 jButtonUpdate = new JButton();\r
3767c118 145 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 220, 90, 20));\r
146 jButtonUpdate.setPreferredSize(new java.awt.Dimension(90, 20));\r
06a19cee 147 jButtonUpdate.setText("Edit");\r
a13899c5 148 jButtonUpdate.addActionListener(this);\r
149 }\r
150 return jButtonUpdate;\r
151 }\r
152\r
a13899c5 153 /**\r
154 This method initializes jScrollPane \r
155 \r
156 @return javax.swing.JScrollPane \r
157 */\r
158 private JScrollPane getJScrollPane() {\r
159 if (jScrollPane == null) {\r
160 jScrollPane = new JScrollPane();\r
161 jScrollPane.setViewportView(getJContentPane());\r
162 }\r
163 return jScrollPane;\r
164 }\r
165\r
166 /**\r
06a19cee 167 This method initializes jScrollPaneTable \r
a13899c5 168 \r
06a19cee 169 @return javax.swing.JScrollPane \r
a13899c5 170 **/\r
06a19cee 171 private JScrollPane getJScrollPaneTable() {\r
172 if (jScrollPaneTable == null) {\r
173 jScrollPaneTable = new JScrollPane();\r
174 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));\r
175 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));\r
176 jScrollPaneTable.setViewportView(getJTable());\r
a13899c5 177 }\r
06a19cee 178 return jScrollPaneTable;\r
a13899c5 179 }\r
180\r
181 /**\r
06a19cee 182 This method initializes jTable \r
a13899c5 183 \r
06a19cee 184 @return javax.swing.JTable \r
a13899c5 185 **/\r
06a19cee 186 private JTable getJTable() {\r
187 if (jTable == null) {\r
188 jTable = new JTable();\r
189 model = new IDefaultTableModel();\r
190 jTable = new JTable(model);\r
191 jTable.setRowHeight(20);\r
192\r
193 model.addColumn("Guid C_Name");\r
194 model.addColumn("Type");\r
195 model.addColumn("Usage");\r
196\r
197 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
198 jTable.getSelectionModel().addListSelectionListener(this);\r
199 jTable.getModel().addTableModelListener(this);\r
200 jTable.addMouseListener(this);\r
a13899c5 201 }\r
06a19cee 202 return jTable;\r
a13899c5 203 }\r
a13899c5 204\r
205 public static void main(String[] args) {\r
206\r
207 }\r
208\r
209 /**\r
210 This method initializes this\r
211 \r
212 **/\r
213 private void init() {\r
214 this.setSize(500, 515);\r
215 this.setContentPane(getJScrollPane());\r
216 this.setTitle("Protocols");\r
a13899c5 217 }\r
218\r
219 /**\r
220 This method initializes this\r
221 Fill values to all fields if these values are not empty\r
222 \r
223 @param inPackageDependencies\r
224\r
225 **/\r
226 private void init(Protocols inProtocols) {\r
227 init();\r
228 this.protocols = inProtocols;\r
229\r
230 if (this.protocols != null) {\r
231 if (this.protocols.getProtocolList().size() > 0) {\r
232 for (int index = 0; index < this.protocols.getProtocolList().size(); index++) {\r
233 String arg0 = protocols.getProtocolList().get(index).getProtocolCName();\r
234 String arg1 = ed.getVProtocolType().get(0);\r
235 String arg2 = null;\r
236 if (protocols.getProtocolList().get(index).getUsage() != null) {\r
06a19cee 237 arg2 = protocols.getProtocolList().get(index).getUsage().toString();\r
a13899c5 238 }\r
06a19cee 239\r
a13899c5 240 String arg3 = protocols.getProtocolList().get(index).getFeatureFlag();\r
241 Vector<String> arg4 = Tools.convertListToVector(protocols.getProtocolList().get(index)\r
242 .getSupArchList());\r
243 String arg5 = protocols.getProtocolList().get(index).getHelpText();\r
244 id = new ProtocolsIdentification(arg0, arg1, arg2, arg3, arg4, arg5);\r
245 vid.addProtocols(id);\r
246 }\r
247 }\r
248 if (this.protocols.getProtocolNotifyList().size() > 0) {\r
249 for (int index = 0; index < this.protocols.getProtocolNotifyList().size(); index++) {\r
250 String arg0 = protocols.getProtocolNotifyList().get(index).getProtocolNotifyCName();\r
251 String arg1 = ed.getVProtocolType().get(1);\r
252 String arg2 = null;\r
253 if (protocols.getProtocolNotifyList().get(index).getUsage() != null) {\r
06a19cee 254 arg2 = protocols.getProtocolNotifyList().get(index).getUsage().toString();\r
a13899c5 255 }\r
06a19cee 256\r
a13899c5 257 String arg3 = protocols.getProtocolNotifyList().get(index).getFeatureFlag();\r
258 Vector<String> arg4 = Tools.convertListToVector(protocols.getProtocolNotifyList().get(index)\r
259 .getSupArchList());\r
260 String arg5 = protocols.getProtocolNotifyList().get(index).getHelpText();\r
261 id = new ProtocolsIdentification(arg0, arg1, arg2, arg3, arg4, arg5);\r
262 vid.addProtocols(id);\r
263 }\r
264 }\r
265 }\r
06a19cee 266 showTable();\r
a13899c5 267 }\r
268\r
269 /**\r
270 This is the default constructor\r
271 \r
272 **/\r
273 public ModuleProtocols() {\r
274 super();\r
275 init();\r
276 this.setVisible(true);\r
277 }\r
278\r
279 /**\r
280 This is the override edit constructor\r
281 \r
282 @param inProtocol The input data of ProtocolsDocument.Protocols\r
283 \r
284 **/\r
a9e882c9 285 public ModuleProtocols(OpeningModuleType inOmt, IFrame iFrame) {\r
a13899c5 286 super();\r
287 this.omt = inOmt;\r
288 this.msa = omt.getXmlMsa();\r
a9e882c9 289 this.parentFrame = iFrame;\r
a13899c5 290 init(msa.getProtocols());\r
291 this.setVisible(true);\r
292 }\r
293\r
a13899c5 294 /**\r
295 This method initializes jContentPane\r
296 \r
297 @return javax.swing.JPanel jContentPane\r
298 \r
299 **/\r
300 private JPanel getJContentPane() {\r
301 if (jContentPane == null) {\r
a13899c5 302 jContentPane = new JPanel();\r
303 jContentPane.setLayout(null);\r
06a19cee 304 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));\r
305\r
a13899c5 306 jContentPane.add(getJButtonAdd(), null);\r
307 jContentPane.add(getJButtonRemove(), null);\r
308 jContentPane.add(getJButtonUpdate(), null);\r
06a19cee 309 jContentPane.add(getJScrollPaneTable(), null);\r
a13899c5 310 }\r
311 return jContentPane;\r
312 }\r
a929458e 313\r
06a19cee 314 private void showEdit(int index) {\r
a9e882c9 315 ProtocolsDlg dlg = new ProtocolsDlg(vid.getProtocols(index), this.parentFrame, omt.getId());\r
06a19cee 316 int result = dlg.showDialog();\r
317 if (result == DataType.RETURN_TYPE_OK) {\r
318 if (index == -1) {\r
319 this.vid.addProtocols(dlg.getId());\r
320 } else {\r
321 this.vid.setProtocols(dlg.getId(), index);\r
322 }\r
323 this.showTable();\r
324 this.save();\r
325 dlg.dispose();\r
326 }\r
327 if (result == DataType.RETURN_TYPE_CANCEL) {\r
328 dlg.dispose();\r
329 }\r
330 }\r
331\r
332 /**\r
333 Clear all table rows\r
334 \r
335 **/\r
336 private void clearAll() {\r
337 if (model != null) {\r
338 for (int index = model.getRowCount() - 1; index >= 0; index--) {\r
339 model.removeRow(index);\r
340 }\r
341 }\r
342 }\r
a13899c5 343\r
344 /**\r
06a19cee 345 Read content of vector and put then into table\r
a13899c5 346 \r
347 **/\r
06a19cee 348 private void showTable() {\r
349 clearAll();\r
a13899c5 350\r
06a19cee 351 if (vid.size() > 0) {\r
352 for (int index = 0; index < vid.size(); index++) {\r
353 model.addRow(vid.toStringVector(index));\r
354 }\r
355 }\r
356 this.jTable.repaint();\r
357 this.jTable.updateUI();\r
a13899c5 358 }\r
359\r
360 /* (non-Javadoc)\r
361 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)\r
362 *\r
363 * Override actionPerformed to listen all actions\r
364 * \r
365 */\r
366 public void actionPerformed(ActionEvent arg0) {\r
367 if (arg0.getSource() == jButtonAdd) {\r
06a19cee 368 showEdit(-1);\r
a13899c5 369 }\r
370 if (arg0.getSource() == jButtonUpdate) {\r
06a19cee 371 if (this.selectedRow < 0) {\r
ed1665f2 372 Log.wrn("Update Protocols", "Please select one record first.");\r
a13899c5 373 return;\r
374 }\r
06a19cee 375 showEdit(selectedRow);\r
a13899c5 376 }\r
a13899c5 377\r
06a19cee 378 if (arg0.getSource() == jButtonRemove) {\r
379 if (jTable.isEditing()) {\r
380 jTable.getCellEditor().stopCellEditing();\r
a13899c5 381 }\r
06a19cee 382 if (selectedRow > -1) {\r
383 this.model.removeRow(selectedRow);\r
384 this.vid.removeProtocols(selectedRow);\r
385 selectedRow = -1;\r
386 this.save();\r
a13899c5 387 }\r
388 }\r
a13899c5 389 }\r
390\r
391 /**\r
392 Save all components of Protocols\r
393 if exists protocols, set the value directly\r
394 if not exists protocols, new an instance first\r
395 \r
396 **/\r
397 public void save() {\r
398 try {\r
399 int count = this.vid.size();\r
400\r
401 this.protocols = Protocols.Factory.newInstance();\r
402 if (count > 0) {\r
403 for (int index = 0; index < count; index++) {\r
404 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(0))) {\r
405 Protocol p = Protocol.Factory.newInstance();\r
406 if (!isEmpty(vid.getProtocols(index).getName())) {\r
407 p.setProtocolCName(vid.getProtocols(index).getName());\r
408 }\r
409 if (!isEmpty(vid.getProtocols(index).getUsage())) {\r
410 p.setUsage(ProtocolUsage.Enum.forString(vid.getProtocols(index).getUsage()));\r
411 }\r
412 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {\r
413 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());\r
414 }\r
415 if (vid.getProtocols(index).getSupArchList() != null\r
416 && vid.getProtocols(index).getSupArchList().size() > 0) {\r
417 p.setSupArchList(vid.getProtocols(index).getSupArchList());\r
418 }\r
419 if (!isEmpty(vid.getProtocols(index).getHelp())) {\r
420 p.setHelpText(vid.getProtocols(index).getHelp());\r
421 }\r
422 this.protocols.addNewProtocol();\r
423 this.protocols.setProtocolArray(protocols.getProtocolList().size() - 1, p);\r
424 }\r
425 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(1))) {\r
426 ProtocolNotify p = ProtocolNotify.Factory.newInstance();\r
427 if (!isEmpty(vid.getProtocols(index).getName())) {\r
428 p.setProtocolNotifyCName(vid.getProtocols(index).getName());\r
429 }\r
430 if (!isEmpty(vid.getProtocols(index).getUsage())) {\r
431 p.setUsage(ProtocolNotifyUsage.Enum.forString(vid.getProtocols(index).getUsage()));\r
432 }\r
433 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {\r
434 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());\r
435 }\r
436 if (vid.getProtocols(index).getSupArchList() != null\r
437 && vid.getProtocols(index).getSupArchList().size() > 0) {\r
438 p.setSupArchList(vid.getProtocols(index).getSupArchList());\r
439 }\r
440 if (!isEmpty(vid.getProtocols(index).getHelp())) {\r
441 p.setHelpText(vid.getProtocols(index).getHelp());\r
442 }\r
443 this.protocols.addNewProtocolNotify();\r
444 this.protocols.setProtocolNotifyArray(protocols.getProtocolNotifyList().size() - 1, p);\r
445 }\r
446 }\r
447 }\r
448\r
449 this.msa.setProtocols(protocols);\r
450 this.omt.setSaved(false);\r
451 } catch (Exception e) {\r
ed1665f2 452 Log.wrn("Update Protocols", e.getMessage());\r
a13899c5 453 Log.err("Update Protocols", e.getMessage());\r
454 }\r
455 }\r
a929458e 456\r
a13899c5 457 /* (non-Javadoc)\r
06a19cee 458 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)\r
459 *\r
a13899c5 460 */\r
06a19cee 461 public void valueChanged(ListSelectionEvent arg0) {\r
462 if (arg0.getValueIsAdjusting()) {\r
a13899c5 463 return;\r
464 }\r
06a19cee 465 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();\r
466 if (lsm.isSelectionEmpty()) {\r
a13899c5 467 return;\r
a13899c5 468 } else {\r
06a19cee 469 selectedRow = lsm.getMinSelectionIndex();\r
a13899c5 470 }\r
a13899c5 471 }\r
472\r
473 /* (non-Javadoc)\r
06a19cee 474 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)\r
a13899c5 475 *\r
a13899c5 476 */\r
06a19cee 477 public void mouseClicked(MouseEvent arg0) {\r
478 if (arg0.getClickCount() == 2) {\r
479 if (this.selectedRow < 0) {\r
480 return;\r
a13899c5 481 } else {\r
06a19cee 482 showEdit(selectedRow);\r
a13899c5 483 }\r
484 }\r
485 }\r
06a19cee 486\r
487 /* (non-Javadoc)\r
488 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)\r
489 * \r
490 * Override componentResized to resize all components when frame's size is changed\r
491 */\r
492 public void componentResized(ComponentEvent arg0) {\r
493 int intCurrentWidth = this.getJContentPane().getWidth();\r
494 int intCurrentHeight = this.getJContentPane().getHeight();\r
495 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;\r
496 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;\r
497\r
8792f60f 498 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
499 intPreferredHeight);\r
500 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
501 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,\r
502 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);\r
503 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
504 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,\r
505 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);\r
506 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
507 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,\r
508 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);\r
06a19cee 509 }\r
a13899c5 510}\r