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