]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleLibraryClassDefinitions.java
28cf57e5823bd15515023da2e64ea3a236863da9
[mirror_edk2.git] / Tools / 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, 80, 20));
100 jButtonAdd.setPreferredSize(new Dimension(80, 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, 80, 20));
117 jButtonRemove.setText("Remove");
118 jButtonRemove.setPreferredSize(new Dimension(80, 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, 80, 20));
134 jButtonUpdate.setText("Edit");
135 jButtonUpdate.setPreferredSize(new Dimension(80, 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 (!isEmpty(vLibraryClass.getLibraryClass(index).getHelp())) {
322 mLibraryClass.setHelpText(vLibraryClass.getLibraryClass(index).getHelp());
323 }
324
325 this.lcd.addNewLibraryClass();
326 this.lcd.setLibraryClassArray(index, mLibraryClass);
327 }
328 }
329
330 if (msa.getLibraryClassDefinitions() == null) {
331 this.msa.addNewLibraryClassDefinitions();
332 }
333 this.msa.setLibraryClassDefinitions(this.lcd);
334
335 this.omt.setSaved(false);
336 } catch (Exception e) {
337 Log.wrn("Update Library Class Definitions", e.getMessage());
338 Log.err("Update Library Class Definitions", e.getMessage());
339 }
340 }
341
342 private void showEdit(int index) {
343 LibraryClassDefsDlg mcdd = new LibraryClassDefsDlg(vLibraryClass.getLibraryClass(index), new IFrame());
344 int result = mcdd.showDialog();
345 if (result == DataType.RETURN_TYPE_OK) {
346 if (index == -1) {
347 this.vLibraryClass.addLibraryClass(mcdd.getLcid());
348 } else {
349 this.vLibraryClass.setLibraryClass(mcdd.getLcid(), index);
350 }
351 this.showTable();
352 this.save();
353 mcdd.dispose();
354 }
355 if (result == DataType.RETURN_TYPE_CANCEL) {
356 mcdd.dispose();
357 }
358 }
359
360 /* (non-Javadoc)
361 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
362 *
363 * Override actionPerformed to listen all actions
364 *
365 */
366 public void actionPerformed(ActionEvent arg0) {
367 if (arg0.getSource() == jButtonAdd) {
368 showEdit(-1);
369 }
370 if (arg0.getSource() == jButtonUpdate) {
371 if (this.selectedRow < 0) {
372 Log.wrn("Update Library Class Definitions", "Please select one record first.");
373 return;
374 }
375 showEdit(selectedRow);
376 }
377
378 if (arg0.getSource() == jButtonRemove) {
379 if (jTable.isEditing()) {
380 jTable.getCellEditor().stopCellEditing();
381 }
382 if (selectedRow > -1) {
383 this.model.removeRow(selectedRow);
384 this.vLibraryClass.removeLibraryClass(selectedRow);
385 selectedRow = -1;
386 this.save();
387 }
388 }
389 }
390
391 /**
392 Clear all table rows
393
394 **/
395 private void clearAll() {
396 if (model != null) {
397 for (int index = model.getRowCount() - 1; index >= 0; index--) {
398 model.removeRow(index);
399 }
400 }
401 }
402
403 /**
404 Read content of vector and put then into table
405
406 **/
407 private void showTable() {
408 clearAll();
409
410 if (vLibraryClass.size() > 0) {
411 for (int index = 0; index < vLibraryClass.size(); index++) {
412 model.addRow(vLibraryClass.toStringVector(index));
413 }
414 }
415 this.jTable.repaint();
416 this.jTable.updateUI();
417 //this.jScrollPane.setViewportView(this.jTable);
418 }
419
420 /* (non-Javadoc)
421 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
422 *
423 */
424 public void valueChanged(ListSelectionEvent arg0) {
425 if (arg0.getValueIsAdjusting()) {
426 return;
427 }
428 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
429 if (lsm.isSelectionEmpty()) {
430 return;
431 } else {
432 selectedRow = lsm.getMinSelectionIndex();
433 }
434 }
435
436 /* (non-Javadoc)
437 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
438 *
439 */
440 public void mouseClicked(MouseEvent arg0) {
441 if (arg0.getClickCount() == 2) {
442 if (this.selectedRow < 0) {
443 return;
444 } else {
445 showEdit(selectedRow);
446 }
447 }
448 }
449
450 /* (non-Javadoc)
451 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
452 *
453 * Override componentResized to resize all components when frame's size is changed
454 */
455 public void componentResized(ComponentEvent arg0) {
456 int intCurrentWidth = this.getJContentPane().getWidth();
457 int intCurrentHeight = this.getJContentPane().getHeight();
458 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
459 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
460
461 resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight);
462 relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
463 DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
464 relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
465 DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
466 relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
467 DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
468 }
469 }