1. Fix EDKT418: Some buttons are not displayed correctly in Linux and MAC OS X operat...
[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 /**
96 This method initializes jButtonAdd
97
98 @return javax.swing.JButton jButtonAdd
99
100 **/
101 private JButton getJButtonAdd() {
102 if (jButtonAdd == null) {
103 jButtonAdd = new JButton();
104 jButtonAdd.setBounds(new java.awt.Rectangle(230, 245, 90, 20));
105 jButtonAdd.setText("Add");
106 jButtonAdd.addActionListener(this);
107 jButtonAdd.setPreferredSize(new java.awt.Dimension(90, 20));
108 }
109 return jButtonAdd;
110 }
111
112 /**
113 This method initializes jButtonRemove
114
115 @return javax.swing.JButton jButtonRemove
116
117 **/
118 private JButton getJButtonRemove() {
119 if (jButtonRemove == null) {
120 jButtonRemove = new JButton();
121 jButtonRemove.setBounds(new java.awt.Rectangle(400, 245, 90, 20));
122 jButtonRemove.setText("Remove");
123 jButtonRemove.addActionListener(this);
124 jButtonRemove.setPreferredSize(new java.awt.Dimension(90, 20));
125 }
126 return jButtonRemove;
127 }
128
129 /**
130 This method initializes jButtonUpdate
131
132 @return javax.swing.JButton jButtonUpdate
133
134 **/
135 private JButton getJButtonUpdate() {
136 if (jButtonUpdate == null) {
137 jButtonUpdate = new JButton();
138 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 245, 90, 20));
139 jButtonUpdate.setPreferredSize(new java.awt.Dimension(90, 20));
140 jButtonUpdate.setText("Edit");
141 jButtonUpdate.addActionListener(this);
142 }
143 return jButtonUpdate;
144 }
145
146 /**
147 This method initializes jScrollPane
148
149 @return javax.swing.JScrollPane
150 */
151 private JScrollPane getJScrollPane() {
152 if (jScrollPane == null) {
153 jScrollPane = new JScrollPane();
154 jScrollPane.setViewportView(getJContentPane());
155 }
156 return jScrollPane;
157 }
158
159 /**
160 This method initializes jScrollPaneTable
161
162 @return javax.swing.JScrollPane
163 **/
164 private JScrollPane getJScrollPaneTable() {
165 if (jScrollPaneTable == null) {
166 jScrollPaneTable = new JScrollPane();
167 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
168 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
169 jScrollPaneTable.setViewportView(getJTable());
170 }
171 return jScrollPaneTable;
172 }
173
174 /**
175 This method initializes jTable
176
177 @return javax.swing.JTable
178 **/
179 private JTable getJTable() {
180 if (jTable == null) {
181 jTable = new JTable();
182 model = new IDefaultTableModel();
183 jTable = new JTable(model);
184 jTable.setRowHeight(20);
185
186 model.addColumn("Name");
187 model.addColumn("Type");
188 model.addColumn("Value");
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("Pcd Coded");
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(PcdCoded inPcdCodeds) {
221 init();
222 this.pcds = inPcdCodeds;
223
224 if (this.pcds != null) {
225 if (this.pcds.getPcdEntryList().size() > 0) {
226 for (int index = 0; index < this.pcds.getPcdEntryList().size(); index++) {
227 String arg0 = pcds.getPcdEntryList().get(index).getCName();
228 String arg1 = pcds.getPcdEntryList().get(index).getTokenSpaceGuidCName();
229
230 String arg2 = pcds.getPcdEntryList().get(index).getFeatureFlag();
231 Vector<String> arg3 = Tools.convertListToVector(pcds.getPcdEntryList().get(index).getSupArchList());
232
233 String arg4 = pcds.getPcdEntryList().get(index).getDefaultValue();
234 String arg5 = pcds.getPcdEntryList().get(index).getHelpText();
235 String arg6 = null;
236 if (pcds.getPcdEntryList().get(index).getPcdItemType() != null) {
237 arg6 = pcds.getPcdEntryList().get(index).getPcdItemType().toString();
238 }
239 String arg7 = null;
240 if (pcds.getPcdEntryList().get(index).getUsage() != null) {
241 arg7 = pcds.getPcdEntryList().get(index).getUsage().toString();
242 }
243 id = new PcdCodedIdentification(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
244 vid.addPcdCoded(id);
245 }
246 }
247 }
248 showTable();
249 }
250
251 /**
252 This is the default constructor
253
254 **/
255 public ModulePCDs() {
256 super();
257 init();
258 this.setVisible(true);
259 }
260
261 /**
262 This is the override edit constructor
263
264 @param inPcds The input data of PCDsDocument.PCDs
265
266 **/
267 public ModulePCDs(OpeningModuleType inOmt) {
268 super();
269 this.omt = inOmt;
270 this.msa = omt.getXmlMsa();
271 init(msa.getPcdCoded());
272 this.setVisible(true);
273 }
274
275 /**
276 This method initializes jContentPane
277
278 @return javax.swing.JPanel jContentPane
279
280 **/
281 private JPanel getJContentPane() {
282 if (jContentPane == null) {
283 jContentPane = new JPanel();
284 jContentPane.setLayout(null);
285 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
286
287 jContentPane.add(getJButtonAdd(), null);
288 jContentPane.add(getJButtonRemove(), null);
289 jContentPane.add(getJButtonUpdate(), null);
290 jContentPane.add(getJScrollPaneTable(), null);
291 }
292 return jContentPane;
293 }
294
295 private void showEdit(int index) {
296 PCDsDlg dlg = new PCDsDlg(vid.getPcdCoded(index), new IFrame(), omt.getId());
297 int result = dlg.showDialog();
298 if (result == DataType.RETURN_TYPE_OK) {
299 if (index == -1) {
300 this.vid.addPcdCoded(dlg.getId());
301 } else {
302 this.vid.setPcdCoded(dlg.getId(), index);
303 }
304 this.showTable();
305 this.save();
306 dlg.dispose();
307 }
308 if (result == DataType.RETURN_TYPE_CANCEL) {
309 dlg.dispose();
310 }
311 }
312
313 /**
314 Clear all table rows
315
316 **/
317 private void clearAll() {
318 if (model != null) {
319 for (int index = model.getRowCount() - 1; index >= 0; index--) {
320 model.removeRow(index);
321 }
322 }
323 }
324
325 /**
326 Read content of vector and put then into table
327
328 **/
329 private void showTable() {
330 clearAll();
331
332 if (vid.size() > 0) {
333 for (int index = 0; index < vid.size(); index++) {
334 model.addRow(vid.toStringVector(index));
335 }
336 }
337 this.jTable.repaint();
338 this.jTable.updateUI();
339 }
340
341 /* (non-Javadoc)
342 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
343 *
344 * Override actionPerformed to listen all actions
345 *
346 */
347 public void actionPerformed(ActionEvent arg0) {
348 if (arg0.getSource() == jButtonAdd) {
349 showEdit(-1);
350 }
351 if (arg0.getSource() == jButtonUpdate) {
352 if (this.selectedRow < 0) {
353 Log.wrn("Update PcdCoded", "Please select one record first.");
354 return;
355 }
356 showEdit(selectedRow);
357 }
358
359 if (arg0.getSource() == jButtonRemove) {
360 if (jTable.isEditing()) {
361 jTable.getCellEditor().stopCellEditing();
362 }
363 if (selectedRow > -1) {
364 this.model.removeRow(selectedRow);
365 this.vid.removePcdCoded(selectedRow);
366 selectedRow = -1;
367 this.save();
368 }
369 }
370 }
371
372 /**
373 Save all components of PCDs
374 if exists pcds, set the value directly
375 if not exists pcds, new an instance first
376
377 **/
378 public void save() {
379 try {
380 int count = this.vid.size();
381
382 this.pcds = PcdCoded.Factory.newInstance();
383 if (count > 0) {
384 for (int index = 0; index < count; index++) {
385 PcdEntry p = PcdEntry.Factory.newInstance();
386 if (!isEmpty(vid.getPcdCoded(index).getName())) {
387 p.setCName(vid.getPcdCoded(index).getName());
388 }
389 if (!isEmpty(vid.getPcdCoded(index).getGuid())) {
390 p.setTokenSpaceGuidCName(vid.getPcdCoded(index).getGuid());
391 }
392 if (!isEmpty(vid.getPcdCoded(index).getFeatureFlag())) {
393 p.setFeatureFlag(vid.getPcdCoded(index).getFeatureFlag());
394 }
395 if (vid.getPcdCoded(index).getSupArchList() != null
396 && vid.getPcdCoded(index).getSupArchList().size() > 0) {
397 p.setSupArchList(vid.getPcdCoded(index).getSupArchList());
398 }
399 if (!isEmpty(vid.getPcdCoded(index).getValue())) {
400 p.setDefaultValue(vid.getPcdCoded(index).getValue());
401 }
402 if (!isEmpty(vid.getPcdCoded(index).getHelp())) {
403 p.setHelpText(vid.getPcdCoded(index).getHelp());
404 }
405 if (!isEmpty(vid.getPcdCoded(index).getType())) {
406 p.setPcdItemType(PcdItemTypes.Enum.forString(vid.getPcdCoded(index).getType()));
407 }
408 if (!isEmpty(vid.getPcdCoded(index).getUsage())) {
409 p.setUsage(PcdUsage.Enum.forString(vid.getPcdCoded(index).getUsage()));
410 }
411 this.pcds.addNewPcdEntry();
412 this.pcds.setPcdEntryArray(pcds.getPcdEntryList().size() - 1, p);
413 }
414 }
415
416 this.msa.setPcdCoded(pcds);
417 this.omt.setSaved(false);
418 } catch (Exception e) {
419 Log.wrn("Update PcdCoded", e.getMessage());
420 Log.err("Update PcdCoded", e.getMessage());
421 }
422 }
423
424 /* (non-Javadoc)
425 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
426 *
427 */
428 public void valueChanged(ListSelectionEvent arg0) {
429 if (arg0.getValueIsAdjusting()) {
430 return;
431 }
432 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
433 if (lsm.isSelectionEmpty()) {
434 return;
435 } else {
436 selectedRow = lsm.getMinSelectionIndex();
437 }
438 }
439
440 /* (non-Javadoc)
441 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
442 *
443 */
444 public void mouseClicked(MouseEvent arg0) {
445 if (arg0.getClickCount() == 2) {
446 if (this.selectedRow < 0) {
447 return;
448 } else {
449 showEdit(selectedRow);
450 }
451 }
452 }
453
454 /* (non-Javadoc)
455 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
456 *
457 * Override componentResized to resize all components when frame's size is changed
458 */
459 public void componentResized(ComponentEvent arg0) {
460 int intCurrentWidth = this.getJContentPane().getWidth();
461 int intCurrentHeight = this.getJContentPane().getHeight();
462 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
463 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
464
465 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
466 intPreferredHeight);
467 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
468 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
469 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
470 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
471 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
472 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
473 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
474 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
475 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
476 }
477 }