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
.far
.FarStringDefinition
;
37 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
38 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
39 import org
.tianocore
.frameworkwizard
.workspace
.WorkspaceTools
;
41 public class DeleteStepTwo
extends IDialog
implements MouseListener
{
46 private static final long serialVersionUID
= -1333748185798962746L;
48 private JPanel jContentPane
= null;
50 private JButton jButtonCancel
= null;
52 private JButton jButtonFinish
= null;
54 private JButton jButtonPrevious
= null;
56 private JTextArea jTextArea
= null;
58 private JLabel jLabel
= null;
60 private JRadioButton jRadioButton
= null;
62 private JRadioButton jRadioButton1
= null;
64 private DeleteStepOne stepOne
= null;
66 public DeleteStepTwo(IDialog iDialog
, boolean modal
, DeleteStepOne stepOne
) {
68 this.stepOne
= stepOne
;
72 * This method initializes jButtonCancel
74 * @return javax.swing.JButton
76 private JButton
getJButtonCancel() {
77 if (jButtonCancel
== null) {
78 jButtonCancel
= new JButton();
79 jButtonCancel
.setBounds(new java
.awt
.Rectangle(570, 330, 90, 20));
80 jButtonCancel
.setText("Cancel");
81 jButtonCancel
.addMouseListener(this);
87 * This method initializes jButtonFinish
89 * @return javax.swing.JButton
91 private JButton
getJButtonFinish() {
92 if (jButtonFinish
== null) {
93 jButtonFinish
= new JButton();
94 jButtonFinish
.setBounds(new java
.awt
.Rectangle(470, 330, 90, 20));
95 jButtonFinish
.setText("Finish");
96 jButtonFinish
.addMouseListener(this);
102 * This method initializes jButtonPrevious
104 * @return javax.swing.JButton
106 private JButton
getJButtonPrevious() {
107 if (jButtonPrevious
== null) {
108 jButtonPrevious
= new JButton();
109 jButtonPrevious
.setBounds(new java
.awt
.Rectangle(370, 330, 90, 20));
110 jButtonPrevious
.setText("Previous");
111 jButtonPrevious
.addMouseListener(this);
113 return jButtonPrevious
;
117 * This method initializes jTextArea
119 * @return javax.swing.JTextArea
121 private JTextArea
getJTextArea() {
122 if (jTextArea
== null) {
123 jTextArea
= new JTextArea();
124 jTextArea
.setBounds(new java
.awt
.Rectangle(30, 7, 642, 50));
125 jTextArea
.setText("Step 2: Choose Delete Mode. \n");
126 jTextArea
.append("Mode 1 only remove register information from framework database. \n");
127 jTextArea
.append("Mode 2 also delete all files from file system. ");
128 jTextArea
.setEditable(false);
134 * This method initializes jRadioButton
136 * @return javax.swing.JRadioButton
138 private JRadioButton
getJRadioButton() {
139 if (jRadioButton
== null) {
140 jRadioButton
= new JRadioButton();
141 jRadioButton
.setBounds(new java
.awt
.Rectangle(40,100,340,20));
142 jRadioButton
.setSelected(true);
143 jRadioButton
.setText("Mode 1: Only remove from framework database");
149 * This method initializes jRadioButton1
151 * @return javax.swing.JRadioButton
153 private JRadioButton
getJRadioButton1() {
154 if (jRadioButton1
== null) {
155 jRadioButton1
= new JRadioButton();
156 jRadioButton1
.setBounds(new java
.awt
.Rectangle(40,140,358,20));
157 jRadioButton1
.setText("Mode 2: Delete all related files from workspace");
159 return jRadioButton1
;
163 * This is the default constructor
165 public DeleteStepTwo(IDialog iDialog
, boolean modal
) {
166 super(iDialog
, modal
);
171 * This method initializes this
175 private void initialize() {
176 this.setSize(700, 400);
177 this.setContentPane(getJContentPane());
178 this.setTitle(FarStringDefinition
.DELETE_STEP_TWO_TITLE
);
179 Dimension d
= Toolkit
.getDefaultToolkit().getScreenSize();
180 this.setLocation((d
.width
- this.getSize().width
) / 2, (d
.height
- this.getSize().height
) / 2);
184 * This method initializes jContentPane
186 * @return javax.swing.JPanel
188 private JPanel
getJContentPane() {
189 if (jContentPane
== null) {
190 jLabel
= new JLabel();
191 jLabel
.setBounds(new java
.awt
.Rectangle(30, 70, 200, 20));
192 jLabel
.setText("Select delete mode: ");
193 jContentPane
= new JPanel();
194 jContentPane
.setLayout(null);
195 jContentPane
.add(getJButtonCancel(), null);
196 jContentPane
.add(getJButtonFinish(), null);
197 jContentPane
.add(getJButtonPrevious(), null);
198 jContentPane
.add(getJTextArea(), null);
199 jContentPane
.add(jLabel
, null);
200 ButtonGroup group
= new ButtonGroup();
201 group
.add(getJRadioButton());
202 group
.add(getJRadioButton1());
203 jContentPane
.add(getJRadioButton(), null);
204 jContentPane
.add(getJRadioButton1(), null);
209 public void mouseClicked(MouseEvent e
) {
210 if (e
.getSource() == jButtonCancel
) {
211 this.setVisible(false);
213 } else if (e
.getSource() == jButtonFinish
) {
214 FarIdentification far
= stepOne
.getSelecedFar();
215 WorkspaceTools wt
= new WorkspaceTools();
217 // If remove all source files
219 if (jRadioButton1
.isSelected()) {
221 Vector
<PackageIdentification
> removePackages
= wt
.getPackagesByFar(far
);
222 Vector
<PlatformIdentification
> removePlatforms
= wt
.getPlatformsByFar(far
);
224 Vector
<PlatformIdentification
> allPlatforms
= wt
.getAllPlatforms();
225 Set
<File
> allPlatformFiles
= new LinkedHashSet
<File
>();
227 Iterator
<PlatformIdentification
> iter
= allPlatforms
.iterator();
228 while (iter
.hasNext()) {
229 allPlatformFiles
.add(iter
.next().getFpdFile());
233 // For all platforms, only remove its FPD file
235 Iterator
<PlatformIdentification
> platfomrIter
= removePlatforms
.iterator();
236 while (platfomrIter
.hasNext()) {
237 PlatformIdentification item
= platfomrIter
.next();
238 allPlatformFiles
.remove(item
.getFpdFile());
239 File parentDir
= item
.getFpdFile().getParentFile();
240 item
.getFpdFile().delete();
242 // Remove all empty parent dir
244 while (parentDir
.listFiles().length
== 0) {
245 File tempFile
= parentDir
;
246 parentDir
= parentDir
.getParentFile();
252 // For all packages, remove all files.
253 // Exception FPD file still in DB
256 Iterator
<PackageIdentification
> packageIter
= removePackages
.iterator();
257 while (packageIter
.hasNext()) {
258 PackageIdentification item
= packageIter
.next();
259 Set
<File
> deleteFiles
= new LinkedHashSet
<File
>();
260 recursiveDir(deleteFiles
, item
.getSpdFile().getParentFile(), allPlatformFiles
);
261 Iterator
<File
> iterDeleteFile
= deleteFiles
.iterator();
262 while (iterDeleteFile
.hasNext()) {
263 deleteFiles(iterDeleteFile
.next());
266 // Remove all empty parent dir
268 File parentDir
= item
.getSpdFile().getParentFile();
269 while (parentDir
.listFiles().length
== 0) {
270 File tempFile
= parentDir
;
271 parentDir
= parentDir
.getParentFile();
280 wt
.removeFarFromDb(far
);
282 this.setVisible(false);
283 this.stepOne
.returnType
= DataType
.RETURN_TYPE_OK
;
285 } else if (e
.getSource() == jButtonPrevious
) {
286 this.setVisible(false);
287 stepOne
.setVisible(true);
291 public void mousePressed(MouseEvent e
) {
292 // TODO Auto-generated method stub
296 public void mouseReleased(MouseEvent e
) {
297 // TODO Auto-generated method stub
301 public void mouseEntered(MouseEvent e
) {
302 // TODO Auto-generated method stub
306 public void mouseExited(MouseEvent e
) {
307 // TODO Auto-generated method stub
311 private void recursiveDir(Set
<File
> files
, File dir
, Set
<File
> platformFiles
) {
312 File
[] fileList
= dir
.listFiles();
313 for (int i
= 0; i
< fileList
.length
; i
++) {
314 if (fileList
[i
].isFile()) {
315 if (!platformFiles
.contains(fileList
[i
])) {
316 files
.add(fileList
[i
]);
319 if (isContain(fileList
[i
], platformFiles
)) {
320 recursiveDir(files
, fileList
[i
], platformFiles
);
322 files
.add(fileList
[i
]);
328 private void deleteFiles(File file
) {
329 if (file
.isDirectory()) {
330 File
[] files
= file
.listFiles();
331 for (int i
= 0; i
< files
.length
; i
++) {
332 deleteFiles(files
[i
]);
338 private boolean isContain(File dir
, Set
<File
> platformFiles
) {
339 Iterator
<File
> iter
= platformFiles
.iterator();
340 while (iter
.hasNext()) {
341 File file
= iter
.next();
342 if (file
.getPath().startsWith(dir
.getPath())) {
344 // continue this FPD file