Révision 2449
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/results/DeleteObject.java (revision 2449) | ||
---|---|---|
28 | 28 |
package org.txm.rcp.handlers.results; |
29 | 29 |
|
30 | 30 |
import java.util.ArrayList; |
31 |
import java.util.Collection; |
|
32 | 31 |
import java.util.HashSet; |
33 | 32 |
import java.util.List; |
34 | 33 |
|
... | ... | |
46 | 45 |
import org.eclipse.swt.SWT; |
47 | 46 |
import org.eclipse.swt.widgets.Display; |
48 | 47 |
import org.eclipse.swt.widgets.MessageBox; |
49 |
import org.eclipse.ui.IEditorPart; |
|
50 |
import org.eclipse.ui.IEditorReference; |
|
51 | 48 |
import org.eclipse.ui.IWorkbenchPage; |
52 | 49 |
import org.eclipse.ui.IWorkbenchWindow; |
53 | 50 |
import org.eclipse.ui.handlers.HandlerUtil; |
54 | 51 |
import org.eclipse.ui.internal.Workbench; |
55 |
import org.txm.Toolbox; |
|
56 |
import org.txm.core.engines.EngineType; |
|
57 | 52 |
import org.txm.core.messages.TXMCoreMessages; |
58 | 53 |
import org.txm.core.results.TXMResult; |
59 | 54 |
import org.txm.objects.CorpusBuild; |
60 | 55 |
import org.txm.objects.Project; |
61 | 56 |
import org.txm.rcp.StatusLine; |
62 | 57 |
import org.txm.rcp.editors.ITXMResultEditor; |
63 |
import org.txm.rcp.editors.TXMEditor; |
|
64 | 58 |
import org.txm.rcp.messages.TXMUIMessages; |
65 | 59 |
import org.txm.rcp.preferences.RCPPreferences; |
66 | 60 |
import org.txm.rcp.utils.JobHandler; |
67 | 61 |
import org.txm.rcp.utils.SWTEditorsUtils; |
68 | 62 |
import org.txm.rcp.views.corpora.CorporaView; |
69 |
import org.txm.searchengine.cqp.corpus.CQPCorpus; |
|
70 | 63 |
import org.txm.searchengine.cqp.corpus.MainCorpus; |
71 |
import org.txm.searchengine.cqp.corpus.Partition; |
|
72 | 64 |
import org.txm.utils.LogMonitor; |
73 | 65 |
import org.txm.utils.logger.Log; |
74 | 66 |
|
... | ... | |
79 | 71 |
* |
80 | 72 |
*/ |
81 | 73 |
public class DeleteObject extends AbstractHandler { |
82 |
|
|
74 |
|
|
83 | 75 |
private static boolean shouldRestartToolbox = false; |
84 |
|
|
76 |
|
|
85 | 77 |
private static boolean continueToDelete = true; |
86 |
|
|
87 |
|
|
88 |
/* (non-Javadoc) |
|
78 |
|
|
79 |
|
|
80 |
/* |
|
81 |
* (non-Javadoc) |
|
89 | 82 |
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
90 | 83 |
*/ |
91 | 84 |
@Override |
92 | 85 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
93 |
|
|
86 |
|
|
94 | 87 |
final IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event); |
95 |
|
|
88 |
|
|
96 | 89 |
if (!askContinueToDelete(selection)) { |
97 | 90 |
return null; |
98 | 91 |
} |
99 |
|
|
92 |
|
|
100 | 93 |
JobHandler job = new JobHandler("Deleting", true) { |
94 |
|
|
101 | 95 |
@Override |
102 | 96 |
protected IStatus run(IProgressMonitor monitor) { |
103 | 97 |
delete(selection); |
... | ... | |
107 | 101 |
job.schedule(); |
108 | 102 |
try { |
109 | 103 |
job.join(); |
110 |
} catch (InterruptedException e) { |
|
104 |
} |
|
105 |
catch (InterruptedException e) { |
|
111 | 106 |
e.printStackTrace(); |
112 | 107 |
return Status.CANCEL_STATUS; |
113 | 108 |
} |
114 | 109 |
return job.getResultObject(); |
115 | 110 |
} |
116 |
|
|
111 |
|
|
117 | 112 |
/** |
118 | 113 |
* |
119 | 114 |
* @param selection |
... | ... | |
121 | 116 |
*/ |
122 | 117 |
public static synchronized boolean askContinueToDelete(final IStructuredSelection selection) { |
123 | 118 |
continueToDelete = true; |
124 |
|
|
119 |
|
|
125 | 120 |
boolean showAlert = !RCPPreferences.getInstance().getBoolean(RCPPreferences.USER_ALERT_DELETE); |
126 |
|
|
121 |
|
|
127 | 122 |
if (showAlert) { |
128 | 123 |
Display.getDefault().syncExec(new Runnable() { |
124 |
|
|
129 | 125 |
@Override |
130 | 126 |
public void run() { |
131 | 127 |
MessageBox messageBox = new MessageBox(Display.getDefault().getActiveShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO); |
... | ... | |
140 | 136 |
} |
141 | 137 |
return continueToDelete; |
142 | 138 |
} |
143 |
|
|
139 |
|
|
144 | 140 |
/** |
145 | 141 |
* Deletes. |
146 | 142 |
* |
... | ... | |
148 | 144 |
*/ |
149 | 145 |
public static HashSet<Object> delete(ISelection sel) { |
150 | 146 |
IStructuredSelection selection = (IStructuredSelection) sel; |
151 |
|
|
147 |
|
|
152 | 148 |
if (selection.size() == 0) { |
153 |
return new HashSet<Object>();
|
|
149 |
return new HashSet<>(); |
|
154 | 150 |
} |
155 |
|
|
151 |
|
|
156 | 152 |
HashSet<Object> objects = new HashSet<Object>(selection.toList()); |
157 |
HashSet<Object> hidden = new HashSet<Object>();
|
|
153 |
HashSet<Object> hidden = new HashSet<>(); |
|
158 | 154 |
for (Object o : objects) { |
159 | 155 |
if (o != null && o instanceof TXMResult) { |
160 | 156 |
TXMResult r = (TXMResult) o; |
... | ... | |
166 | 162 |
} |
167 | 163 |
} |
168 | 164 |
objects.addAll(hidden); |
169 |
return delete(new ArrayList<Object>(objects));
|
|
165 |
return delete(new ArrayList<>(objects)); |
|
170 | 166 |
} |
171 |
|
|
172 |
|
|
167 |
|
|
168 |
|
|
173 | 169 |
/** |
174 | 170 |
* Deletes. |
175 | 171 |
* |
176 | 172 |
* TODO: select the parent object of the deleted object |
177 | 173 |
* |
178 | 174 |
* @param objects the objects to delete |
179 |
* @return
|
|
175 |
* @return |
|
180 | 176 |
*/ |
181 | 177 |
public synchronized static HashSet<Object> delete(final List<Object> objects) { |
182 | 178 |
StatusLine.setMessage(NLS.bind(TXMUIMessages.deletingP0, objects)); |
183 |
|
|
184 |
final HashSet<Object> allObjectDeleted = new HashSet<Object>();
|
|
179 |
|
|
180 |
final HashSet<Object> allObjectDeleted = new HashSet<>(); |
|
185 | 181 |
shouldRestartToolbox = false; |
186 | 182 |
while (objects.size() > 0) { |
187 | 183 |
Object o = objects.get(0); |
... | ... | |
189 | 185 |
List<Object> deleted = delete(o); |
190 | 186 |
allObjectDeleted.addAll(deleted); |
191 | 187 |
if (deleted.size() == 0) { |
192 |
StatusLine.setMessage(TXMUIMessages.errorColonDeleteReturnedFalse+" : "+o);
|
|
193 |
return new HashSet<Object>();
|
|
188 |
StatusLine.setMessage(TXMUIMessages.errorColonDeleteReturnedFalse + " : " + o);
|
|
189 |
return new HashSet<>(); |
|
194 | 190 |
} |
195 | 191 |
// remove results already deleted |
196 | 192 |
objects.removeAll(deleted); |
197 |
} catch(Exception e) { |
|
198 |
Log.severe(NLS.bind(TXMUIMessages.errorWhileDeletingP0ColonP1, o, e)); |
|
199 |
StatusLine.error(NLS.bind(TXMUIMessages.errorWhileDeletingP0ColonP1, o, e)); |
|
193 |
} |
|
194 |
catch (Exception e) { |
|
195 |
Log.severe(NLS.bind(TXMUIMessages.errorWhileDeletingP0ColonP1, o, e)); |
|
196 |
StatusLine.error(NLS.bind(TXMUIMessages.errorWhileDeletingP0ColonP1, o, e)); |
|
200 | 197 |
org.txm.utils.logger.Log.printStackTrace(e); |
201 |
return new HashSet<Object>();
|
|
198 |
return new HashSet<>(); |
|
202 | 199 |
} |
203 | 200 |
} |
204 |
|
|
201 |
|
|
205 | 202 |
if (allObjectDeleted.size() > 0) { |
206 |
|
|
203 |
|
|
207 | 204 |
if (shouldRestartToolbox) { |
208 | 205 |
StatusLine.setMessage(TXMUIMessages.info_txmIsReady); |
209 | 206 |
} |
210 |
|
|
207 |
|
|
211 | 208 |
Display.getDefault().syncExec(new Runnable() { |
209 |
|
|
212 | 210 |
@Override |
213 | 211 |
public void run() { |
214 | 212 |
try { |
215 | 213 |
// FIXME: why this is commented? became useless? I needed to add the line below for the refresh CorporaView.refresh(); |
216 |
// RestartTXM.reloadViews();
|
|
217 |
|
|
214 |
// RestartTXM.reloadViews();
|
|
215 |
|
|
218 | 216 |
CorporaView.refresh(); |
219 |
|
|
220 |
//close corresponding editors |
|
221 |
for(Object obj : allObjectDeleted) { |
|
217 |
|
|
218 |
// close corresponding editors
|
|
219 |
for (Object obj : allObjectDeleted) {
|
|
222 | 220 |
closeEditorOf(obj, false); |
223 | 221 |
} |
224 |
} catch(Exception e) { |
|
225 | 222 |
} |
223 |
catch (Exception e) { |
|
224 |
} |
|
226 | 225 |
} |
227 | 226 |
}); |
228 | 227 |
} |
229 |
|
|
228 |
|
|
230 | 229 |
StatusLine.setMessage(""); //$NON-NLS-1$ |
231 | 230 |
return allObjectDeleted; |
232 | 231 |
} |
233 |
|
|
232 |
|
|
234 | 233 |
/** |
235 | 234 |
* Deletes the specified object. |
236 | 235 |
* |
... | ... | |
239 | 238 |
* @param o the o |
240 | 239 |
* @return the list |
241 | 240 |
*/ |
242 |
// FIXME: when TXMResult will be implemented, remove all unused code |
|
243 | 241 |
public static List<Object> delete(Object o) { |
244 |
ArrayList<Object> deleted = new ArrayList<Object>(); |
|
245 |
// if (o instanceof TXMResult) { |
|
246 |
// Log.info(NLS.bind(TXMUIMessages.deletedColonP0, ((TXMResult) o).getName())); |
|
247 |
// |
|
248 |
// try { |
|
249 |
// if (o instanceof CQPCorpus) { |
|
250 |
// CQPCorpus c = (CQPCorpus)o; |
|
251 |
// if (o instanceof MainCorpus) { |
|
252 |
// shouldRestartToolbox = true; |
|
253 |
// } |
|
254 |
// deleted.addAll(c.clearResults()); |
|
255 |
// deleted.addAll(c.getSubcorpora()); |
|
256 |
// deleted.addAll(c.getPartitions()); |
|
257 |
// |
|
258 |
// if (!((TXMResult) o).delete()) { |
|
259 |
// Log.info(TXMUIMessages.errorColonDeleteReturnedFalse); |
|
260 |
// return new ArrayList<Object>(0); |
|
261 |
// } |
|
262 |
// } else if (o instanceof Partition) { |
|
263 |
// Partition p = (Partition)o; |
|
264 |
// for (int i = 0 ; i < p.getChildren().size() ; i++) { |
|
265 |
// deleted.addAll(delete(p.getChildren().get(i))); |
|
266 |
// i--; |
|
267 |
// } |
|
268 |
// if (!((TXMResult) o).delete()) { |
|
269 |
// Log.info(TXMUIMessages.errorColonDeleteReturnedFalse); |
|
270 |
// return new ArrayList<Object>(0); |
|
271 |
// } |
|
272 |
// } |
|
273 |
// } catch (Exception e) { |
|
274 |
// org.txm.utils.logger.Log.printStackTrace(e); |
|
275 |
// } |
|
276 |
// } |
|
277 |
// // FIXME: when TXMResult will be implemented, only this code must be left |
|
242 |
ArrayList<Object> deleted = new ArrayList<>(); |
|
278 | 243 |
if (o instanceof TXMResult) { |
279 |
TXMResult r = (TXMResult)o; |
|
244 |
TXMResult r = (TXMResult) o;
|
|
280 | 245 |
List<TXMResult> children = r.getDeepChildren(); |
281 | 246 |
if (r.isLocked()) return deleted; |
282 |
|
|
247 |
|
|
283 | 248 |
if (r instanceof MainCorpus) { |
284 |
Project p = ((MainCorpus)o).getProject(); |
|
249 |
Project p = ((MainCorpus) o).getProject();
|
|
285 | 250 |
if (p != null) { |
286 | 251 |
p.delete(); |
287 |
} else { |
|
288 |
((MainCorpus)o).delete(); |
|
289 | 252 |
} |
290 |
} else { |
|
253 |
else { |
|
254 |
((MainCorpus) o).delete(); |
|
255 |
} |
|
256 |
} |
|
257 |
else { |
|
291 | 258 |
r.delete(); |
292 | 259 |
} |
293 |
Log.info(NLS.bind(TXMUIMessages.deletedColonP0, o)); |
|
294 | 260 |
deleted.add(o); |
295 | 261 |
deleted.addAll(children); |
296 |
} else if (o instanceof IProject) { |
|
262 |
} |
|
263 |
else if (o instanceof IProject) { |
|
297 | 264 |
try { |
298 |
((IProject)o).delete(true, new LogMonitor()); |
|
265 |
((IProject) o).delete(true, new LogMonitor());
|
|
299 | 266 |
deleted.add(o); |
300 |
} catch (CoreException e) { |
|
267 |
} |
|
268 |
catch (CoreException e) { |
|
301 | 269 |
// TODO Auto-generated catch block |
302 | 270 |
e.printStackTrace(); |
303 | 271 |
} |
304 |
} else { |
|
272 |
} |
|
273 |
else { |
|
305 | 274 |
Log.severe(TXMCoreMessages.bind("Can not delete: {0}.", o)); |
306 | 275 |
} |
307 |
|
|
276 |
|
|
308 | 277 |
return deleted; |
309 | 278 |
} |
310 |
|
|
279 |
|
|
311 | 280 |
/** |
312 | 281 |
* Close the editors which uses an Object. |
313 | 282 |
* |
... | ... | |
318 | 287 |
if (!(o instanceof TXMResult)) { |
319 | 288 |
return; |
320 | 289 |
} |
321 |
|
|
290 |
|
|
322 | 291 |
TXMResult r = (TXMResult) o; |
323 |
List<TXMResult> all = new ArrayList<TXMResult>();//r.getDeepChildren();
|
|
292 |
List<TXMResult> all = new ArrayList<>();// r.getDeepChildren();
|
|
324 | 293 |
all.add(r); |
325 | 294 |
if (children) { |
326 | 295 |
all.addAll(r.getDeepChildren()); |
... | ... | |
328 | 297 |
ArrayList<ITXMResultEditor<?>> editors = SWTEditorsUtils.getEditors(); |
329 | 298 |
for (TXMResult tr : all) { // close editors of the result and its children |
330 | 299 |
for (ITXMResultEditor<?> editor : editors) { |
331 |
if (tr == editor.getResult()) {
|
|
300 |
if (tr == editor.getResult()) {
|
|
332 | 301 |
editor.getEditorSite().getPage().closeEditor(editor, true); |
333 | 302 |
} |
334 | 303 |
} |
335 | 304 |
} |
336 | 305 |
} |
337 |
|
|
306 |
|
|
338 | 307 |
/** |
339 | 308 |
* Gets the pages. |
340 | 309 |
* |
... | ... | |
346 | 315 |
} |
347 | 316 |
return new IWorkbenchPage[0]; |
348 | 317 |
} |
349 |
} |
|
318 |
} |
Formats disponibles : Unified diff