Use table and popup window to add/edit all elements of module instead of original...
[mirror_edk2.git] / Tools / 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 /**
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, 195, 80, 20));
102 jButtonAdd.setText("Add");
103 jButtonAdd.addActionListener(this);
104 jButtonAdd.setPreferredSize(new java.awt.Dimension(80, 20));
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, 195, 80, 20));
119 jButtonRemove.setText("Remove");
120 jButtonRemove.addActionListener(this);
121 jButtonRemove.setPreferredSize(new java.awt.Dimension(80, 20));
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, 195, 80, 20));
136 jButtonUpdate.setPreferredSize(new java.awt.Dimension(80, 20));
137 jButtonUpdate.setText("Edit");
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("Guid C_Name");
184 model.addColumn("Usage");
185
186 jTable.setSelectionMode(ListSelectionModel.SINGLE_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 method initializes this
200
201 **/
202 private void init() {
203 this.setSize(500, 515);
204 this.setContentPane(getJScrollPane());
205 this.setTitle("Guids");
206 }
207
208 /**
209 This method initializes this
210 Fill values to all fields if these values are not empty
211
212 @param inPackageDependencies
213
214 **/
215 private void init(Guids inGuids) {
216 init();
217 this.guids = inGuids;
218
219 if (this.guids != null) {
220 if (this.guids.getGuidCNamesList().size() > 0) {
221 for (int index = 0; index < this.guids.getGuidCNamesList().size(); index++) {
222 String arg0 = guids.getGuidCNamesList().get(index).getGuidCName();
223 String arg1 = null;
224 if (guids.getGuidCNamesList().get(index).getUsage() != null) {
225 arg1 = guids.getGuidCNamesList().get(index).getUsage().toString();
226 }
227
228 String arg2 = guids.getGuidCNamesList().get(index).getFeatureFlag();
229 Vector<String> arg3 = Tools.convertListToVector(guids.getGuidCNamesList().get(index)
230 .getSupArchList());
231 String arg4 = guids.getGuidCNamesList().get(index).getHelpText();
232
233 id = new GuidsIdentification(arg0, arg1, arg2, arg3, arg4);
234 vid.addGuids(id);
235 }
236 }
237 }
238 showTable();
239 }
240
241 /**
242 This is the default constructor
243
244 **/
245 public ModuleGuids() {
246 super();
247 init();
248 this.setVisible(true);
249 }
250
251 /**
252 This is the override edit constructor
253
254 @param inGuids The input data of GuidsDocument.Guids
255
256 **/
257 public ModuleGuids(OpeningModuleType inOmt) {
258 super();
259 this.omt = inOmt;
260 this.msa = omt.getXmlMsa();
261 init(msa.getGuids());
262 this.setVisible(true);
263 }
264
265 /**
266 This method initializes jContentPane
267
268 @return javax.swing.JPanel jContentPane
269
270 **/
271 private JPanel getJContentPane() {
272 if (jContentPane == null) {
273 jContentPane = new JPanel();
274 jContentPane.setLayout(null);
275 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
276
277 jContentPane.add(getJButtonAdd(), null);
278 jContentPane.add(getJButtonRemove(), null);
279 jContentPane.add(getJButtonUpdate(), null);
280 jContentPane.add(getJScrollPaneTable(), null);
281 }
282 return jContentPane;
283 }
284
285 private void showEdit(int index) {
286 GuidsDlg dlg = new GuidsDlg(vid.getGuids(index), new IFrame());
287 int result = dlg.showDialog();
288 if (result == DataType.RETURN_TYPE_OK) {
289 if (index == -1) {
290 this.vid.addGuids(dlg.getId());
291 } else {
292 this.vid.setGuids(dlg.getId(), index);
293 }
294 this.showTable();
295 this.save();
296 dlg.dispose();
297 }
298 if (result == DataType.RETURN_TYPE_CANCEL) {
299 dlg.dispose();
300 }
301 }
302
303 /**
304 Clear all table rows
305
306 **/
307 private void clearAll() {
308 if (model != null) {
309 for (int index = model.getRowCount() - 1; index >= 0; index--) {
310 model.removeRow(index);
311 }
312 }
313 }
314
315 /**
316 Read content of vector and put then into table
317
318 **/
319 private void showTable() {
320 clearAll();
321
322 if (vid.size() > 0) {
323 for (int index = 0; index < vid.size(); index++) {
324 model.addRow(vid.toStringVector(index));
325 }
326 }
327 this.jTable.repaint();
328 this.jTable.updateUI();
329 }
330
331 /* (non-Javadoc)
332 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
333 *
334 * Override actionPerformed to listen all actions
335 *
336 */
337 public void actionPerformed(ActionEvent arg0) {
338 if (arg0.getSource() == jButtonAdd) {
339 showEdit(-1);
340 }
341 if (arg0.getSource() == jButtonUpdate) {
342 if (this.selectedRow < 0) {
343 Log.err("Please select one record first.");
344 return;
345 }
346 showEdit(selectedRow);
347 }
348
349 if (arg0.getSource() == jButtonRemove) {
350 if (jTable.isEditing()) {
351 jTable.getCellEditor().stopCellEditing();
352 }
353 if (selectedRow > -1) {
354 this.model.removeRow(selectedRow);
355 this.vid.removeGuids(selectedRow);
356 selectedRow = -1;
357 this.save();
358 }
359 }
360 }
361
362 /**
363 Save all components of Guids
364 if exists guids, set the value directly
365 if not exists guids, new an instance first
366
367 **/
368 public void save() {
369 try {
370 int count = this.vid.size();
371
372 this.guids = Guids.Factory.newInstance();
373 if (count > 0) {
374 for (int index = 0; index < count; index++) {
375 GuidCNames p = GuidCNames.Factory.newInstance();
376 if (!isEmpty(vid.getGuids(index).getName())) {
377 p.setGuidCName(vid.getGuids(index).getName());
378 }
379 if (!isEmpty(vid.getGuids(index).getUsage())) {
380 p.setUsage(GuidUsage.Enum.forString(vid.getGuids(index).getUsage()));
381 }
382 if (!isEmpty(vid.getGuids(index).getFeatureFlag())) {
383 p.setFeatureFlag(vid.getGuids(index).getFeatureFlag());
384 }
385 if (vid.getGuids(index).getSupArchList() != null && vid.getGuids(index).getSupArchList().size() > 0) {
386 p.setSupArchList(vid.getGuids(index).getSupArchList());
387 }
388 if (!isEmpty(vid.getGuids(index).getHelp())) {
389 p.setHelpText(vid.getGuids(index).getHelp());
390 }
391 this.guids.addNewGuidCNames();
392 this.guids.setGuidCNamesArray(guids.getGuidCNamesList().size() - 1, p);
393 }
394 }
395
396 this.msa.setGuids(guids);
397 this.omt.setSaved(false);
398 } catch (Exception e) {
399 Log.err("Update Guids", e.getMessage());
400 }
401 }
402
403 /* (non-Javadoc)
404 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
405 *
406 */
407 public void valueChanged(ListSelectionEvent arg0) {
408 if (arg0.getValueIsAdjusting()) {
409 return;
410 }
411 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
412 if (lsm.isSelectionEmpty()) {
413 return;
414 } else {
415 selectedRow = lsm.getMinSelectionIndex();
416 }
417 }
418
419 /* (non-Javadoc)
420 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
421 *
422 */
423 public void mouseClicked(MouseEvent arg0) {
424 if (arg0.getClickCount() == 2) {
425 if (this.selectedRow < 0) {
426 return;
427 } else {
428 showEdit(selectedRow);
429 }
430 }
431 }
432
433 /* (non-Javadoc)
434 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
435 *
436 * Override componentResized to resize all components when frame's size is changed
437 */
438 public void componentResized(ComponentEvent arg0) {
439 int intCurrentWidth = this.getJContentPane().getWidth();
440 int intCurrentHeight = this.getJContentPane().getHeight();
441 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
442 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
443
444 resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight);
445 relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
446 DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
447 relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
448 DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
449 relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
450 DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
451 }
452 }