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