1. Update release note to use jdk1.5.0_06 to take place of jdk1.5.0_04
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / far / updateui / UpdateStepTwo.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.updateui;
15
16 import java.awt.Dimension;
17 import java.awt.Toolkit;
18 import java.awt.event.MouseEvent;
19 import java.awt.event.MouseListener;
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.Vector;
27
28 import javax.swing.JPanel;
29 import javax.swing.JTextArea;
30 import javax.swing.JButton;
31 import javax.swing.JScrollPane;
32 import javax.swing.JLabel;
33 import javax.swing.JTable;
34 import javax.swing.ListSelectionModel;
35 import javax.swing.table.DefaultTableModel;
36
37 import org.tianocore.frameworkwizard.common.DataType;
38 import org.tianocore.frameworkwizard.common.Log;
39 import org.tianocore.frameworkwizard.common.Tools;
40 import org.tianocore.frameworkwizard.common.ui.IDialog;
41 import org.tianocore.frameworkwizard.far.AggregationOperation;
42 import org.tianocore.frameworkwizard.far.DistributeRule;
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 UpdateStepTwo extends IDialog implements MouseListener {
50
51 /**
52 *
53 */
54 private static final long serialVersionUID = -4400145363721213110L;
55
56 private JPanel jContentPane = null;
57
58 private JTextArea jTextArea = null;
59
60 private UpdateStepOne stepOne = null;
61
62 private JButton jButtonCancel = null;
63
64 private JButton jButtonFinish = null;
65
66 private JButton jButtonPrevious = null;
67
68 private JScrollPane jScrollPane = null;
69
70 private JLabel jLabel = null;
71
72 private JTable jTablePackage = null;
73
74 private PartialTableModel model = null;
75
76 List<PackageIdentification> updatPkgList = new ArrayList<PackageIdentification>();
77
78 public UpdateStepTwo(IDialog iDialog, boolean modal, UpdateStepOne stepOne) {
79 this(iDialog, modal);
80 this.stepOne = stepOne;
81 }
82
83 /**
84 * This method initializes jTextArea
85 *
86 * @return javax.swing.JTextArea
87 */
88 private JTextArea getJTextArea() {
89 if (jTextArea == null) {
90 jTextArea = new JTextArea();
91 jTextArea.setBounds(new java.awt.Rectangle(30, 7, 642, 50));
92 jTextArea.setText("Step 2: Summary. \n");
93 jTextArea.setEditable(false);
94 }
95 return jTextArea;
96 }
97
98 /**
99 * This method initializes jButtonCancel
100 *
101 * @return javax.swing.JButton
102 */
103 private JButton getJButtonCancel() {
104 if (jButtonCancel == null) {
105 jButtonCancel = new JButton();
106 jButtonCancel.setBounds(new java.awt.Rectangle(570, 330, 90, 20));
107 jButtonCancel.setText("Cancel");
108 jButtonCancel.addMouseListener(this);
109 }
110 return jButtonCancel;
111 }
112
113 /**
114 * This method initializes jButtonFinish
115 *
116 * @return javax.swing.JButton
117 */
118 private JButton getJButtonFinish() {
119 if (jButtonFinish == null) {
120 jButtonFinish = new JButton();
121 jButtonFinish.setBounds(new java.awt.Rectangle(470, 330, 90, 20));
122 jButtonFinish.setText("Finish");
123 jButtonFinish.addMouseListener(this);
124 }
125 return jButtonFinish;
126 }
127
128 /**
129 * This method initializes jButtonPrevious
130 *
131 * @return javax.swing.JButton
132 */
133 private JButton getJButtonPrevious() {
134 if (jButtonPrevious == null) {
135 jButtonPrevious = new JButton();
136 jButtonPrevious.setBounds(new java.awt.Rectangle(370, 330, 90, 20));
137 jButtonPrevious.setText("Previous");
138 jButtonPrevious.addMouseListener(this);
139 }
140 return jButtonPrevious;
141 }
142
143 /**
144 * This method initializes jScrollPane
145 *
146 * @return javax.swing.JScrollPane
147 */
148 private JScrollPane getJScrollPane() {
149 if (jScrollPane == null) {
150 jScrollPane = new JScrollPane();
151 jScrollPane.setBounds(new java.awt.Rectangle(30, 100, 642, 170));
152 jScrollPane.setViewportView(getJTablePackage());
153 }
154 return jScrollPane;
155 }
156
157 /**
158 * This method initializes jTablePackage
159 *
160 * @return javax.swing.JTable
161 */
162 private JTable getJTablePackage() {
163 if (jTablePackage == null) {
164 jTablePackage = new JTable();
165 model = new PartialTableModel();
166 jTablePackage = new JTable(model);
167 jTablePackage.setRowHeight(20);
168 jTablePackage.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
169 model.addColumn("Name");
170 model.addColumn("Version");
171 model.addColumn("Guid");
172 model.addColumn("Path");
173
174 jTablePackage.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
175 }
176 return jTablePackage;
177 }
178
179 public void prepareTable() {
180 model.setRowCount(0);
181 try {
182 Far far = stepOne.getFar();
183 List<PackageIdentification> packagesInFar = far.mainfest.getPackageList();
184
185 WorkspaceTools wt = new WorkspaceTools();
186 List<PackageIdentification> packagesInDb = wt.getAllPackages();
187
188 updatPkgList = AggregationOperation.intersection(packagesInDb, packagesInFar);
189 //
190 // Change here to get packages and platforms from FAR
191 //
192 Iterator<PackageIdentification> iter = updatPkgList.iterator();//packageList.iterator();
193 while (iter.hasNext()) {
194 String[] str = new String[4];
195 PackageIdentification item = iter.next();
196 str[0] = item.getName();
197 str[1] = item.getVersion();
198 str[2] = item.getGuid();
199 str[3] = Tools.getFilePathOnly(Tools.getRelativePath(item.getPath(), Workspace.getCurrentWorkspace()));
200 model.addRow(str);
201 }
202 } catch (Exception ex) {
203 ex.printStackTrace();
204 }
205 }
206
207 /**
208 * This is the default constructor
209 */
210 public UpdateStepTwo(IDialog iDialog, boolean modal) {
211 super(iDialog, modal);
212 initialize();
213 }
214
215 /**
216 * This method initializes this
217 *
218 * @return void
219 */
220 private void initialize() {
221 this.setSize(700, 400);
222 this.setContentPane(getJContentPane());
223 this.setTitle("Update Framework Archive(FAR) - Step 2: Summary");
224 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
225 this.setLocation((d.width - this.getSize().width) / 2, (d.height - this.getSize().height) / 2);
226 }
227
228 /**
229 * This method initializes jContentPane
230 *
231 * @return javax.swing.JPanel
232 */
233 private JPanel getJContentPane() {
234 if (jContentPane == null) {
235 jLabel = new JLabel();
236 jLabel.setBounds(new java.awt.Rectangle(30, 70, 281, 20));
237 jLabel.setText("Following packages will be updated: ");
238 jContentPane = new JPanel();
239 jContentPane.setLayout(null);
240 jContentPane.add(getJTextArea(), null);
241 jContentPane.add(getJButtonCancel(), null);
242 jContentPane.add(getJButtonFinish(), null);
243 jContentPane.add(getJButtonPrevious(), null);
244 jContentPane.add(getJScrollPane(), null);
245 jContentPane.add(jLabel, null);
246 }
247 return jContentPane;
248 }
249
250 public void mouseClicked(MouseEvent e) {
251 if (e.getSource() == jButtonCancel) {
252 this.setVisible(false);
253 } else if (e.getSource() == jButtonFinish) {
254 //
255 // Check depedency ?
256 //
257 WorkspaceTools wsTools = new WorkspaceTools();
258
259 Iterator<PackageIdentification> iter = updatPkgList.iterator();
260 List<PackageIdentification> depResultList = new ArrayList<PackageIdentification>();
261 while (iter.hasNext()) {
262 List<PackageIdentification> depPkgList = stepOne.getFar().getPackageDependencies(iter.next());
263 depResultList = AggregationOperation.union(depResultList, depPkgList);
264 }
265
266 List<PackageIdentification> dbPkgList = DistributeRule.vectorToList(wsTools.getAllPackages());
267 List<PackageIdentification> resultList = AggregationOperation
268 .minus(
269 depResultList,
270 AggregationOperation
271 .union(
272 this.updatPkgList,
273 dbPkgList));
274 Iterator resultIter = resultList.iterator();
275 while (resultIter.hasNext()) {
276 Log.err("Missing dependency package " + ((PackageIdentification) resultIter.next()).toString()
277 + "in workspace!");
278 return;
279 }
280
281 //
282 // Remove all update packages
283 //
284 //
285 // For all packages, remove all files.
286 // Exception FPD file still in DB
287 //
288 Vector<PlatformIdentification> allPlatforms = wsTools.getAllPlatforms();
289 Set<File> allPlatformFiles = new LinkedHashSet<File>();
290
291 Iterator<PlatformIdentification> allPlfIter = allPlatforms.iterator();
292 while (iter.hasNext()) {
293 allPlatformFiles.add(allPlfIter.next().getFpdFile());
294 }
295
296 Iterator<PackageIdentification> packageIter = this.updatPkgList.iterator();
297 while (packageIter.hasNext()) {
298 PackageIdentification item = packageIter.next();
299 Set<File> deleteFiles = new LinkedHashSet<File>();
300 recursiveDir(deleteFiles, item.getSpdFile().getParentFile(), allPlatformFiles);
301 Iterator<File> iterDeleteFile = deleteFiles.iterator();
302 while (iterDeleteFile.hasNext()) {
303 deleteFiles(iterDeleteFile.next());
304 }
305 //
306 // Remove all empty parent dir
307 //
308 File parentDir = item.getSpdFile().getParentFile();
309 while (parentDir.listFiles().length == 0) {
310 File tempFile = parentDir;
311 parentDir = parentDir.getParentFile();
312 tempFile.delete();
313 }
314 }
315
316 //
317 // Install all update packages
318 //
319 Iterator<PackageIdentification> updataIter = this.updatPkgList.iterator();
320 while (updataIter.hasNext()) {
321 PackageIdentification pkgId = updataIter.next();
322 try {
323 stepOne.getFar().installPackage(pkgId, new File(pkgId.getSpdFile().getParent()));
324 } catch (Exception ex) {
325 Log.err("Can install " + pkgId.toString() + " pakcage, please check it!");
326 }
327
328 }
329 this.stepOne.returnType = DataType.RETURN_TYPE_OK;
330
331 this.setVisible(false);
332 this.dispose();
333 } else if (e.getSource() == jButtonPrevious) {
334 this.setVisible(false);
335 stepOne.setVisible(true);
336 }
337 }
338
339 public void mousePressed(MouseEvent e) {
340 // TODO Auto-generated method stub
341
342 }
343
344 public void mouseReleased(MouseEvent e) {
345 // TODO Auto-generated method stub
346
347 }
348
349 public void mouseEntered(MouseEvent e) {
350 // TODO Auto-generated method stub
351
352 }
353
354 public void mouseExited(MouseEvent e) {
355 // TODO Auto-generated method stub
356
357 }
358
359 private void recursiveDir(Set<File> files, File dir, Set<File> platformFiles) {
360 File[] fileList = dir.listFiles();
361 for (int i = 0; i < fileList.length; i++) {
362 if (fileList[i].isFile()) {
363 if (!platformFiles.contains(fileList[i])) {
364 files.add(fileList[i]);
365 }
366 } else {
367 if (isContain(fileList[i], platformFiles)) {
368 recursiveDir(files, fileList[i], platformFiles);
369 } else {
370 files.add(fileList[i]);
371 }
372 }
373 }
374 }
375
376 private void deleteFiles(File file) {
377 if (file.isDirectory()) {
378 File[] files = file.listFiles();
379 for (int i = 0; i < files.length; i++) {
380 deleteFiles(files[i]);
381 }
382 }
383 file.delete();
384 }
385
386 private boolean isContain(File dir, Set<File> platformFiles) {
387 Iterator<File> iter = platformFiles.iterator();
388 while (iter.hasNext()) {
389 File file = iter.next();
390 if (file.getPath().startsWith(dir.getPath())) {
391 //
392 // continue this FPD file
393 //
394 return true;
395 }
396 }
397 return false;
398 }
399 }
400
401 class PartialTableModel extends DefaultTableModel {
402 /**
403 *
404 */
405 private static final long serialVersionUID = 1L;
406
407 public boolean isCellEditable(int row, int col) {
408 switch (col) {
409 case 3:
410 return false;
411 default:
412 return false;
413 }
414 }
415 }