Fixed a spelling error and changed the Package Headers Tab title to be Package Header...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / module / ui / ModulePackageDependencies.java
1 /** @file
2
3 The file is used to create, update Include 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.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
31 import org.tianocore.PackageDependenciesDocument.PackageDependencies;
32 import org.tianocore.PackageDependenciesDocument.PackageDependencies.Package;
33 import org.tianocore.frameworkwizard.common.DataType;
34 import org.tianocore.frameworkwizard.common.IDefaultTableModel;
35 import org.tianocore.frameworkwizard.common.Log;
36 import org.tianocore.frameworkwizard.common.Tools;
37 import org.tianocore.frameworkwizard.common.Identifications.OpeningModuleType;
38 import org.tianocore.frameworkwizard.common.ui.IFrame;
39 import org.tianocore.frameworkwizard.common.ui.IInternalFrame;
40 import org.tianocore.frameworkwizard.module.Identifications.PackageDependencies.PackageDependenciesIdentification;
41 import org.tianocore.frameworkwizard.module.Identifications.PackageDependencies.PackageDependenciesVector;
42 import org.tianocore.frameworkwizard.module.ui.dialog.PackageDepDlg;
43 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
44 import org.tianocore.frameworkwizard.workspace.WorkspaceTools;
45
46 /**
47 The class is used to create, update Include of MSA/MBD file
48 It extends IInternalFrame
49
50
51
52 **/
53 public class ModulePackageDependencies extends IInternalFrame {
54
55 ///
56 /// Define class Serial Version UID
57 ///
58 private static final long serialVersionUID = 3465193035145152131L;
59
60 //
61 //Define class members
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 JScrollPane jScrollPane = null;
72
73 private JScrollPane jScrollPaneTable = null;
74
75 private JTable jTable = null;
76
77 //
78 // Not used by UI
79 //
80 private OpeningModuleType omt = null;
81
82 private ModuleSurfaceArea msa = null;
83
84 private PackageDependencies pd = null;
85
86 private PackageDependenciesIdentification pdid = null;
87
88 private PackageDependenciesVector vid = new PackageDependenciesVector();
89
90 private WorkspaceTools wt = new WorkspaceTools();
91
92 private Vector<PackageIdentification> vPackage = wt.getAllPackages();
93
94 private IDefaultTableModel model = null;
95
96 private int selectedRow = -1;
97
98 /**
99 This method initializes jButtonAdd
100
101 @return javax.swing.JButton jButtonAdd
102
103 **/
104 private JButton getJButtonAdd() {
105 if (jButtonAdd == null) {
106 jButtonAdd = new JButton();
107 jButtonAdd.setBounds(new java.awt.Rectangle(230, 445, 80, 20));
108 jButtonAdd.setText("Add");
109 jButtonAdd.addActionListener(this);
110 jButtonAdd.setPreferredSize(new java.awt.Dimension(80, 20));
111 }
112 return jButtonAdd;
113 }
114
115 /**
116 This method initializes jButtonRemove
117
118 @return javax.swing.JButton jButtonRemove
119
120 **/
121 private JButton getJButtonRemove() {
122 if (jButtonRemove == null) {
123 jButtonRemove = new JButton();
124 jButtonRemove.setBounds(new java.awt.Rectangle(400, 445, 80, 20));
125 jButtonRemove.setText("Remove");
126 jButtonRemove.addActionListener(this);
127 jButtonRemove.setPreferredSize(new java.awt.Dimension(80, 20));
128 }
129 return jButtonRemove;
130 }
131
132 /**
133 This method initializes jButtonUpdate
134
135 @return javax.swing.JButton jButtonUpdate
136
137 **/
138 private JButton getJButtonUpdate() {
139 if (jButtonUpdate == null) {
140 jButtonUpdate = new JButton();
141 jButtonUpdate.setBounds(new java.awt.Rectangle(315, 445, 80, 20));
142 jButtonUpdate.setPreferredSize(new java.awt.Dimension(80, 20));
143 jButtonUpdate.setText("Edit");
144 jButtonUpdate.addActionListener(this);
145 }
146 return jButtonUpdate;
147 }
148
149 /**
150 This method initializes jScrollPane
151
152 @return javax.swing.JScrollPane
153 */
154 private JScrollPane getJScrollPane() {
155 if (jScrollPane == null) {
156 jScrollPane = new JScrollPane();
157 jScrollPane.setViewportView(getJContentPane());
158 }
159 return jScrollPane;
160 }
161
162 /**
163 This method initializes jScrollPaneTable
164
165 @return javax.swing.JScrollPane
166 **/
167 private JScrollPane getJScrollPaneTable() {
168 if (jScrollPaneTable == null) {
169 jScrollPaneTable = new JScrollPane();
170 jScrollPaneTable.setBounds(new java.awt.Rectangle(15, 10, 470, 420));
171 jScrollPaneTable.setPreferredSize(new Dimension(470, 420));
172 jScrollPaneTable.setViewportView(getJTable());
173 }
174 return jScrollPaneTable;
175 }
176
177 /**
178 This method initializes jTable
179
180 @return javax.swing.JTable
181 **/
182 private JTable getJTable() {
183 if (jTable == null) {
184 jTable = new JTable();
185 model = new IDefaultTableModel();
186 jTable = new JTable(model);
187 jTable.setRowHeight(20);
188
189 model.addColumn("Package Name");
190 model.addColumn("Package Version");
191
192 jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
193 jTable.getSelectionModel().addListSelectionListener(this);
194 jTable.getModel().addTableModelListener(this);
195 jTable.addMouseListener(this);
196 }
197 return jTable;
198 }
199
200 public static void main(String[] args) {
201
202 }
203
204 /**
205 This is the default constructor
206
207 **/
208 public ModulePackageDependencies() {
209 super();
210 init();
211 this.setVisible(true);
212 }
213
214 /**
215 This is the override edit constructor
216
217 @param
218
219 **/
220 public ModulePackageDependencies(OpeningModuleType inOmt) {
221 super();
222 this.omt = inOmt;
223 this.msa = omt.getXmlMsa();
224 init(msa.getPackageDependencies());
225 this.setVisible(true);
226 }
227
228 /**
229 This method initializes this
230
231 **/
232 private void init() {
233 this.setSize(500, 515);
234 this.setContentPane(getJScrollPane());
235 this.setTitle("Package Dependencies");
236 }
237
238 /**
239 This method initializes this
240 Fill values to all fields if these values are not empty
241
242 @param inPackageDependencies
243
244 **/
245 private void init(PackageDependencies inPackageDependencies) {
246 init();
247 this.pd = inPackageDependencies;
248
249 if (this.pd != null) {
250 if (this.pd.getPackageList().size() > 0) {
251 for (int index = 0; index < this.pd.getPackageList().size(); index++) {
252 String arg0 = "";
253 String arg1 = pd.getPackageList().get(index).getPackageVersion();
254 String arg2 = pd.getPackageList().get(index).getPackageGuid();
255 //
256 // If no guid, skip current item
257 //
258 if (arg2 == null) {
259 continue;
260 }
261 for (int indexJ = 0; indexJ < this.vPackage.size(); indexJ++) {
262 if (vPackage.get(indexJ).getGuid().equals(arg2)) {
263 arg0 = vPackage.get(indexJ).getName();
264 }
265 }
266 String arg3 = pd.getPackageList().get(index).getFeatureFlag();
267 Vector<String> arg4 = Tools.convertListToVector(pd.getPackageList().get(index).getSupArchList());
268 pdid = new PackageDependenciesIdentification(arg0, arg1, arg2, arg3, arg4);
269 vid.addPackageDependencies(pdid);
270 }
271 }
272 }
273 showTable();
274 }
275
276 /**
277 This method initializes jContentPane
278
279 @return javax.swing.JPanel jContentPane
280
281 **/
282 private JPanel getJContentPane() {
283 if (jContentPane == null) {
284 jContentPane = new JPanel();
285 jContentPane.setLayout(null);
286 jContentPane.setPreferredSize(new java.awt.Dimension(490, 490));
287
288 jContentPane.add(getJButtonAdd(), null);
289 jContentPane.add(getJButtonRemove(), null);
290 jContentPane.add(getJButtonUpdate(), null);
291 jContentPane.add(getJScrollPaneTable(), null);
292 }
293 return jContentPane;
294 }
295
296 private void showEdit(int index) {
297 PackageDepDlg pdd = new PackageDepDlg(vid.getPackageDependencies(index), new IFrame());
298 int result = pdd.showDialog();
299 if (result == DataType.RETURN_TYPE_OK) {
300 if (index == -1) {
301 this.vid.addPackageDependencies(pdd.getPdid());
302 } else {
303 this.vid.setPackageDependencies(pdd.getPdid(), index);
304 }
305 this.showTable();
306 this.save();
307 pdd.dispose();
308 }
309 if (result == DataType.RETURN_TYPE_CANCEL) {
310 pdd.dispose();
311 }
312 }
313
314 /**
315 Clear all table rows
316
317 **/
318 private void clearAll() {
319 if (model != null) {
320 for (int index = model.getRowCount() - 1; index >= 0; index--) {
321 model.removeRow(index);
322 }
323 }
324 }
325
326 /**
327 Read content of vector and put then into table
328
329 **/
330 private void showTable() {
331 clearAll();
332
333 if (vid.size() > 0) {
334 for (int index = 0; index < vid.size(); index++) {
335 model.addRow(vid.toStringVector(index));
336 }
337 }
338 this.jTable.repaint();
339 this.jTable.updateUI();
340 //this.jScrollPane.setViewportView(this.jTable);
341 }
342
343 /* (non-Javadoc)
344 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
345 *
346 * Override actionPerformed to listen all actions
347 *
348 */
349 public void actionPerformed(ActionEvent arg0) {
350 if (arg0.getSource() == jButtonAdd) {
351 showEdit(-1);
352 }
353 if (arg0.getSource() == jButtonUpdate) {
354 if (this.selectedRow < 0) {
355 Log.err("Please select one record first.");
356 return;
357 }
358 showEdit(selectedRow);
359 }
360
361 if (arg0.getSource() == jButtonRemove) {
362 if (jTable.isEditing()) {
363 jTable.getCellEditor().stopCellEditing();
364 }
365 if (selectedRow > -1) {
366 this.model.removeRow(selectedRow);
367 this.vid.removePackageDependencies(selectedRow);
368 selectedRow = -1;
369 this.save();
370 }
371 }
372 }
373
374 /**
375 Save all components of Includes
376 if exists includes, set the value directly
377 if not exists includes, new an instance first
378
379 **/
380 public void save() {
381 try {
382 //
383 //Save as file name
384 //
385 int count = this.vid.size();
386
387 this.pd = PackageDependencies.Factory.newInstance();
388 if (count > 0) {
389 for (int index = 0; index < count; index++) {
390 Package p = Package.Factory.newInstance();
391
392 if (!isEmpty(vid.getPackageDependencies(index).getVersion())) {
393 p.setPackageVersion(vid.getPackageDependencies(index).getVersion());
394 }
395 if (!isEmpty(vid.getPackageDependencies(index).getGuid())) {
396 p.setPackageGuid(vid.getPackageDependencies(index).getGuid());
397 }
398 if (!isEmpty(vid.getPackageDependencies(index).getFeatureFlag())) {
399 p.setFeatureFlag(vid.getPackageDependencies(index).getFeatureFlag());
400 }
401 if (vid.getPackageDependencies(index).getSupArchList() != null
402 && vid.getPackageDependencies(index).getSupArchList().size() > 0) {
403 p.setSupArchList(vid.getPackageDependencies(index).getSupArchList());
404 }
405
406 this.pd.addNewPackage();
407 this.pd.setPackageArray(index, p);
408 }
409 }
410 this.msa.setPackageDependencies(pd);
411 this.omt.setSaved(false);
412 } catch (Exception e) {
413 e.printStackTrace();
414 Log.err("Update Package Dependencies", e.getMessage());
415 }
416 }
417
418 /* (non-Javadoc)
419 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
420 *
421 */
422 public void valueChanged(ListSelectionEvent arg0) {
423 if (arg0.getValueIsAdjusting()) {
424 return;
425 }
426 ListSelectionModel lsm = (ListSelectionModel) arg0.getSource();
427 if (lsm.isSelectionEmpty()) {
428 return;
429 } else {
430 selectedRow = lsm.getMinSelectionIndex();
431 }
432 }
433
434 /* (non-Javadoc)
435 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
436 *
437 */
438 public void mouseClicked(MouseEvent arg0) {
439 if (arg0.getClickCount() == 2) {
440 if (this.selectedRow < 0) {
441 return;
442 } else {
443 showEdit(selectedRow);
444 }
445 }
446 }
447
448 /* (non-Javadoc)
449 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
450 *
451 * Override componentResized to resize all components when frame's size is changed
452 */
453 public void componentResized(ComponentEvent arg0) {
454 int intCurrentWidth = this.getJContentPane().getWidth();
455 int intCurrentHeight = this.getJContentPane().getHeight();
456 int intPreferredWidth = this.getJContentPane().getPreferredSize().width;
457 int intPreferredHeight = this.getJContentPane().getPreferredSize().height;
458
459 resizeComponent(this.jScrollPaneTable, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight);
460 relocateComponent(this.jButtonAdd, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
461 DataType.SPACE_TO_RIGHT_FOR_ADD_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_ADD_BUTTON);
462 relocateComponent(this.jButtonRemove, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
463 DataType.SPACE_TO_RIGHT_FOR_REMOVE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_REMOVE_BUTTON);
464 relocateComponent(this.jButtonUpdate, intCurrentWidth, intCurrentHeight, intPreferredWidth, intPreferredHeight,
465 DataType.SPACE_TO_RIGHT_FOR_UPDATE_BUTTON, DataType.SPACE_TO_BOTTOM_FOR_UPDATE_BUTTON);
466 }
467 }