]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleDataHubs.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 / ModuleDataHubs.java
CommitLineData
a13899c5 1/** @file\r
2 \r
3 The file is used to create, update DataHub 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.DataHubUsage;\r
32import org.tianocore.DataHubsDocument;\r
33import org.tianocore.DataHubsDocument.DataHubs;\r
34import org.tianocore.DataHubsDocument.DataHubs.DataHubRecord;\r
35import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\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.DataHubs.DataHubsIdentification;\r
44import org.tianocore.frameworkwizard.module.Identifications.DataHubs.DataHubsVector;\r
06a19cee 45import org.tianocore.frameworkwizard.module.ui.dialog.DataHubsDlg;\r
a13899c5 46\r
47/**\r
48 The class is used to create, update DataHub of MSA/MBD file\r
49 It extends IInternalFrame\r
50 \r
51\r
52\r
53 **/\r
54public class ModuleDataHubs extends IInternalFrame {\r
55\r
56 ///\r
57 /// Define class Serial Version UID\r
58 ///\r
59 private static final long serialVersionUID = -3667906991966638892L;\r
60\r
61 //\r
62 //Define class members\r
63 //\r
64 private JPanel jContentPane = null;\r
65\r
a13899c5 66 private JButton jButtonAdd = null;\r
67\r
68 private JButton jButtonRemove = null;\r
69\r
70 private JButton jButtonUpdate = null;\r
71\r
72 private JScrollPane jScrollPane = null;\r
73\r
06a19cee 74 private JScrollPane jScrollPaneTable = null;\r
a13899c5 75\r
06a19cee 76 private JTable jTable = null;\r
a13899c5 77\r
78 //\r
79 // Not used by UI\r
80 //\r
a13899c5 81 private OpeningModuleType omt = null;\r
82\r
83 private ModuleSurfaceArea msa = null;\r
84\r
85 private DataHubsDocument.DataHubs dataHubs = null;\r
86\r
87 private DataHubsIdentification id = null;\r
88\r
89 private DataHubsVector vid = new DataHubsVector();\r
90\r
06a19cee 91 private IDefaultTableModel model = null;\r
a13899c5 92\r
06a19cee 93 private int selectedRow = -1;\r
a9e882c9 94 \r
95 private IFrame parentFrame = null;\r
a13899c5 96\r
97 /**\r
98 This method initializes jButtonAdd \r
99 \r
100 @return javax.swing.JButton jButtonAdd\r
101 \r
102 **/\r
103 private JButton getJButtonAdd() {\r
104 if (jButtonAdd == null) {\r
105 jButtonAdd = new JButton();\r
3767c118 106 jButtonAdd.setBounds(new java.awt.Rectangle(230, 195, 90, 20));\r
a13899c5 107 jButtonAdd.setText("Add");\r
108 jButtonAdd.addActionListener(this);\r
3767c118 109 jButtonAdd.setPreferredSize(new java.awt.Dimension(90, 20));\r
a13899c5 110 }\r
111 return jButtonAdd;\r
112 }\r
113\r
114 /**\r
115 This method initializes jButtonRemove \r
116 \r
117 @return javax.swing.JButton jButtonRemove\r
118 \r
119 **/\r
120 private JButton getJButtonRemove() {\r
121 if (jButtonRemove == null) {\r
122 jButtonRemove = new JButton();\r
3767c118 123 jButtonRemove.setBounds(new java.awt.Rectangle(400, 195, 90, 20));\r
a13899c5 124 jButtonRemove.setText("Remove");\r
125 jButtonRemove.addActionListener(this);\r
3767c118 126 jButtonRemove.setPreferredSize(new java.awt.Dimension(90, 20));\r
a13899c5 127 }\r
128 return jButtonRemove;\r
129 }\r
130\r
131 /**\r
132 This method initializes jButtonUpdate \r
133 \r
134 @return javax.swing.JButton jButtonUpdate\r
135 \r
136 **/\r
137 private JButton getJButtonUpdate() {\r
138 if (jButtonUpdate == null) {\r
139 jButtonUpdate = new JButton();\r
3767c118 140 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 195, 90, 20));\r
141 jButtonUpdate.setPreferredSize(new java.awt.Dimension(90, 20));\r
06a19cee 142 jButtonUpdate.setText("Edit");\r
a13899c5 143 jButtonUpdate.addActionListener(this);\r
144 }\r
145 return jButtonUpdate;\r
146 }\r
147\r
a13899c5 148 /**\r
149 This method initializes jScrollPane \r
150 \r
151 @return javax.swing.JScrollPane \r
152 */\r
153 private JScrollPane getJScrollPane() {\r
154 if (jScrollPane == null) {\r
155 jScrollPane = new JScrollPane();\r
156 jScrollPane.setViewportView(getJContentPane());\r
157 }\r
158 return jScrollPane;\r
159 }\r
160\r
161 /**\r
06a19cee 162 This method initializes jScrollPaneTable \r
163 \r
a13899c5 164 @return javax.swing.JScrollPane \r
a13899c5 165 **/\r
06a19cee 166 private JScrollPane getJScrollPaneTable() {\r
167 if (jScrollPaneTable == null) {\r
168 jScrollPaneTable = new JScrollPane();\r
169 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));\r
170 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));\r
171 jScrollPaneTable.setViewportView(getJTable());\r
a13899c5 172 }\r
06a19cee 173 return jScrollPaneTable;\r
a13899c5 174 }\r
175\r
176 /**\r
06a19cee 177 This method initializes jTable \r
a13899c5 178 \r
06a19cee 179 @return javax.swing.JTable \r
a13899c5 180 **/\r
06a19cee 181 private JTable getJTable() {\r
182 if (jTable == null) {\r
183 jTable = new JTable();\r
184 model = new IDefaultTableModel();\r
185 jTable = new JTable(model);\r
186 jTable.setRowHeight(20);\r
187\r
188 model.addColumn("Data Hub C_Name");\r
189 model.addColumn("Usage");\r
190\r
191 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
192 jTable.getSelectionModel().addListSelectionListener(this);\r
193 jTable.getModel().addTableModelListener(this);\r
194 jTable.addMouseListener(this);\r
a13899c5 195 }\r
06a19cee 196 return jTable;\r
a13899c5 197 }\r
198\r
199 public static void main(String[] args) {\r
200\r
201 }\r
202\r
203 /**\r
204 This method initializes this\r
205 \r
206 **/\r
207 private void init() {\r
208 this.setSize(500, 515);\r
209 this.setContentPane(getJScrollPane());\r
210 this.setTitle("Data Hubs");\r
a13899c5 211 }\r
212\r
213 /**\r
214 This method initializes this\r
215 Fill values to all fields if these values are not empty\r
216 \r
217 @param inPackageDependencies\r
218\r
219 **/\r
220 private void init(DataHubs inDataHubs) {\r
221 init();\r
222 this.dataHubs = inDataHubs;\r
223\r
224 if (this.dataHubs != null) {\r
225 if (this.dataHubs.getDataHubRecordList().size() > 0) {\r
226 for (int index = 0; index < this.dataHubs.getDataHubRecordList().size(); index++) {\r
227 String arg0 = dataHubs.getDataHubRecordList().get(index).getDataHubCName();\r
228 String arg1 = null;\r
229 if (dataHubs.getDataHubRecordList().get(index).getUsage() != null) {\r
06a19cee 230 arg1 = dataHubs.getDataHubRecordList().get(index).getUsage().toString();\r
a13899c5 231 }\r
06a19cee 232\r
a13899c5 233 String arg2 = dataHubs.getDataHubRecordList().get(index).getFeatureFlag();\r
234 Vector<String> arg3 = Tools.convertListToVector(dataHubs.getDataHubRecordList().get(index)\r
235 .getSupArchList());\r
236 String arg4 = dataHubs.getDataHubRecordList().get(index).getHelpText();\r
06a19cee 237\r
a13899c5 238 id = new DataHubsIdentification(arg0, arg1, arg2, arg3, arg4);\r
239 vid.addDataHubs(id);\r
240 }\r
241 }\r
242 }\r
06a19cee 243 showTable();\r
a13899c5 244 }\r
245\r
246 /**\r
247 This is the default constructor\r
248 \r
249 **/\r
250 public ModuleDataHubs() {\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 inDataHubs The input DataHubsDocument.DataHubs\r
260 \r
261 **/\r
a9e882c9 262 public ModuleDataHubs(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.getDataHubs());\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
a13899c5 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 DataHubsDlg dlg = new DataHubsDlg(vid.getDataHubs(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.addDataHubs(dlg.getId());\r
297 } else {\r
298 this.vid.setDataHubs(dlg.getId(), index);\r
299 }\r
300 this.showTable();\r
301 this.save();\r
302 dlg.dispose();\r
303 }\r
304 if (result == DataType.RETURN_TYPE_CANCEL) {\r
305 dlg.dispose();\r
306 }\r
307 }\r
308\r
a13899c5 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
319 }\r
320\r
321 /**\r
322 Read content of vector and put then into table\r
323 \r
324 **/\r
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
331 }\r
332 }\r
333 this.jTable.repaint();\r
334 this.jTable.updateUI();\r
a13899c5 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
06a19cee 345 showEdit(-1);\r
a13899c5 346 }\r
347 if (arg0.getSource() == jButtonUpdate) {\r
06a19cee 348 if (this.selectedRow < 0) {\r
ed1665f2 349 Log.wrn("Update Data Hubs", "Please select one record first.");\r
a13899c5 350 return;\r
351 }\r
06a19cee 352 showEdit(selectedRow);\r
a13899c5 353 }\r
354\r
06a19cee 355 if (arg0.getSource() == jButtonRemove) {\r
356 if (jTable.isEditing()) {\r
357 jTable.getCellEditor().stopCellEditing();\r
a13899c5 358 }\r
06a19cee 359 if (selectedRow > -1) {\r
360 this.model.removeRow(selectedRow);\r
361 this.vid.removeDataHubs(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 DataHubs\r
370 if exists dataHubs, set the value directly\r
371 if not exists dataHubs, 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.dataHubs = DataHubs.Factory.newInstance();\r
379 if (count > 0) {\r
380 for (int index = 0; index < count; index++) {\r
381 DataHubRecord p = DataHubRecord.Factory.newInstance();\r
382 if (!isEmpty(vid.getDataHubs(index).getName())) {\r
383 p.setDataHubCName(vid.getDataHubs(index).getName());\r
384 }\r
385 if (!isEmpty(vid.getDataHubs(index).getUsage())) {\r
386 p.setUsage(DataHubUsage.Enum.forString(vid.getDataHubs(index).getUsage()));\r
387 }\r
388 if (!isEmpty(vid.getDataHubs(index).getFeatureFlag())) {\r
389 p.setFeatureFlag(vid.getDataHubs(index).getFeatureFlag());\r
390 }\r
391 if (vid.getDataHubs(index).getSupArchList() != null\r
392 && vid.getDataHubs(index).getSupArchList().size() > 0) {\r
393 p.setSupArchList(vid.getDataHubs(index).getSupArchList());\r
394 }\r
395 if (!isEmpty(vid.getDataHubs(index).getHelp())) {\r
396 p.setHelpText(vid.getDataHubs(index).getHelp());\r
397 }\r
398 this.dataHubs.addNewDataHubRecord();\r
399 this.dataHubs.setDataHubRecordArray(dataHubs.getDataHubRecordList().size() - 1, p);\r
400 }\r
401 }\r
402\r
403 this.msa.setDataHubs(dataHubs);\r
404 this.omt.setSaved(false);\r
405 } catch (Exception e) {\r
ed1665f2 406 Log.wrn("Update Data Hubs", e.getMessage());\r
a13899c5 407 Log.err("Update Data Hubs", e.getMessage());\r
408 }\r
409 }\r
410\r
411 /* (non-Javadoc)\r
06a19cee 412 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)\r
413 *\r
a13899c5 414 */\r
06a19cee 415 public void valueChanged(ListSelectionEvent arg0) {\r
416 if (arg0.getValueIsAdjusting()) {\r
a13899c5 417 return;\r
418 }\r
06a19cee 419 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();\r
420 if (lsm.isSelectionEmpty()) {\r
a13899c5 421 return;\r
a13899c5 422 } else {\r
06a19cee 423 selectedRow = lsm.getMinSelectionIndex();\r
a13899c5 424 }\r
a13899c5 425 }\r
426\r
06a19cee 427 /* (non-Javadoc)\r
428 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)\r
429 *\r
430 */\r
431 public void mouseClicked(MouseEvent arg0) {\r
432 if (arg0.getClickCount() == 2) {\r
433 if (this.selectedRow < 0) {\r
434 return;\r
435 } else {\r
436 showEdit(selectedRow);\r
437 }\r
a13899c5 438 }\r
a13899c5 439 }\r
440\r
441 /* (non-Javadoc)\r
06a19cee 442 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)\r
a13899c5 443 * \r
06a19cee 444 * Override componentResized to resize all components when frame's size is changed\r
a13899c5 445 */\r
06a19cee 446 public void componentResized(ComponentEvent arg0) {\r
447 int intCurrentWidth = this.getJContentPane().getWidth();\r
448 int intCurrentHeight = this.getJContentPane().getHeight();\r
449 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;\r
450 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;\r
451\r
8792f60f 452 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
453 intPreferredHeight);\r
454 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
455 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,\r
456 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);\r
457 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
458 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,\r
459 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);\r
460 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,\r
461 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,\r
462 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);\r
a13899c5 463 }\r
464}\r