1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.model;
|
11
|
|
12
|
import java.io.BufferedWriter;
|
13
|
import java.io.File;
|
14
|
import java.io.FileOutputStream;
|
15
|
import java.io.OutputStreamWriter;
|
16
|
import java.io.Writer;
|
17
|
import java.lang.reflect.InvocationTargetException;
|
18
|
import java.text.SimpleDateFormat;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.Calendar;
|
21
|
import java.util.Collection;
|
22
|
import java.util.LinkedHashMap;
|
23
|
import java.util.List;
|
24
|
import java.util.Map;
|
25
|
import java.util.Set;
|
26
|
import java.util.TreeSet;
|
27
|
import java.util.UUID;
|
28
|
import java.util.zip.ZipEntry;
|
29
|
import java.util.zip.ZipOutputStream;
|
30
|
|
31
|
import org.apache.commons.lang.StringUtils;
|
32
|
import org.apache.log4j.Logger;
|
33
|
import org.eclipse.core.commands.ExecutionException;
|
34
|
import org.eclipse.core.commands.NotEnabledException;
|
35
|
import org.eclipse.core.commands.NotHandledException;
|
36
|
import org.eclipse.core.commands.common.NotDefinedException;
|
37
|
import org.eclipse.core.commands.operations.AbstractOperation;
|
38
|
import org.eclipse.core.commands.operations.IOperationHistory;
|
39
|
import org.eclipse.core.runtime.IAdaptable;
|
40
|
import org.eclipse.core.runtime.IProgressMonitor;
|
41
|
import org.eclipse.core.runtime.IStatus;
|
42
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
43
|
import org.eclipse.core.runtime.OperationCanceledException;
|
44
|
import org.eclipse.core.runtime.Status;
|
45
|
import org.eclipse.core.runtime.SubProgressMonitor;
|
46
|
import org.eclipse.core.runtime.jobs.ISchedulingRule;
|
47
|
import org.eclipse.core.runtime.jobs.Job;
|
48
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
49
|
import org.eclipse.jface.action.IStatusLineManager;
|
50
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
51
|
import org.eclipse.jface.resource.ColorRegistry;
|
52
|
import org.eclipse.jface.resource.FontRegistry;
|
53
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
54
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
55
|
import org.eclipse.swt.graphics.Color;
|
56
|
import org.eclipse.swt.graphics.Font;
|
57
|
import org.eclipse.swt.widgets.Display;
|
58
|
import org.eclipse.swt.widgets.Shell;
|
59
|
import org.eclipse.ui.IViewPart;
|
60
|
import org.eclipse.ui.IViewReference;
|
61
|
import org.eclipse.ui.IWorkbench;
|
62
|
import org.eclipse.ui.IWorkbenchPage;
|
63
|
import org.eclipse.ui.IWorkbenchWindow;
|
64
|
import org.eclipse.ui.PartInitException;
|
65
|
import org.eclipse.ui.PlatformUI;
|
66
|
import org.eclipse.ui.handlers.IHandlerService;
|
67
|
import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
|
68
|
import org.eclipse.ui.part.EditorPart;
|
69
|
import org.eclipse.ui.progress.IProgressConstants;
|
70
|
import org.eclipse.ui.progress.IProgressService;
|
71
|
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
|
72
|
import org.eclipse.ui.themes.ITheme;
|
73
|
import org.eclipse.ui.themes.IThemeManager;
|
74
|
|
75
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
76
|
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
|
77
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
78
|
import eu.etaxonomy.cdm.io.common.ExportDataWrapper;
|
79
|
import eu.etaxonomy.cdm.io.common.ExportResult;
|
80
|
import eu.etaxonomy.cdm.io.common.ExportResultType;
|
81
|
import eu.etaxonomy.cdm.io.common.ExportType;
|
82
|
import eu.etaxonomy.cdm.model.common.IEnumTerm;
|
83
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
84
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
85
|
import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
|
86
|
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
|
87
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
88
|
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
|
89
|
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
|
90
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
91
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
92
|
import eu.etaxonomy.taxeditor.ui.dialog.ReportTextDialog;
|
93
|
import eu.etaxonomy.taxeditor.view.AbstractCdmDataViewer;
|
94
|
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
|
95
|
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
|
96
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
97
|
|
98
|
/**
|
99
|
*
|
100
|
* @author n.hoffmann
|
101
|
* @created 11.05.2009
|
102
|
* @version 1.0
|
103
|
*/
|
104
|
public abstract class AbstractUtility {
|
105
|
|
106
|
private static final Logger logger = Logger.getLogger(AbstractUtility.class);
|
107
|
|
108
|
/** Constant <code>statusLineManager</code> */
|
109
|
protected static IStatusLineManager statusLineManager;
|
110
|
/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
|
111
|
public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
|
112
|
|
113
|
public static MPart getActivePart() {
|
114
|
return EventUtility.getActivePart();
|
115
|
}
|
116
|
|
117
|
public static boolean closeAll() {
|
118
|
if(getActivePage()!=null){
|
119
|
return getActivePage().closeAllEditors(true);
|
120
|
}
|
121
|
return false;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Close the given editor.
|
126
|
*
|
127
|
* @param editor
|
128
|
* The <tt>MultipageTaxonEditor</tt> to close.
|
129
|
* @return <tt>true</tt> on success
|
130
|
*/
|
131
|
public static boolean close(EditorPart editor) {
|
132
|
return getActivePage() != null ? getActivePage().closeEditor(editor, true):false;
|
133
|
}
|
134
|
|
135
|
public static Shell getShell() {
|
136
|
|
137
|
return TaxeditorStorePlugin.getDefault().getWorkbench()
|
138
|
.getActiveWorkbenchWindow().getShell();
|
139
|
}
|
140
|
|
141
|
public static IWorkbenchPage getActivePage() {
|
142
|
try{
|
143
|
return TaxeditorStorePlugin.getDefault().getWorkbench()
|
144
|
.getActiveWorkbenchWindow().getActivePage();
|
145
|
} catch(NullPointerException npe){
|
146
|
return null;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
public static Object getActiveE4Part() {
|
151
|
//TODO merge with getActivePart()
|
152
|
return getActivePart();
|
153
|
}
|
154
|
|
155
|
public static IWorkbench getWorkbench() {
|
156
|
return TaxeditorStorePlugin.getDefault().getWorkbench();
|
157
|
}
|
158
|
|
159
|
public static IWorkbenchWindow getWorkbenchWindow() {
|
160
|
if (getWorkbench().getWorkbenchWindowCount() > 1) {
|
161
|
throw new IllegalStateException("More than one workbench window");
|
162
|
}
|
163
|
return getWorkbench().getWorkbenchWindows()[0];
|
164
|
}
|
165
|
|
166
|
public static IViewPart showView(String id) {
|
167
|
try {
|
168
|
return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
|
169
|
.getActivePage()
|
170
|
.showView(id, null, IWorkbenchPage.VIEW_VISIBLE);
|
171
|
} catch (PartInitException e) {
|
172
|
MessagingUtils.messageDialog("Error opening view", AbstractUtility.class, "Could not open view: " + id, e);
|
173
|
return null;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
public static void hideView(IViewPart view) {
|
178
|
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
|
179
|
.hideView(view);
|
180
|
}
|
181
|
|
182
|
public static IViewPart getView(String id, boolean restore) {
|
183
|
IWorkbench workbench = PlatformUI.getWorkbench();
|
184
|
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
|
185
|
IViewReference[] references = null;
|
186
|
if(activeWorkbenchWindow!=null && activeWorkbenchWindow.getActivePage()!=null){
|
187
|
references = activeWorkbenchWindow.getActivePage().getViewReferences();
|
188
|
}
|
189
|
else if(workbench.getWorkbenchWindows().length>0 && workbench.getWorkbenchWindows()[0].getActivePage()!=null){
|
190
|
references = workbench.getWorkbenchWindows()[0].getActivePage().getViewReferences();
|
191
|
}
|
192
|
if(references!=null){
|
193
|
for (IViewReference reference : references) {
|
194
|
if (reference.getId().equals(id)) {
|
195
|
return reference.getView(restore);
|
196
|
}
|
197
|
}
|
198
|
}
|
199
|
return null;
|
200
|
}
|
201
|
|
202
|
public static Object getService(Class api) {
|
203
|
return TaxeditorStorePlugin.getDefault().getWorkbench().getService(api);
|
204
|
}
|
205
|
|
206
|
public static ITheme getCurrentTheme() {
|
207
|
IThemeManager themeManager = TaxeditorStorePlugin.getDefault()
|
208
|
.getWorkbench().getThemeManager();
|
209
|
return themeManager.getCurrentTheme();
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Fonts registered to the plugin may be obtained with the Eclipse themeing
|
214
|
* functionality. Thus fonts are chooseable by the user via
|
215
|
* Preferences->General->Appearance->Colors and Fonts
|
216
|
*
|
217
|
* @return the FontRegistry for the current theme
|
218
|
*/
|
219
|
public static FontRegistry getFontRegistry() {
|
220
|
return getCurrentTheme().getFontRegistry();
|
221
|
}
|
222
|
|
223
|
public static Font getFont(String symbolicName) {
|
224
|
return getFontRegistry().get(symbolicName);
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* Color registered to the plugin may be obtained with the Eclipse themeing
|
229
|
* functionality. Thus colors are editable by the user via
|
230
|
* Preferences->General->Appearance->Colors and Fonts
|
231
|
*
|
232
|
* @return the ColorRegistry for the current theme
|
233
|
*/
|
234
|
public static ColorRegistry getColorRegistry() {
|
235
|
return getCurrentTheme().getColorRegistry();
|
236
|
}
|
237
|
|
238
|
public static Color getColor(String symbolicName) {
|
239
|
return getColorRegistry().get(symbolicName);
|
240
|
}
|
241
|
|
242
|
public static IStatus executeOperation(final AbstractPostOperation operation) {
|
243
|
if (getOperationHistory() == null) {
|
244
|
throw new IllegalArgumentException(
|
245
|
"There is no operation history for this context");
|
246
|
}
|
247
|
|
248
|
final IAdaptable uiInfoAdapter = WorkspaceUndoUtil
|
249
|
.getUIInfoAdapter(getShell());
|
250
|
|
251
|
IRunnableWithProgress runnable = new IRunnableWithProgress() {
|
252
|
|
253
|
@Override
|
254
|
public void run(IProgressMonitor monitor)
|
255
|
throws InvocationTargetException, InterruptedException {
|
256
|
String operationlabel = operation.getLabel();
|
257
|
monitor.beginTask(operationlabel, 100);
|
258
|
IStatus status = Status.CANCEL_STATUS;
|
259
|
try {
|
260
|
operation.addContext(IOperationHistory.GLOBAL_UNDO_CONTEXT);
|
261
|
status = getOperationHistory().execute(operation, monitor,
|
262
|
uiInfoAdapter);
|
263
|
} catch (ExecutionException e) {
|
264
|
|
265
|
MessagingUtils.operationDialog(this, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
|
266
|
|
267
|
} finally {
|
268
|
monitor.done();
|
269
|
}
|
270
|
|
271
|
String statusString = status.equals(Status.OK_STATUS) ? "completed"
|
272
|
: "cancelled";
|
273
|
setStatusLine(operationlabel + " " + statusString + ".");
|
274
|
|
275
|
}
|
276
|
};
|
277
|
|
278
|
try {
|
279
|
runInUI(runnable, null);
|
280
|
} catch (Exception e) {
|
281
|
MessagingUtils.messageDialog("Error executing operation", AbstractUtility.class, "An error occured while executing " + operation.getLabel(), e);
|
282
|
}
|
283
|
|
284
|
IPostOperationEnabled postOperationEnabled = operation
|
285
|
.getPostOperationEnabled();
|
286
|
if (postOperationEnabled != null) {
|
287
|
postOperationEnabled.onComplete();
|
288
|
}
|
289
|
return Status.OK_STATUS;
|
290
|
}
|
291
|
|
292
|
public static IStatus executeOperation(final AbstractOperation operation, final RemotingCdmHandlerE4 handler) {
|
293
|
return executeOperation_internal(operation, handler);
|
294
|
}
|
295
|
|
296
|
public static IStatus executeOperation(final AbstractOperation operation, final RemotingCdmHandler handler) {
|
297
|
return executeOperation_internal(operation, handler);
|
298
|
}
|
299
|
|
300
|
private static IStatus executeOperation_internal(final AbstractOperation operation, final Object handler) {
|
301
|
if (getOperationHistory() == null) {
|
302
|
throw new IllegalArgumentException(
|
303
|
"There is no operation history for this context");
|
304
|
}
|
305
|
|
306
|
final IAdaptable uiInfoAdapter = WorkspaceUndoUtil
|
307
|
.getUIInfoAdapter(getShell());
|
308
|
|
309
|
IRunnableWithProgress runnable = new IRunnableWithProgress() {
|
310
|
|
311
|
@Override
|
312
|
public void run(IProgressMonitor monitor)
|
313
|
throws InvocationTargetException, InterruptedException {
|
314
|
String operationlabel = operation.getLabel();
|
315
|
monitor.beginTask(operationlabel, 100);
|
316
|
IStatus status = Status.CANCEL_STATUS;
|
317
|
try {
|
318
|
operation.addContext(IOperationHistory.GLOBAL_UNDO_CONTEXT);
|
319
|
status = getOperationHistory().execute(operation, monitor,
|
320
|
uiInfoAdapter);
|
321
|
if(handler instanceof RemotingCdmHandler) {
|
322
|
((RemotingCdmHandler) handler).postOperation(status);
|
323
|
}
|
324
|
else if(handler instanceof RemotingCdmHandlerE4) {
|
325
|
((RemotingCdmHandlerE4) handler).postOperation(status);
|
326
|
}
|
327
|
} catch (ExecutionException e) {
|
328
|
MessagingUtils.operationDialog(this, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
|
329
|
} finally {
|
330
|
monitor.done();
|
331
|
}
|
332
|
|
333
|
String statusString = status.equals(Status.OK_STATUS) ? "completed"
|
334
|
: "cancelled";
|
335
|
setStatusLine(operationlabel + " " + statusString + ".");
|
336
|
|
337
|
}
|
338
|
};
|
339
|
|
340
|
try {
|
341
|
runInUI(runnable, null);
|
342
|
} catch (Exception e) {
|
343
|
MessagingUtils.messageDialog("Error executing operation", AbstractUtility.class, "An error occured while executing " + operation.getLabel(), e);
|
344
|
}
|
345
|
|
346
|
return Status.OK_STATUS;
|
347
|
}
|
348
|
|
349
|
/**
|
350
|
* Executes a remoting monitored operation
|
351
|
*
|
352
|
* @param label for the operation
|
353
|
* @param uuid of the remoting monitor already started on the server
|
354
|
* @param pollInterval in milliseconds
|
355
|
* @param cancelable flag which determines whether the operation can be cancelled
|
356
|
* @param postOp callback for running post operation logic
|
357
|
* @return
|
358
|
*/
|
359
|
public static IStatus executeMoniteredExport(final String label,
|
360
|
final UUID uuid,
|
361
|
final int pollInterval,
|
362
|
final boolean cancelable,
|
363
|
final IPostMoniteredOperationEnabled postOp,
|
364
|
final IFeedbackGenerator feedbackGenerator,
|
365
|
String urlString,
|
366
|
boolean createZip) {
|
367
|
|
368
|
try {
|
369
|
// get the remoting monitor the first time to make sure that the
|
370
|
// operation is valid
|
371
|
final IProgressMonitorService progressMonitorService = CdmApplicationState.getCurrentAppConfig().getProgressMonitorService();
|
372
|
final IRemotingProgressMonitor firstRemotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
|
373
|
if(firstRemotingMonitor == null) {
|
374
|
throw new IllegalStateException("Remoting progress monitor is null");
|
375
|
}
|
376
|
|
377
|
Job job = new Job(label) {
|
378
|
|
379
|
|
380
|
@Override
|
381
|
public IStatus run(IProgressMonitor monitor) {
|
382
|
// run the monitor until the operation is finished
|
383
|
monitor.beginTask("Start", 100);
|
384
|
IRemotingProgressMonitor remotingMonitor;
|
385
|
try {
|
386
|
remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor(label,
|
387
|
uuid,
|
388
|
pollInterval,
|
389
|
postOp,
|
390
|
feedbackGenerator,
|
391
|
monitor);
|
392
|
} catch (Exception ex) {
|
393
|
return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ex);
|
394
|
}
|
395
|
final StringBuilder reportSb = new StringBuilder();
|
396
|
if (remotingMonitor.getResult() instanceof ExportResult){
|
397
|
ExportResult result = (ExportResult)remotingMonitor.getResult();
|
398
|
|
399
|
reportSb.append(result.createReport());
|
400
|
|
401
|
if(!StringUtils.isBlank(reportSb.toString())) {
|
402
|
Display.getDefault().asyncExec(new Runnable() {
|
403
|
@Override
|
404
|
public void run() {
|
405
|
// display reports with possibility to save
|
406
|
ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
|
407
|
dialog.setTitle(label + " Report");
|
408
|
dialog.setReportText(reportSb.toString());
|
409
|
dialog.open();
|
410
|
}
|
411
|
});
|
412
|
}
|
413
|
|
414
|
if (urlString != null){
|
415
|
ExportDataWrapper data = result.getExportData();
|
416
|
try{
|
417
|
if (result.getExportData().getType().equals(ExportResultType.BYTE_ARRAY)){
|
418
|
byte[] exportData = (byte[])data.getExportData();
|
419
|
if(exportData != null){
|
420
|
File file = new File(urlString);
|
421
|
FileOutputStream stream = new FileOutputStream(file);
|
422
|
Writer out = new BufferedWriter(new OutputStreamWriter(
|
423
|
stream, "UTF8"));
|
424
|
|
425
|
stream.write(exportData);
|
426
|
out.flush();
|
427
|
stream.close();
|
428
|
}
|
429
|
} else if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
|
430
|
Map<String, byte[]> resultMap = (Map<String, byte[]>)data.getExportData();
|
431
|
Set<String> keySet = resultMap.keySet();
|
432
|
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
|
433
|
Calendar cal = Calendar.getInstance();
|
434
|
String fileEnding = ".csv";
|
435
|
|
436
|
if (createZip){
|
437
|
File file = new File(urlString+File.separator + sdf.format(cal.getTime())+ ".zip");
|
438
|
FileOutputStream stream = new FileOutputStream(file);
|
439
|
ZipOutputStream zos = new ZipOutputStream(stream);
|
440
|
for (String key: keySet){
|
441
|
byte[] fileData = resultMap.get(key);
|
442
|
ZipEntry entry = new ZipEntry( key + fileEnding);
|
443
|
zos.putNextEntry(entry);
|
444
|
zos.write(fileData);
|
445
|
zos.closeEntry();
|
446
|
}
|
447
|
zos.close();
|
448
|
}else{
|
449
|
if(result.getExportType().equals(ExportType.DWCA)){
|
450
|
|
451
|
File file = new File(urlString);
|
452
|
FileOutputStream stream = new FileOutputStream(file);
|
453
|
ZipOutputStream zos = new ZipOutputStream(stream);
|
454
|
for (String key: keySet){
|
455
|
byte[] fileData = resultMap.get(key);
|
456
|
ZipEntry entry = new ZipEntry( key + fileEnding);
|
457
|
zos.putNextEntry(entry);
|
458
|
zos.write(fileData);
|
459
|
zos.closeEntry();
|
460
|
}
|
461
|
zos.close();
|
462
|
}else{
|
463
|
for (String key: keySet){
|
464
|
byte[] fileData = resultMap.get(key);
|
465
|
File file = new File(urlString+File.separator + key + fileEnding);
|
466
|
FileOutputStream stream = new FileOutputStream(file);
|
467
|
Writer out = new BufferedWriter(new OutputStreamWriter(
|
468
|
stream, "UTF8"));
|
469
|
stream.write(fileData);
|
470
|
stream.close();
|
471
|
}
|
472
|
}
|
473
|
}
|
474
|
}else{
|
475
|
logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString());
|
476
|
}
|
477
|
} catch(Exception e){
|
478
|
logger.error(e.getStackTrace());
|
479
|
}
|
480
|
}
|
481
|
}
|
482
|
return Status.OK_STATUS;
|
483
|
}
|
484
|
|
485
|
@Override
|
486
|
protected void canceling() {
|
487
|
CdmStore.getCurrentApplicationConfiguration().getProgressMonitorService().cancel(uuid);
|
488
|
}
|
489
|
};
|
490
|
|
491
|
// configure the job
|
492
|
job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
|
493
|
job.setUser(true);
|
494
|
// schedule job
|
495
|
job.schedule();
|
496
|
|
497
|
} catch (Exception e) {
|
498
|
MessagingUtils.errorDialog("Error executing operation",
|
499
|
AbstractUtility.class,
|
500
|
"An error occured while executing " + label,
|
501
|
TaxeditorStorePlugin.PLUGIN_ID,
|
502
|
e,
|
503
|
true);
|
504
|
}
|
505
|
|
506
|
return Status.OK_STATUS;
|
507
|
}
|
508
|
|
509
|
public static IOperationHistory getOperationHistory() {
|
510
|
return getWorkbench().getOperationSupport().getOperationHistory();
|
511
|
}
|
512
|
|
513
|
public static void setStatusLine(final String message) {
|
514
|
Display.getDefault().asyncExec(new Runnable() {
|
515
|
|
516
|
@Override
|
517
|
public void run() {
|
518
|
statusLineManager.setMessage(message);
|
519
|
}
|
520
|
|
521
|
});
|
522
|
|
523
|
}
|
524
|
|
525
|
public static IProgressMonitor getMonitor() {
|
526
|
statusLineManager.setCancelEnabled(false);
|
527
|
return statusLineManager.getProgressMonitor();
|
528
|
}
|
529
|
|
530
|
/**
|
531
|
* Starts either the given {@link IProgressMonitor} if it's not
|
532
|
* <code>null</code> or a new {@link NullProgressMonitor}.
|
533
|
*
|
534
|
* @param progressMonitor
|
535
|
* The {@link IProgressMonitor} or <code>null</code> if no
|
536
|
* progress should be reported.
|
537
|
* @param taskName
|
538
|
* The name of the main task.
|
539
|
* @param steps
|
540
|
* The number of steps this task is subdivided into.
|
541
|
* @return The {@link IProgressMonitor}.
|
542
|
*/
|
543
|
public static IProgressMonitor startMainMonitor(
|
544
|
IProgressMonitor progressMonitor, String taskName, int steps) {
|
545
|
IProgressMonitor newMonitor = progressMonitor;
|
546
|
if (newMonitor == null) {
|
547
|
newMonitor = new NullProgressMonitor();
|
548
|
}
|
549
|
newMonitor.beginTask(taskName == null ? "" : taskName, steps);
|
550
|
newMonitor.subTask(" ");
|
551
|
return newMonitor;
|
552
|
}
|
553
|
|
554
|
/**
|
555
|
* Creates a {@link SubProgressMonitor} if the given
|
556
|
* {@link IProgressMonitor} is not <code>null</code> and not a
|
557
|
* {@link NullProgressMonitor}.
|
558
|
*
|
559
|
* @param progressMonitor
|
560
|
* The parent {@link IProgressMonitor} of the
|
561
|
* {@link SubProgressMonitor} to be created.
|
562
|
* @param ticks
|
563
|
* The number of steps this subtask is subdivided into. Must be a
|
564
|
* positive number and must not be
|
565
|
* {@link IProgressMonitor#UNKNOWN}.
|
566
|
* @return The {@link IProgressMonitor}.
|
567
|
*/
|
568
|
public static IProgressMonitor getSubProgressMonitor(
|
569
|
IProgressMonitor progressMonitor, int ticks) {
|
570
|
if (progressMonitor == null) {
|
571
|
return new NullProgressMonitor();
|
572
|
}
|
573
|
if (progressMonitor instanceof NullProgressMonitor) {
|
574
|
return progressMonitor;
|
575
|
}
|
576
|
|
577
|
return new SubProgressMonitor(progressMonitor, ticks);
|
578
|
}
|
579
|
|
580
|
/**
|
581
|
* Checks whether the user canceled this operation. If not canceled, the
|
582
|
* given number of steps are declared as done.
|
583
|
*
|
584
|
* @param newMonitor
|
585
|
* a {@link org.eclipse.core.runtime.IProgressMonitor} object.
|
586
|
* @param steps
|
587
|
* a int.
|
588
|
*/
|
589
|
public static void workedChecked(IProgressMonitor newMonitor, int steps) {
|
590
|
// In case the progress monitor was canceled throw an exception.
|
591
|
if (newMonitor.isCanceled()) {
|
592
|
throw new OperationCanceledException();
|
593
|
}
|
594
|
// Otherwise declare this step as done.
|
595
|
newMonitor.worked(steps);
|
596
|
}
|
597
|
|
598
|
/**
|
599
|
* Present a progress dialog to the user. This dialog will block the UI
|
600
|
*
|
601
|
* @param runnable
|
602
|
* an implementation of {@link IRunnableWithProgress}
|
603
|
* @throws java.lang.InterruptedException
|
604
|
* if any.
|
605
|
* @throws java.lang.reflect.InvocationTargetException
|
606
|
* if any.
|
607
|
*/
|
608
|
public static void busyCursorWhile(IRunnableWithProgress runnable)
|
609
|
throws InvocationTargetException, InterruptedException {
|
610
|
getProgressService().busyCursorWhile(runnable);
|
611
|
}
|
612
|
|
613
|
public static void runInUI(IRunnableWithProgress runnable,
|
614
|
ISchedulingRule rule) throws InvocationTargetException,
|
615
|
InterruptedException {
|
616
|
getProgressService().runInUI(getWorkbenchWindow(), runnable, rule);
|
617
|
}
|
618
|
|
619
|
public static void run(boolean fork, boolean cancelable,
|
620
|
IRunnableWithProgress runnable) throws InvocationTargetException,
|
621
|
InterruptedException {
|
622
|
getProgressService().run(fork, cancelable, runnable);
|
623
|
}
|
624
|
|
625
|
public static IProgressService getProgressService() {
|
626
|
IWorkbench workbench = PlatformUI.getWorkbench();
|
627
|
return workbench.getProgressService();
|
628
|
}
|
629
|
|
630
|
public static IWorkbenchSiteProgressService getProgressService2() {
|
631
|
return (IWorkbenchSiteProgressService) getService(IWorkbenchSiteProgressService.class);
|
632
|
}
|
633
|
|
634
|
public static String getPluginId() {
|
635
|
return "eu.taxeditor";
|
636
|
}
|
637
|
|
638
|
public static Object getActiveE4Editor() {
|
639
|
MPart activePart = EventUtility.getActivePart();
|
640
|
if(activePart!=null && activePart.getObject()!=null
|
641
|
&& activePart.getObject() instanceof IE4SavablePart){
|
642
|
return activePart.getObject();
|
643
|
}
|
644
|
return null;
|
645
|
}
|
646
|
|
647
|
public static DetailsViewPart getDetailsView() {
|
648
|
return (DetailsViewPart) getView(DetailsViewPart.ID, false);
|
649
|
}
|
650
|
|
651
|
public static void refreshDetailsViewer() {
|
652
|
if (getDetailsView() != null) {
|
653
|
((AbstractCdmDataViewer) getDetailsView().getViewer()).refresh();
|
654
|
}
|
655
|
}
|
656
|
|
657
|
public static void reflowDetailsViewer() {
|
658
|
if (getDetailsView() != null) {
|
659
|
((AbstractCdmDataViewer) getDetailsView().getViewer()).reflow();
|
660
|
}
|
661
|
}
|
662
|
|
663
|
public static SupplementalDataViewPart getSupplementalDataView() {
|
664
|
return (SupplementalDataViewPart) getView(SupplementalDataViewPart.ID,
|
665
|
false);
|
666
|
}
|
667
|
|
668
|
public static void reflowSupplementalViewer() {
|
669
|
if (getSupplementalDataView() != null) {
|
670
|
((AbstractCdmDataViewer) getSupplementalDataView().getViewer())
|
671
|
.reflow();
|
672
|
}
|
673
|
}
|
674
|
|
675
|
|
676
|
/**
|
677
|
* Orders a Collection of {@link IEnumTerm}s according to the term
|
678
|
* hierarchy. <br>
|
679
|
* <br>
|
680
|
* The returned map will be be ordered primarily by root elements,
|
681
|
* secondarily by the child elements and their children resp., both ascending alphabetically. <br>
|
682
|
* @param terms
|
683
|
* A {@link Collection} of {@link IEnumTerm}s for which the term
|
684
|
* hierarchy should be created
|
685
|
* @return a map which holds the terms as keys and their string
|
686
|
* representation via {@link IEnumTerm#getMessage()} as values
|
687
|
*/
|
688
|
public static <T extends IEnumTerm<T>> LinkedHashMap<T, String> orderTerms(Collection<T> terms) {
|
689
|
TreeSet<TermNode<T>> parentElements = new TreeSet<TermNode<T>>();
|
690
|
parentElements.addAll(getTermHierarchy(terms));
|
691
|
|
692
|
// create list according to the type hierarchy (root elements alphabetically with recursive children also alphabetically)
|
693
|
LinkedHashMap<T, String> result = new LinkedHashMap<T, String>();
|
694
|
parseTermTree(parentElements, result, -1);
|
695
|
return result;
|
696
|
}
|
697
|
|
698
|
private static<T extends IEnumTerm<T>> void parseTermTree(Collection<TermNode<T>> children, LinkedHashMap<T, String> result, int depth){
|
699
|
depth++;
|
700
|
for(TermNode<T> node:children){
|
701
|
String indentString = "";
|
702
|
for(int i=0;i<depth;i++){
|
703
|
indentString += " ";
|
704
|
}
|
705
|
if(depth>0){
|
706
|
indentString += "- ";
|
707
|
}
|
708
|
result.put(node.term, indentString + node.term.getMessage());
|
709
|
parseTermTree(node.children, result, depth);
|
710
|
}
|
711
|
}
|
712
|
|
713
|
private static<T extends IEnumTerm<T>> void addToParents(List<TermNode<T>> parents, Collection<T> terms){
|
714
|
List<TermNode<T>> hasChildrenList = new ArrayList<TermNode<T>>();
|
715
|
for(T term:terms){
|
716
|
// only terms with parents
|
717
|
if(term.getKindOf()!=null){
|
718
|
TermNode<T> parentNode = new TermNode<T>(term.getKindOf());
|
719
|
TermNode<T> childNode = new TermNode<T>(term);
|
720
|
if(parents.contains(parentNode)){
|
721
|
// parent found in parent list -> add this term to parent's child list
|
722
|
parents.get(parents.indexOf(parentNode)).addChild(childNode);
|
723
|
if(!term.getGeneralizationOf().isEmpty()){
|
724
|
// has more children -> add to list which will be the parent for the next recursion
|
725
|
hasChildrenList.add(childNode);
|
726
|
}
|
727
|
}
|
728
|
}
|
729
|
}
|
730
|
if(!hasChildrenList.isEmpty()){
|
731
|
addToParents(hasChildrenList, terms);
|
732
|
}
|
733
|
}
|
734
|
|
735
|
private static<T extends IEnumTerm<T>> List<TermNode<T>> getTermHierarchy(Collection<T> terms){
|
736
|
List<TermNode<T>> parents = new ArrayList<TermNode<T>>();
|
737
|
// get root elements
|
738
|
for(T term:terms){
|
739
|
T parentTerm = term.getKindOf();
|
740
|
if(parentTerm==null){
|
741
|
// root element
|
742
|
parents.add(new TermNode<T>(term));
|
743
|
}
|
744
|
}
|
745
|
addToParents(parents, terms);
|
746
|
return parents;
|
747
|
}
|
748
|
|
749
|
@SuppressWarnings("unchecked")
|
750
|
/**
|
751
|
* Recursively iterates over all term parents until no more parent is found i.e. the root node
|
752
|
* @param term The term for which the parent should be found
|
753
|
* @return the root terms of the term hierarchy
|
754
|
*/
|
755
|
private static<T extends IEnumTerm<T>> T getParentFor(T term){
|
756
|
// PP: cast should be safe. Why is Eclipse complaining??
|
757
|
T parent = term.getKindOf();
|
758
|
if(parent==null){
|
759
|
return term;
|
760
|
}
|
761
|
else{
|
762
|
return getParentFor(term.getKindOf());
|
763
|
}
|
764
|
}
|
765
|
|
766
|
private static class TermNode<T extends IEnumTerm<T>> implements Comparable<TermNode<T>>{
|
767
|
private final T term;
|
768
|
private final TreeSet<TermNode<T>> children;
|
769
|
|
770
|
public TermNode(T term) {
|
771
|
super();
|
772
|
this.term = term;
|
773
|
this.children = new TreeSet<TermNode<T>>();
|
774
|
}
|
775
|
|
776
|
public void addChild(TermNode<T> child){
|
777
|
this.children.add(child);
|
778
|
}
|
779
|
|
780
|
public TreeSet<TermNode<T>> getChildren() {
|
781
|
return children;
|
782
|
}
|
783
|
|
784
|
public T getTerm() {
|
785
|
return term;
|
786
|
}
|
787
|
|
788
|
@Override
|
789
|
public int hashCode() {
|
790
|
final int prime = 31;
|
791
|
int result = 1;
|
792
|
result = prime * result + ((term == null) ? 0 : term.hashCode());
|
793
|
return result;
|
794
|
}
|
795
|
|
796
|
@Override
|
797
|
public boolean equals(Object obj) {
|
798
|
if (this == obj) {
|
799
|
return true;
|
800
|
}
|
801
|
if (obj == null) {
|
802
|
return false;
|
803
|
}
|
804
|
if (getClass() != obj.getClass()) {
|
805
|
return false;
|
806
|
}
|
807
|
TermNode other = (TermNode) obj;
|
808
|
if (term == null) {
|
809
|
if (other.term != null) {
|
810
|
return false;
|
811
|
}
|
812
|
} else if (!term.equals(other.term)) {
|
813
|
return false;
|
814
|
}
|
815
|
return true;
|
816
|
}
|
817
|
|
818
|
@Override
|
819
|
public int compareTo(TermNode<T> that) {
|
820
|
return this.term.getMessage().compareTo(that.term.getMessage());
|
821
|
}
|
822
|
}
|
823
|
|
824
|
|
825
|
public static void executeCommand(String commandId, Object source, String pluginId) {
|
826
|
IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
|
827
|
Exception exception = null;
|
828
|
try {
|
829
|
handlerService.executeCommand(commandId, null);
|
830
|
} catch (ExecutionException e) {
|
831
|
exception = e;
|
832
|
} catch (NotDefinedException e) {
|
833
|
exception = e;
|
834
|
} catch (NotEnabledException e) {
|
835
|
exception = e;
|
836
|
} catch (NotHandledException e) {
|
837
|
exception = e;
|
838
|
} finally {
|
839
|
if(exception != null) {
|
840
|
MessagingUtils.errorDialog("Error executing command",
|
841
|
source,
|
842
|
"Could not execute command with id " + commandId ,
|
843
|
pluginId,
|
844
|
exception,
|
845
|
true);
|
846
|
}
|
847
|
}
|
848
|
}
|
849
|
|
850
|
public static Object getElementsFromSelectionChangedEvent(SelectionChangedEvent event) {
|
851
|
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
|
852
|
Object selectionToSet = selection;
|
853
|
if(selection.size() == 1){
|
854
|
selectionToSet = selection.getFirstElement();
|
855
|
}
|
856
|
else if(!selection.isEmpty()){
|
857
|
selectionToSet = selection.toArray();
|
858
|
}
|
859
|
return selectionToSet;
|
860
|
}
|
861
|
|
862
|
/**
|
863
|
* Executes a remoting monitored import
|
864
|
*
|
865
|
* @param label for the import
|
866
|
* @param uuid of the remoting monitor already started on the server
|
867
|
* @param pollInterval in milliseconds
|
868
|
* @param cancelable flag which determines whether the operation can be cancelled
|
869
|
* @param postOp callback for running post operation logic
|
870
|
* @return
|
871
|
*/
|
872
|
public static IStatus executeMoniteredOperation(final String label,
|
873
|
final UUID uuid,
|
874
|
final int pollInterval,
|
875
|
final boolean cancelable,
|
876
|
final IPostMoniteredOperationEnabled postOp,
|
877
|
final IFeedbackGenerator feedbackGenerator) {
|
878
|
|
879
|
try{
|
880
|
// get the remoting monitor the first time to make sure that the
|
881
|
// operation is valid
|
882
|
final IProgressMonitorService progressMonitorService = CdmApplicationState.getCurrentAppConfig().getProgressMonitorService();
|
883
|
final IRemotingProgressMonitor firstRemotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
|
884
|
if(firstRemotingMonitor == null) {
|
885
|
throw new IllegalStateException("Remoting progress monitor is null");
|
886
|
}
|
887
|
|
888
|
Job job = new Job(label) {
|
889
|
|
890
|
|
891
|
@Override
|
892
|
public IStatus run(IProgressMonitor monitor) {
|
893
|
// run the monitor until the operation is finished
|
894
|
monitor.beginTask("Start", 100);
|
895
|
IRemotingProgressMonitor remotingMonitor;
|
896
|
try {
|
897
|
remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor(label,
|
898
|
uuid,
|
899
|
pollInterval,
|
900
|
postOp,
|
901
|
feedbackGenerator,
|
902
|
monitor);
|
903
|
} catch (Exception ex) {
|
904
|
return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ex);
|
905
|
}
|
906
|
final StringBuilder reportSb = new StringBuilder();
|
907
|
// collect reports
|
908
|
// for(String report : remotingMonitor.getResult()) {
|
909
|
reportSb.append(((ExportResult)remotingMonitor.getResult()).createReport());
|
910
|
// }
|
911
|
if(!StringUtils.isBlank(reportSb.toString())) {
|
912
|
Display.getDefault().asyncExec(new Runnable() {
|
913
|
@Override
|
914
|
public void run() {
|
915
|
// display reports with possibility to save
|
916
|
ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
|
917
|
dialog.setTitle(label + " Report");
|
918
|
dialog.setReportText(reportSb.toString());
|
919
|
dialog.open();
|
920
|
}
|
921
|
});
|
922
|
}
|
923
|
return Status.OK_STATUS;
|
924
|
}
|
925
|
|
926
|
@Override
|
927
|
protected void canceling() {
|
928
|
CdmStore.getCurrentApplicationConfiguration().getProgressMonitorService().cancel(uuid);
|
929
|
}
|
930
|
};
|
931
|
|
932
|
// configure the job
|
933
|
job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
|
934
|
job.setUser(true);
|
935
|
// schedule job
|
936
|
job.schedule();
|
937
|
|
938
|
} catch (Exception e) {
|
939
|
MessagingUtils.errorDialog("Error executing operation",
|
940
|
AbstractUtility.class,
|
941
|
"An error occured while executing " + label,
|
942
|
TaxeditorStorePlugin.PLUGIN_ID,
|
943
|
e,
|
944
|
true);
|
945
|
}
|
946
|
|
947
|
return Status.OK_STATUS;
|
948
|
}
|
949
|
|
950
|
}
|