]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/module/ui/ModuleSourceFiles.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 / ModuleSourceFiles.java
1 /** @file
2
3 The file is used to create, update SourceFile 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.JCheckBox;
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.SourceFilesDocument;
32 import org.tianocore.FilenameDocument.Filename;
33 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
34 import org.tianocore.SourceFilesDocument.SourceFiles;
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.SourceFiles.SourceFilesIdentification;
43 import org.tianocore.frameworkwizard.module.Identifications.SourceFiles.SourceFilesVector;
44 import org.tianocore.frameworkwizard.module.ui.dialog.SourceFilesDlg;
45
46 /**
47 The class is used to create, update SourceFile of MSA/MBD file
48 It extends IInternalFrame
49
50 **/
51 public class ModuleSourceFiles extends IInternalFrame {
52
53 ///
54 /// Define class Serial Version UID
55 ///
56 private static final long serialVersionUID = -6765742852142775378L;
57
58 //
59 // Define class members
60 //
61 private SourceFilesDocument.SourceFiles sourceFiles = null;
62
63 private JPanel jContentPane = null;
64
65 private JButton jButtonAdd = null;
66
67 private JButton jButtonRemove = null;
68
69 private JButton jButtonUpdate = null;
70
71 private JCheckBox jCheckBoxArch = null;
72
73 private JScrollPane jScrollPane = null;
74
75 private JScrollPane jScrollPaneTable = null;
76
77 private JTable jTable = null;
78
79 //
80 // Not used by UI
81 //
82 private OpeningModuleType omt = null;
83
84 private ModuleSurfaceArea msa = null;
85
86 private SourceFilesVector vSourceFiles = new SourceFilesVector();
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, 220, 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, 220, 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, 220, 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("File Name");
186 model.addColumn("Tag Name");
187 model.addColumn("Tool Code");
188 model.addColumn("Tool Chain Family");
189
190 jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
191 jTable.getSelectionModel().addListSelectionListener(this);
192 jTable.getModel().addTableModelListener(this);
193 jTable.addMouseListener(this);
194 }
195 return jTable;
196 }
197
198 public static void main(String[] args) {
199
200 }
201
202 /**
203 This is the default constructor
204
205 **/
206 public ModuleSourceFiles() {
207 super();
208 init();
209 this.setVisible(true);
210 }
211
212 /**
213 This is the override edit constructor
214
215 @param
216
217 **/
218 public ModuleSourceFiles(OpeningModuleType inOmt, IFrame iFrame) {
219 super();
220 this.omt = inOmt;
221 this.msa = omt.getXmlMsa();
222 this.parentFrame = iFrame;
223 init(msa.getSourceFiles());
224 this.setVisible(true);
225 }
226
227 /**
228 This method initializes this
229 Fill values to all fields if these values are not empty
230
231
232 @param inSourceFiles The input data of SourceFilesDocument.SourceFiles
233
234 **/
235 private void init(SourceFilesDocument.SourceFiles inSourceFiles) {
236 init();
237 this.sourceFiles = inSourceFiles;
238
239 if (this.sourceFiles != null) {
240 if (this.sourceFiles.getFilenameList().size() > 0) {
241 for (int index = 0; index < this.sourceFiles.getFilenameList().size(); index++) {
242 String name = sourceFiles.getFilenameList().get(index).getStringValue();
243 String tagName = sourceFiles.getFilenameList().get(index).getTagName();
244 String toolCode = sourceFiles.getFilenameList().get(index).getToolCode();
245 String tcf = sourceFiles.getFilenameList().get(index).getToolChainFamily();
246 String featureFlag = sourceFiles.getFilenameList().get(index).getFeatureFlag();
247 Vector<String> arch = Tools.convertListToVector(sourceFiles.getFilenameList().get(index)
248 .getSupArchList());
249 SourceFilesIdentification sfid = new SourceFilesIdentification(name, tagName, toolCode, tcf,
250 featureFlag, arch);
251 vSourceFiles.addSourceFiles(sfid);
252 }
253 }
254 }
255 showTable();
256 }
257
258 /**
259 This method initializes this
260
261 **/
262 private void init() {
263 this.setSize(500, 515);
264 this.setContentPane(getJScrollPane());
265 this.setTitle("Source Files");
266 this.setViewMode(false);
267 }
268
269 /**
270 Disable all components when the mode is view
271
272 @param isView true - The view mode; false - The non-view mode
273
274 **/
275 public void setViewMode(boolean isView) {
276 if (isView) {
277 this.jButtonAdd.setEnabled(!isView);
278 this.jButtonRemove.setEnabled(!isView);
279 this.jButtonUpdate.setEnabled(!isView);
280 this.jCheckBoxArch.setEnabled(!isView);
281 }
282 }
283
284 /**
285 This method initializes jContentPane
286
287 @return javax.swing.JPanel jContentPane
288
289 **/
290 private JPanel getJContentPane() {
291 if (jContentPane == null) {
292 jContentPane = new JPanel();
293 jContentPane.setLayout(null);
294 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
295
296 jContentPane.add(getJButtonAdd(), null);
297 jContentPane.add(getJButtonRemove(), null);
298 jContentPane.add(getJButtonUpdate(), null);
299 jContentPane.add(getJScrollPaneTable(), null);
300 }
301 return jContentPane;
302 }
303
304 /**
305 Save all components of SourceFiles
306 if exists sourceFiles, set the value directly
307 if not exists sourceFiles, new an instance first
308
309 **/
310 public void save() {
311 try {
312 //
313 //Save as file name
314 //
315 int count = this.vSourceFiles.size();
316
317 this.sourceFiles = SourceFiles.Factory.newInstance();
318 if (count > 0) {
319 for (int index = 0; index < count; index++) {
320 Filename mFilename = Filename.Factory.newInstance();
321 if (!isEmpty(vSourceFiles.getSourceFiles(index).getFilename())) {
322 mFilename.setStringValue(vSourceFiles.getSourceFiles(index).getFilename());
323 }
324 if (!isEmpty(vSourceFiles.getSourceFiles(index).getTagName())) {
325 mFilename.setTagName(vSourceFiles.getSourceFiles(index).getTagName());
326 }
327 if (!isEmpty(vSourceFiles.getSourceFiles(index).getToolCode())) {
328 mFilename.setToolCode(vSourceFiles.getSourceFiles(index).getToolCode());
329 }
330 if (!isEmpty(vSourceFiles.getSourceFiles(index).getToolChainFamily())) {
331 mFilename.setToolChainFamily(vSourceFiles.getSourceFiles(index).getToolChainFamily());
332 }
333 if (!isEmpty(vSourceFiles.getSourceFiles(index).getFeatureFlag())) {
334 mFilename.setFeatureFlag(vSourceFiles.getSourceFiles(index).getFeatureFlag());
335 }
336 if (vSourceFiles.getSourceFiles(index).getSupArchList() != null
337 && vSourceFiles.getSourceFiles(index).getSupArchList().size() > 0) {
338 mFilename.setSupArchList(vSourceFiles.getSourceFiles(index).getSupArchList());
339 }
340
341 this.sourceFiles.addNewFilename();
342 this.sourceFiles.setFilenameArray(index, mFilename);
343 }
344 }
345 this.msa.setSourceFiles(sourceFiles);
346 this.omt.setSaved(false);
347 } catch (Exception e) {
348 Log.wrn("Update Source Files", e.getMessage());
349 Log.err("Update Source Files", e.getMessage());
350 }
351 }
352
353 private void showEdit(int index) {
354 SourceFilesDlg sfd = new SourceFilesDlg(this.vSourceFiles.getSourceFiles(index), this.parentFrame, omt.getId());
355 int result = sfd.showDialog();
356 if (result == DataType.RETURN_TYPE_OK) {
357 if (index == -1) {
358 for (int indexI = 0; indexI < sfd.getSfid().length; indexI++) {
359 this.vSourceFiles.addSourceFiles(sfd.getSfid()[indexI]);
360 }
361 } else {
362 this.vSourceFiles.setSourceFiles(sfd.getSfid()[0], index);
363 }
364 this.showTable();
365 this.save();
366 sfd.dispose();
367 }
368 if (result == DataType.RETURN_TYPE_CANCEL) {
369 sfd.dispose();
370 }
371 }
372
373 /* (non-Javadoc)
374 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
375 *
376 * Override actionPerformed to listen all actions
377 *
378 */
379 public void actionPerformed(ActionEvent arg0) {
380 if (arg0.getSource() == jButtonAdd) {
381 showEdit(-1);
382 }
383 if (arg0.getSource() == jButtonUpdate) {
384 if (this.selectedRow < 0) {
385 Log.wrn("Update Source Files", "Please select one record first.");
386 return;
387 }
388 showEdit(selectedRow);
389 }
390
391 if (arg0.getSource() == jButtonRemove) {
392 if (jTable.isEditing()) {
393 jTable.getCellEditor().stopCellEditing();
394 }
395
396 int selectedRows[] = this.jTable.getSelectedRows();
397
398 if (selectedRows != null) {
399 for (int index = selectedRows.length - 1; index > -1; index--) {
400 this.model.removeRow(selectedRows[index]);
401 this.vSourceFiles.removeSourceFiles(selectedRows[index]);
402 }
403 selectedRow = -1;
404 this.save();
405 }
406 }
407 }
408
409 /**
410 Clear all table rows
411
412 **/
413 private void clearAll() {
414 if (model != null) {
415 for (int index = model.getRowCount() - 1; index >= 0; index--) {
416 model.removeRow(index);
417 }
418 }
419 }
420
421 /**
422 Read content of vector and put then into table
423
424 **/
425 private void showTable() {
426 clearAll();
427
428 if (vSourceFiles.size() > 0) {
429 for (int index = 0; index < vSourceFiles.size(); index++) {
430 model.addRow(vSourceFiles.toStringVector(index));
431 }
432 }
433 this.jTable.repaint();
434 this.jTable.updateUI();
435 //this.jScrollPane.setViewportView(this.jTable);
436 }
437
438 /* (non-Javadoc)
439 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
440 *
441 */
442 public void valueChanged(ListSelectionEvent arg0) {
443 if (arg0.getValueIsAdjusting()) {
444 return;
445 }
446 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
447 if (lsm.isSelectionEmpty()) {
448 return;
449 } else {
450 selectedRow = lsm.getMinSelectionIndex();
451 }
452 }
453
454 /* (non-Javadoc)
455 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
456 *
457 */
458 public void mouseClicked(MouseEvent arg0) {
459 if (arg0.getClickCount() == 2) {
460 if (this.selectedRow < 0) {
461 return;
462 } else {
463 showEdit(selectedRow);
464 }
465 }
466 }
467
468 /* (non-Javadoc)
469 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
470 *
471 * Override componentResized to resize all components when frame's size is changed
472 */
473 public void componentResized(ComponentEvent arg0) {
474 int intCurrentWidth = this.getJContentPane().getWidth();
475 int intCurrentHeight = this.getJContentPane().getHeight();
476 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
477 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
478
479 Tools.resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth,
480 intPreferredHeight);
481 Tools.relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth,
482 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON,
483 DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
484 Tools.relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth,
485 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON,
486 DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
487 Tools.relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth,
488 intPreferredHeight, DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON,
489 DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
490 }
491 }