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