2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.model
;
12 import java
.io
.BufferedWriter
;
14 import java
.io
.FileOutputStream
;
15 import java
.io
.OutputStreamWriter
;
16 import java
.io
.Writer
;
17 import java
.text
.SimpleDateFormat
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Calendar
;
20 import java
.util
.Collection
;
21 import java
.util
.LinkedHashMap
;
22 import java
.util
.List
;
25 import java
.util
.TreeSet
;
26 import java
.util
.UUID
;
27 import java
.util
.zip
.ZipEntry
;
28 import java
.util
.zip
.ZipOutputStream
;
30 import org
.apache
.commons
.lang
.StringUtils
;
31 import org
.apache
.log4j
.Logger
;
32 import org
.eclipse
.core
.commands
.ExecutionException
;
33 import org
.eclipse
.core
.commands
.NotEnabledException
;
34 import org
.eclipse
.core
.commands
.NotHandledException
;
35 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
36 import org
.eclipse
.core
.commands
.operations
.AbstractOperation
;
37 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
38 import org
.eclipse
.core
.runtime
.IAdaptable
;
39 import org
.eclipse
.core
.runtime
.ICoreRunnable
;
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
.Job
;
47 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
48 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
49 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
50 import org
.eclipse
.jface
.action
.IStatusLineManager
;
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
.IWorkbench
;
60 import org
.eclipse
.ui
.PlatformUI
;
61 import org
.eclipse
.ui
.handlers
.IHandlerService
;
62 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
63 import org
.eclipse
.ui
.progress
.IProgressConstants
;
64 import org
.eclipse
.ui
.progress
.IProgressService
;
65 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
66 import org
.eclipse
.ui
.themes
.ITheme
;
67 import org
.eclipse
.ui
.themes
.IThemeManager
;
69 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
70 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
71 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
72 import eu
.etaxonomy
.cdm
.io
.common
.ExportDataWrapper
;
73 import eu
.etaxonomy
.cdm
.io
.common
.ExportResult
;
74 import eu
.etaxonomy
.cdm
.io
.common
.ExportResultType
;
75 import eu
.etaxonomy
.cdm
.io
.common
.ExportType
;
76 import eu
.etaxonomy
.cdm
.model
.common
.IEnumTerm
;
77 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
78 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
79 import eu
.etaxonomy
.taxeditor
.operation
.IFeedbackGenerator
;
80 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
81 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
82 import eu
.etaxonomy
.taxeditor
.operation
.RemotingCdmHandler
;
83 import eu
.etaxonomy
.taxeditor
.operation
.e4
.RemotingCdmHandlerE4
;
84 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
85 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
86 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.ReportTextDialog
;
87 import eu
.etaxonomy
.taxeditor
.view
.e4
.AbstractCdmDataViewerE4
;
88 import eu
.etaxonomy
.taxeditor
.view
.e4
.details
.DetailsPartE4
;
89 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
97 public abstract class AbstractUtility
{
99 private static final Logger logger
= Logger
.getLogger(AbstractUtility
.class);
101 /** Constant <code>statusLineManager</code> */
102 protected static IStatusLineManager statusLineManager
;
103 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
104 public static final String DATE_FORMAT_NOW
= "yyyyMMddHHmm";
106 public static Object
getActivePart() {
107 MPart activePart
= EventUtility
.getActivePart();
108 if(activePart
!=null){
109 return activePart
.getObject();
114 public static Shell
getShell() {
115 return EventUtility
.getShell();
118 public static IWorkbench
getWorkbench() {
119 return TaxeditorStorePlugin
.getDefault().getWorkbench();
122 public static Object
getService(Class api
) {
123 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
126 public static ITheme
getCurrentTheme() {
127 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault()
128 .getWorkbench().getThemeManager();
129 return themeManager
.getCurrentTheme();
133 * Fonts registered to the plugin may be obtained with the Eclipse themeing
134 * functionality. Thus fonts are chooseable by the user via
135 * Preferences->General->Appearance->Colors and Fonts
137 * @return the FontRegistry for the current theme
139 public static FontRegistry
getFontRegistry() {
140 return getCurrentTheme().getFontRegistry();
143 public static Font
getFont(String symbolicName
) {
144 return getFontRegistry().get(symbolicName
);
148 * Color registered to the plugin may be obtained with the Eclipse themeing
149 * functionality. Thus colors are editable by the user via
150 * Preferences->General->Appearance->Colors and Fonts
152 * @return the ColorRegistry for the current theme
154 public static ColorRegistry
getColorRegistry() {
155 return getCurrentTheme().getColorRegistry();
158 public static Color
getColor(String symbolicName
) {
159 return getColorRegistry().get(symbolicName
);
162 public static IStatus
executeOperation(final AbstractPostOperation operation
, UISynchronize sync
) {
163 if (getOperationHistory() == null) {
164 throw new IllegalArgumentException(
165 "There is no operation history for this context");
168 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
169 .getUIInfoAdapter(getShell());
171 Job job
= Job
.create(operation
.getLabel(), (ICoreRunnable
) monitor
-> {
172 sync
.syncExec(() -> {
173 String operationlabel
= operation
.getLabel();
174 monitor
.beginTask(operationlabel
, 100);
175 IStatus status
= Status
.CANCEL_STATUS
;
177 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
178 status
= operation
.execute(monitor
, uiInfoAdapter
);
179 } catch (ExecutionException e
) {
181 MessagingUtils
.operationDialog(AbstractUtility
.class, e
, TaxeditorStorePlugin
.PLUGIN_ID
, operationlabel
, null);
187 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
189 setStatusLine(operationlabel
+ " " + statusString
+ ".");
196 } catch (Exception e
) {
197 MessagingUtils
.messageDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
200 IPostOperationEnabled postOperationEnabled
= operation
201 .getPostOperationEnabled();
202 if (postOperationEnabled
!= null) {
203 postOperationEnabled
.onComplete();
205 return Status
.OK_STATUS
;
208 public static IStatus
executeOperation(final AbstractOperation operation
, final RemotingCdmHandlerE4 handler
, UISynchronize sync
) {
209 return executeOperation_internal(operation
, handler
, sync
);
212 public static IStatus
executeOperation(final AbstractOperation operation
, final RemotingCdmHandler handler
, UISynchronize sync
) {
213 return executeOperation_internal(operation
, handler
, sync
);
216 private static IStatus
executeOperation_internal(final AbstractOperation operation
, final Object handler
,
217 UISynchronize sync
) {
218 if (getOperationHistory() == null) {
219 throw new IllegalArgumentException(
220 "There is no operation history for this context");
223 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
224 .getUIInfoAdapter(getShell());
227 Job job
= Job
.create(operation
.getLabel(), (ICoreRunnable
) monitor
-> {
228 sync
.syncExec(() -> {
229 String operationlabel
= operation
.getLabel();
230 monitor
.beginTask(operationlabel
, 100);
231 IStatus status
= Status
.CANCEL_STATUS
;
233 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
234 status
= operation
.execute(monitor
,
236 if(handler
instanceof RemotingCdmHandler
) {
237 ((RemotingCdmHandler
) handler
).postOperation(status
);
239 else if(handler
instanceof RemotingCdmHandlerE4
) {
240 ((RemotingCdmHandlerE4
) handler
).postOperation(status
);
242 } catch (ExecutionException e
) {
243 MessagingUtils
.operationDialog(AbstractUtility
.class, e
, TaxeditorStorePlugin
.PLUGIN_ID
, operationlabel
, null);
248 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
250 setStatusLine(operationlabel
+ " " + statusString
+ ".");
256 sync
.syncExec(()->job
.schedule());
257 } catch (Exception e
) {
258 MessagingUtils
.messageDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
261 return Status
.OK_STATUS
;
265 * Executes a remoting monitored operation
267 * @param label for the operation
268 * @param uuid of the remoting monitor already started on the server
269 * @param pollInterval in milliseconds
270 * @param cancelable flag which determines whether the operation can be cancelled
271 * @param postOp callback for running post operation logic
274 public static IStatus
executeMoniteredExport(final String label
,
276 final int pollInterval
,
277 final boolean cancelable
,
278 final IPostMoniteredOperationEnabled postOp
,
279 final IFeedbackGenerator feedbackGenerator
,
284 // get the remoting monitor the first time to make sure that the
285 // operation is valid
286 final IProgressMonitorService progressMonitorService
= CdmApplicationState
.getCurrentAppConfig().getProgressMonitorService();
287 final IRemotingProgressMonitor firstRemotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
288 if(firstRemotingMonitor
== null) {
289 throw new IllegalStateException("Remoting progress monitor is null");
292 Job job
= new Job(label
) {
296 public IStatus
run(IProgressMonitor monitor
) {
297 // run the monitor until the operation is finished
298 monitor
.beginTask("Start", 100);
299 IRemotingProgressMonitor remotingMonitor
;
301 remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor(label
,
307 } catch (Exception ex
) {
308 return new Status(Status
.ERROR
, TaxeditorStorePlugin
.PLUGIN_ID
, "Operation Interrupted", ex
);
310 final StringBuilder reportSb
= new StringBuilder();
311 if (remotingMonitor
.getResult() instanceof ExportResult
){
312 ExportResult result
= (ExportResult
)remotingMonitor
.getResult();
314 reportSb
.append(result
.createReport());
316 if(!StringUtils
.isBlank(reportSb
.toString())) {
317 Display
.getDefault().asyncExec(new Runnable() {
320 // display reports with possibility to save
321 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
322 dialog
.setTitle(label
+ " Report");
323 dialog
.setReportText(reportSb
.toString());
329 if (urlString
!= null){
330 ExportDataWrapper data
= result
.getExportData();
332 if (result
.getExportData().getType().equals(ExportResultType
.BYTE_ARRAY
)){
333 byte[] exportData
= (byte[])data
.getExportData();
334 if(exportData
!= null){
335 File file
= new File(urlString
);
336 FileOutputStream stream
= new FileOutputStream(file
);
337 Writer out
= new BufferedWriter(new OutputStreamWriter(
340 stream
.write(exportData
);
344 } else if (result
.getExportData().getType().equals(ExportResultType
.MAP_BYTE_ARRAY
)){
345 Map
<String
, byte[]> resultMap
= (Map
<String
, byte[]>)data
.getExportData();
346 Set
<String
> keySet
= resultMap
.keySet();
347 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
348 Calendar cal
= Calendar
.getInstance();
349 String fileEnding
= ".csv";
352 File file
= new File(urlString
+File
.separator
+ sdf
.format(cal
.getTime())+ ".zip");
353 FileOutputStream stream
= new FileOutputStream(file
);
354 ZipOutputStream zos
= new ZipOutputStream(stream
);
355 for (String key
: keySet
){
356 byte[] fileData
= resultMap
.get(key
);
357 ZipEntry entry
= new ZipEntry( key
+ fileEnding
);
358 zos
.putNextEntry(entry
);
364 if(result
.getExportType().equals(ExportType
.DWCA
)){
366 File file
= new File(urlString
);
367 FileOutputStream stream
= new FileOutputStream(file
);
368 ZipOutputStream zos
= new ZipOutputStream(stream
);
369 for (String key
: keySet
){
370 byte[] fileData
= resultMap
.get(key
);
371 ZipEntry entry
= new ZipEntry( key
+ fileEnding
);
372 zos
.putNextEntry(entry
);
378 for (String key
: keySet
){
379 byte[] fileData
= resultMap
.get(key
);
380 File file
= new File(urlString
+File
.separator
+ key
+ fileEnding
);
381 FileOutputStream stream
= new FileOutputStream(file
);
382 Writer out
= new BufferedWriter(new OutputStreamWriter(
384 stream
.write(fileData
);
390 logger
.error("This kind of result data is not supported yet." + result
.getExportData().getType().toString());
392 } catch(Exception e
){
393 logger
.error(e
.getStackTrace());
397 return Status
.OK_STATUS
;
401 protected void canceling() {
402 CdmStore
.getCurrentApplicationConfiguration().getProgressMonitorService().cancel(uuid
);
407 job
.setProperty(IProgressConstants
.KEEP_PROPERTY
, true);
412 } catch (Exception e
) {
413 MessagingUtils
.errorDialog("Error executing operation",
414 AbstractUtility
.class,
415 "An error occured while executing " + label
,
416 TaxeditorStorePlugin
.PLUGIN_ID
,
421 return Status
.OK_STATUS
;
424 public static IOperationHistory
getOperationHistory() {
425 return getWorkbench().getOperationSupport().getOperationHistory();
428 public static void setStatusLine(final String message
) {
429 Display
.getDefault().asyncExec(new Runnable() {
433 statusLineManager
.setMessage(message
);
440 public static IProgressMonitor
getMonitor() {
441 statusLineManager
.setCancelEnabled(false);
442 return statusLineManager
.getProgressMonitor();
446 * Starts either the given {@link IProgressMonitor} if it's not
447 * <code>null</code> or a new {@link NullProgressMonitor}.
449 * @param progressMonitor
450 * The {@link IProgressMonitor} or <code>null</code> if no
451 * progress should be reported.
453 * The name of the main task.
455 * The number of steps this task is subdivided into.
456 * @return The {@link IProgressMonitor}.
458 public static IProgressMonitor
startMainMonitor(
459 IProgressMonitor progressMonitor
, String taskName
, int steps
) {
460 IProgressMonitor newMonitor
= progressMonitor
;
461 if (newMonitor
== null) {
462 newMonitor
= new NullProgressMonitor();
464 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
465 newMonitor
.subTask(" ");
470 * Creates a {@link SubProgressMonitor} if the given
471 * {@link IProgressMonitor} is not <code>null</code> and not a
472 * {@link NullProgressMonitor}.
474 * @param progressMonitor
475 * The parent {@link IProgressMonitor} of the
476 * {@link SubProgressMonitor} to be created.
478 * The number of steps this subtask is subdivided into. Must be a
479 * positive number and must not be
480 * {@link IProgressMonitor#UNKNOWN}.
481 * @return The {@link IProgressMonitor}.
483 public static IProgressMonitor
getSubProgressMonitor(
484 IProgressMonitor progressMonitor
, int ticks
) {
485 if (progressMonitor
== null) {
486 return new NullProgressMonitor();
488 if (progressMonitor
instanceof NullProgressMonitor
) {
489 return progressMonitor
;
492 return new SubProgressMonitor(progressMonitor
, ticks
);
496 * Checks whether the user canceled this operation. If not canceled, the
497 * given number of steps are declared as done.
500 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
504 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
505 // In case the progress monitor was canceled throw an exception.
506 if (newMonitor
.isCanceled()) {
507 throw new OperationCanceledException();
509 // Otherwise declare this step as done.
510 newMonitor
.worked(steps
);
513 public static IProgressService
getProgressService() {
514 IWorkbench workbench
= PlatformUI
.getWorkbench();
515 return workbench
.getProgressService();
518 public static IWorkbenchSiteProgressService
getProgressService2() {
519 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
522 public static String
getPluginId() {
523 return "eu.taxeditor";
526 public static Object
getActiveEditor() {
527 MPart activePart
= EventUtility
.getActivePart();
528 if(activePart
!=null && activePart
.getObject()!=null
529 && activePart
.getObject() instanceof IE4SavablePart
){
530 return activePart
.getObject();
535 public static DetailsPartE4
getDetailsView(EPartService partService
) {
536 MPart part
= partService
.findPart("eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4");
538 return (DetailsPartE4
) part
.getObject();
543 public static void refreshDetailsViewer(EPartService partService
) {
544 if (getDetailsView(partService
) != null) {
545 ((AbstractCdmDataViewerE4
) getDetailsView(partService
).getViewer()).refresh();
549 public static void reflowDetailsViewer(EPartService partService
) {
550 if (getDetailsView(partService
) != null) {
551 ((AbstractCdmDataViewerE4
) getDetailsView(partService
).getViewer()).reflow();
557 * Orders a Collection of {@link IEnumTerm}s according to the term
560 * The returned map will be be ordered primarily by root elements,
561 * secondarily by the child elements and their children resp., both ascending alphabetically. <br>
563 * A {@link Collection} of {@link IEnumTerm}s for which the term
564 * hierarchy should be created
565 * @return a map which holds the terms as keys and their string
566 * representation via {@link IEnumTerm#getMessage()} as values
568 public static <T
extends IEnumTerm
<T
>> LinkedHashMap
<T
, String
> orderTerms(Collection
<T
> terms
) {
569 TreeSet
<TermNode
<T
>> parentElements
= new TreeSet
<TermNode
<T
>>();
570 parentElements
.addAll(getTermHierarchy(terms
));
572 // create list according to the type hierarchy (root elements alphabetically with recursive children also alphabetically)
573 LinkedHashMap
<T
, String
> result
= new LinkedHashMap
<T
, String
>();
574 parseTermTree(parentElements
, result
, -1);
578 private static<T
extends IEnumTerm
<T
>> void parseTermTree(Collection
<TermNode
<T
>> children
, LinkedHashMap
<T
, String
> result
, int depth
){
580 for(TermNode
<T
> node
:children
){
581 String indentString
= "";
582 for(int i
=0;i
<depth
;i
++){
586 indentString
+= "- ";
588 result
.put(node
.term
, indentString
+ node
.term
.getMessage());
589 parseTermTree(node
.children
, result
, depth
);
593 private static<T
extends IEnumTerm
<T
>> void addToParents(List
<TermNode
<T
>> parents
, Collection
<T
> terms
){
594 List
<TermNode
<T
>> hasChildrenList
= new ArrayList
<TermNode
<T
>>();
596 // only terms with parents
597 if(term
.getKindOf()!=null){
598 TermNode
<T
> parentNode
= new TermNode
<T
>(term
.getKindOf());
599 TermNode
<T
> childNode
= new TermNode
<T
>(term
);
600 if(parents
.contains(parentNode
)){
601 // parent found in parent list -> add this term to parent's child list
602 parents
.get(parents
.indexOf(parentNode
)).addChild(childNode
);
603 if(!term
.getGeneralizationOf().isEmpty()){
604 // has more children -> add to list which will be the parent for the next recursion
605 hasChildrenList
.add(childNode
);
610 if(!hasChildrenList
.isEmpty()){
611 addToParents(hasChildrenList
, terms
);
615 private static<T
extends IEnumTerm
<T
>> List
<TermNode
<T
>> getTermHierarchy(Collection
<T
> terms
){
616 List
<TermNode
<T
>> parents
= new ArrayList
<TermNode
<T
>>();
619 T parentTerm
= term
.getKindOf();
620 if(parentTerm
==null){
622 parents
.add(new TermNode
<T
>(term
));
625 addToParents(parents
, terms
);
629 @SuppressWarnings("unchecked")
631 * Recursively iterates over all term parents until no more parent is found i.e. the root node
632 * @param term The term for which the parent should be found
633 * @return the root terms of the term hierarchy
635 private static<T
extends IEnumTerm
<T
>> T
getParentFor(T term
){
636 // PP: cast should be safe. Why is Eclipse complaining??
637 T parent
= term
.getKindOf();
642 return getParentFor(term
.getKindOf());
646 private static class TermNode
<T
extends IEnumTerm
<T
>> implements Comparable
<TermNode
<T
>>{
647 private final T term
;
648 private final TreeSet
<TermNode
<T
>> children
;
650 public TermNode(T term
) {
653 this.children
= new TreeSet
<TermNode
<T
>>();
656 public void addChild(TermNode
<T
> child
){
657 this.children
.add(child
);
660 public TreeSet
<TermNode
<T
>> getChildren() {
669 public int hashCode() {
670 final int prime
= 31;
672 result
= prime
* result
+ ((term
== null) ?
0 : term
.hashCode());
677 public boolean equals(Object obj
) {
684 if (getClass() != obj
.getClass()) {
687 TermNode other
= (TermNode
) obj
;
689 if (other
.term
!= null) {
692 } else if (!term
.equals(other
.term
)) {
699 public int compareTo(TermNode
<T
> that
) {
700 return this.term
.getMessage().compareTo(that
.term
.getMessage());
705 public static void executeCommand(String commandId
, Object source
, String pluginId
) {
706 IHandlerService handlerService
= (IHandlerService
) AbstractUtility
.getService(IHandlerService
.class);
707 Exception exception
= null;
709 handlerService
.executeCommand(commandId
, null);
710 } catch (ExecutionException e
) {
712 } catch (NotDefinedException e
) {
714 } catch (NotEnabledException e
) {
716 } catch (NotHandledException e
) {
719 if(exception
!= null) {
720 MessagingUtils
.errorDialog("Error executing command",
722 "Could not execute command with id " + commandId
,
730 public static Object
getElementsFromSelectionChangedEvent(SelectionChangedEvent event
) {
731 IStructuredSelection selection
= (IStructuredSelection
) event
.getSelection();
732 Object selectionToSet
= selection
;
733 if(selection
.size() == 1){
734 selectionToSet
= selection
.getFirstElement();
736 else if(!selection
.isEmpty()){
737 selectionToSet
= selection
.toArray();
739 return selectionToSet
;
743 * Executes a remoting monitored import
745 * @param label for the import
746 * @param uuid of the remoting monitor already started on the server
747 * @param pollInterval in milliseconds
748 * @param cancelable flag which determines whether the operation can be cancelled
749 * @param postOp callback for running post operation logic
752 public static IStatus
executeMoniteredOperation(final String label
,
754 final int pollInterval
,
755 final boolean cancelable
,
756 final IPostMoniteredOperationEnabled postOp
,
757 final IFeedbackGenerator feedbackGenerator
) {
760 // get the remoting monitor the first time to make sure that the
761 // operation is valid
762 final IProgressMonitorService progressMonitorService
= CdmApplicationState
.getCurrentAppConfig().getProgressMonitorService();
763 final IRemotingProgressMonitor firstRemotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
764 if(firstRemotingMonitor
== null) {
765 throw new IllegalStateException("Remoting progress monitor is null");
768 Job job
= new Job(label
) {
772 public IStatus
run(IProgressMonitor monitor
) {
773 // run the monitor until the operation is finished
774 monitor
.beginTask("Start", 100);
775 IRemotingProgressMonitor remotingMonitor
;
777 remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor(label
,
783 } catch (Exception ex
) {
784 return new Status(Status
.ERROR
, TaxeditorStorePlugin
.PLUGIN_ID
, "Operation Interrupted", ex
);
786 final StringBuilder reportSb
= new StringBuilder();
788 // for(String report : remotingMonitor.getResult()) {
789 reportSb
.append(((ExportResult
)remotingMonitor
.getResult()).createReport());
791 if(!StringUtils
.isBlank(reportSb
.toString())) {
792 Display
.getDefault().asyncExec(new Runnable() {
795 // display reports with possibility to save
796 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
797 dialog
.setTitle(label
+ " Report");
798 dialog
.setReportText(reportSb
.toString());
803 return Status
.OK_STATUS
;
807 protected void canceling() {
808 CdmStore
.getCurrentApplicationConfiguration().getProgressMonitorService().cancel(uuid
);
813 job
.setProperty(IProgressConstants
.KEEP_PROPERTY
, true);
818 } catch (Exception e
) {
819 MessagingUtils
.errorDialog("Error executing operation",
820 AbstractUtility
.class,
821 "An error occured while executing " + label
,
822 TaxeditorStorePlugin
.PLUGIN_ID
,
827 return Status
.OK_STATUS
;