]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/installui/InstallStepTwo.java
1. Wrap text by word when showing a message box
[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
328 if (jTablePackage.isEditing()) {
329 jTablePackage.getCellEditor().stopCellEditing();
330 }
331
332 if (jTablePlatform.isEditing()) {
333 jTablePlatform.getCellEditor().stopCellEditing();
334 }
335
336 List<String> packageList = new ArrayList<String>();
337 List<String> platformList = new ArrayList<String>();
338 //
339 // Add some logic process here
340 // Guid Check, File Check etc.
341 //
342 Set<File> allNewPath = new LinkedHashSet<File>();
343 Map<PackageIdentification, File> packageMap = new LinkedHashMap<PackageIdentification, File>();
344 for (int i = 0; i < packageModel.getRowCount(); i++) {
345 File toFile = new File(Workspace.getCurrentWorkspace() + File.separatorChar
346 + packageModel.getValueAt(i, 3));
347 if (!isPackagePathValid(toFile)) {
348 Log.wrn("Install far", packageVector.get(i) + " path already has package now. ");
349 return;
350 }
351 if (allNewPath.contains(toFile)) {
352 Log.wrn("Install far", "Path " + packageModel.getValueAt(i, 3) + " is specified by twice. ");
353 return;
354 }
355 allNewPath.add(toFile);
356 File spdFile = new File((String) packageModel.getValueAt(i, 3) + File.separatorChar
357 + packageVector.get(i).getSpdFile().getName());
358 packageList.add(spdFile.getPath());
359 packageMap.put(packageVector.get(i), toFile);
360 }
361
362 Map<PlatformIdentification, File> platformMap = new LinkedHashMap<PlatformIdentification, File>();
363 for (int i = 0; i < platformModel.getRowCount(); i++) {
364 File toFile = new File(Workspace.getCurrentWorkspace() + File.separatorChar
365 + platformModel.getValueAt(i, 3));
366 if (!isPlatformPathValid(toFile)) {
367 Log.wrn("Install far", platformVector.get(i) + " path already has platform now. ");
368 return;
369 }
370 File fpdFile = new File((String) platformModel.getValueAt(i, 3) + File.separatorChar
371 + platformVector.get(i).getFpdFile().getName());
372 platformList.add(fpdFile.getPath());
373 platformMap.put(platformVector.get(i), toFile);
374 }
375
376 //
377 //
378 //
379 Far far = stepOne.getFar();
380 try {
381 far.InstallFar(platformMap, packageMap);
382 //
383 // Add to database
384 //
385 WorkspaceTools wt = new WorkspaceTools();
386 wt.addFarToDb(packageList, platformList, far.mainfest.getHeader());
387 } catch (Exception ex) {
388 Log.wrn("Install far", ex.getMessage());
389 Log.err("Install far", ex.getMessage());
390 return;
391 }
392
393 this.setVisible(false);
394 this.stepOne.returnType = DataType.RETURN_TYPE_OK;
395 this.dispose();
396 } else if (e.getSource() == jButtonPrevious) {
397 this.setVisible(false);
398 stepOne.setVisible(true);
399 }
400 }
401
402 private boolean isPackagePathValid(File spdFile) {
403 WorkspaceTools wt = new WorkspaceTools();
404 List<PackageIdentification> allPackages = wt.getAllPackages();
405 Iterator<PackageIdentification> iter = allPackages.iterator();
406
407 while (iter.hasNext()) {
408 PackageIdentification item = iter.next();
409 if (isPathContainMutual(spdFile, item.getSpdFile())) {
410 return false;
411 }
412 }
413 return true;
414 }
415
416 private boolean isPlatformPathValid(File fpdFile) {
417 WorkspaceTools wt = new WorkspaceTools();
418 List<PlatformIdentification> allPlatforms = wt.getAllPlatforms();
419 Iterator<PlatformIdentification> iter = allPlatforms.iterator();
420
421 while (iter.hasNext()) {
422 PlatformIdentification item = iter.next();
423 if (isPathContainMutual(fpdFile, item.getFpdFile())) {
424 return false;
425 }
426 }
427 return true;
428 }
429
430 private boolean isPathContainMutual(File path1, File path2) {
431 String s1 = Tools.addFileSeparator(path1.getPath());
432 String s2 = Tools.addFileSeparator(path2.getParent());
433
434 if (s1.length() > s2.length()) {
435 if (s1.substring(0, s2.length()).equalsIgnoreCase(s2)) {
436 return true;
437 }
438 } else {
439 if (s2.substring(0, s1.length()).equalsIgnoreCase(s1)) {
440 return true;
441 }
442 }
443 return false;
444 }
445
446 public void mousePressed(MouseEvent e) {
447 // TODO Auto-generated method stub
448
449 }
450
451 public void mouseReleased(MouseEvent e) {
452 // TODO Auto-generated method stub
453
454 }
455
456 public void mouseEntered(MouseEvent e) {
457 // TODO Auto-generated method stub
458
459 }
460
461 public void mouseExited(MouseEvent e) {
462 // TODO Auto-generated method stub
463
464 }
465
466 }
467
468 class PartialEditableTableModel extends DefaultTableModel {
469 /**
470 *
471 */
472 private static final long serialVersionUID = 1L;
473
474 public boolean isCellEditable(int row, int col) {
475 switch (col) {
476 case 3:
477 return true;
478 default:
479 return false;
480 }
481 }
482 }