Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.modules.project.ant; |
15 |
|
16 |
import java.awt.Dialog; |
17 |
import java.awt.GridBagConstraints; |
18 |
import java.awt.Insets; |
19 |
import java.io.IOException; |
20 |
import java.text.MessageFormat; |
21 |
import java.util.ArrayList; |
22 |
import java.util.Collections; |
23 |
import java.util.Iterator; |
24 |
import java.util.List; |
25 |
import javax.swing.JComponent; |
26 |
import javax.swing.JLabel; |
27 |
import javax.swing.JPanel; |
28 |
import javax.swing.SwingUtilities; |
29 |
import org.netbeans.api.progress.ProgressHandle; |
30 |
import org.netbeans.api.progress.ProgressHandleFactory; |
31 |
import org.netbeans.api.project.Project; |
32 |
import org.netbeans.api.project.ProjectUtils; |
33 |
import org.netbeans.api.project.ui.OpenProjects; |
34 |
import org.netbeans.api.project.ProjectOperations; |
35 |
import org.netbeans.spi.project.support.ant.AntProjectHelper; |
36 |
import org.openide.DialogDescriptor; |
37 |
import org.openide.DialogDisplayer; |
38 |
import org.openide.ErrorManager; |
39 |
import org.openide.NotifyDescriptor; |
40 |
import org.openide.filesystems.FileObject; |
41 |
import org.openide.filesystems.FileUtil; |
42 |
import org.openide.util.HelpCtx; |
43 |
import org.openide.util.NbBundle; |
44 |
import org.openide.util.RequestProcessor; |
45 |
|
46 |
|
47 |
/** |
48 |
* |
49 |
* @author Jan Lahoda |
50 |
*/ |
51 |
public final class DefaultAntProjectOperation { |
52 |
|
53 |
private static final ErrorManager ERR = ErrorManager.getDefault(); // NOI18N |
54 |
|
55 |
private DefaultAntProjectOperation() { |
56 |
} |
57 |
|
58 |
private static String getDisplayName(Project project) { |
59 |
return ProjectUtils.getInformation(project).getDisplayName(); |
60 |
} |
61 |
|
62 |
//<editor-fold defaultstate="collapsed" desc="Delete Operation"> |
63 |
/** |
64 |
* @return true if success |
65 |
*/ |
66 |
private static boolean performDelete(AntProjectHelper helper, Project project, List/*FileObject>*/ toDelete, ProgressHandle handle) { |
67 |
try { |
68 |
String progressFormat = NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Progress_Deleting_File"); |
69 |
|
70 |
handle.start(toDelete.size() + 1 /*clean*/); |
71 |
|
72 |
int done = 0; |
73 |
|
74 |
handle.progress(NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Progress_Cleaning_Project")); |
75 |
|
76 |
ProjectOperations.getDefault().performClean(project); |
77 |
|
78 |
handle.progress(++done); |
79 |
|
80 |
for (Iterator i = toDelete.iterator(); i.hasNext(); ) { |
81 |
FileObject f = (FileObject) i.next(); |
82 |
|
83 |
handle.progress(MessageFormat.format(progressFormat, new Object[] {FileUtil.getFileDisplayName(f)})); |
84 |
|
85 |
if (f != null) |
86 |
f.delete(); |
87 |
|
88 |
handle.progress(++done); |
89 |
} |
90 |
|
91 |
FileObject projectFolder = project.getProjectDirectory(); |
92 |
|
93 |
if (projectFolder.getChildren().length == 0) { |
94 |
//empty, delete: |
95 |
projectFolder.delete(); |
96 |
} |
97 |
|
98 |
handle.finish(); |
99 |
|
100 |
ProjectOperations.getDefault().notifyDeleted(project); |
101 |
return true; |
102 |
} catch (IOException e) { |
103 |
String displayName = getDisplayName(project); |
104 |
String message = NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Project_cannot_be_deleted.", new Object[] {displayName}); |
105 |
|
106 |
ErrorManager.getDefault().annotate(e, message); |
107 |
ErrorManager.getDefault().notify(ErrorManager.USER, e); |
108 |
|
109 |
return false; |
110 |
} |
111 |
} |
112 |
|
113 |
public static void deleteProject(final Project project) { |
114 |
Runnable r = new Runnable() { |
115 |
public void run() { |
116 |
deleteProject(project, new GUIUserInputHandler()); |
117 |
} |
118 |
}; |
119 |
|
120 |
if (SwingUtilities.isEventDispatchThread()) { |
121 |
r.run(); |
122 |
} else { |
123 |
SwingUtilities.invokeLater(r); |
124 |
} |
125 |
} |
126 |
|
127 |
/*package private*/static void deleteProject(final Project project, final UserInputHandler handler) { |
128 |
String displayName = getDisplayName(project); |
129 |
String projectFolder = FileUtil.getFileDisplayName(project.getProjectDirectory()); |
130 |
|
131 |
final AntProjectHelper helper = AntBasedProjectFactorySingleton.getHelperFor(project); |
132 |
|
133 |
assert helper != null; |
134 |
|
135 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
136 |
ERR.log(ErrorManager.INFORMATIONAL, "delete started: " + displayName); // NOI18N |
137 |
} |
138 |
|
139 |
List/*<FileObject>*/ metadataFiles = ProjectOperations.getDefault().getMetadataFiles(project); |
140 |
List/*<FileObject>*/ dataFiles = ProjectOperations.getDefault().getDataFiles(project); |
141 |
List/*<FileObject>*/ allFiles = new ArrayList/*<FileObject>*/(); |
142 |
|
143 |
allFiles.addAll(metadataFiles); |
144 |
allFiles.addAll(dataFiles); |
145 |
|
146 |
int userAnswer = handler.userConfirmation(displayName, projectFolder, !dataFiles.isEmpty()); |
147 |
List/*<FileObject>*/ toDeleteImpl = null; |
148 |
|
149 |
switch (userAnswer) { |
150 |
case UserInputHandler.USER_CANCEL: |
151 |
return ; |
152 |
case UserInputHandler.USER_OK_METADATA: |
153 |
toDeleteImpl = metadataFiles; |
154 |
break; |
155 |
case UserInputHandler.USER_OK_ALL: |
156 |
toDeleteImpl = allFiles; |
157 |
break; |
158 |
default: |
159 |
throw new IllegalStateException("Invalid user answer: " + userAnswer); |
160 |
} |
161 |
|
162 |
final ProgressHandle handle = handler.getProgressHandle(); |
163 |
final List/*<FileObject>*/ toDelete = toDeleteImpl; |
164 |
final boolean[] result = new boolean[1]; |
165 |
|
166 |
OpenProjects.getDefault().close(new Project[] {project}); |
167 |
|
168 |
handler.delete(new Runnable() { |
169 |
public void run() { |
170 |
result[0] = performDelete(helper, project, toDelete, handle); |
171 |
} |
172 |
}); |
173 |
|
174 |
/* if (result[0]) { |
175 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
176 |
ERR.log(ErrorManager.INFORMATIONAL, "delete successfull"); // NOI18N |
177 |
} |
178 |
|
179 |
String message = NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Project_Deleted_Caption", new Object[] {displayName}); |
180 |
NotifyDescriptor nd = new NotifyDescriptor.Message(message); |
181 |
|
182 |
DialogDisplayer.getDefault().notify(nd); |
183 |
}*/ |
184 |
|
185 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
186 |
ERR.log(ErrorManager.INFORMATIONAL, "delete done: " + displayName); // NOI18N |
187 |
} |
188 |
} |
189 |
|
190 |
/*package private*/interface UserInputHandler { |
191 |
|
192 |
public int USER_CANCEL = 1; |
193 |
public int USER_OK_METADATA = 2; |
194 |
public int USER_OK_ALL = 3; |
195 |
|
196 |
public abstract int userConfirmation(String displayName, String projectFolder, boolean enableData); |
197 |
|
198 |
public abstract ProgressHandle getProgressHandle(); |
199 |
|
200 |
public abstract void delete(Runnable r); |
201 |
|
202 |
} |
203 |
|
204 |
private static final class GUIUserInputHandler implements UserInputHandler { |
205 |
|
206 |
public int userConfirmation(String displayName, String projectFolder, boolean enableData) { |
207 |
DefaultAntProjectDeletePanel deletePanel = new DefaultAntProjectDeletePanel(displayName, projectFolder, enableData); |
208 |
|
209 |
String caption = NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Delete_Project_Caption"); |
210 |
|
211 |
DialogDescriptor dd = new DialogDescriptor(deletePanel, caption); |
212 |
|
213 |
DialogDisplayer.getDefault().createDialog(dd).setVisible(true); |
214 |
|
215 |
if (dd.getValue() == DialogDescriptor.OK_OPTION) { |
216 |
if (deletePanel.isDeleteSources()) { |
217 |
return USER_OK_ALL; |
218 |
} else { |
219 |
return USER_OK_METADATA; |
220 |
} |
221 |
} else { |
222 |
return USER_CANCEL; |
223 |
} |
224 |
} |
225 |
|
226 |
private ProgressHandle handle = null; |
227 |
|
228 |
public synchronized ProgressHandle getProgressHandle() { |
229 |
if (handle == null) { |
230 |
handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Delete_Project_Caption")); |
231 |
} |
232 |
|
233 |
return handle; |
234 |
} |
235 |
|
236 |
public void delete(final Runnable r) { |
237 |
String inProgressCaption = NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Project_Deletion_in_Progress_Caption"); |
238 |
DialogDescriptor dd2 = new DialogDescriptor(createProgressDialog(handle), inProgressCaption, true, new Object[0], null, DialogDescriptor.BOTTOM_ALIGN, HelpCtx.DEFAULT_HELP, null); |
239 |
|
240 |
final Dialog dialog = DialogDisplayer.getDefault().createDialog(dd2); |
241 |
final boolean[] result = new boolean[1]; |
242 |
|
243 |
RequestProcessor.getDefault().post(new Runnable() { |
244 |
public void run() { |
245 |
r.run(); |
246 |
|
247 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
248 |
ERR.log(ErrorManager.INFORMATIONAL, "delete finished"); // NOI18N |
249 |
} |
250 |
|
251 |
SwingUtilities.invokeLater(new Runnable() { |
252 |
public void run() { |
253 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
254 |
ERR.log(ErrorManager.INFORMATIONAL, "calling hide on progress dialog"); // NOI18N |
255 |
} |
256 |
|
257 |
dialog.hide(); |
258 |
|
259 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
260 |
ERR.log(ErrorManager.INFORMATIONAL, "done"); // NOI18N |
261 |
} |
262 |
} |
263 |
}); |
264 |
} |
265 |
}); |
266 |
|
267 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
268 |
ERR.log(ErrorManager.INFORMATIONAL, "showing progress dialog"); // NOI18N |
269 |
} |
270 |
|
271 |
dialog.show(); |
272 |
|
273 |
if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { |
274 |
ERR.log(ErrorManager.INFORMATIONAL, "progress dialog hidden"); // NOI18N |
275 |
} |
276 |
} |
277 |
|
278 |
} |
279 |
|
280 |
private static JComponent createProgressDialog(ProgressHandle handle) { |
281 |
JPanel dialog = new JPanel(); |
282 |
|
283 |
GridBagConstraints gridBagConstraints; |
284 |
|
285 |
JLabel jLabel1 = new JLabel(); |
286 |
JComponent progress = ProgressHandleFactory.createProgressComponent(handle); |
287 |
JPanel padding = new JPanel(); |
288 |
|
289 |
dialog.setLayout(new java.awt.GridBagLayout()); |
290 |
|
291 |
jLabel1.setText(NbBundle.getMessage(DefaultAntProjectOperation.class, "LBL_Deleting_Project")); |
292 |
gridBagConstraints = new GridBagConstraints(); |
293 |
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; |
294 |
gridBagConstraints.insets = new Insets(12, 12, 0, 12); |
295 |
dialog.add(jLabel1, gridBagConstraints); |
296 |
|
297 |
gridBagConstraints = new GridBagConstraints(); |
298 |
gridBagConstraints.gridx = 0; |
299 |
gridBagConstraints.gridy = 1; |
300 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; |
301 |
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; |
302 |
gridBagConstraints.weightx = 1.0; |
303 |
gridBagConstraints.insets = new Insets(12, 12, 0, 12); |
304 |
dialog.add(progress, gridBagConstraints); |
305 |
|
306 |
gridBagConstraints = new GridBagConstraints(); |
307 |
gridBagConstraints.gridx = 0; |
308 |
gridBagConstraints.gridy = 2; |
309 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL | GridBagConstraints.VERTICAL; |
310 |
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; |
311 |
gridBagConstraints.weightx = 1.0; |
312 |
gridBagConstraints.weighty = 1.0; |
313 |
gridBagConstraints.insets = new Insets(12, 12, 12, 12); |
314 |
dialog.add(padding, gridBagConstraints); |
315 |
|
316 |
return dialog; |
317 |
} |
318 |
//</editor-fold> |
319 |
|
320 |
} |