]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/deleteui/DeleteStepTwo.java
17a565d858e3ed995edc0d0a1141a47b92986cbf
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / far / deleteui / DeleteStepTwo.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.deleteui;
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.Iterator;
22 import java.util.LinkedHashSet;
23 import java.util.Set;
24 import java.util.Vector;
25
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;
32
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;
39
40 public class DeleteStepTwo extends IDialog implements MouseListener {
41
42 /**
43 *
44 */
45 private static final long serialVersionUID = -1333748185798962746L;
46
47 private JPanel jContentPane = null;
48
49 private JButton jButtonCancel = null;
50
51 private JButton jButtonFinish = null;
52
53 private JButton jButtonPrevious = null;
54
55 private JTextArea jTextArea = null;
56
57 private JLabel jLabel = null;
58
59 private JRadioButton jRadioButton = null;
60
61 private JRadioButton jRadioButton1 = null;
62
63 private DeleteStepOne stepOne = null;
64
65 public DeleteStepTwo(IDialog iDialog, boolean modal, DeleteStepOne stepOne) {
66 this(iDialog, modal);
67 this.stepOne = stepOne;
68 }
69
70 /**
71 * This method initializes jButtonCancel
72 *
73 * @return javax.swing.JButton
74 */
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);
81 }
82 return jButtonCancel;
83 }
84
85 /**
86 * This method initializes jButtonFinish
87 *
88 * @return javax.swing.JButton
89 */
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);
96 }
97 return jButtonFinish;
98 }
99
100 /**
101 * This method initializes jButtonPrevious
102 *
103 * @return javax.swing.JButton
104 */
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);
111 }
112 return jButtonPrevious;
113 }
114
115 /**
116 * This method initializes jTextArea
117 *
118 * @return javax.swing.JTextArea
119 */
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);
128 }
129 return jTextArea;
130 }
131
132 /**
133 * This method initializes jRadioButton
134 *
135 * @return javax.swing.JRadioButton
136 */
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");
143 }
144 return jRadioButton;
145 }
146
147 /**
148 * This method initializes jRadioButton1
149 *
150 * @return javax.swing.JRadioButton
151 */
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");
157 }
158 return jRadioButton1;
159 }
160
161 /**
162 * This is the default constructor
163 */
164 public DeleteStepTwo(IDialog iDialog, boolean modal) {
165 super(iDialog, modal);
166 initialize();
167 }
168
169 /**
170 * This method initializes this
171 *
172 * @return void
173 */
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);
180 }
181
182 /**
183 * This method initializes jContentPane
184 *
185 * @return javax.swing.JPanel
186 */
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);
204 }
205 return jContentPane;
206 }
207
208 public void mouseClicked(MouseEvent e) {
209 if (e.getSource() == jButtonCancel) {
210 this.setVisible(false);
211 this.dispose();
212 } else if (e.getSource() == jButtonFinish) {
213 FarIdentification far = stepOne.getSelecedFar();
214 WorkspaceTools wt = new WorkspaceTools();
215 //
216 // If remove all source files
217 //
218 if (jRadioButton1.isSelected()) {
219
220 Vector<PackageIdentification> removePackages = wt.getPackagesByFar(far);
221 Vector<PlatformIdentification> removePlatforms = wt.getPlatformsByFar(far);
222
223 Vector<PlatformIdentification> allPlatforms = wt.getAllPlatforms();
224 Set<File> allPlatformFiles = new LinkedHashSet<File>();
225
226 Iterator<PlatformIdentification> iter = allPlatforms.iterator();
227 while (iter.hasNext()) {
228 allPlatformFiles.add(iter.next().getFpdFile());
229 }
230
231 //
232 // For all platforms, only remove its FPD file
233 //
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();
240 //
241 // Remove all empty parent dir
242 //
243 while (parentDir.listFiles().length == 0) {
244 File tempFile = parentDir;
245 parentDir = parentDir.getParentFile();
246 tempFile.delete();
247 }
248 }
249
250 //
251 // For all packages, remove all files.
252 // Exception FPD file still in DB
253 //
254
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());
263 }
264 //
265 // Remove all empty parent dir
266 //
267 File parentDir = item.getSpdFile().getParentFile();
268 while (parentDir.listFiles().length == 0) {
269 File tempFile = parentDir;
270 parentDir = parentDir.getParentFile();
271 tempFile.delete();
272 }
273 }
274 }
275
276 //
277 // Update DB file
278 //
279 wt.removeFarFromDb(far);
280
281 this.setVisible(false);
282 this.stepOne.returnType = DataType.RETURN_TYPE_OK;
283 this.dispose();
284 } else if (e.getSource() == jButtonPrevious) {
285 this.setVisible(false);
286 stepOne.setVisible(true);
287 }
288 }
289
290 public void mousePressed(MouseEvent e) {
291 // TODO Auto-generated method stub
292
293 }
294
295 public void mouseReleased(MouseEvent e) {
296 // TODO Auto-generated method stub
297
298 }
299
300 public void mouseEntered(MouseEvent e) {
301 // TODO Auto-generated method stub
302
303 }
304
305 public void mouseExited(MouseEvent e) {
306 // TODO Auto-generated method stub
307
308 }
309
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]);
316 }
317 } else {
318 if (isContain(fileList[i], platformFiles)) {
319 recursiveDir(files, fileList[i], platformFiles);
320 } else {
321 files.add(fileList[i]);
322 }
323 }
324 }
325 }
326
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]);
332 }
333 }
334 file.delete();
335 }
336
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())) {
342 //
343 // continue this FPD file
344 //
345 return true;
346 }
347 }
348 return false;
349 }
350 }