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