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