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