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
.deleteui
;
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
.Iterator
;
22 import java
.util
.LinkedHashSet
;
24 import java
.util
.Vector
;
26 import javax
.swing
.ButtonGroup
;
27 import javax
.swing
.JPanel
;
28 import javax
.swing
.JButton
;
29 import javax
.swing
.JTextArea
;
30 import javax
.swing
.JLabel
;
31 import javax
.swing
.JRadioButton
;
33 import org
.tianocore
.frameworkwizard
.common
.DataType
;
34 import org
.tianocore
.frameworkwizard
.common
.ui
.IDialog
;
35 import org
.tianocore
.frameworkwizard
.far
.FarIdentification
;
36 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
37 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
38 import org
.tianocore
.frameworkwizard
.workspace
.WorkspaceTools
;
40 public class DeleteStepTwo
extends IDialog
implements MouseListener
{
45 private static final long serialVersionUID
= -1333748185798962746L;
47 private JPanel jContentPane
= null;
49 private JButton jButtonCancel
= null;
51 private JButton jButtonFinish
= null;
53 private JButton jButtonPrevious
= null;
55 private JTextArea jTextArea
= null;
57 private JLabel jLabel
= null;
59 private JRadioButton jRadioButton
= null;
61 private JRadioButton jRadioButton1
= null;
63 private DeleteStepOne stepOne
= null;
65 public DeleteStepTwo(IDialog iDialog
, boolean modal
, DeleteStepOne stepOne
) {
67 this.stepOne
= stepOne
;
71 * This method initializes jButtonCancel
73 * @return javax.swing.JButton
75 private JButton
getJButtonCancel() {
76 if (jButtonCancel
== null) {
77 jButtonCancel
= new JButton();
78 jButtonCancel
.setBounds(new java
.awt
.Rectangle(570, 330, 90, 20));
79 jButtonCancel
.setText("Cancel");
80 jButtonCancel
.addMouseListener(this);
86 * This method initializes jButtonFinish
88 * @return javax.swing.JButton
90 private JButton
getJButtonFinish() {
91 if (jButtonFinish
== null) {
92 jButtonFinish
= new JButton();
93 jButtonFinish
.setBounds(new java
.awt
.Rectangle(470, 330, 90, 20));
94 jButtonFinish
.setText("Finish");
95 jButtonFinish
.addMouseListener(this);
101 * This method initializes jButtonPrevious
103 * @return javax.swing.JButton
105 private JButton
getJButtonPrevious() {
106 if (jButtonPrevious
== null) {
107 jButtonPrevious
= new JButton();
108 jButtonPrevious
.setBounds(new java
.awt
.Rectangle(370, 330, 90, 20));
109 jButtonPrevious
.setText("Previous");
110 jButtonPrevious
.addMouseListener(this);
112 return jButtonPrevious
;
116 * This method initializes jTextArea
118 * @return javax.swing.JTextArea
120 private JTextArea
getJTextArea() {
121 if (jTextArea
== null) {
122 jTextArea
= new JTextArea();
123 jTextArea
.setBounds(new java
.awt
.Rectangle(30, 7, 642, 50));
124 jTextArea
.setText("Step 2: Choose Delete Mode. \n");
125 jTextArea
.append("Mode 1 only remove register information from framework database. \n");
126 jTextArea
.append("Mode 2 also delete all files from file system. ");
127 jTextArea
.setEditable(false);
133 * This method initializes jRadioButton
135 * @return javax.swing.JRadioButton
137 private JRadioButton
getJRadioButton() {
138 if (jRadioButton
== null) {
139 jRadioButton
= new JRadioButton();
140 jRadioButton
.setBounds(new java
.awt
.Rectangle(100, 100, 340, 20));
141 jRadioButton
.setSelected(true);
142 jRadioButton
.setText("Mode 1: Only remove from framework database");
148 * This method initializes jRadioButton1
150 * @return javax.swing.JRadioButton
152 private JRadioButton
getJRadioButton1() {
153 if (jRadioButton1
== null) {
154 jRadioButton1
= new JRadioButton();
155 jRadioButton1
.setBounds(new java
.awt
.Rectangle(100, 140, 358, 20));
156 jRadioButton1
.setText("Mode 2: Delete all related files from workspace");
158 return jRadioButton1
;
162 * This is the default constructor
164 public DeleteStepTwo(IDialog iDialog
, boolean modal
) {
165 super(iDialog
, modal
);
170 * This method initializes this
174 private void initialize() {
175 this.setSize(700, 400);
176 this.setContentPane(getJContentPane());
177 this.setTitle("Delete Framework Archive(FAR) - Step 2: Choose Delete Mode");
178 Dimension d
= Toolkit
.getDefaultToolkit().getScreenSize();
179 this.setLocation((d
.width
- this.getSize().width
) / 2, (d
.height
- this.getSize().height
) / 2);
183 * This method initializes jContentPane
185 * @return javax.swing.JPanel
187 private JPanel
getJContentPane() {
188 if (jContentPane
== null) {
189 jLabel
= new JLabel();
190 jLabel
.setBounds(new java
.awt
.Rectangle(30, 70, 200, 20));
191 jLabel
.setText("Select delete mode: ");
192 jContentPane
= new JPanel();
193 jContentPane
.setLayout(null);
194 jContentPane
.add(getJButtonCancel(), null);
195 jContentPane
.add(getJButtonFinish(), null);
196 jContentPane
.add(getJButtonPrevious(), null);
197 jContentPane
.add(getJTextArea(), null);
198 jContentPane
.add(jLabel
, null);
199 ButtonGroup group
= new ButtonGroup();
200 group
.add(getJRadioButton());
201 group
.add(getJRadioButton1());
202 jContentPane
.add(getJRadioButton(), null);
203 jContentPane
.add(getJRadioButton1(), null);
208 public void mouseClicked(MouseEvent e
) {
209 if (e
.getSource() == jButtonCancel
) {
210 this.setVisible(false);
212 } else if (e
.getSource() == jButtonFinish
) {
213 FarIdentification far
= stepOne
.getSelecedFar();
214 WorkspaceTools wt
= new WorkspaceTools();
216 // If remove all source files
218 if (jRadioButton1
.isSelected()) {
220 Vector
<PackageIdentification
> removePackages
= wt
.getPackagesByFar(far
);
221 Vector
<PlatformIdentification
> removePlatforms
= wt
.getPlatformsByFar(far
);
223 Vector
<PlatformIdentification
> allPlatforms
= wt
.getAllPlatforms();
224 Set
<File
> allPlatformFiles
= new LinkedHashSet
<File
>();
226 Iterator
<PlatformIdentification
> iter
= allPlatforms
.iterator();
227 while (iter
.hasNext()) {
228 allPlatformFiles
.add(iter
.next().getFpdFile());
232 // For all platforms, only remove its FPD file
234 Iterator
<PlatformIdentification
> platfomrIter
= removePlatforms
.iterator();
235 while (platfomrIter
.hasNext()) {
236 PlatformIdentification item
= platfomrIter
.next();
237 allPlatformFiles
.remove(item
.getFpdFile());
238 File parentDir
= item
.getFpdFile().getParentFile();
239 item
.getFpdFile().delete();
241 // Remove all empty parent dir
243 while (parentDir
.listFiles().length
== 0) {
244 File tempFile
= parentDir
;
245 parentDir
= parentDir
.getParentFile();
251 // For all packages, remove all files.
252 // Exception FPD file still in DB
255 Iterator
<PackageIdentification
> packageIter
= removePackages
.iterator();
256 while (packageIter
.hasNext()) {
257 PackageIdentification item
= packageIter
.next();
258 Set
<File
> deleteFiles
= new LinkedHashSet
<File
>();
259 recursiveDir(deleteFiles
, item
.getSpdFile().getParentFile(), allPlatformFiles
);
260 Iterator
<File
> iterDeleteFile
= deleteFiles
.iterator();
261 while (iterDeleteFile
.hasNext()) {
262 deleteFiles(iterDeleteFile
.next());
265 // Remove all empty parent dir
267 File parentDir
= item
.getSpdFile().getParentFile();
268 while (parentDir
.listFiles().length
== 0) {
269 File tempFile
= parentDir
;
270 parentDir
= parentDir
.getParentFile();
279 wt
.removeFarFromDb(far
);
281 this.setVisible(false);
282 this.stepOne
.returnType
= DataType
.RETURN_TYPE_OK
;
284 } else if (e
.getSource() == jButtonPrevious
) {
285 this.setVisible(false);
286 stepOne
.setVisible(true);
290 public void mousePressed(MouseEvent e
) {
291 // TODO Auto-generated method stub
295 public void mouseReleased(MouseEvent e
) {
296 // TODO Auto-generated method stub
300 public void mouseEntered(MouseEvent e
) {
301 // TODO Auto-generated method stub
305 public void mouseExited(MouseEvent e
) {
306 // TODO Auto-generated method stub
310 private void recursiveDir(Set
<File
> files
, File dir
, Set
<File
> platformFiles
) {
311 File
[] fileList
= dir
.listFiles();
312 for (int i
= 0; i
< fileList
.length
; i
++) {
313 if (fileList
[i
].isFile()) {
314 if (!platformFiles
.contains(fileList
[i
])) {
315 files
.add(fileList
[i
]);
318 if (isContain(fileList
[i
], platformFiles
)) {
319 recursiveDir(files
, fileList
[i
], platformFiles
);
321 files
.add(fileList
[i
]);
327 private void deleteFiles(File file
) {
328 if (file
.isDirectory()) {
329 File
[] files
= file
.listFiles();
330 for (int i
= 0; i
< files
.length
; i
++) {
331 deleteFiles(files
[i
]);
337 private boolean isContain(File dir
, Set
<File
> platformFiles
) {
338 Iterator
<File
> iter
= platformFiles
.iterator();
339 while (iter
.hasNext()) {
340 File file
= iter
.next();
341 if (file
.getPath().startsWith(dir
.getPath())) {
343 // continue this FPD file