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