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