]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/installui/InstallStepTwo.java
1. Adjust UI for far operations
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / far / installui / InstallStepTwo.java
1 /** @file
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 package org.tianocore.frameworkwizard.far.installui;
15
16 import java.awt.Color;
17 import java.awt.Dimension;
18 import java.awt.Toolkit;
19 import java.awt.event.MouseEvent;
20 import java.awt.event.MouseListener;
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import javax.swing.JPanel;
31 import javax.swing.JTextArea;
32 import javax.swing.JButton;
33 import javax.swing.JLabel;
34 import javax.swing.JScrollPane;
35 import javax.swing.ListSelectionModel;
36 import javax.swing.table.DefaultTableModel;
37 import javax.swing.JTable;
38
39 import org.tianocore.frameworkwizard.common.DataType;
40 import org.tianocore.frameworkwizard.common.Log;
41 import org.tianocore.frameworkwizard.common.Tools;
42 import org.tianocore.frameworkwizard.common.ui.IDialog;
43 import org.tianocore.frameworkwizard.far.Far;
44 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
45 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
46 import org.tianocore.frameworkwizard.workspace.Workspace;
47 import org.tianocore.frameworkwizard.workspace.WorkspaceTools;
48
49 public class InstallStepTwo extends IDialog implements MouseListener {
50
51 /**
52 *
53 */
54 private static final long serialVersionUID = 4583090421587036969L;
55
56 private JPanel jContentPane = null;
57
58 private JTextArea jTextArea = null;
59
60 private PartialEditableTableModel packageModel = null;
61
62 private PartialEditableTableModel platformModel = null;
63
64 private InstallStepOne stepOne = null;
65
66 private JButton jButtonCancel = null;
67
68 private JButton jButtonFinish = null;
69
70 private JButton jButtonPrevious = null;
71
72 private JLabel jLabel = null;
73
74 private JScrollPane jScrollPane = null;
75
76 private JTable jTablePackage = null;
77
78 private JLabel jLabel1 = null;
79
80 private JScrollPane jScrollPane1 = null;
81
82 private JTable jTablePlatform = null;
83
84 List<PlatformIdentification> platformVector = null;
85
86 List<PackageIdentification> packageVector = null;
87
88 public InstallStepTwo(IDialog iDialog, boolean modal, InstallStepOne stepOne) {
89 this(iDialog, modal);
90 this.stepOne = stepOne;
91 }
92
93 /**
94 * This method initializes jTextArea
95 *
96 * @return javax.swing.JTextArea
97 */
98 private JTextArea getJTextArea() {
99 if (jTextArea == null) {
100 jTextArea = new JTextArea();
101 jTextArea.setBounds(new java.awt.Rectangle(30, 7, 642, 50));
102 jTextArea.setText("Step 2: Set Path for Packages and Platforms. \n");
103 jTextArea.setCaretColor(Color.RED);
104 jTextArea.append("Note that path is relative to WORKSPACE. ");
105 jTextArea.setEditable(false);
106 }
107 return jTextArea;
108 }
109
110 /**
111 * This method initializes jButtonCancel
112 *
113 * @return javax.swing.JButton
114 */
115 private JButton getJButtonCancel() {
116 if (jButtonCancel == null) {
117 jButtonCancel = new JButton();
118 jButtonCancel.setBounds(new java.awt.Rectangle(570, 330, 90, 20));
119 jButtonCancel.setText("Cancel");
120 jButtonCancel.addMouseListener(this);
121 }
122 return jButtonCancel;
123 }
124
125 /**
126 * This method initializes jButtonFinish
127 *
128 * @return javax.swing.JButton
129 */
130 private JButton getJButtonFinish() {
131 if (jButtonFinish == null) {
132 jButtonFinish = new JButton();
133 jButtonFinish.setBounds(new java.awt.Rectangle(470, 330, 90, 20));
134 jButtonFinish.setText("Finish");
135 jButtonFinish.addMouseListener(this);
136 }
137 return jButtonFinish;
138 }
139
140 /**
141 * This method initializes jButtonPrevious
142 *
143 * @return javax.swing.JButton
144 */
145 private JButton getJButtonPrevious() {
146 if (jButtonPrevious == null) {
147 jButtonPrevious = new JButton();
148 jButtonPrevious.setBounds(new java.awt.Rectangle(370, 330, 90, 20));
149 jButtonPrevious.setText("Previous");
150 jButtonPrevious.addMouseListener(this);
151 }
152 return jButtonPrevious;
153 }
154
155 /**
156 * This method initializes jScrollPane
157 *
158 * @return javax.swing.JScrollPane
159 */
160 private JScrollPane getJScrollPane() {
161 if (jScrollPane == null) {
162 jScrollPane = new JScrollPane();
163 jScrollPane.setBounds(new java.awt.Rectangle(30, 80, 642, 110));
164 jScrollPane.setViewportView(getJTablePackage());
165 }
166 return jScrollPane;
167 }
168
169 /**
170 * This method initializes jTable
171 *
172 * @return javax.swing.JTable
173 */
174 private JTable getJTablePackage() {
175 if (jTablePackage == null) {
176 jTablePackage = new JTable();
177 packageModel = new PartialEditableTableModel();
178 jTablePackage = new JTable(packageModel);
179 jTablePackage.setRowHeight(20);
180 jTablePackage.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
181 packageModel.addColumn("Name");
182 packageModel.addColumn("Version");
183 packageModel.addColumn("Default Path");
184 packageModel.addColumn("Install To");
185
186 jTablePackage.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
187 }
188 return jTablePackage;
189 }
190
191 public void preparePackageTable() {
192 packageModel.setRowCount(0);
193 //
194 // Change here to get packages and platforms from FAR
195 //
196 try {
197 Far far = stepOne.getFar();
198
199 packageVector = far.mainfest.getPackageList();
200 Iterator<PackageIdentification> iter = packageVector.iterator();
201 while (iter.hasNext()) {
202 String[] str = new String[4];
203 PackageIdentification item = iter.next();
204 str[0] = item.getName();
205 str[1] = item.getVersion();
206 str[2] = Tools.getFilePathOnly(Tools.getRelativePath(item.getPath(), Workspace.getCurrentWorkspace()));
207 str[3] = Tools.getFilePathOnly(Tools.getRelativePath(item.getPath(), Workspace.getCurrentWorkspace()));
208 packageModel.addRow(str);
209 }
210 } catch (Exception e) {
211 }
212 }
213
214 /**
215 * This method initializes jScrollPane1
216 *
217 * @return javax.swing.JScrollPane
218 */
219 private JScrollPane getJScrollPane1() {
220 if (jScrollPane1 == null) {
221 jScrollPane1 = new JScrollPane();
222 jScrollPane1.setBounds(new java.awt.Rectangle(30, 215, 642, 110));
223 jScrollPane1.setViewportView(getJTablePlatform());
224 }
225 return jScrollPane1;
226 }
227
228 /**
229 * This method initializes jTablePlatform
230 *
231 * @return javax.swing.JTable
232 */
233 private JTable getJTablePlatform() {
234 if (jTablePlatform == null) {
235 jTablePlatform = new JTable();
236 platformModel = new PartialEditableTableModel();
237 jTablePlatform = new JTable(platformModel);
238 jTablePlatform.setRowHeight(20);
239 jTablePlatform.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
240 platformModel.addColumn("Name");
241 platformModel.addColumn("Version");
242 platformModel.addColumn("Default Path");
243 platformModel.addColumn("Install To");
244
245 jTablePlatform.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
246 preparePlatformTable();
247 }
248 return jTablePlatform;
249 }
250
251 public void preparePlatformTable() {
252 platformModel.setRowCount(0);
253 //
254 // Change here to get packages and platforms from FAR
255 //
256 try {
257 Far far = stepOne.getFar();
258
259 platformVector = far.mainfest.getPlatformList();
260 Iterator<PlatformIdentification> iter = platformVector.iterator();
261 while (iter.hasNext()) {
262 String[] str = new String[4];
263 PlatformIdentification item = iter.next();
264 str[0] = item.getName();
265 str[1] = item.getVersion();
266 str[2] = Tools.getFilePathOnly(Tools.getRelativePath(item.getPath(), Workspace.getCurrentWorkspace()));
267 str[3] = Tools.getFilePathOnly(Tools.getRelativePath(item.getPath(), Workspace.getCurrentWorkspace()));
268 platformModel.addRow(str);
269 }
270 } catch (Exception e) {
271 }
272 }
273
274 /**
275 * This is the default constructor
276 */
277 public InstallStepTwo(IDialog iDialog, boolean modal) {
278 super(iDialog, modal);
279 initialize();
280 }
281
282 /**
283 * This method initializes this
284 *
285 * @return void
286 */
287 private void initialize() {
288 this.setSize(700, 400);
289 this.setContentPane(getJContentPane());
290 this.setTitle("Install Framework Archive(FAR) - Step 2: Set Path for Packages and Platforms");
291 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
292 this.setLocation((d.width - this.getSize().width) / 2, (d.height - this.getSize().height) / 2);
293 }
294
295 /**
296 * This method initializes jContentPane
297 *
298 * @return javax.swing.JPanel
299 */
300 private JPanel getJContentPane() {
301 if (jContentPane == null) {
302 jLabel1 = new JLabel();
303 jLabel1.setBounds(new java.awt.Rectangle(30, 195, 348, 18));
304 jLabel1.setText("Edit pathes for platforms: ");
305 jLabel = new JLabel();
306 jLabel.setBounds(new java.awt.Rectangle(29, 60, 366, 20));
307 jLabel.setText("Edit pathes for packages");
308 jContentPane = new JPanel();
309 jContentPane.setLayout(null);
310 jContentPane.add(getJTextArea(), null);
311 jContentPane.add(getJButtonCancel(), null);
312 jContentPane.add(getJButtonFinish(), null);
313 jContentPane.add(getJButtonPrevious(), null);
314 jContentPane.add(jLabel, null);
315 jContentPane.add(getJScrollPane(), null);
316 jContentPane.add(jLabel1, null);
317 jContentPane.add(getJScrollPane1(), null);
318 }
319 return jContentPane;
320 }
321
322 public void mouseClicked(MouseEvent e) {
323 if (e.getSource() == jButtonCancel) {
324 this.setVisible(false);
325 this.dispose();
326 } else if (e.getSource() == jButtonFinish) {
327 List<String> packageList = new ArrayList<String>();
328 List<String> platformList = new ArrayList<String>();
329 //
330 // Add some logic process here
331 // Guid Check, File Check etc.
332 //
333 Set<File> allNewPath = new LinkedHashSet<File>();
334 Map<PackageIdentification, File> packageMap = new LinkedHashMap<PackageIdentification, File>();
335 for (int i = 0; i < packageModel.getRowCount(); i++) {
336 File toFile = new File(Workspace.getCurrentWorkspace() + File.separatorChar
337 + packageModel.getValueAt(i, 3));
338 if (!isPackagePathValid(toFile)) {
339 Log.err(packageVector.get(i) + " path already has package now. ");
340 return;
341 }
342 if (allNewPath.contains(toFile)) {
343 Log.err("Path " + packageModel.getValueAt(i, 3) + " is specified by twice. ");
344 return;
345 }
346 allNewPath.add(toFile);
347 File spdFile = new File((String) packageModel.getValueAt(i, 3) + File.separatorChar
348 + packageVector.get(i).getSpdFile().getName());
349 packageList.add(spdFile.getPath());
350 packageMap.put(packageVector.get(i), toFile);
351 }
352
353 Map<PlatformIdentification, File> platformMap = new LinkedHashMap<PlatformIdentification, File>();
354 for (int i = 0; i < platformModel.getRowCount(); i++) {
355 File toFile = new File(Workspace.getCurrentWorkspace() + File.separatorChar
356 + platformModel.getValueAt(i, 3));
357 if (!isPlatformPathValid(toFile)) {
358 Log.err(platformVector.get(i) + " path already has platform now. ");
359 return;
360 }
361 File fpdFile = new File((String) platformModel.getValueAt(i, 3) + File.separatorChar
362 + platformVector.get(i).getFpdFile().getName());
363 platformList.add(fpdFile.getPath());
364 platformMap.put(platformVector.get(i), toFile);
365 }
366
367 //
368 //
369 //
370 Far far = stepOne.getFar();
371 try {
372 far.InstallFar(platformMap, packageMap);
373 //
374 // Add to database
375 //
376 WorkspaceTools wt = new WorkspaceTools();
377 wt.addFarToDb(packageList, platformList, far.mainfest.getHeader());
378 } catch (Exception ex) {
379 ex.printStackTrace();
380 Log.err("Install error. ");
381 return;
382 }
383
384 this.setVisible(false);
385 this.stepOne.returnType = DataType.RETURN_TYPE_OK;
386 this.dispose();
387 } else if (e.getSource() == jButtonPrevious) {
388 this.setVisible(false);
389 stepOne.setVisible(true);
390 }
391 }
392
393 private boolean isPackagePathValid(File spdFile) {
394 WorkspaceTools wt = new WorkspaceTools();
395 List<PackageIdentification> allPackages = wt.getAllPackages();
396 Iterator<PackageIdentification> iter = allPackages.iterator();
397
398 while (iter.hasNext()) {
399 PackageIdentification item = iter.next();
400 if (isPathContainMutual(spdFile, item.getSpdFile())) {
401 return false;
402 }
403 }
404 return true;
405 }
406
407 private boolean isPlatformPathValid(File fpdFile) {
408 WorkspaceTools wt = new WorkspaceTools();
409 List<PlatformIdentification> allPlatforms = wt.getAllPlatforms();
410 Iterator<PlatformIdentification> iter = allPlatforms.iterator();
411
412 while (iter.hasNext()) {
413 PlatformIdentification item = iter.next();
414 if (isPathContainMutual(fpdFile, item.getFpdFile())) {
415 return false;
416 }
417 }
418 return true;
419 }
420
421 private boolean isPathContainMutual(File path1, File path2) {
422 if (path1.getPath().startsWith(path2.getParent())) {
423 return true;
424 }
425 if (path2.getPath().startsWith(path1.getPath())) {
426 return true;
427 }
428 return false;
429 }
430
431 public void mousePressed(MouseEvent e) {
432 // TODO Auto-generated method stub
433
434 }
435
436 public void mouseReleased(MouseEvent e) {
437 // TODO Auto-generated method stub
438
439 }
440
441 public void mouseEntered(MouseEvent e) {
442 // TODO Auto-generated method stub
443
444 }
445
446 public void mouseExited(MouseEvent e) {
447 // TODO Auto-generated method stub
448
449 }
450
451 }
452
453 class PartialEditableTableModel extends DefaultTableModel {
454 /**
455 *
456 */
457 private static final long serialVersionUID = 1L;
458
459 public boolean isCellEditable(int row, int col) {
460 switch (col) {
461 case 3:
462 return true;
463 default:
464 return false;
465 }
466 }
467 }