3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Collection
;
16 import java
.util
.LinkedHashMap
;
17 import java
.util
.List
;
18 import java
.util
.TreeSet
;
19 import java
.util
.UUID
;
21 import org
.apache
.commons
.lang
.StringUtils
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.eclipse
.core
.commands
.ExecutionException
;
24 import org
.eclipse
.core
.commands
.NotEnabledException
;
25 import org
.eclipse
.core
.commands
.NotHandledException
;
26 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
27 import org
.eclipse
.core
.commands
.operations
.AbstractOperation
;
28 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
29 import org
.eclipse
.core
.runtime
.IAdaptable
;
30 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
31 import org
.eclipse
.core
.runtime
.IStatus
;
32 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
33 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
34 import org
.eclipse
.core
.runtime
.Status
;
35 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
36 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
37 import org
.eclipse
.core
.runtime
.jobs
.Job
;
38 import org
.eclipse
.jface
.action
.IStatusLineManager
;
39 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
40 import org
.eclipse
.jface
.resource
.ColorRegistry
;
41 import org
.eclipse
.jface
.resource
.FontRegistry
;
42 import org
.eclipse
.jface
.window
.ApplicationWindow
;
43 import org
.eclipse
.swt
.graphics
.Color
;
44 import org
.eclipse
.swt
.graphics
.Font
;
45 import org
.eclipse
.swt
.widgets
.Display
;
46 import org
.eclipse
.swt
.widgets
.Shell
;
47 import org
.eclipse
.ui
.IEditorPart
;
48 import org
.eclipse
.ui
.IViewPart
;
49 import org
.eclipse
.ui
.IViewReference
;
50 import org
.eclipse
.ui
.IWorkbench
;
51 import org
.eclipse
.ui
.IWorkbenchPage
;
52 import org
.eclipse
.ui
.IWorkbenchPart
;
53 import org
.eclipse
.ui
.IWorkbenchWindow
;
54 import org
.eclipse
.ui
.PartInitException
;
55 import org
.eclipse
.ui
.PlatformUI
;
56 import org
.eclipse
.ui
.handlers
.IHandlerService
;
57 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
58 import org
.eclipse
.ui
.part
.EditorPart
;
59 import org
.eclipse
.ui
.progress
.IProgressConstants
;
60 import org
.eclipse
.ui
.progress
.IProgressService
;
61 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
62 import org
.eclipse
.ui
.themes
.ITheme
;
63 import org
.eclipse
.ui
.themes
.IThemeManager
;
65 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
66 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
67 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
68 import eu
.etaxonomy
.cdm
.model
.common
.IEnumTerm
;
69 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
70 import eu
.etaxonomy
.taxeditor
.operation
.IFeedbackGenerator
;
71 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
72 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
73 import eu
.etaxonomy
.taxeditor
.operation
.RemotingCdmHandler
;
74 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
75 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
76 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.ReportTextDialog
;
77 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmDataViewer
;
78 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
79 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
83 * Abstract AbstractUtility class.
90 public abstract class AbstractUtility
{
92 private static final Logger logger
= Logger
.getLogger(AbstractUtility
.class);
94 /** Constant <code>statusLineManager</code> */
95 protected static IStatusLineManager statusLineManager
;
105 public static boolean closeAll() {
106 if(getActivePage()!=null){
107 return getActivePage().closeAllEditors(true);
113 * Close the given editor.
116 * The <tt>MultipageTaxonEditor</tt> to close.
117 * @return <tt>true</tt> on success
119 public static boolean close(EditorPart editor
) {
120 return getActivePage().closeEditor(editor
, true);
128 * @return a {@link org.eclipse.swt.widgets.Shell} object.
130 public static Shell
getShell() {
132 return TaxeditorStorePlugin
.getDefault().getWorkbench()
133 .getActiveWorkbenchWindow().getShell();
141 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
143 public static IWorkbenchPage
getActivePage() {
145 return TaxeditorStorePlugin
.getDefault().getWorkbench()
146 .getActiveWorkbenchWindow().getActivePage();
154 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
156 public static IWorkbenchPart
getActivePart() {
157 return getActivePage() != null ?
getActivePage().getActivePart() : null;
160 public static IWorkbench
getWorkbench() {
161 return TaxeditorStorePlugin
.getDefault().getWorkbench();
169 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
171 public static ApplicationWindow
getWorkbenchWindow() {
172 if (getWorkbench().getWorkbenchWindowCount() > 1) {
173 throw new IllegalStateException("More than one workbench window");
175 return (ApplicationWindow
) getWorkbench().getWorkbenchWindows()[0];
184 * a {@link java.lang.String} object.
185 * @return a {@link org.eclipse.ui.IViewPart} object.
187 public static IViewPart
showView(String id
) {
189 return PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
191 .showView(id
, null, IWorkbenchPage
.VIEW_VISIBLE
);
192 } catch (PartInitException e
) {
193 MessagingUtils
.messageDialog("Error opening view", AbstractUtility
.class, "Could not open view: " + id
, e
);
204 * a {@link org.eclipse.ui.IViewPart} object.
206 public static void hideView(IViewPart view
) {
207 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
217 * a {@link java.lang.String} object.
220 * @return a {@link org.eclipse.ui.IViewPart} object.
222 public static IViewPart
getView(String id
, boolean restore
) {
223 IWorkbench workbench
= PlatformUI
.getWorkbench();
224 IWorkbenchWindow activeWorkbenchWindow
= workbench
.getActiveWorkbenchWindow();
225 IViewReference
[] references
= null;
226 if(activeWorkbenchWindow
!=null && activeWorkbenchWindow
.getActivePage()!=null){
227 references
= activeWorkbenchWindow
.getActivePage().getViewReferences();
229 else if(workbench
.getWorkbenchWindows().length
>0 && workbench
.getWorkbenchWindows()[0].getActivePage()!=null){
230 references
= workbench
.getWorkbenchWindows()[0].getActivePage().getViewReferences();
232 if(references
!=null){
233 for (IViewReference reference
: references
) {
234 if (reference
.getId().equals(id
)) {
235 return reference
.getView(restore
);
248 * a {@link java.lang.Class} object.
249 * @return a {@link java.lang.Object} object.
251 public static Object
getService(Class api
) {
252 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
260 * @return a {@link org.eclipse.ui.themes.ITheme} object.
262 public static ITheme
getCurrentTheme() {
263 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault()
264 .getWorkbench().getThemeManager();
265 return themeManager
.getCurrentTheme();
269 * Fonts registered to the plugin may be obtained with the Eclipse themeing
270 * functionality. Thus fonts are chooseable by the user via
271 * Preferences->General->Appearance->Colors and Fonts
273 * @return the FontRegistry for the current theme
275 public static FontRegistry
getFontRegistry() {
276 return getCurrentTheme().getFontRegistry();
284 * @param symbolicName
285 * a {@link java.lang.String} object.
286 * @return a {@link org.eclipse.swt.graphics.Font} object.
288 public static Font
getFont(String symbolicName
) {
289 return getFontRegistry().get(symbolicName
);
293 * Color registered to the plugin may be obtained with the Eclipse themeing
294 * functionality. Thus colors are editable by the user via
295 * Preferences->General->Appearance->Colors and Fonts
297 * @return the ColorRegistry for the current theme
299 public static ColorRegistry
getColorRegistry() {
300 return getCurrentTheme().getColorRegistry();
308 * @param symbolicName
309 * a {@link java.lang.String} object.
310 * @return a {@link org.eclipse.swt.graphics.Color} object.
312 public static Color
getColor(String symbolicName
) {
313 return getColorRegistry().get(symbolicName
);
323 * {@link eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation}
325 * @return a {@link org.eclipse.core.runtime.IStatus} object.
327 public static IStatus
executeOperation(final AbstractPostOperation operation
) {
328 if (getOperationHistory() == null) {
329 throw new IllegalArgumentException(
330 "There is no operation history for this context");
333 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
334 .getUIInfoAdapter(getShell());
336 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
339 public void run(IProgressMonitor monitor
)
340 throws InvocationTargetException
, InterruptedException
{
341 String operationlabel
= operation
.getLabel();
342 monitor
.beginTask(operationlabel
, 100);
343 IStatus status
= Status
.CANCEL_STATUS
;
345 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
346 status
= getOperationHistory().execute(operation
, monitor
,
348 } catch (ExecutionException e
) {
350 MessagingUtils
.operationDialog(this, e
, TaxeditorStorePlugin
.PLUGIN_ID
, operationlabel
, null);
356 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
358 setStatusLine(operationlabel
+ " " + statusString
+ ".");
364 runInUI(runnable
, null);
365 } catch (Exception e
) {
366 MessagingUtils
.messageDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
369 IPostOperationEnabled postOperationEnabled
= operation
370 .getPostOperationEnabled();
371 if (postOperationEnabled
!= null) {
372 postOperationEnabled
.onComplete();
374 return Status
.OK_STATUS
;
377 public static IStatus
executeOperation(final AbstractOperation operation
, final RemotingCdmHandler handler
) {
378 if (getOperationHistory() == null) {
379 throw new IllegalArgumentException(
380 "There is no operation history for this context");
383 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
384 .getUIInfoAdapter(getShell());
386 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
389 public void run(IProgressMonitor monitor
)
390 throws InvocationTargetException
, InterruptedException
{
391 String operationlabel
= operation
.getLabel();
392 monitor
.beginTask(operationlabel
, 100);
393 IStatus status
= Status
.CANCEL_STATUS
;
395 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
396 status
= getOperationHistory().execute(operation
, monitor
,
398 if(handler
!= null) {
399 handler
.postOperation(status
);
401 } catch (ExecutionException e
) {
402 MessagingUtils
.operationDialog(this, e
, TaxeditorStorePlugin
.PLUGIN_ID
, operationlabel
, null);
407 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
409 setStatusLine(operationlabel
+ " " + statusString
+ ".");
415 runInUI(runnable
, null);
416 } catch (Exception e
) {
417 MessagingUtils
.messageDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
420 return Status
.OK_STATUS
;
424 * Executes a remoting monitored operation
426 * @param label for the operation
427 * @param uuid of the remoting monitor already started on the server
428 * @param pollInterval in milliseconds
429 * @param cancelable flag which determines whether the operation can be cancelled
430 * @param postOp callback for running post operation logic
433 public static IStatus
executeMoniteredOperation(final String label
,
435 final int pollInterval
,
436 final boolean cancelable
,
437 final IPostMoniteredOperationEnabled postOp
,
438 final IFeedbackGenerator feedbackGenerator
) {
441 // get the remoting monitor the first time to make sure that the
442 // operation is valid
443 final IProgressMonitorService progressMonitorService
= CdmApplicationState
.getCurrentAppConfig().getProgressMonitorService();
444 final IRemotingProgressMonitor firstRemotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
445 if(firstRemotingMonitor
== null) {
446 throw new IllegalStateException("Remoting progress monitor is null");
449 Job job
= new Job(label
) {
453 public IStatus
run(IProgressMonitor monitor
) {
454 // run the monitor until the operation is finished
455 IRemotingProgressMonitor remotingMonitor
;
457 remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor(label
,
463 } catch (Exception ex
) {
464 return new Status(Status
.ERROR
, TaxeditorStorePlugin
.PLUGIN_ID
, "Operation Interrupted", ex
);
466 final StringBuilder reportSb
= new StringBuilder();
468 for(String report
: remotingMonitor
.getReports()) {
469 reportSb
.append(report
);
471 if(!StringUtils
.isBlank(reportSb
.toString())) {
472 Display
.getDefault().asyncExec(new Runnable() {
475 // display reports with possibility to save
476 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
477 dialog
.setTitle(label
+ " Report");
478 dialog
.setReportText(reportSb
.toString());
483 return Status
.OK_STATUS
;
487 protected void canceling() {
488 CdmStore
.getCurrentApplicationConfiguration().getProgressMonitorService().cancel(uuid
);
493 job
.setProperty(IProgressConstants
.KEEP_PROPERTY
, true);
498 } catch (Exception e
) {
499 MessagingUtils
.errorDialog("Error executing operation",
500 AbstractUtility
.class,
501 "An error occured while executing " + label
,
502 TaxeditorStorePlugin
.PLUGIN_ID
,
507 return Status
.OK_STATUS
;
513 * getOperationHistory
516 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory}
519 public static IOperationHistory
getOperationHistory() {
520 return getWorkbench().getOperationSupport().getOperationHistory();
529 * a {@link java.lang.String} object.
531 public static void setStatusLine(final String message
) {
532 Display
.getDefault().asyncExec(new Runnable() {
536 statusLineManager
.setMessage(message
);
548 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
550 public static IProgressMonitor
getMonitor() {
551 statusLineManager
.setCancelEnabled(false);
552 return statusLineManager
.getProgressMonitor();
556 * Starts either the given {@link IProgressMonitor} if it's not
557 * <code>null</code> or a new {@link NullProgressMonitor}.
559 * @param progressMonitor
560 * The {@link IProgressMonitor} or <code>null</code> if no
561 * progress should be reported.
563 * The name of the main task.
565 * The number of steps this task is subdivided into.
566 * @return The {@link IProgressMonitor}.
568 public static IProgressMonitor
startMainMonitor(
569 IProgressMonitor progressMonitor
, String taskName
, int steps
) {
570 IProgressMonitor newMonitor
= progressMonitor
;
571 if (newMonitor
== null) {
572 newMonitor
= new NullProgressMonitor();
574 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
575 newMonitor
.subTask(" ");
580 * Creates a {@link SubProgressMonitor} if the given
581 * {@link IProgressMonitor} is not <code>null</code> and not a
582 * {@link NullProgressMonitor}.
584 * @param progressMonitor
585 * The parent {@link IProgressMonitor} of the
586 * {@link SubProgressMonitor} to be created.
588 * The number of steps this subtask is subdivided into. Must be a
589 * positive number and must not be
590 * {@link IProgressMonitor#UNKNOWN}.
591 * @return The {@link IProgressMonitor}.
593 public static IProgressMonitor
getSubProgressMonitor(
594 IProgressMonitor progressMonitor
, int ticks
) {
595 if (progressMonitor
== null) {
596 return new NullProgressMonitor();
598 if (progressMonitor
instanceof NullProgressMonitor
) {
599 return progressMonitor
;
602 return new SubProgressMonitor(progressMonitor
, ticks
);
606 * Checks whether the user canceled this operation. If not canceled, the
607 * given number of steps are declared as done.
610 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
614 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
615 // In case the progress monitor was canceled throw an exception.
616 if (newMonitor
.isCanceled()) {
617 throw new OperationCanceledException();
619 // Otherwise declare this step as done.
620 newMonitor
.worked(steps
);
624 * Present a progress dialog to the user. This dialog will block the UI
627 * an implementation of {@link IRunnableWithProgress}
628 * @throws java.lang.InterruptedException
630 * @throws java.lang.reflect.InvocationTargetException
633 public static void busyCursorWhile(IRunnableWithProgress runnable
)
634 throws InvocationTargetException
, InterruptedException
{
635 getProgressService().busyCursorWhile(runnable
);
643 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
645 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
648 * a {@link org.eclipse.core.runtime.jobs.ISchedulingRule}
650 * @throws java.lang.reflect.InvocationTargetException
652 * @throws java.lang.InterruptedException
655 public static void runInUI(IRunnableWithProgress runnable
,
656 ISchedulingRule rule
) throws InvocationTargetException
,
657 InterruptedException
{
658 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
671 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
673 * @throws java.lang.reflect.InvocationTargetException
675 * @throws java.lang.InterruptedException
678 public static void run(boolean fork
, boolean cancelable
,
679 IRunnableWithProgress runnable
) throws InvocationTargetException
,
680 InterruptedException
{
681 getProgressService().run(fork
, cancelable
, runnable
);
689 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
691 public static IProgressService
getProgressService() {
692 IWorkbench workbench
= PlatformUI
.getWorkbench();
693 return workbench
.getProgressService();
698 * getProgressService2
701 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService}
704 public static IWorkbenchSiteProgressService
getProgressService2() {
705 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
713 * @return a {@link java.lang.String} object.
715 public static String
getPluginId() {
716 return "eu.taxeditor";
724 * @return a {@link org.eclipse.ui.IEditorPart} object.
726 public static IEditorPart
getActiveEditor() {
727 return getActivePage() != null ?
getActivePage().getActiveEditor()
736 * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart}
739 public static DetailsViewPart
getDetailsView() {
740 return (DetailsViewPart
) getView(DetailsViewPart
.ID
, false);
745 * refreshDetailsViewer
748 public static void refreshDetailsViewer() {
749 if (getDetailsView() != null) {
750 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
756 * reflowDetailsViewer
759 public static void reflowDetailsViewer() {
760 if (getDetailsView() != null) {
761 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).reflow();
765 public static SupplementalDataViewPart
getSupplementalDataView() {
766 return (SupplementalDataViewPart
) getView(SupplementalDataViewPart
.ID
,
770 public static void reflowSupplementalViewer() {
771 if (getSupplementalDataView() != null) {
772 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer())
779 * Orders a Collection of {@link IEnumTerm}s according to the term
782 * The returned map will be be ordered primarily by root elements,
783 * secondarily by the child elements and their children resp., both ascending alphabetically. <br>
785 * A {@link Collection} of {@link IEnumTerm}s for which the term
786 * hierarchy should be created
787 * @return a map which holds the terms as keys and their string
788 * representation via {@link IEnumTerm#getMessage()} as values
790 public static <T
extends IEnumTerm
<T
>> LinkedHashMap
<T
, String
> orderTerms(Collection
<T
> terms
) {
791 TreeSet
<TermNode
<T
>> parentElements
= new TreeSet
<TermNode
<T
>>();
792 parentElements
.addAll(getTermHierarchy(terms
));
794 // create list according to the type hierarchy (root elements alphabetically with recursive children also alphabetically)
795 LinkedHashMap
<T
, String
> result
= new LinkedHashMap
<T
, String
>();
796 parseTermTree(parentElements
, result
, -1);
800 private static<T
extends IEnumTerm
<T
>> void parseTermTree(Collection
<TermNode
<T
>> children
, LinkedHashMap
<T
, String
> result
, int depth
){
802 for(TermNode
<T
> node
:children
){
803 String indentString
= "";
804 for(int i
=0;i
<depth
;i
++){
808 indentString
+= "- ";
810 result
.put(node
.term
, indentString
+ node
.term
.getMessage());
811 parseTermTree(node
.children
, result
, depth
);
815 private static<T
extends IEnumTerm
<T
>> void addToParents(List
<TermNode
<T
>> parents
, Collection
<T
> terms
){
816 List
<TermNode
<T
>> hasChildrenList
= new ArrayList
<TermNode
<T
>>();
818 // only terms with parents
819 if(term
.getKindOf()!=null){
820 TermNode
<T
> parentNode
= new TermNode
<T
>(term
.getKindOf());
821 TermNode
<T
> childNode
= new TermNode
<T
>(term
);
822 if(parents
.contains(parentNode
)){
823 // parent found in parent list -> add this term to parent's child list
824 parents
.get(parents
.indexOf(parentNode
)).addChild(childNode
);
825 if(!term
.getGeneralizationOf().isEmpty()){
826 // has more children -> add to list which will be the parent for the next recursion
827 hasChildrenList
.add(childNode
);
832 if(!hasChildrenList
.isEmpty()){
833 addToParents(hasChildrenList
, terms
);
837 private static<T
extends IEnumTerm
<T
>> List
<TermNode
<T
>> getTermHierarchy(Collection
<T
> terms
){
838 List
<TermNode
<T
>> parents
= new ArrayList
<TermNode
<T
>>();
841 T parentTerm
= term
.getKindOf();
842 if(parentTerm
==null){
844 parents
.add(new TermNode
<T
>(term
));
847 addToParents(parents
, terms
);
851 @SuppressWarnings("unchecked")
853 * Recursively iterates over all term parents until no more parent is found i.e. the root node
854 * @param term The term for which the parent should be found
855 * @return the root terms of the term hierarchy
857 private static<T
extends IEnumTerm
<T
>> T
getParentFor(T term
){
858 // PP: cast should be safe. Why is Eclipse complaining??
859 T parent
= term
.getKindOf();
864 return getParentFor(term
.getKindOf());
868 private static class TermNode
<T
extends IEnumTerm
<T
>> implements Comparable
<TermNode
<T
>>{
869 private final T term
;
870 private final TreeSet
<TermNode
<T
>> children
;
876 public TermNode(T term
) {
879 this.children
= new TreeSet
<TermNode
<T
>>();
882 public void addChild(TermNode
<T
> child
){
883 this.children
.add(child
);
887 * @return the children
889 public TreeSet
<TermNode
<T
>> getChildren() {
901 * @see java.lang.Object#hashCode()
904 public int hashCode() {
905 final int prime
= 31;
907 result
= prime
* result
+ ((term
== null) ?
0 : term
.hashCode());
912 * @see java.lang.Object#equals(java.lang.Object)
915 public boolean equals(Object obj
) {
922 if (getClass() != obj
.getClass()) {
925 TermNode other
= (TermNode
) obj
;
927 if (other
.term
!= null) {
930 } else if (!term
.equals(other
.term
)) {
937 * @see java.lang.Comparable#compareTo(java.lang.Object)
940 public int compareTo(TermNode
<T
> that
) {
941 return this.term
.getMessage().compareTo(that
.term
.getMessage());
946 public static void executeCommand(String commandId
, Object source
, String pluginId
) {
947 IHandlerService handlerService
= (IHandlerService
) AbstractUtility
.getService(IHandlerService
.class);
948 Exception exception
= null;
950 handlerService
.executeCommand(commandId
, null);
951 } catch (ExecutionException e
) {
953 } catch (NotDefinedException e
) {
955 } catch (NotEnabledException e
) {
957 } catch (NotHandledException e
) {
960 if(exception
!= null) {
961 MessagingUtils
.errorDialog("Error executing command",
963 "Could not execute command with id " + commandId
,