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 / ModuleDataHubs.java
1 /** @file
2
3 The file is used to create, update DataHub 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
16 package org.tianocore.frameworkwizard.module.ui;
17
18 import java.awt.Dimension;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ComponentEvent;
21 import java.awt.event.MouseEvent;
22 import java.util.Vector;
23
24 import javax.swing.JButton;
25 import javax.swing.JPanel;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTable;
28 import javax.swing.ListSelectionModel;
29 import javax.swing.event.ListSelectionEvent;
30
31 import org.tianocore.DataHubUsage;
32 import org.tianocore.DataHubsDocument;
33 import org.tianocore.DataHubsDocument.DataHubs;
34 import org.tianocore.DataHubsDocument.DataHubs.DataHubRecord;
35 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
36 import org.tianocore.frameworkwizard.common.DataType;
37 import org.tianocore.frameworkwizard.common.IDefaultTableModel;
38 import org.tianocore.frameworkwizard.common.Log;
39 import org.tianocore.frameworkwizard.common.Tools;
40 import org.tianocore.frameworkwizard.common.Identifications.OpeningModuleType;
41 import org.tianocore.frameworkwizard.common.ui.IFrame;
42 import org.tianocore.frameworkwizard.common.ui.IInternalFrame;
43 import org.tianocore.frameworkwizard.module.Identifications.DataHubs.DataHubsIdentification;
44 import org.tianocore.frameworkwizard.module.Identifications.DataHubs.DataHubsVector;
45 import org.tianocore.frameworkwizard.module.ui.dialog.DataHubsDlg;
46
47 /**
48 The class is used to create, update DataHub of MSA/MBD file
49 It extends IInternalFrame
50
51
52
53 **/
54 public class ModuleDataHubs extends IInternalFrame {
55
56 ///
57 /// Define class Serial Version UID
58 ///
59 private static final long serialVersionUID = -3667906991966638892L;
60
61 //
62 //Define class members
63 //
64 private JPanel jContentPane = null;
65
66 private JButton jButtonAdd = null;
67
68 private JButton jButtonRemove = null;
69
70 private JButton jButtonUpdate = null;
71
72 private JScrollPane jScrollPane = null;
73
74 private JScrollPane jScrollPaneTable = null;
75
76 private JTable jTable = null;
77
78 //
79 // Not used by UI
80 //
81 private OpeningModuleType omt = null;
82
83 private ModuleSurfaceArea msa = null;
84
85 private DataHubsDocument.DataHubs dataHubs = null;
86
87 private DataHubsIdentification id = null;
88
89 private DataHubsVector vid = new DataHubsVector();
90
91 private IDefaultTableModel model = null;
92
93 private int selectedRow = -1;
94
95 /**
96 This method initializes jButtonAdd
97
98 @return javax.swing.JButton jButtonAdd
99
100 **/
101 private JButton getJButtonAdd() {
102 if (jButtonAdd == null) {
103 jButtonAdd = new JButton();
104 jButtonAdd.setBounds(new java.awt.Rectangle(230, 195, 80, 20));
105 jButtonAdd.setText("Add");
106 jButtonAdd.addActionListener(this);
107 jButtonAdd.setPreferredSize(new java.awt.Dimension(80, 20));
108 }
109 return jButtonAdd;
110 }
111
112 /**
113 This method initializes jButtonRemove
114
115 @return javax.swing.JButton jButtonRemove
116
117 **/
118 private JButton getJButtonRemove() {
119 if (jButtonRemove == null) {
120 jButtonRemove = new JButton();
121 jButtonRemove.setBounds(new java.awt.Rectangle(400, 195, 80, 20));
122 jButtonRemove.setText("Remove");
123 jButtonRemove.addActionListener(this);
124 jButtonRemove.setPreferredSize(new java.awt.Dimension(80, 20));
125 }
126 return jButtonRemove;
127 }
128
129 /**
130 This method initializes jButtonUpdate
131
132 @return javax.swing.JButton jButtonUpdate
133
134 **/
135 private JButton getJButtonUpdate() {
136 if (jButtonUpdate == null) {
137 jButtonUpdate = new JButton();
138 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 195, 80, 20));
139 jButtonUpdate.setPreferredSize(new java.awt.Dimension(80, 20));
140 jButtonUpdate.setText("Edit");
141 jButtonUpdate.addActionListener(this);
142 }
143 return jButtonUpdate;
144 }
145
146 /**
147 This method initializes jScrollPane
148
149 @return javax.swing.JScrollPane
150 */
151 private JScrollPane getJScrollPane() {
152 if (jScrollPane == null) {
153 jScrollPane = new JScrollPane();
154 jScrollPane.setViewportView(getJContentPane());
155 }
156 return jScrollPane;
157 }
158
159 /**
160 This method initializes jScrollPaneTable
161
162 @return javax.swing.JScrollPane
163 **/
164 private JScrollPane getJScrollPaneTable() {
165 if (jScrollPaneTable == null) {
166 jScrollPaneTable = new JScrollPane();
167 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
168 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
169 jScrollPaneTable.setViewportView(getJTable());
170 }
171 return jScrollPaneTable;
172 }
173
174 /**
175 This method initializes jTable
176
177 @return javax.swing.JTable
178 **/
179 private JTable getJTable() {
180 if (jTable == null) {
181 jTable = new JTable();
182 model = new IDefaultTableModel();
183 jTable = new JTable(model);
184 jTable.setRowHeight(20);
185
186 model.addColumn("Data Hub C_Name");
187 model.addColumn("Usage");
188
189 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
190 jTable.getSelectionModel().addListSelectionListener(this);
191 jTable.getModel().addTableModelListener(this);
192 jTable.addMouseListener(this);
193 }
194 return jTable;
195 }
196
197 public static void main(String[] args) {
198
199 }
200
201 /**
202 This method initializes this
203
204 **/
205 private void init() {
206 this.setSize(500, 515);
207 this.setContentPane(getJScrollPane());
208 this.setTitle("Data Hubs");
209 }
210
211 /**
212 This method initializes this
213 Fill values to all fields if these values are not empty
214
215 @param inPackageDependencies
216
217 **/
218 private void init(DataHubs inDataHubs) {
219 init();
220 this.dataHubs = inDataHubs;
221
222 if (this.dataHubs != null) {
223 if (this.dataHubs.getDataHubRecordList().size() > 0) {
224 for (int index = 0; index < this.dataHubs.getDataHubRecordList().size(); index++) {
225 String arg0 = dataHubs.getDataHubRecordList().get(index).getDataHubCName();
226 String arg1 = null;
227 if (dataHubs.getDataHubRecordList().get(index).getUsage() != null) {
228 arg1 = dataHubs.getDataHubRecordList().get(index).getUsage().toString();
229 }
230
231 String arg2 = dataHubs.getDataHubRecordList().get(index).getFeatureFlag();
232 Vector<String> arg3 = Tools.convertListToVector(dataHubs.getDataHubRecordList().get(index)
233 .getSupArchList());
234 String arg4 = dataHubs.getDataHubRecordList().get(index).getHelpText();
235
236 id = new DataHubsIdentification(arg0, arg1, arg2, arg3, arg4);
237 vid.addDataHubs(id);
238 }
239 }
240 }
241 showTable();
242 }
243
244 /**
245 This is the default constructor
246
247 **/
248 public ModuleDataHubs() {
249 super();
250 init();
251 this.setVisible(true);
252 }
253
254 /**
255 This is the override edit constructor
256
257 @param inDataHubs The input DataHubsDocument.DataHubs
258
259 **/
260 public ModuleDataHubs(OpeningModuleType inOmt) {
261 super();
262 this.omt = inOmt;
263 this.msa = omt.getXmlMsa();
264 init(msa.getDataHubs());
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 DataHubsDlg dlg = new DataHubsDlg(vid.getDataHubs(index), new IFrame());
290 int result = dlg.showDialog();
291 if (result == DataType.RETURN_TYPE_OK) {
292 if (index == -1) {
293 this.vid.addDataHubs(dlg.getId());
294 } else {
295 this.vid.setDataHubs(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.err("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 if (selectedRow > -1) {
357 this.model.removeRow(selectedRow);
358 this.vid.removeDataHubs(selectedRow);
359 selectedRow = -1;
360 this.save();
361 }
362 }
363 }
364
365 /**
366 Save all components of DataHubs
367 if exists dataHubs, set the value directly
368 if not exists dataHubs, new an instance first
369
370 **/
371 public void save() {
372 try {
373 int count = this.vid.size();
374
375 this.dataHubs = DataHubs.Factory.newInstance();
376 if (count > 0) {
377 for (int index = 0; index < count; index++) {
378 DataHubRecord p = DataHubRecord.Factory.newInstance();
379 if (!isEmpty(vid.getDataHubs(index).getName())) {
380 p.setDataHubCName(vid.getDataHubs(index).getName());
381 }
382 if (!isEmpty(vid.getDataHubs(index).getUsage())) {
383 p.setUsage(DataHubUsage.Enum.forString(vid.getDataHubs(index).getUsage()));
384 }
385 if (!isEmpty(vid.getDataHubs(index).getFeatureFlag())) {
386 p.setFeatureFlag(vid.getDataHubs(index).getFeatureFlag());
387 }
388 if (vid.getDataHubs(index).getSupArchList() != null
389 && vid.getDataHubs(index).getSupArchList().size() > 0) {
390 p.setSupArchList(vid.getDataHubs(index).getSupArchList());
391 }
392 if (!isEmpty(vid.getDataHubs(index).getHelp())) {
393 p.setHelpText(vid.getDataHubs(index).getHelp());
394 }
395 this.dataHubs.addNewDataHubRecord();
396 this.dataHubs.setDataHubRecordArray(dataHubs.getDataHubRecordList().size() - 1, p);
397 }
398 }
399
400 this.msa.setDataHubs(dataHubs);
401 this.omt.setSaved(false);
402 } catch (Exception e) {
403 Log.err("Update Data Hubs", e.getMessage());
404 }
405 }
406
407 /* (non-Javadoc)
408 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
409 *
410 */
411 public void valueChanged(ListSelectionEvent arg0) {
412 if (arg0.getValueIsAdjusting()) {
413 return;
414 }
415 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
416 if (lsm.isSelectionEmpty()) {
417 return;
418 } else {
419 selectedRow = lsm.getMinSelectionIndex();
420 }
421 }
422
423 /* (non-Javadoc)
424 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
425 *
426 */
427 public void mouseClicked(MouseEvent arg0) {
428 if (arg0.getClickCount() == 2) {
429 if (this.selectedRow < 0) {
430 return;
431 } else {
432 showEdit(selectedRow);
433 }
434 }
435 }
436
437 /* (non-Javadoc)
438 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
439 *
440 * Override componentResized to resize all components when frame's size is changed
441 */
442 public void componentResized(ComponentEvent arg0) {
443 int intCurrentWidth = this.getJContentPane().getWidth();
444 int intCurrentHeight = this.getJContentPane().getHeight();
445 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
446 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
447
448 resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight);
449 relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
450 DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
451 relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
452 DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
453 relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
454 DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
455 }
456 }