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
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.
14 package org
.tianocore
.frameworkwizard
.far
.updateui
;
16 import java
.awt
.Dimension
;
17 import java
.awt
.Toolkit
;
18 import java
.awt
.event
.MouseEvent
;
19 import java
.awt
.event
.MouseListener
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Iterator
;
23 import java
.util
.LinkedHashSet
;
24 import java
.util
.List
;
26 import java
.util
.Vector
;
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
;
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
;
50 public class UpdateStepTwo
extends IDialog
implements MouseListener
{
55 private static final long serialVersionUID
= -4400145363721213110L;
57 private JPanel jContentPane
= null;
59 private JTextArea jTextArea
= null;
61 private UpdateStepOne stepOne
= null;
63 private JButton jButtonCancel
= null;
65 private JButton jButtonFinish
= null;
67 private JButton jButtonPrevious
= null;
69 private JScrollPane jScrollPane
= null;
71 private JLabel jLabel
= null;
73 private JTable jTablePackage
= null;
75 private PartialTableModel model
= null;
77 List
<PackageIdentification
> updatPkgList
= new ArrayList
<PackageIdentification
>();
79 public UpdateStepTwo(IDialog iDialog
, boolean modal
, UpdateStepOne stepOne
) {
81 this.stepOne
= stepOne
;
85 * This method initializes jTextArea
87 * @return javax.swing.JTextArea
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);
100 * This method initializes jButtonCancel
102 * @return javax.swing.JButton
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);
111 return jButtonCancel
;
115 * This method initializes jButtonFinish
117 * @return javax.swing.JButton
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);
126 return jButtonFinish
;
130 * This method initializes jButtonPrevious
132 * @return javax.swing.JButton
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);
141 return jButtonPrevious
;
145 * This method initializes jScrollPane
147 * @return javax.swing.JScrollPane
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());
159 * This method initializes jTablePackage
161 * @return javax.swing.JTable
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");
175 jTablePackage
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
177 return jTablePackage
;
180 public void prepareTable() {
181 model
.setRowCount(0);
183 Far far
= stepOne
.getFar();
184 List
<PackageIdentification
> packagesInFar
= far
.mainfest
.getPackageList();
186 WorkspaceTools wt
= new WorkspaceTools();
187 List
<PackageIdentification
> packagesInDb
= wt
.getAllPackages();
189 updatPkgList
= AggregationOperation
.intersection(packagesInDb
, packagesInFar
);
191 // Change here to get packages and platforms from FAR
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()));
203 } catch (Exception ex
) {
204 ex
.printStackTrace();
209 * This is the default constructor
211 public UpdateStepTwo(IDialog iDialog
, boolean modal
) {
212 super(iDialog
, modal
);
217 * This method initializes this
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);
230 * This method initializes jContentPane
232 * @return javax.swing.JPanel
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);
251 public void mouseClicked(MouseEvent e
) {
252 if (e
.getSource() == jButtonCancel
) {
253 this.setVisible(false);
254 } else if (e
.getSource() == jButtonFinish
) {
258 WorkspaceTools wsTools
= new WorkspaceTools();
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
);
267 List
<PackageIdentification
> dbPkgList
= DistributeRule
.vectorToList(wsTools
.getAllPackages());
268 List
<PackageIdentification
> resultList
= AggregationOperation
275 Iterator resultIter
= resultList
.iterator();
276 while (resultIter
.hasNext()) {
277 Log
.wrn("Update far", "Missing dependency package " + ((PackageIdentification
) resultIter
.next()).toString()
283 // Remove all update packages
286 // For all packages, remove all files.
287 // Exception FPD file still in DB
289 Vector
<PlatformIdentification
> allPlatforms
= wsTools
.getAllPlatforms();
290 Set
<File
> allPlatformFiles
= new LinkedHashSet
<File
>();
292 Iterator
<PlatformIdentification
> allPlfIter
= allPlatforms
.iterator();
293 while (iter
.hasNext()) {
294 allPlatformFiles
.add(allPlfIter
.next().getFpdFile());
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());
307 // Remove all empty parent dir
309 File parentDir
= item
.getSpdFile().getParentFile();
310 while (parentDir
.listFiles().length
== 0) {
311 File tempFile
= parentDir
;
312 parentDir
= parentDir
.getParentFile();
318 // Install all update packages
320 Iterator
<PackageIdentification
> updataIter
= this.updatPkgList
.iterator();
321 while (updataIter
.hasNext()) {
322 PackageIdentification pkgId
= updataIter
.next();
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());
331 this.stepOne
.returnType
= DataType
.RETURN_TYPE_OK
;
333 this.setVisible(false);
335 } else if (e
.getSource() == jButtonPrevious
) {
336 this.setVisible(false);
337 stepOne
.setVisible(true);
341 public void mousePressed(MouseEvent e
) {
342 // TODO Auto-generated method stub
346 public void mouseReleased(MouseEvent e
) {
347 // TODO Auto-generated method stub
351 public void mouseEntered(MouseEvent e
) {
352 // TODO Auto-generated method stub
356 public void mouseExited(MouseEvent e
) {
357 // TODO Auto-generated method stub
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
]);
369 if (isContain(fileList
[i
], platformFiles
)) {
370 recursiveDir(files
, fileList
[i
], platformFiles
);
372 files
.add(fileList
[i
]);
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
]);
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())) {
394 // continue this FPD file
403 class PartialTableModel
extends DefaultTableModel
{
407 private static final long serialVersionUID
= 1L;
409 public boolean isCellEditable(int row
, int col
) {