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 / ModuleSystemTables.java
1 /** @file
2
3 The file is used to create, update SystemTable 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.SystemTableUsage;
31 import org.tianocore.SystemTablesDocument;
32 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
33 import org.tianocore.SystemTablesDocument.SystemTables;
34 import org.tianocore.SystemTablesDocument.SystemTables.SystemTableCNames;
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.SystemTables.SystemTablesIdentification;
43 import org.tianocore.frameworkwizard.module.Identifications.SystemTables.SystemTablesVector;
44 import org.tianocore.frameworkwizard.module.ui.dialog.SystemTablesDlg;
45
46 /**
47 The class is used to create, update SystemTable of MSA/MBD file
48 It extends IInternalFrame
49
50
51
52 **/
53 public class ModuleSystemTables extends IInternalFrame {
54
55 ///
56 /// Define class Serial Version UID
57 ///
58 private static final long serialVersionUID = 7488769180379442276L;
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 SystemTablesDocument.SystemTables systemTables = null;
85
86 private SystemTablesIdentification id = null;
87
88 private SystemTablesVector vid = new SystemTablesVector();
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, 195, 90, 20));
104 jButtonAdd.setText("Add");
105 jButtonAdd.addActionListener(this);
106 jButtonAdd.setPreferredSize(new java.awt.Dimension(90, 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, 195, 90, 20));
121 jButtonRemove.setText("Remove");
122 jButtonRemove.addActionListener(this);
123 jButtonRemove.setPreferredSize(new java.awt.Dimension(90, 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, 195, 90, 20));
138 jButtonUpdate.setPreferredSize(new java.awt.Dimension(90, 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("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("System Tables");
208 }
209
210 /**
211 This method initializes this
212 Fill values to all fields if these values are not empty
213
214 @param inSystemTables
215
216 **/
217 private void init(SystemTables inSystemTables) {
218 init();
219 this.systemTables = inSystemTables;
220
221 if (this.systemTables != null) {
222 if (this.systemTables.getSystemTableCNamesList().size() > 0) {
223 for (int index = 0; index < this.systemTables.getSystemTableCNamesList().size(); index++) {
224 String arg0 = systemTables.getSystemTableCNamesList().get(index).getSystemTableCName();
225 String arg1 = null;
226 if (systemTables.getSystemTableCNamesList().get(index).getUsage() != null) {
227 arg1 = systemTables.getSystemTableCNamesList().get(index).getUsage().toString();
228 }
229
230 String arg2 = systemTables.getSystemTableCNamesList().get(index).getFeatureFlag();
231 Vector<String> arg3 = Tools.convertListToVector(systemTables.getSystemTableCNamesList().get(index)
232 .getSupArchList());
233 String arg4 = systemTables.getSystemTableCNamesList().get(index).getHelpText();
234
235 id = new SystemTablesIdentification(arg0, arg1, arg2, arg3, arg4);
236 vid.addSystemTables(id);
237 }
238 }
239 }
240 showTable();
241 }
242
243 /**
244 This is the default constructor
245
246 **/
247 public ModuleSystemTables() {
248 super();
249 init();
250 this.setVisible(true);
251 }
252
253 /**
254 This is the override edit constructor
255
256 @param inSystemTables The input data of SystemTablesDocument.SystemTables
257
258 **/
259 public ModuleSystemTables(OpeningModuleType inOmt) {
260 super();
261 this.omt = inOmt;
262 this.msa = omt.getXmlMsa();
263 init(msa.getSystemTables());
264 this.setVisible(true);
265 }
266
267 /**
268 This method initializes jContentPane
269
270 @return javax.swing.JPanel jContentPane
271
272 **/
273 private JPanel getJContentPane() {
274 if (jContentPane == null) {
275
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 SystemTablesDlg dlg = new SystemTablesDlg(vid.getSystemTables(index), new IFrame(), omt.getId());
290 int result = dlg.showDialog();
291 if (result == DataType.RETURN_TYPE_OK) {
292 if (index == -1) {
293 this.vid.addSystemTables(dlg.getId());
294 } else {
295 this.vid.setSystemTables(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 System Tables", "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.removeSystemTables(selectedRow);
359 selectedRow = -1;
360 this.save();
361 }
362 }
363 }
364
365 /**
366 Save all components of SystemTables
367 if exists systemTables, set the value directly
368 if not exists systemTables, new an instance first
369
370 **/
371 public void save() {
372 try {
373 int count = this.vid.size();
374
375 this.systemTables = SystemTables.Factory.newInstance();
376 if (count > 0) {
377 for (int index = 0; index < count; index++) {
378 SystemTableCNames p = SystemTableCNames.Factory.newInstance();
379 if (!isEmpty(vid.getSystemTables(index).getName())) {
380 p.setSystemTableCName(vid.getSystemTables(index).getName());
381 }
382 if (!isEmpty(vid.getSystemTables(index).getUsage())) {
383 p.setUsage(SystemTableUsage.Enum.forString(vid.getSystemTables(index).getUsage()));
384 }
385 if (!isEmpty(vid.getSystemTables(index).getFeatureFlag())) {
386 p.setFeatureFlag(vid.getSystemTables(index).getFeatureFlag());
387 }
388 if (vid.getSystemTables(index).getSupArchList() != null
389 && vid.getSystemTables(index).getSupArchList().size() > 0) {
390 p.setSupArchList(vid.getSystemTables(index).getSupArchList());
391 }
392 if (!isEmpty(vid.getSystemTables(index).getHelp())) {
393 p.setHelpText(vid.getSystemTables(index).getHelp());
394 }
395 this.systemTables.addNewSystemTableCNames();
396 this.systemTables.setSystemTableCNamesArray(systemTables.getSystemTableCNamesList().size() - 1, p);
397 }
398 }
399
400 this.msa.setSystemTables(systemTables);
401 this.omt.setSaved(false);
402 } catch (Exception e) {
403 Log.wrn("Update System Tables", e.getMessage());
404 Log.err("Update System Tables", e.getMessage());
405 }
406 }
407
408 /* (non-Javadoc)
409 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
410 *
411 */
412 public void valueChanged(ListSelectionEvent arg0) {
413 if (arg0.getValueIsAdjusting()) {
414 return;
415 }
416 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
417 if (lsm.isSelectionEmpty()) {
418 return;
419 } else {
420 selectedRow = lsm.getMinSelectionIndex();
421 }
422 }
423
424 /* (non-Javadoc)
425 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
426 *
427 */
428 public void mouseClicked(MouseEvent arg0) {
429 if (arg0.getClickCount() == 2) {
430 if (this.selectedRow < 0) {
431 return;
432 } else {
433 showEdit(selectedRow);
434 }
435 }
436 }
437
438 /* (non-Javadoc)
439 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
440 *
441 * Override componentResized to resize all components when frame's size is changed
442 */
443 public void componentResized(ComponentEvent arg0) {
444 int intCurrentWidth = this.getJContentPane().getWidth();
445 int intCurrentHeight = this.getJContentPane().getHeight();
446 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
447 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
448
449 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
450 intPreferredHeight);
451 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
452 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
453 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
454 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
455 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
456 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
457 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
458 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
459 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
460 }
461 }