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