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