]> git.proxmox.com Git - mirror_edk2.git/blame - 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
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
ff7bc6c4 197 jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);\r
06a19cee 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
ff7bc6c4 382 \r
383 int selectedRows[] = this.jTable.getSelectedRows();\r
384 \r
385 if (selectedRows != null) {\r
386 for (int index = selectedRows.length - 1; index > -1; index--) {\r
387 this.model.removeRow(selectedRows[index]);\r
388 this.vid.removeProtocols(selectedRows[index]);\r
389 }\r
06a19cee 390 selectedRow = -1;\r
391 this.save();\r
a13899c5 392 }\r
393 }\r
a13899c5 394 }\r
395\r
396 /**\r
397 Save all components of Protocols\r
398 if exists protocols, set the value directly\r
399 if not exists protocols, new an instance first\r
400 \r
401 **/\r
402 public void save() {\r
403 try {\r
404 int count = this.vid.size();\r
405\r
406 this.protocols = Protocols.Factory.newInstance();\r
407 if (count > 0) {\r
408 for (int index = 0; index < count; index++) {\r
409 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(0))) {\r
410 Protocol p = Protocol.Factory.newInstance();\r
411 if (!isEmpty(vid.getProtocols(index).getName())) {\r
412 p.setProtocolCName(vid.getProtocols(index).getName());\r
413 }\r
414 if (!isEmpty(vid.getProtocols(index).getUsage())) {\r
415 p.setUsage(ProtocolUsage.Enum.forString(vid.getProtocols(index).getUsage()));\r
416 }\r
417 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {\r
418 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());\r
419 }\r
420 if (vid.getProtocols(index).getSupArchList() != null\r
421 && vid.getProtocols(index).getSupArchList().size() > 0) {\r
422 p.setSupArchList(vid.getProtocols(index).getSupArchList());\r
423 }\r
424 if (!isEmpty(vid.getProtocols(index).getHelp())) {\r
425 p.setHelpText(vid.getProtocols(index).getHelp());\r
426 }\r
427 this.protocols.addNewProtocol();\r
428 this.protocols.setProtocolArray(protocols.getProtocolList().size() - 1, p);\r
429 }\r
430 if (vid.getProtocols(index).getType().equals(ed.getVProtocolType().get(1))) {\r
431 ProtocolNotify p = ProtocolNotify.Factory.newInstance();\r
432 if (!isEmpty(vid.getProtocols(index).getName())) {\r
433 p.setProtocolNotifyCName(vid.getProtocols(index).getName());\r
434 }\r
435 if (!isEmpty(vid.getProtocols(index).getUsage())) {\r
436 p.setUsage(ProtocolNotifyUsage.Enum.forString(vid.getProtocols(index).getUsage()));\r
437 }\r
438 if (!isEmpty(vid.getProtocols(index).getFeatureFlag())) {\r
439 p.setFeatureFlag(vid.getProtocols(index).getFeatureFlag());\r
440 }\r
441 if (vid.getProtocols(index).getSupArchList() != null\r
442 && vid.getProtocols(index).getSupArchList().size() > 0) {\r
443 p.setSupArchList(vid.getProtocols(index).getSupArchList());\r
444 }\r
445 if (!isEmpty(vid.getProtocols(index).getHelp())) {\r
446 p.setHelpText(vid.getProtocols(index).getHelp());\r
447 }\r
448 this.protocols.addNewProtocolNotify();\r
449 this.protocols.setProtocolNotifyArray(protocols.getProtocolNotifyList().size() - 1, p);\r
450 }\r
451 }\r
452 }\r
453\r
454 this.msa.setProtocols(protocols);\r
455 this.omt.setSaved(false);\r
456 } catch (Exception e) {\r
ed1665f2 457 Log.wrn("Update Protocols", e.getMessage());\r
a13899c5 458 Log.err("Update Protocols", e.getMessage());\r
459 }\r
460 }\r
a929458e 461\r
a13899c5 462 /* (non-Javadoc)\r
06a19cee 463 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)\r
464 *\r
a13899c5 465 */\r
06a19cee 466 public void valueChanged(ListSelectionEvent arg0) {\r
467 if (arg0.getValueIsAdjusting()) {\r
a13899c5 468 return;\r
469 }\r
06a19cee 470 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();\r
471 if (lsm.isSelectionEmpty()) {\r
a13899c5 472 return;\r
a13899c5 473 } else {\r
06a19cee 474 selectedRow = lsm.getMinSelectionIndex();\r
a13899c5 475 }\r
a13899c5 476 }\r
477\r
478 /* (non-Javadoc)\r
06a19cee 479 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)\r
a13899c5 480 *\r
a13899c5 481 */\r
06a19cee 482 public void mouseClicked(MouseEvent arg0) {\r
483 if (arg0.getClickCount() == 2) {\r
484 if (this.selectedRow < 0) {\r
485 return;\r
a13899c5 486 } else {\r
06a19cee 487 showEdit(selectedRow);\r
a13899c5 488 }\r
489 }\r
490 }\r
06a19cee 491\r
492 /* (non-Javadoc)\r
493 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)\r
494 * \r
495 * Override componentResized to resize all components when frame's size is changed\r
496 */\r
497 public void componentResized(ComponentEvent arg0) {\r
498 int intCurrentWidth = this.getJContentPane().getWidth();\r
499 int intCurrentHeight = this.getJContentPane().getHeight();\r
500 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;\r
501 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;\r
502\r
8792f60f 503 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
504 intPreferredHeight);\r
505 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
506 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,\r
507 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);\r
508 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
509 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,\r
510 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);\r
511 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
512 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,\r
513 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);\r
06a19cee 514 }\r
a13899c5 515}\r