]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleLibraryClassDefinitions.java
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 / ModuleLibraryClassDefinitions.java
1 /** @file
2
3 The file is used to create, update Library Class Definition 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.LibraryClassDefinitionsDocument;
31 import org.tianocore.LibraryUsage;
32 import org.tianocore.ModuleSurfaceAreaDocument;
33 import org.tianocore.LibraryClassDefinitionsDocument.LibraryClassDefinitions;
34 import org.tianocore.LibraryClassDocument.LibraryClass;
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.LibraryClass.LibraryClassIdentification;
43 import org.tianocore.frameworkwizard.module.Identifications.LibraryClass.LibraryClassVector;
44 import org.tianocore.frameworkwizard.module.ui.dialog.LibraryClassDefsDlg;
45
46 /**
47 The class is used to create, update Library Class Definition of MSA/MBD file
48 It extends IInternalFrame
49
50 **/
51 public class ModuleLibraryClassDefinitions extends IInternalFrame {
52
53 ///
54 /// Define class Serial Version UID
55 ///
56 private static final long serialVersionUID = -1743248695411382857L;
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 for UI
77 //
78 private ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = null;
79
80 private LibraryClassDefinitions lcd = null;
81
82 private LibraryClassVector vLibraryClass = new LibraryClassVector();
83
84 private OpeningModuleType omt = null;
85
86 private IDefaultTableModel model = null;
87
88 private int selectedRow = -1;
89
90 /**
91 This method initializes jButtonAdd
92
93 @return javax.swing.JButton jButtonAdd
94
95 **/
96 private JButton getJButtonAdd() {
97 if (jButtonAdd == null) {
98 jButtonAdd = new JButton();
99 jButtonAdd.setBounds(new java.awt.Rectangle(230, 330, 90, 20));
100 jButtonAdd.setPreferredSize(new Dimension(90, 20));
101 jButtonAdd.setText("Add");
102 jButtonAdd.addActionListener(this);
103 }
104 return jButtonAdd;
105 }
106
107 /**
108 This method initializes jButtonRemove
109
110 @return javax.swing.JButton jButtonRemove
111
112 **/
113 private JButton getJButtonRemove() {
114 if (jButtonRemove == null) {
115 jButtonRemove = new JButton();
116 jButtonRemove.setBounds(new java.awt.Rectangle(400, 330, 90, 20));
117 jButtonRemove.setText("Remove");
118 jButtonRemove.setPreferredSize(new Dimension(90, 20));
119 jButtonRemove.addActionListener(this);
120 }
121 return jButtonRemove;
122 }
123
124 /**
125 This method initializes jButtonUpdate
126
127 @return javax.swing.JButton jButtonUpdate
128
129 **/
130 private JButton getJButtonUpdate() {
131 if (jButtonUpdate == null) {
132 jButtonUpdate = new JButton();
133 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 330, 90, 20));
134 jButtonUpdate.setText("Edit");
135 jButtonUpdate.setPreferredSize(new Dimension(90, 20));
136 jButtonUpdate.addActionListener(this);
137 }
138 return jButtonUpdate;
139 }
140
141 /**
142 This method initializes jScrollPane
143
144 @return javax.swing.JScrollPane
145 */
146 private JScrollPane getJScrollPane() {
147 if (jScrollPane == null) {
148 jScrollPane = new JScrollPane();
149 jScrollPane.setViewportView(getJContentPane());
150 }
151 return jScrollPane;
152 }
153
154 /**
155 This method initializes jScrollPaneTable
156
157 @return javax.swing.JScrollPane
158 **/
159 private JScrollPane getJScrollPaneTable() {
160 if (jScrollPaneTable == null) {
161 jScrollPaneTable = new JScrollPane();
162 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
163 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
164 jScrollPaneTable.setViewportView(getJTable());
165 }
166 return jScrollPaneTable;
167 }
168
169 /**
170 This method initializes jTable
171
172 @return javax.swing.JTable
173 **/
174 private JTable getJTable() {
175 if (jTable == null) {
176 jTable = new JTable();
177 model = new IDefaultTableModel();
178 jTable = new JTable(model);
179 jTable.setRowHeight(20);
180
181 model.addColumn("Library Class Name");
182 model.addColumn("Usage");
183
184 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
185 jTable.getSelectionModel().addListSelectionListener(this);
186 jTable.getModel().addTableModelListener(this);
187 jTable.addMouseListener(this);
188 }
189 return jTable;
190 }
191
192 public static void main(String[] args) {
193
194 }
195
196 /**
197 This is the default constructor
198
199 **/
200 public ModuleLibraryClassDefinitions() {
201 super();
202 init();
203 this.setVisible(true);
204 }
205
206 /**
207 This is the override edit constructor
208
209 @param
210
211 **/
212 public ModuleLibraryClassDefinitions(OpeningModuleType inOmt) {
213 super();
214 this.omt = inOmt;
215 this.msa = omt.getXmlMsa();
216 init(msa.getLibraryClassDefinitions());
217 this.setVisible(true);
218 }
219
220 /**
221 This method initializes this
222 Fill values to all fields if these values are not empty
223
224 @param inLibraryClassDefinitions The input data of LibraryClassDefinitionsDocument.LibraryClassDefinitions
225
226 **/
227 private void init(LibraryClassDefinitionsDocument.LibraryClassDefinitions inLibraryClassDefinitions) {
228 init();
229 this.lcd = inLibraryClassDefinitions;
230 if (this.lcd != null) {
231 if (this.lcd.getLibraryClassList().size() > 0) {
232 for (int index = 0; index < this.lcd.getLibraryClassList().size(); index++) {
233 String name = lcd.getLibraryClassList().get(index).getKeyword();
234 String usage = null;
235 if (lcd.getLibraryClassList().get(index).getUsage() != null) {
236 usage = lcd.getLibraryClassList().get(index).getUsage().toString();
237 }
238 String version = lcd.getLibraryClassList().get(index).getRecommendedInstanceVersion();
239 String guid = lcd.getLibraryClassList().get(index).getRecommendedInstanceGuid();
240 String featureFlag = lcd.getLibraryClassList().get(index).getFeatureFlag();
241 Vector<String> arch = Tools.convertListToVector(lcd.getLibraryClassList().get(index)
242 .getSupArchList());
243 Vector<String> module = Tools.convertListToVector(lcd.getLibraryClassList().get(index)
244 .getSupModuleList());
245 String help = lcd.getLibraryClassList().get(index).getHelpText();
246 LibraryClassIdentification lcid = new LibraryClassIdentification(name, usage, version, guid, arch,
247 featureFlag, module, help);
248 vLibraryClass.addLibraryClass(lcid);
249 }
250 }
251 }
252 //
253 // Update the list
254 //
255 showTable();
256 }
257
258 /**
259 This method initializes this
260
261 **/
262 private void init() {
263 this.setContentPane(getJScrollPane());
264 this.setTitle("Library Class Definitions");
265 this.setBounds(new java.awt.Rectangle(0, 0, 500, 515));
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 /**
289 Save all components of Mbd Header
290 if exists mbdHeader, set the value directly
291 if not exists mbdHeader, new an instance first
292
293 **/
294 public void save() {
295 try {
296 int intLibraryCount = this.vLibraryClass.size();
297
298 lcd = LibraryClassDefinitions.Factory.newInstance();
299 if (intLibraryCount > 0) {
300 for (int index = 0; index < intLibraryCount; index++) {
301 LibraryClass mLibraryClass = LibraryClass.Factory.newInstance();
302
303 mLibraryClass.setKeyword(vLibraryClass.getLibraryClass(index).getLibraryClassName());
304 mLibraryClass
305 .setUsage(LibraryUsage.Enum.forString(vLibraryClass.getLibraryClass(index).getUsage()));
306 if (!isEmpty(vLibraryClass.getLibraryClass(index).getRecommendedInstanceVersion())) {
307 mLibraryClass.setRecommendedInstanceVersion(vLibraryClass.getLibraryClass(index)
308 .getRecommendedInstanceVersion());
309 }
310 if (!isEmpty(vLibraryClass.getLibraryClass(index).getRecommendedInstanceGuid())) {
311 mLibraryClass.setRecommendedInstanceGuid(vLibraryClass.getLibraryClass(index)
312 .getRecommendedInstanceGuid());
313 }
314 if (!isEmpty(vLibraryClass.getLibraryClass(index).getFeatureFlag())) {
315 mLibraryClass.setFeatureFlag(vLibraryClass.getLibraryClass(index).getFeatureFlag());
316 }
317 if (vLibraryClass.getLibraryClass(index).getSupArchList() != null
318 && vLibraryClass.getLibraryClass(index).getSupArchList().size() > 0) {
319 mLibraryClass.setSupArchList(vLibraryClass.getLibraryClass(index).getSupArchList());
320 }
321 if (vLibraryClass.getLibraryClass(index).getSupModuleList() != null
322 && vLibraryClass.getLibraryClass(index).getSupModuleList().size() > 0) {
323 mLibraryClass.setSupModuleList(vLibraryClass.getLibraryClass(index).getSupModuleList());
324 }
325 if (!isEmpty(vLibraryClass.getLibraryClass(index).getHelp())) {
326 mLibraryClass.setHelpText(vLibraryClass.getLibraryClass(index).getHelp());
327 }
328
329 this.lcd.addNewLibraryClass();
330 this.lcd.setLibraryClassArray(index, mLibraryClass);
331 }
332 }
333
334 if (msa.getLibraryClassDefinitions() == null) {
335 this.msa.addNewLibraryClassDefinitions();
336 }
337 this.msa.setLibraryClassDefinitions(this.lcd);
338
339 this.omt.setSaved(false);
340 } catch (Exception e) {
341 Log.wrn("Update Library Class Definitions", e.getMessage());
342 Log.err("Update Library Class Definitions", e.getMessage());
343 }
344 }
345
346 private void showEdit(int index) {
347 LibraryClassDefsDlg mcdd = new LibraryClassDefsDlg(vLibraryClass.getLibraryClass(index), new IFrame(), omt.getId());
348 int result = mcdd.showDialog();
349 if (result == DataType.RETURN_TYPE_OK) {
350 if (index == -1) {
351 this.vLibraryClass.addLibraryClass(mcdd.getLcid());
352 } else {
353 this.vLibraryClass.setLibraryClass(mcdd.getLcid(), index);
354 }
355 this.showTable();
356 this.save();
357 mcdd.dispose();
358 }
359 if (result == DataType.RETURN_TYPE_CANCEL) {
360 mcdd.dispose();
361 }
362 }
363
364 /* (non-Javadoc)
365 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
366 *
367 * Override actionPerformed to listen all actions
368 *
369 */
370 public void actionPerformed(ActionEvent arg0) {
371 if (arg0.getSource() == jButtonAdd) {
372 showEdit(-1);
373 }
374 if (arg0.getSource() == jButtonUpdate) {
375 if (this.selectedRow < 0) {
376 Log.wrn("Update Library Class Definitions", "Please select one record first.");
377 return;
378 }
379 showEdit(selectedRow);
380 }
381
382 if (arg0.getSource() == jButtonRemove) {
383 if (jTable.isEditing()) {
384 jTable.getCellEditor().stopCellEditing();
385 }
386 if (selectedRow > -1) {
387 this.model.removeRow(selectedRow);
388 this.vLibraryClass.removeLibraryClass(selectedRow);
389 selectedRow = -1;
390 this.save();
391 }
392 }
393 }
394
395 /**
396 Clear all table rows
397
398 **/
399 private void clearAll() {
400 if (model != null) {
401 for (int index = model.getRowCount() - 1; index >= 0; index--) {
402 model.removeRow(index);
403 }
404 }
405 }
406
407 /**
408 Read content of vector and put then into table
409
410 **/
411 private void showTable() {
412 clearAll();
413
414 if (vLibraryClass.size() > 0) {
415 for (int index = 0; index < vLibraryClass.size(); index++) {
416 model.addRow(vLibraryClass.toStringVector(index));
417 }
418 }
419 this.jTable.repaint();
420 this.jTable.updateUI();
421 //this.jScrollPane.setViewportView(this.jTable);
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 }