]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleLibraryClassDefinitions.java
1. Finish Enhancement EDKT345: Support deleting multiple rows together when remove...
[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 private IFrame parentFrame = null;
91
92 /**
93 This method initializes jButtonAdd
94
95 @return javax.swing.JButton jButtonAdd
96
97 **/
98 private JButton getJButtonAdd() {
99 if (jButtonAdd == null) {
100 jButtonAdd = new JButton();
101 jButtonAdd.setBounds(new java.awt.Rectangle(230, 330, 90, 20));
102 jButtonAdd.setPreferredSize(new Dimension(90, 20));
103 jButtonAdd.setText("Add");
104 jButtonAdd.addActionListener(this);
105 }
106 return jButtonAdd;
107 }
108
109 /**
110 This method initializes jButtonRemove
111
112 @return javax.swing.JButton jButtonRemove
113
114 **/
115 private JButton getJButtonRemove() {
116 if (jButtonRemove == null) {
117 jButtonRemove = new JButton();
118 jButtonRemove.setBounds(new java.awt.Rectangle(400, 330, 90, 20));
119 jButtonRemove.setText("Remove");
120 jButtonRemove.setPreferredSize(new Dimension(90, 20));
121 jButtonRemove.addActionListener(this);
122 }
123 return jButtonRemove;
124 }
125
126 /**
127 This method initializes jButtonUpdate
128
129 @return javax.swing.JButton jButtonUpdate
130
131 **/
132 private JButton getJButtonUpdate() {
133 if (jButtonUpdate == null) {
134 jButtonUpdate = new JButton();
135 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 330, 90, 20));
136 jButtonUpdate.setText("Edit");
137 jButtonUpdate.setPreferredSize(new Dimension(90, 20));
138 jButtonUpdate.addActionListener(this);
139 }
140 return jButtonUpdate;
141 }
142
143 /**
144 This method initializes jScrollPane
145
146 @return javax.swing.JScrollPane
147 */
148 private JScrollPane getJScrollPane() {
149 if (jScrollPane == null) {
150 jScrollPane = new JScrollPane();
151 jScrollPane.setViewportView(getJContentPane());
152 }
153 return jScrollPane;
154 }
155
156 /**
157 This method initializes jScrollPaneTable
158
159 @return javax.swing.JScrollPane
160 **/
161 private JScrollPane getJScrollPaneTable() {
162 if (jScrollPaneTable == null) {
163 jScrollPaneTable = new JScrollPane();
164 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
165 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
166 jScrollPaneTable.setViewportView(getJTable());
167 }
168 return jScrollPaneTable;
169 }
170
171 /**
172 This method initializes jTable
173
174 @return javax.swing.JTable
175 **/
176 private JTable getJTable() {
177 if (jTable == null) {
178 jTable = new JTable();
179 model = new IDefaultTableModel();
180 jTable = new JTable(model);
181 jTable.setRowHeight(20);
182
183 model.addColumn("Library Class Name");
184 model.addColumn("Usage");
185
186 jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
187 jTable.getSelectionModel().addListSelectionListener(this);
188 jTable.getModel().addTableModelListener(this);
189 jTable.addMouseListener(this);
190 }
191 return jTable;
192 }
193
194 public static void main(String[] args) {
195
196 }
197
198 /**
199 This is the default constructor
200
201 **/
202 public ModuleLibraryClassDefinitions() {
203 super();
204 init();
205 this.setVisible(true);
206 }
207
208 /**
209 This is the override edit constructor
210
211 @param
212
213 **/
214 public ModuleLibraryClassDefinitions(OpeningModuleType inOmt, IFrame iFrame) {
215 super();
216 this.omt = inOmt;
217 this.msa = omt.getXmlMsa();
218 this.parentFrame = iFrame;
219 init(msa.getLibraryClassDefinitions());
220 this.setVisible(true);
221 }
222
223 /**
224 This method initializes this
225 Fill values to all fields if these values are not empty
226
227 @param inLibraryClassDefinitions The input data of LibraryClassDefinitionsDocument.LibraryClassDefinitions
228
229 **/
230 private void init(LibraryClassDefinitionsDocument.LibraryClassDefinitions inLibraryClassDefinitions) {
231 init();
232 this.lcd = inLibraryClassDefinitions;
233 if (this.lcd != null) {
234 if (this.lcd.getLibraryClassList().size() > 0) {
235 for (int index = 0; index < this.lcd.getLibraryClassList().size(); index++) {
236 String name = lcd.getLibraryClassList().get(index).getKeyword();
237 String usage = null;
238 if (lcd.getLibraryClassList().get(index).getUsage() != null) {
239 usage = lcd.getLibraryClassList().get(index).getUsage().toString();
240 }
241 String version = lcd.getLibraryClassList().get(index).getRecommendedInstanceVersion();
242 String guid = lcd.getLibraryClassList().get(index).getRecommendedInstanceGuid();
243 String featureFlag = lcd.getLibraryClassList().get(index).getFeatureFlag();
244 Vector<String> arch = Tools.convertListToVector(lcd.getLibraryClassList().get(index)
245 .getSupArchList());
246 Vector<String> module = Tools.convertListToVector(lcd.getLibraryClassList().get(index)
247 .getSupModuleList());
248 String help = lcd.getLibraryClassList().get(index).getHelpText();
249 LibraryClassIdentification lcid = new LibraryClassIdentification(name, usage, version, guid, arch,
250 featureFlag, module, help);
251 vLibraryClass.addLibraryClass(lcid);
252 }
253 }
254 }
255 //
256 // Update the list
257 //
258 showTable();
259 }
260
261 /**
262 This method initializes this
263
264 **/
265 private void init() {
266 this.setContentPane(getJScrollPane());
267 this.setTitle("Library Class Definitions");
268 this.setBounds(new java.awt.Rectangle(0, 0, 500, 515));
269 }
270
271 /**
272 This method initializes jContentPane
273
274 @return javax.swing.JPanel jContentPane
275
276 **/
277 private JPanel getJContentPane() {
278 if (jContentPane == null) {
279 jContentPane = new JPanel();
280 jContentPane.setLayout(null);
281 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
282
283 jContentPane.add(getJButtonAdd(), null);
284 jContentPane.add(getJButtonRemove(), null);
285 jContentPane.add(getJButtonUpdate(), null);
286 jContentPane.add(getJScrollPaneTable(), null);
287 }
288 return jContentPane;
289 }
290
291 /**
292 Save all components of Mbd Header
293 if exists mbdHeader, set the value directly
294 if not exists mbdHeader, new an instance first
295
296 **/
297 public void save() {
298 try {
299 int intLibraryCount = this.vLibraryClass.size();
300
301 lcd = LibraryClassDefinitions.Factory.newInstance();
302 if (intLibraryCount > 0) {
303 for (int index = 0; index < intLibraryCount; index++) {
304 LibraryClass mLibraryClass = LibraryClass.Factory.newInstance();
305
306 mLibraryClass.setKeyword(vLibraryClass.getLibraryClass(index).getLibraryClassName());
307 mLibraryClass
308 .setUsage(LibraryUsage.Enum.forString(vLibraryClass.getLibraryClass(index).getUsage()));
309 if (!isEmpty(vLibraryClass.getLibraryClass(index).getRecommendedInstanceVersion())) {
310 mLibraryClass.setRecommendedInstanceVersion(vLibraryClass.getLibraryClass(index)
311 .getRecommendedInstanceVersion());
312 }
313 if (!isEmpty(vLibraryClass.getLibraryClass(index).getRecommendedInstanceGuid())) {
314 mLibraryClass.setRecommendedInstanceGuid(vLibraryClass.getLibraryClass(index)
315 .getRecommendedInstanceGuid());
316 }
317 if (!isEmpty(vLibraryClass.getLibraryClass(index).getFeatureFlag())) {
318 mLibraryClass.setFeatureFlag(vLibraryClass.getLibraryClass(index).getFeatureFlag());
319 }
320 if (vLibraryClass.getLibraryClass(index).getSupArchList() != null
321 && vLibraryClass.getLibraryClass(index).getSupArchList().size() > 0) {
322 mLibraryClass.setSupArchList(vLibraryClass.getLibraryClass(index).getSupArchList());
323 }
324 if (vLibraryClass.getLibraryClass(index).getSupModuleList() != null
325 && vLibraryClass.getLibraryClass(index).getSupModuleList().size() > 0) {
326 mLibraryClass.setSupModuleList(vLibraryClass.getLibraryClass(index).getSupModuleList());
327 }
328 if (!isEmpty(vLibraryClass.getLibraryClass(index).getHelp())) {
329 mLibraryClass.setHelpText(vLibraryClass.getLibraryClass(index).getHelp());
330 }
331
332 this.lcd.addNewLibraryClass();
333 this.lcd.setLibraryClassArray(index, mLibraryClass);
334 }
335 }
336
337 if (msa.getLibraryClassDefinitions() == null) {
338 this.msa.addNewLibraryClassDefinitions();
339 }
340 this.msa.setLibraryClassDefinitions(this.lcd);
341
342 this.omt.setSaved(false);
343 } catch (Exception e) {
344 Log.wrn("Update Library Class Definitions", e.getMessage());
345 Log.err("Update Library Class Definitions", e.getMessage());
346 }
347 }
348
349 private void showEdit(int index) {
350 LibraryClassDefsDlg mcdd = new LibraryClassDefsDlg(vLibraryClass.getLibraryClass(index), this.parentFrame, omt.getId());
351 int result = mcdd.showDialog();
352 if (result == DataType.RETURN_TYPE_OK) {
353 if (index == -1) {
354 this.vLibraryClass.addLibraryClass(mcdd.getLcid());
355 } else {
356 this.vLibraryClass.setLibraryClass(mcdd.getLcid(), index);
357 }
358 this.showTable();
359 this.save();
360 mcdd.dispose();
361 }
362 if (result == DataType.RETURN_TYPE_CANCEL) {
363 mcdd.dispose();
364 }
365 }
366
367 /* (non-Javadoc)
368 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
369 *
370 * Override actionPerformed to listen all actions
371 *
372 */
373 public void actionPerformed(ActionEvent arg0) {
374 if (arg0.getSource() == jButtonAdd) {
375 showEdit(-1);
376 }
377 if (arg0.getSource() == jButtonUpdate) {
378 if (this.selectedRow < 0) {
379 Log.wrn("Update Library Class Definitions", "Please select one record first.");
380 return;
381 }
382 showEdit(selectedRow);
383 }
384
385 if (arg0.getSource() == jButtonRemove) {
386 if (jTable.isEditing()) {
387 jTable.getCellEditor().stopCellEditing();
388 }
389
390 int selectedRows[] = this.jTable.getSelectedRows();
391
392 if (selectedRows != null) {
393 for (int index = selectedRows.length - 1; index > -1; index--) {
394 this.model.removeRow(selectedRows[index]);
395 this.vLibraryClass.removeLibraryClass(selectedRows[index]);
396 }
397 selectedRow = -1;
398 this.save();
399 }
400 }
401 }
402
403 /**
404 Clear all table rows
405
406 **/
407 private void clearAll() {
408 if (model != null) {
409 for (int index = model.getRowCount() - 1; index >= 0; index--) {
410 model.removeRow(index);
411 }
412 }
413 }
414
415 /**
416 Read content of vector and put then into table
417
418 **/
419 private void showTable() {
420 clearAll();
421
422 if (vLibraryClass.size() > 0) {
423 for (int index = 0; index < vLibraryClass.size(); index++) {
424 model.addRow(vLibraryClass.toStringVector(index));
425 }
426 }
427 this.jTable.repaint();
428 this.jTable.updateUI();
429 //this.jScrollPane.setViewportView(this.jTable);
430 }
431
432 /* (non-Javadoc)
433 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
434 *
435 */
436 public void valueChanged(ListSelectionEvent arg0) {
437 if (arg0.getValueIsAdjusting()) {
438 return;
439 }
440 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
441 if (lsm.isSelectionEmpty()) {
442 return;
443 } else {
444 selectedRow = lsm.getMinSelectionIndex();
445 }
446 }
447
448 /* (non-Javadoc)
449 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
450 *
451 */
452 public void mouseClicked(MouseEvent arg0) {
453 if (arg0.getClickCount() == 2) {
454 if (this.selectedRow < 0) {
455 return;
456 } else {
457 showEdit(selectedRow);
458 }
459 }
460 }
461
462 /* (non-Javadoc)
463 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
464 *
465 * Override componentResized to resize all components when frame's size is changed
466 */
467 public void componentResized(ComponentEvent arg0) {
468 int intCurrentWidth = this.getJContentPane().getWidth();
469 int intCurrentHeight = this.getJContentPane().getHeight();
470 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
471 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
472
473 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
474 intPreferredHeight);
475 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
476 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
477 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
478 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
479 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
480 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
481 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
482 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
483 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
484 }
485 }