]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleGuids.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 / ModuleGuids.java
1 /** @file
2
3 The file is used to create, update Guids 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.GuidUsage;
31 import org.tianocore.GuidsDocument;
32 import org.tianocore.GuidsDocument.Guids;
33 import org.tianocore.GuidsDocument.Guids.GuidCNames;
34 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
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.Guids.GuidsIdentification;
43 import org.tianocore.frameworkwizard.module.Identifications.Guids.GuidsVector;
44 import org.tianocore.frameworkwizard.module.ui.dialog.GuidsDlg;
45
46 /**
47 The class is used to create, update Guids of MSA/MBD file
48 It extends IInternalFrame
49
50 **/
51 public class ModuleGuids extends IInternalFrame {
52
53 ///
54 /// Define class Serial Version UID
55 ///
56 private static final long serialVersionUID = 6710858997766979803L;
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 used by UI
77 //
78 private OpeningModuleType omt = null;
79
80 private ModuleSurfaceArea msa = null;
81
82 private GuidsDocument.Guids guids = null;
83
84 private GuidsIdentification id = null;
85
86 private GuidsVector vid = new GuidsVector();
87
88 private IDefaultTableModel model = null;
89
90 private int selectedRow = -1;
91
92 private IFrame parentFrame = null;
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.MULTIPLE_INTERVAL_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("Guids");
208 }
209
210 /**
211 This method initializes this
212 Fill values to all fields if these values are not empty
213
214 @param inPackageDependencies
215
216 **/
217 private void init(Guids inGuids) {
218 init();
219 this.guids = inGuids;
220
221 if (this.guids != null) {
222 if (this.guids.getGuidCNamesList().size() > 0) {
223 for (int index = 0; index < this.guids.getGuidCNamesList().size(); index++) {
224 String arg0 = guids.getGuidCNamesList().get(index).getGuidCName();
225 String arg1 = null;
226 if (guids.getGuidCNamesList().get(index).getUsage() != null) {
227 arg1 = guids.getGuidCNamesList().get(index).getUsage().toString();
228 }
229
230 String arg2 = guids.getGuidCNamesList().get(index).getFeatureFlag();
231 Vector<String> arg3 = Tools.convertListToVector(guids.getGuidCNamesList().get(index)
232 .getSupArchList());
233 String arg4 = guids.getGuidCNamesList().get(index).getHelpText();
234
235 id = new GuidsIdentification(arg0, arg1, arg2, arg3, arg4);
236 vid.addGuids(id);
237 }
238 }
239 }
240 showTable();
241 }
242
243 /**
244 This is the default constructor
245
246 **/
247 public ModuleGuids() {
248 super();
249 init();
250 this.setVisible(true);
251 }
252
253 /**
254 This is the override edit constructor
255
256 @param inGuids The input data of GuidsDocument.Guids
257
258 **/
259 public ModuleGuids(OpeningModuleType inOmt, IFrame iFrame) {
260 super();
261 this.omt = inOmt;
262 this.msa = omt.getXmlMsa();
263 this.parentFrame = iFrame;
264 init(msa.getGuids());
265 this.setVisible(true);
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 private void showEdit(int index) {
289 GuidsDlg dlg = new GuidsDlg(vid.getGuids(index), this.parentFrame, omt.getId());
290 int result = dlg.showDialog();
291 if (result == DataType.RETURN_TYPE_OK) {
292 if (index == -1) {
293 this.vid.addGuids(dlg.getId());
294 } else {
295 this.vid.setGuids(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 Guids", "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
357 int selectedRows[] = this.jTable.getSelectedRows();
358
359 if (selectedRows != null) {
360 for (int index = selectedRows.length - 1; index > -1; index--) {
361 this.model.removeRow(selectedRows[index]);
362 this.vid.removeGuids(selectedRows[index]);
363 }
364 selectedRow = -1;
365 this.save();
366 }
367 }
368 }
369
370 /**
371 Save all components of Guids
372 if exists guids, set the value directly
373 if not exists guids, new an instance first
374
375 **/
376 public void save() {
377 try {
378 int count = this.vid.size();
379
380 this.guids = Guids.Factory.newInstance();
381 if (count > 0) {
382 for (int index = 0; index < count; index++) {
383 GuidCNames p = GuidCNames.Factory.newInstance();
384 if (!isEmpty(vid.getGuids(index).getName())) {
385 p.setGuidCName(vid.getGuids(index).getName());
386 }
387 if (!isEmpty(vid.getGuids(index).getUsage())) {
388 p.setUsage(GuidUsage.Enum.forString(vid.getGuids(index).getUsage()));
389 }
390 if (!isEmpty(vid.getGuids(index).getFeatureFlag())) {
391 p.setFeatureFlag(vid.getGuids(index).getFeatureFlag());
392 }
393 if (vid.getGuids(index).getSupArchList() != null && vid.getGuids(index).getSupArchList().size() > 0) {
394 p.setSupArchList(vid.getGuids(index).getSupArchList());
395 }
396 if (!isEmpty(vid.getGuids(index).getHelp())) {
397 p.setHelpText(vid.getGuids(index).getHelp());
398 }
399 this.guids.addNewGuidCNames();
400 this.guids.setGuidCNamesArray(guids.getGuidCNamesList().size() - 1, p);
401 }
402 }
403
404 this.msa.setGuids(guids);
405 this.omt.setSaved(false);
406 } catch (Exception e) {
407 Log.wrn("Update Guids", e.getMessage());
408 Log.err("Update Guids", e.getMessage());
409 }
410 }
411
412 /* (non-Javadoc)
413 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
414 *
415 */
416 public void valueChanged(ListSelectionEvent arg0) {
417 if (arg0.getValueIsAdjusting()) {
418 return;
419 }
420 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
421 if (lsm.isSelectionEmpty()) {
422 return;
423 } else {
424 selectedRow = lsm.getMinSelectionIndex();
425 }
426 }
427
428 /* (non-Javadoc)
429 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
430 *
431 */
432 public void mouseClicked(MouseEvent arg0) {
433 if (arg0.getClickCount() == 2) {
434 if (this.selectedRow < 0) {
435 return;
436 } else {
437 showEdit(selectedRow);
438 }
439 }
440 }
441
442 /* (non-Javadoc)
443 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
444 *
445 * Override componentResized to resize all components when frame's size is changed
446 */
447 public void componentResized(ComponentEvent arg0) {
448 int intCurrentWidth = this.getJContentPane().getWidth();
449 int intCurrentHeight = this.getJContentPane().getHeight();
450 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
451 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
452
453 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
454 intPreferredHeight);
455 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
456 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
457 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
458 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
459 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
460 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
461 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
462 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
463 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
464 }
465 }