]>
Commit | Line | Data |
---|---|---|
5a24e806 | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2006, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | **/\r | |
13 | \r | |
14 | package org.tianocore.frameworkwizard.far.deleteui;\r | |
15 | \r | |
16 | import java.awt.Dimension;\r | |
17 | import java.awt.Toolkit;\r | |
18 | import java.awt.event.MouseEvent;\r | |
19 | import java.awt.event.MouseListener;\r | |
20 | import java.io.File;\r | |
21 | import java.util.Iterator;\r | |
22 | import java.util.LinkedHashSet;\r | |
23 | import java.util.Set;\r | |
24 | import java.util.Vector;\r | |
25 | \r | |
26 | import javax.swing.ButtonGroup;\r | |
27 | import javax.swing.JPanel;\r | |
28 | import javax.swing.JButton;\r | |
29 | import javax.swing.JTextArea;\r | |
30 | import javax.swing.JLabel;\r | |
31 | import javax.swing.JRadioButton;\r | |
32 | \r | |
33 | import org.tianocore.frameworkwizard.common.DataType;\r | |
34 | import org.tianocore.frameworkwizard.common.ui.IDialog;\r | |
35 | import org.tianocore.frameworkwizard.far.FarIdentification;\r | |
36 | import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r | |
37 | import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r | |
38 | import org.tianocore.frameworkwizard.workspace.WorkspaceTools;\r | |
39 | \r | |
40 | public class DeleteStepTwo extends IDialog implements MouseListener {\r | |
41 | \r | |
42 | /**\r | |
43 | * \r | |
44 | */\r | |
45 | private static final long serialVersionUID = -1333748185798962746L;\r | |
46 | \r | |
47 | private JPanel jContentPane = null;\r | |
48 | \r | |
49 | private JButton jButtonCancel = null;\r | |
50 | \r | |
51 | private JButton jButtonFinish = null;\r | |
52 | \r | |
53 | private JButton jButtonPrevious = null;\r | |
54 | \r | |
55 | private JTextArea jTextArea = null;\r | |
56 | \r | |
57 | private JLabel jLabel = null;\r | |
58 | \r | |
59 | private JRadioButton jRadioButton = null;\r | |
60 | \r | |
61 | private JRadioButton jRadioButton1 = null;\r | |
62 | \r | |
63 | private DeleteStepOne stepOne = null;\r | |
64 | \r | |
65 | public DeleteStepTwo(IDialog iDialog, boolean modal, DeleteStepOne stepOne) {\r | |
66 | this(iDialog, modal);\r | |
67 | this.stepOne = stepOne;\r | |
68 | }\r | |
69 | \r | |
70 | /**\r | |
71 | * This method initializes jButtonCancel \r | |
72 | * \r | |
73 | * @return javax.swing.JButton \r | |
74 | */\r | |
75 | private JButton getJButtonCancel() {\r | |
76 | if (jButtonCancel == null) {\r | |
77 | jButtonCancel = new JButton();\r | |
78 | jButtonCancel.setBounds(new java.awt.Rectangle(570, 330, 90, 20));\r | |
79 | jButtonCancel.setText("Cancel");\r | |
80 | jButtonCancel.addMouseListener(this);\r | |
81 | }\r | |
82 | return jButtonCancel;\r | |
83 | }\r | |
84 | \r | |
85 | /**\r | |
86 | * This method initializes jButtonFinish \r | |
87 | * \r | |
88 | * @return javax.swing.JButton \r | |
89 | */\r | |
90 | private JButton getJButtonFinish() {\r | |
91 | if (jButtonFinish == null) {\r | |
92 | jButtonFinish = new JButton();\r | |
93 | jButtonFinish.setBounds(new java.awt.Rectangle(470, 330, 90, 20));\r | |
94 | jButtonFinish.setText("Finish");\r | |
95 | jButtonFinish.addMouseListener(this);\r | |
96 | }\r | |
97 | return jButtonFinish;\r | |
98 | }\r | |
99 | \r | |
100 | /**\r | |
101 | * This method initializes jButtonPrevious \r | |
102 | * \r | |
103 | * @return javax.swing.JButton \r | |
104 | */\r | |
105 | private JButton getJButtonPrevious() {\r | |
106 | if (jButtonPrevious == null) {\r | |
107 | jButtonPrevious = new JButton();\r | |
108 | jButtonPrevious.setBounds(new java.awt.Rectangle(370, 330, 90, 20));\r | |
109 | jButtonPrevious.setText("Previous");\r | |
110 | jButtonPrevious.addMouseListener(this);\r | |
111 | }\r | |
112 | return jButtonPrevious;\r | |
113 | }\r | |
114 | \r | |
115 | /**\r | |
116 | * This method initializes jTextArea \r | |
117 | * \r | |
118 | * @return javax.swing.JTextArea \r | |
119 | */\r | |
120 | private JTextArea getJTextArea() {\r | |
121 | if (jTextArea == null) {\r | |
122 | jTextArea = new JTextArea();\r | |
123 | jTextArea.setBounds(new java.awt.Rectangle(30, 7, 642, 50));\r | |
124 | jTextArea.setText("Step 2: Choose Delete Mode. \n");\r | |
125 | jTextArea.append("Mode 1 only remove register information from framework database. \n");\r | |
126 | jTextArea.append("Mode 2 also delete all files from file system. ");\r | |
127 | jTextArea.setEditable(false);\r | |
128 | }\r | |
129 | return jTextArea;\r | |
130 | }\r | |
131 | \r | |
132 | /**\r | |
133 | * This method initializes jRadioButton \r | |
134 | * \r | |
135 | * @return javax.swing.JRadioButton \r | |
136 | */\r | |
137 | private JRadioButton getJRadioButton() {\r | |
138 | if (jRadioButton == null) {\r | |
139 | jRadioButton = new JRadioButton();\r | |
a45e9a40 | 140 | jRadioButton.setBounds(new java.awt.Rectangle(40,100,340,20));\r |
5a24e806 | 141 | jRadioButton.setSelected(true);\r |
142 | jRadioButton.setText("Mode 1: Only remove from framework database");\r | |
143 | }\r | |
144 | return jRadioButton;\r | |
145 | }\r | |
146 | \r | |
147 | /**\r | |
148 | * This method initializes jRadioButton1 \r | |
149 | * \r | |
150 | * @return javax.swing.JRadioButton \r | |
151 | */\r | |
152 | private JRadioButton getJRadioButton1() {\r | |
153 | if (jRadioButton1 == null) {\r | |
154 | jRadioButton1 = new JRadioButton();\r | |
a45e9a40 | 155 | jRadioButton1.setBounds(new java.awt.Rectangle(40,140,358,20));\r |
5a24e806 | 156 | jRadioButton1.setText("Mode 2: Delete all related files from workspace");\r |
157 | }\r | |
158 | return jRadioButton1;\r | |
159 | }\r | |
160 | \r | |
161 | /**\r | |
162 | * This is the default constructor\r | |
163 | */\r | |
164 | public DeleteStepTwo(IDialog iDialog, boolean modal) {\r | |
165 | super(iDialog, modal);\r | |
166 | initialize();\r | |
167 | }\r | |
168 | \r | |
169 | /**\r | |
170 | * This method initializes this\r | |
171 | * \r | |
172 | * @return void\r | |
173 | */\r | |
174 | private void initialize() {\r | |
175 | this.setSize(700, 400);\r | |
176 | this.setContentPane(getJContentPane());\r | |
177 | this.setTitle("Delete Framework Archive(FAR) - Step 2: Choose Delete Mode");\r | |
178 | Dimension d = Toolkit.getDefaultToolkit().getScreenSize();\r | |
179 | this.setLocation((d.width - this.getSize().width) / 2, (d.height - this.getSize().height) / 2);\r | |
180 | }\r | |
181 | \r | |
182 | /**\r | |
183 | * This method initializes jContentPane\r | |
184 | * \r | |
185 | * @return javax.swing.JPanel\r | |
186 | */\r | |
187 | private JPanel getJContentPane() {\r | |
188 | if (jContentPane == null) {\r | |
189 | jLabel = new JLabel();\r | |
190 | jLabel.setBounds(new java.awt.Rectangle(30, 70, 200, 20));\r | |
191 | jLabel.setText("Select delete mode: ");\r | |
192 | jContentPane = new JPanel();\r | |
193 | jContentPane.setLayout(null);\r | |
194 | jContentPane.add(getJButtonCancel(), null);\r | |
195 | jContentPane.add(getJButtonFinish(), null);\r | |
196 | jContentPane.add(getJButtonPrevious(), null);\r | |
197 | jContentPane.add(getJTextArea(), null);\r | |
198 | jContentPane.add(jLabel, null);\r | |
199 | ButtonGroup group = new ButtonGroup();\r | |
200 | group.add(getJRadioButton());\r | |
201 | group.add(getJRadioButton1());\r | |
202 | jContentPane.add(getJRadioButton(), null);\r | |
203 | jContentPane.add(getJRadioButton1(), null);\r | |
204 | }\r | |
205 | return jContentPane;\r | |
206 | }\r | |
207 | \r | |
208 | public void mouseClicked(MouseEvent e) {\r | |
209 | if (e.getSource() == jButtonCancel) {\r | |
210 | this.setVisible(false);\r | |
211 | this.dispose();\r | |
212 | } else if (e.getSource() == jButtonFinish) {\r | |
213 | FarIdentification far = stepOne.getSelecedFar();\r | |
214 | WorkspaceTools wt = new WorkspaceTools();\r | |
215 | //\r | |
216 | // If remove all source files\r | |
217 | //\r | |
218 | if (jRadioButton1.isSelected()) {\r | |
219 | \r | |
220 | Vector<PackageIdentification> removePackages = wt.getPackagesByFar(far);\r | |
221 | Vector<PlatformIdentification> removePlatforms = wt.getPlatformsByFar(far);\r | |
222 | \r | |
223 | Vector<PlatformIdentification> allPlatforms = wt.getAllPlatforms();\r | |
224 | Set<File> allPlatformFiles = new LinkedHashSet<File>();\r | |
225 | \r | |
226 | Iterator<PlatformIdentification> iter = allPlatforms.iterator();\r | |
227 | while (iter.hasNext()) {\r | |
228 | allPlatformFiles.add(iter.next().getFpdFile());\r | |
229 | }\r | |
230 | \r | |
231 | //\r | |
232 | // For all platforms, only remove its FPD file\r | |
233 | //\r | |
234 | Iterator<PlatformIdentification> platfomrIter = removePlatforms.iterator();\r | |
235 | while (platfomrIter.hasNext()) {\r | |
236 | PlatformIdentification item = platfomrIter.next();\r | |
237 | allPlatformFiles.remove(item.getFpdFile());\r | |
238 | File parentDir = item.getFpdFile().getParentFile();\r | |
239 | item.getFpdFile().delete();\r | |
240 | //\r | |
241 | // Remove all empty parent dir\r | |
242 | //\r | |
243 | while (parentDir.listFiles().length == 0) {\r | |
244 | File tempFile = parentDir;\r | |
245 | parentDir = parentDir.getParentFile();\r | |
246 | tempFile.delete();\r | |
247 | }\r | |
248 | }\r | |
249 | \r | |
250 | //\r | |
251 | // For all packages, remove all files. \r | |
252 | // Exception FPD file still in DB\r | |
253 | //\r | |
254 | \r | |
255 | Iterator<PackageIdentification> packageIter = removePackages.iterator();\r | |
256 | while (packageIter.hasNext()) {\r | |
257 | PackageIdentification item = packageIter.next();\r | |
258 | Set<File> deleteFiles = new LinkedHashSet<File>();\r | |
259 | recursiveDir(deleteFiles, item.getSpdFile().getParentFile(), allPlatformFiles);\r | |
260 | Iterator<File> iterDeleteFile = deleteFiles.iterator();\r | |
261 | while (iterDeleteFile.hasNext()) {\r | |
262 | deleteFiles(iterDeleteFile.next());\r | |
263 | }\r | |
264 | //\r | |
265 | // Remove all empty parent dir\r | |
266 | //\r | |
267 | File parentDir = item.getSpdFile().getParentFile();\r | |
268 | while (parentDir.listFiles().length == 0) {\r | |
269 | File tempFile = parentDir;\r | |
270 | parentDir = parentDir.getParentFile();\r | |
271 | tempFile.delete();\r | |
272 | }\r | |
273 | }\r | |
274 | }\r | |
275 | \r | |
276 | //\r | |
277 | // Update DB file\r | |
278 | //\r | |
279 | wt.removeFarFromDb(far);\r | |
280 | \r | |
281 | this.setVisible(false);\r | |
282 | this.stepOne.returnType = DataType.RETURN_TYPE_OK;\r | |
283 | this.dispose();\r | |
284 | } else if (e.getSource() == jButtonPrevious) {\r | |
285 | this.setVisible(false);\r | |
286 | stepOne.setVisible(true);\r | |
287 | }\r | |
288 | }\r | |
289 | \r | |
290 | public void mousePressed(MouseEvent e) {\r | |
291 | // TODO Auto-generated method stub\r | |
292 | \r | |
293 | }\r | |
294 | \r | |
295 | public void mouseReleased(MouseEvent e) {\r | |
296 | // TODO Auto-generated method stub\r | |
297 | \r | |
298 | }\r | |
299 | \r | |
300 | public void mouseEntered(MouseEvent e) {\r | |
301 | // TODO Auto-generated method stub\r | |
302 | \r | |
303 | }\r | |
304 | \r | |
305 | public void mouseExited(MouseEvent e) {\r | |
306 | // TODO Auto-generated method stub\r | |
307 | \r | |
308 | }\r | |
309 | \r | |
310 | private void recursiveDir(Set<File> files, File dir, Set<File> platformFiles) {\r | |
311 | File[] fileList = dir.listFiles();\r | |
312 | for (int i = 0; i < fileList.length; i++) {\r | |
313 | if (fileList[i].isFile()) {\r | |
314 | if (!platformFiles.contains(fileList[i])) {\r | |
315 | files.add(fileList[i]);\r | |
316 | }\r | |
317 | } else {\r | |
318 | if (isContain(fileList[i], platformFiles)) {\r | |
319 | recursiveDir(files, fileList[i], platformFiles);\r | |
320 | } else {\r | |
321 | files.add(fileList[i]);\r | |
322 | }\r | |
323 | }\r | |
324 | }\r | |
325 | }\r | |
326 | \r | |
327 | private void deleteFiles(File file) {\r | |
328 | if (file.isDirectory()) {\r | |
329 | File[] files = file.listFiles();\r | |
330 | for (int i = 0; i < files.length; i++) {\r | |
331 | deleteFiles(files[i]);\r | |
332 | }\r | |
333 | }\r | |
334 | file.delete();\r | |
335 | }\r | |
336 | \r | |
337 | private boolean isContain(File dir, Set<File> platformFiles) {\r | |
338 | Iterator<File> iter = platformFiles.iterator();\r | |
339 | while (iter.hasNext()) {\r | |
340 | File file = iter.next();\r | |
341 | if (file.getPath().startsWith(dir.getPath())) {\r | |
342 | //\r | |
343 | // continue this FPD file\r | |
344 | //\r | |
345 | return true;\r | |
346 | }\r | |
347 | }\r | |
348 | return false;\r | |
349 | }\r | |
350 | }\r |