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 / ModuleVariables.java
1 /** @file
2
3 The file is used to create, update Variable 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
16 package org.tianocore.frameworkwizard.module.ui;
17
18 import java.awt.Dimension;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ComponentEvent;
21 import java.awt.event.MouseEvent;
22 import java.util.Vector;
23
24 import javax.swing.JButton;
25 import javax.swing.JPanel;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTable;
28 import javax.swing.ListSelectionModel;
29 import javax.swing.event.ListSelectionEvent;
30
31 import org.tianocore.VariableUsage;
32 import org.tianocore.VariablesDocument;
33 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
34 import org.tianocore.VariablesDocument.Variables;
35 import org.tianocore.VariablesDocument.Variables.Variable;
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.Variables.VariablesIdentification;
44 import org.tianocore.frameworkwizard.module.Identifications.Variables.VariablesVector;
45 import org.tianocore.frameworkwizard.module.ui.dialog.VariablesDlg;
46
47 /**
48 The class is used to create, update Variable of MSA/MBD file
49 It extends IInternalFrame
50
51 **/
52 public class ModuleVariables extends IInternalFrame {
53
54 ///
55 /// Define class Serial Version UID
56 ///
57 private static final long serialVersionUID = -6998982978030439446L;
58
59 //
60 //Define class members
61 //
62 private JPanel jContentPane = null;
63
64 private JButton jButtonAdd = null;
65
66 private JButton jButtonRemove = null;
67
68 private JButton jButtonUpdate = null;
69
70 private JScrollPane jScrollPane = null;
71
72 private JScrollPane jScrollPaneTable = null;
73
74 private JTable jTable = null;
75
76 //
77 // Not used by UI
78 //
79 private OpeningModuleType omt = null;
80
81 private ModuleSurfaceArea msa = null;
82
83 private VariablesDocument.Variables variables = null;
84
85 private VariablesIdentification id = null;
86
87 private VariablesVector vid = new VariablesVector();
88
89 private IDefaultTableModel model = null;
90
91 private int selectedRow = -1;
92
93 /**
94 This method initializes jButtonAdd
95
96 @return javax.swing.JButton jButtonAdd
97
98 **/
99 private JButton getJButtonAdd() {
100 if (jButtonAdd == null) {
101 jButtonAdd = new JButton();
102 jButtonAdd.setBounds(new java.awt.Rectangle(230, 220, 90, 20));
103 jButtonAdd.setText("Add");
104 jButtonAdd.addActionListener(this);
105 jButtonAdd.setPreferredSize(new java.awt.Dimension(90, 20));
106 }
107 return jButtonAdd;
108 }
109
110 /**
111 This method initializes jButtonRemove
112
113 @return javax.swing.JButton jButtonRemove
114
115 **/
116 private JButton getJButtonRemove() {
117 if (jButtonRemove == null) {
118 jButtonRemove = new JButton();
119 jButtonRemove.setBounds(new java.awt.Rectangle(400, 220, 90, 20));
120 jButtonRemove.setText("Remove");
121 jButtonRemove.addActionListener(this);
122 jButtonRemove.setPreferredSize(new java.awt.Dimension(90, 20));
123 }
124 return jButtonRemove;
125 }
126
127 /**
128 This method initializes jButtonUpdate
129
130 @return javax.swing.JButton jButtonUpdate
131
132 **/
133 private JButton getJButtonUpdate() {
134 if (jButtonUpdate == null) {
135 jButtonUpdate = new JButton();
136 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 220, 90, 20));
137 jButtonUpdate.setPreferredSize(new java.awt.Dimension(90, 20));
138 jButtonUpdate.setText("Edit");
139 jButtonUpdate.addActionListener(this);
140 }
141 return jButtonUpdate;
142 }
143
144 /**
145 This method initializes jScrollPane
146
147 @return javax.swing.JScrollPane
148 */
149 private JScrollPane getJScrollPane() {
150 if (jScrollPane == null) {
151 jScrollPane = new JScrollPane();
152 jScrollPane.setViewportView(getJContentPane());
153 }
154 return jScrollPane;
155 }
156
157 /**
158 This method initializes jScrollPaneTable
159
160 @return javax.swing.JScrollPane
161 **/
162 private JScrollPane getJScrollPaneTable() {
163 if (jScrollPaneTable == null) {
164 jScrollPaneTable = new JScrollPane();
165 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
166 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
167 jScrollPaneTable.setViewportView(getJTable());
168 }
169 return jScrollPaneTable;
170 }
171
172 /**
173 This method initializes jTable
174
175 @return javax.swing.JTable
176 **/
177 private JTable getJTable() {
178 if (jTable == null) {
179 jTable = new JTable();
180 model = new IDefaultTableModel();
181 jTable = new JTable(model);
182 jTable.setRowHeight(20);
183
184 model.addColumn("Name");
185 model.addColumn("Guid C_Name");
186 model.addColumn("Usage");
187
188 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
189 jTable.getSelectionModel().addListSelectionListener(this);
190 jTable.getModel().addTableModelListener(this);
191 jTable.addMouseListener(this);
192 }
193 return jTable;
194 }
195
196 public static void main(String[] args) {
197
198 }
199
200 /**
201 This method initializes this
202
203 **/
204 private void init() {
205 this.setSize(500, 515);
206 this.setContentPane(getJScrollPane());
207 this.setTitle("Variables");
208 }
209
210 /**
211 This method initializes this
212 Fill values to all fields if these values are not empty
213
214 @param inPackageDependencies
215
216 **/
217 private void init(Variables inVariables) {
218 init();
219 this.variables = inVariables;
220
221 if (this.variables != null) {
222 if (this.variables.getVariableList().size() > 0) {
223 for (int index = 0; index < this.variables.getVariableList().size(); index++) {
224 String arg0 = Tools.convertUnicodeHexStringToString(variables.getVariableList().get(index)
225 .getVariableName());
226 String arg1 = variables.getVariableList().get(index).getGuidCName();
227 String arg2 = null;
228 if (variables.getVariableList().get(index).getUsage() != null) {
229 arg2 = variables.getVariableList().get(index).getUsage().toString();
230 }
231
232 String arg3 = variables.getVariableList().get(index).getFeatureFlag();
233 Vector<String> arg4 = Tools.convertListToVector(variables.getVariableList().get(index)
234 .getSupArchList());
235 String arg5 = variables.getVariableList().get(index).getHelpText();
236 id = new VariablesIdentification(arg0, arg1, arg2, arg3, arg4, arg5);
237 vid.addVariables(id);
238 }
239 }
240 }
241 showTable();
242 }
243
244 /**
245 This is the default constructor
246
247 **/
248 public ModuleVariables() {
249 super();
250 init();
251 this.setVisible(true);
252 }
253
254 /**
255 This is the override edit constructor
256
257 @param inVariables The input data of VariablesDocument.Variables
258
259 **/
260 public ModuleVariables(OpeningModuleType inOmt) {
261 super();
262 this.omt = inOmt;
263 this.msa = omt.getXmlMsa();
264 init(msa.getVariables());
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 VariablesDlg dlg = new VariablesDlg(vid.getVariables(index), new IFrame(), omt.getId());
290 int result = dlg.showDialog();
291 if (result == DataType.RETURN_TYPE_OK) {
292 if (index == -1) {
293 this.vid.addVariables(dlg.getId());
294 } else {
295 this.vid.setVariables(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.wrn("Update Variables", "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.removeVariables(selectedRow);
359 selectedRow = -1;
360 this.save();
361 }
362 }
363 }
364
365 /**
366 Save all components of Variables
367 if exists variables, set the value directly
368 if not exists variables, new an instance first
369
370 **/
371 public void save() {
372 try {
373 int count = this.vid.size();
374
375 this.variables = Variables.Factory.newInstance();
376 if (count > 0) {
377 for (int index = 0; index < count; index++) {
378 Variable p = Variable.Factory.newInstance();
379 if (!isEmpty(vid.getVariables(index).getName())) {
380 p.setVariableName(Tools.convertStringToUnicodeHexString(vid.getVariables(index).getName()));
381 }
382 if (!isEmpty(vid.getVariables(index).getGuid())) {
383 p.setGuidCName(vid.getVariables(index).getGuid());
384 }
385 if (!isEmpty(vid.getVariables(index).getUsage())) {
386 p.setUsage(VariableUsage.Enum.forString(vid.getVariables(index).getUsage()));
387 }
388 if (!isEmpty(vid.getVariables(index).getFeatureFlag())) {
389 p.setFeatureFlag(vid.getVariables(index).getFeatureFlag());
390 }
391 if (vid.getVariables(index).getSupArchList() != null
392 && vid.getVariables(index).getSupArchList().size() > 0) {
393 p.setSupArchList(vid.getVariables(index).getSupArchList());
394 }
395 if (!isEmpty(vid.getVariables(index).getHelp())) {
396 p.setHelpText(vid.getVariables(index).getHelp());
397 }
398 this.variables.addNewVariable();
399 this.variables.setVariableArray(variables.getVariableList().size() - 1, p);
400 }
401 }
402
403 this.msa.setVariables(variables);
404 this.omt.setSaved(false);
405 } catch (Exception e) {
406 Log.wrn("Update Variables", e.getMessage());
407 Log.err("Update Variables", e.getMessage());
408 }
409 }
410
411 /* (non-Javadoc)
412 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
413 *
414 */
415 public void valueChanged(ListSelectionEvent arg0) {
416 if (arg0.getValueIsAdjusting()) {
417 return;
418 }
419 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
420 if (lsm.isSelectionEmpty()) {
421 return;
422 } else {
423 selectedRow = lsm.getMinSelectionIndex();
424 }
425 }
426
427 /* (non-Javadoc)
428 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
429 *
430 */
431 public void mouseClicked(MouseEvent arg0) {
432 if (arg0.getClickCount() == 2) {
433 if (this.selectedRow < 0) {
434 return;
435 } else {
436 showEdit(selectedRow);
437 }
438 }
439 }
440
441 /* (non-Javadoc)
442 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
443 *
444 * Override componentResized to resize all components when frame's size is changed
445 */
446 public void componentResized(ComponentEvent arg0) {
447 int intCurrentWidth = this.getJContentPane().getWidth();
448 int intCurrentHeight = this.getJContentPane().getHeight();
449 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
450 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
451
452 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
453 intPreferredHeight);
454 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
455 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
456 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
457 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
458 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
459 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
460 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
461 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
462 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
463 }
464 }