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