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
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
22 import org
.eclipse
.core
.runtime
.IAdaptable
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
30 import org
.eclipse
.jface
.action
.IStatusLineManager
;
31 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
32 import org
.eclipse
.jface
.resource
.ColorRegistry
;
33 import org
.eclipse
.jface
.resource
.FontRegistry
;
34 import org
.eclipse
.jface
.window
.ApplicationWindow
;
35 import org
.eclipse
.swt
.graphics
.Color
;
36 import org
.eclipse
.swt
.graphics
.Font
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
39 import org
.eclipse
.ui
.IEditorPart
;
40 import org
.eclipse
.ui
.IViewPart
;
41 import org
.eclipse
.ui
.IViewReference
;
42 import org
.eclipse
.ui
.IWorkbench
;
43 import org
.eclipse
.ui
.IWorkbenchPage
;
44 import org
.eclipse
.ui
.IWorkbenchPart
;
45 import org
.eclipse
.ui
.PartInitException
;
46 import org
.eclipse
.ui
.PlatformUI
;
47 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
48 import org
.eclipse
.ui
.part
.EditorPart
;
49 import org
.eclipse
.ui
.progress
.IProgressService
;
50 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
51 import org
.eclipse
.ui
.themes
.ITheme
;
52 import org
.eclipse
.ui
.themes
.IThemeManager
;
54 import eu
.etaxonomy
.cdm
.model
.common
.IEnumTerm
;
55 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
56 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
57 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
58 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmDataViewer
;
59 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
60 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
64 * Abstract AbstractUtility class.
71 public abstract class AbstractUtility
{
73 /** Constant <code>statusLineManager</code> */
74 protected static IStatusLineManager statusLineManager
;
84 public static boolean closeAll() {
85 return getActivePage().closeAllEditors(true);
89 * Close the given editor.
92 * The <tt>MultipageTaxonEditor</tt> to close.
93 * @return <tt>true</tt> on success
95 public static boolean close(EditorPart editor
) {
96 return getActivePage().closeEditor(editor
, true);
104 * @return a {@link org.eclipse.swt.widgets.Shell} object.
106 public static Shell
getShell() {
108 return TaxeditorStorePlugin
.getDefault().getWorkbench()
109 .getActiveWorkbenchWindow().getShell();
117 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
119 public static IWorkbenchPage
getActivePage() {
121 return TaxeditorStorePlugin
.getDefault().getWorkbench()
122 .getActiveWorkbenchWindow().getActivePage();
130 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
132 public static IWorkbenchPart
getActivePart() {
133 return getActivePage() != null ?
getActivePage().getActivePart() : null;
136 public static IWorkbench
getWorkbench() {
137 return TaxeditorStorePlugin
.getDefault().getWorkbench();
145 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
147 public static ApplicationWindow
getWorkbenchWindow() {
148 if (getWorkbench().getWorkbenchWindowCount() > 1) {
149 throw new IllegalStateException("More than one workbench window");
151 return (ApplicationWindow
) getWorkbench().getWorkbenchWindows()[0];
160 * a {@link java.lang.String} object.
161 * @return a {@link org.eclipse.ui.IViewPart} object.
163 public static IViewPart
showView(String id
) {
165 return PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
167 .showView(id
, null, IWorkbenchPage
.VIEW_VISIBLE
);
168 } catch (PartInitException e
) {
169 MessagingUtils
.messageDialog("Error opening view", AbstractUtility
.class, "Could not open view: " + id
, e
);
180 * a {@link org.eclipse.ui.IViewPart} object.
182 public static void hideView(IViewPart view
) {
183 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
193 * a {@link java.lang.String} object.
196 * @return a {@link org.eclipse.ui.IViewPart} object.
198 public static IViewPart
getView(String id
, boolean restore
) {
199 IViewReference
[] references
= PlatformUI
.getWorkbench()
200 .getActiveWorkbenchWindow().getActivePage().getViewReferences();
201 for (IViewReference reference
: references
) {
202 if (reference
.getId().equals(id
)) {
203 return reference
.getView(restore
);
215 * a {@link java.lang.Class} object.
216 * @return a {@link java.lang.Object} object.
218 public static Object
getService(Class api
) {
219 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
227 * @return a {@link org.eclipse.ui.themes.ITheme} object.
229 public static ITheme
getCurrentTheme() {
230 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault()
231 .getWorkbench().getThemeManager();
232 return themeManager
.getCurrentTheme();
236 * Fonts registered to the plugin may be obtained with the Eclipse themeing
237 * functionality. Thus fonts are chooseable by the user via
238 * Preferences->General->Appearance->Colors and Fonts
240 * @return the FontRegistry for the current theme
242 public static FontRegistry
getFontRegistry() {
243 return getCurrentTheme().getFontRegistry();
251 * @param symbolicName
252 * a {@link java.lang.String} object.
253 * @return a {@link org.eclipse.swt.graphics.Font} object.
255 public static Font
getFont(String symbolicName
) {
256 return getFontRegistry().get(symbolicName
);
260 * Color registered to the plugin may be obtained with the Eclipse themeing
261 * functionality. Thus colors are editable by the user via
262 * Preferences->General->Appearance->Colors and Fonts
264 * @return the ColorRegistry for the current theme
266 public static ColorRegistry
getColorRegistry() {
267 return getCurrentTheme().getColorRegistry();
275 * @param symbolicName
276 * a {@link java.lang.String} object.
277 * @return a {@link org.eclipse.swt.graphics.Color} object.
279 public static Color
getColor(String symbolicName
) {
280 return getColorRegistry().get(symbolicName
);
290 * {@link eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation}
292 * @return a {@link org.eclipse.core.runtime.IStatus} object.
294 public static IStatus
executeOperation(final AbstractPostOperation operation
) {
295 if (getOperationHistory() == null) {
296 throw new IllegalArgumentException(
297 "There is no operation history for this context");
300 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
301 .getUIInfoAdapter(getShell());
303 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
306 public void run(IProgressMonitor monitor
)
307 throws InvocationTargetException
, InterruptedException
{
308 String operationlabel
= operation
.getLabel();
309 monitor
.beginTask(operationlabel
, 100);
310 IStatus status
= Status
.CANCEL_STATUS
;
312 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
313 status
= getOperationHistory().execute(operation
, monitor
,
315 } catch (ExecutionException e
) {
317 MessagingUtils
.operationDialog(this, e
, TaxeditorStorePlugin
.PLUGIN_ID
, operationlabel
, null);
323 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
325 setStatusLine(operationlabel
+ " " + statusString
+ ".");
331 runInUI(runnable
, null);
332 } catch (Exception e
) {
333 MessagingUtils
.messageDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
336 IPostOperationEnabled postOperationEnabled
= operation
337 .getPostOperationEnabled();
338 if (postOperationEnabled
!= null) {
339 postOperationEnabled
.onComplete();
341 return Status
.OK_STATUS
;
346 * getOperationHistory
349 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory}
352 public static IOperationHistory
getOperationHistory() {
353 return getWorkbench().getOperationSupport().getOperationHistory();
362 * a {@link java.lang.String} object.
364 public static void setStatusLine(final String message
) {
365 Display
.getDefault().asyncExec(new Runnable() {
369 statusLineManager
.setMessage(message
);
381 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
383 public static IProgressMonitor
getMonitor() {
384 statusLineManager
.setCancelEnabled(false);
385 return statusLineManager
.getProgressMonitor();
389 * Starts either the given {@link IProgressMonitor} if it's not
390 * <code>null</code> or a new {@link NullProgressMonitor}.
392 * @param progressMonitor
393 * The {@link IProgressMonitor} or <code>null</code> if no
394 * progress should be reported.
396 * The name of the main task.
398 * The number of steps this task is subdivided into.
399 * @return The {@link IProgressMonitor}.
401 public static IProgressMonitor
startMainMonitor(
402 IProgressMonitor progressMonitor
, String taskName
, int steps
) {
403 IProgressMonitor newMonitor
= progressMonitor
;
404 if (newMonitor
== null) {
405 newMonitor
= new NullProgressMonitor();
407 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
408 newMonitor
.subTask(" ");
413 * Creates a {@link SubProgressMonitor} if the given
414 * {@link IProgressMonitor} is not <code>null</code> and not a
415 * {@link NullProgressMonitor}.
417 * @param progressMonitor
418 * The parent {@link IProgressMonitor} of the
419 * {@link SubProgressMonitor} to be created.
421 * The number of steps this subtask is subdivided into. Must be a
422 * positive number and must not be
423 * {@link IProgressMonitor#UNKNOWN}.
424 * @return The {@link IProgressMonitor}.
426 public static IProgressMonitor
getSubProgressMonitor(
427 IProgressMonitor progressMonitor
, int ticks
) {
428 if (progressMonitor
== null) {
429 return new NullProgressMonitor();
431 if (progressMonitor
instanceof NullProgressMonitor
) {
432 return progressMonitor
;
435 return new SubProgressMonitor(progressMonitor
, ticks
);
439 * Checks whether the user canceled this operation. If not canceled, the
440 * given number of steps are declared as done.
443 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
447 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
448 // In case the progress monitor was canceled throw an exception.
449 if (newMonitor
.isCanceled()) {
450 throw new OperationCanceledException();
452 // Otherwise declare this step as done.
453 newMonitor
.worked(steps
);
457 * Present a progress dialog to the user. This dialog will block the UI
460 * an implementation of {@link IRunnableWithProgress}
461 * @throws java.lang.InterruptedException
463 * @throws java.lang.reflect.InvocationTargetException
466 public static void busyCursorWhile(IRunnableWithProgress runnable
)
467 throws InvocationTargetException
, InterruptedException
{
468 getProgressService().busyCursorWhile(runnable
);
476 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
478 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
481 * a {@link org.eclipse.core.runtime.jobs.ISchedulingRule}
483 * @throws java.lang.reflect.InvocationTargetException
485 * @throws java.lang.InterruptedException
488 public static void runInUI(IRunnableWithProgress runnable
,
489 ISchedulingRule rule
) throws InvocationTargetException
,
490 InterruptedException
{
491 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
504 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
506 * @throws java.lang.reflect.InvocationTargetException
508 * @throws java.lang.InterruptedException
511 public static void run(boolean fork
, boolean cancelable
,
512 IRunnableWithProgress runnable
) throws InvocationTargetException
,
513 InterruptedException
{
514 getProgressService().run(fork
, cancelable
, runnable
);
522 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
524 public static IProgressService
getProgressService() {
525 IWorkbench workbench
= PlatformUI
.getWorkbench();
526 return workbench
.getProgressService();
531 * getProgressService2
534 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService}
537 public static IWorkbenchSiteProgressService
getProgressService2() {
538 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
546 * @return a {@link java.lang.String} object.
548 public static String
getPluginId() {
549 return "eu.taxeditor";
557 * @return a {@link org.eclipse.ui.IEditorPart} object.
559 public static IEditorPart
getActiveEditor() {
560 return getActivePage() != null ?
getActivePage().getActiveEditor()
569 * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart}
572 public static DetailsViewPart
getDetailsView() {
573 return (DetailsViewPart
) getView(DetailsViewPart
.ID
, false);
578 * refreshDetailsViewer
581 public static void refreshDetailsViewer() {
582 if (getDetailsView() != null) {
583 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
589 * reflowDetailsViewer
592 public static void reflowDetailsViewer() {
593 if (getDetailsView() != null) {
594 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).reflow();
598 public static SupplementalDataViewPart
getSupplementalDataView() {
599 return (SupplementalDataViewPart
) getView(SupplementalDataViewPart
.ID
,
603 public static void reflowSupplementalViewer() {
604 if (getSupplementalDataView() != null) {
605 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer())
612 * Orders a Collection of {@link IEnumTerm}s according to the term
615 * The returned map will be be ordered primarily by root elements,
616 * secondarily by the child elements and their children resp., both ascending alphabetically. <br>
618 * A {@link Collection} of {@link IEnumTerm}s for which the term
619 * hierarchy should be created
620 * @return a map which holds the terms as keys and their string
621 * representation via {@link IEnumTerm#getMessage()} as values
623 public static <T
extends IEnumTerm
<T
>> LinkedHashMap
<T
, String
> orderTerms(Collection
<T
> terms
) {
624 TreeSet
<TermNode
<T
>> parentElements
= new TreeSet
<TermNode
<T
>>();
625 parentElements
.addAll(getTermHierarchy(terms
));
627 // create list according to the type hierarchy (root elements alphabetically with recursive children also alphabetically)
628 LinkedHashMap
<T
, String
> result
= new LinkedHashMap
<T
, String
>();
629 parseTermTree(parentElements
, result
, -1);
633 private static<T
extends IEnumTerm
<T
>> void parseTermTree(Collection
<TermNode
<T
>> children
, LinkedHashMap
<T
, String
> result
, int depth
){
635 for(TermNode
<T
> node
:children
){
636 String indentString
= "";
637 for(int i
=0;i
<depth
;i
++){
641 indentString
+= "- ";
643 result
.put(node
.term
, indentString
+ node
.term
.getMessage());
644 parseTermTree(node
.children
, result
, depth
);
648 private static<T
extends IEnumTerm
<T
>> void addToParents(List
<TermNode
<T
>> parents
, Collection
<T
> terms
){
649 List
<TermNode
<T
>> hasChildrenList
= new ArrayList
<TermNode
<T
>>();
651 // only terms with parents
652 if(term
.getKindOf()!=null){
653 TermNode
<T
> parentNode
= new TermNode
<T
>(term
.getKindOf());
654 TermNode
<T
> childNode
= new TermNode
<T
>(term
);
655 if(parents
.contains(parentNode
)){
656 // parent found in parent list -> add this term to parent's child list
657 parents
.get(parents
.indexOf(parentNode
)).addChild(childNode
);
658 if(!term
.getGeneralizationOf().isEmpty()){
659 // has more children -> add to list which will be the parent for the next recursion
660 hasChildrenList
.add(childNode
);
665 if(!hasChildrenList
.isEmpty()){
666 addToParents(hasChildrenList
, terms
);
670 private static<T
extends IEnumTerm
<T
>> List
<TermNode
<T
>> getTermHierarchy(Collection
<T
> terms
){
671 List
<TermNode
<T
>> parents
= new ArrayList
<TermNode
<T
>>();
674 T parentTerm
= term
.getKindOf();
675 if(parentTerm
==null){
677 parents
.add(new TermNode
<T
>(term
));
680 addToParents(parents
, terms
);
684 @SuppressWarnings("unchecked")
686 * Recursively iterates over all term parents until no more parent is found i.e. the root node
687 * @param term The term for which the parent should be found
688 * @return the root terms of the term hierarchy
690 private static<T
extends IEnumTerm
<T
>> T
getParentFor(T term
){
691 // PP: cast should be safe. Why is Eclipse complaining??
692 T parent
= term
.getKindOf();
697 return getParentFor(term
.getKindOf());
701 private static class TermNode
<T
extends IEnumTerm
<T
>> implements Comparable
<TermNode
<T
>>{
702 private final T term
;
703 private final TreeSet
<TermNode
<T
>> children
;
709 public TermNode(T term
) {
712 this.children
= new TreeSet
<TermNode
<T
>>();
715 public void addChild(TermNode
<T
> child
){
716 this.children
.add(child
);
720 * @return the children
722 public TreeSet
<TermNode
<T
>> getChildren() {
734 * @see java.lang.Object#hashCode()
737 public int hashCode() {
738 final int prime
= 31;
740 result
= prime
* result
+ ((term
== null) ?
0 : term
.hashCode());
745 * @see java.lang.Object#equals(java.lang.Object)
748 public boolean equals(Object obj
) {
755 if (getClass() != obj
.getClass()) {
758 TermNode other
= (TermNode
) obj
;
760 if (other
.term
!= null) {
763 } else if (!term
.equals(other
.term
)) {
770 * @see java.lang.Comparable#compareTo(java.lang.Object)
773 public int compareTo(TermNode
<T
> that
) {
774 return this.term
.getMessage().compareTo(that
.term
.getMessage());