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