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
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.core
.commands
.ExecutionException
;
17 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
18 import org
.eclipse
.core
.runtime
.IAdaptable
;
19 import org
.eclipse
.core
.runtime
.ILog
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
27 import org
.eclipse
.jface
.action
.IStatusLineManager
;
28 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
30 import org
.eclipse
.jface
.resource
.ColorRegistry
;
31 import org
.eclipse
.jface
.resource
.FontRegistry
;
32 import org
.eclipse
.jface
.window
.ApplicationWindow
;
33 import org
.eclipse
.swt
.graphics
.Color
;
34 import org
.eclipse
.swt
.graphics
.Font
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Shell
;
37 import org
.eclipse
.ui
.IEditorPart
;
38 import org
.eclipse
.ui
.IViewPart
;
39 import org
.eclipse
.ui
.IViewReference
;
40 import org
.eclipse
.ui
.IWorkbench
;
41 import org
.eclipse
.ui
.IWorkbenchPage
;
42 import org
.eclipse
.ui
.IWorkbenchPart
;
43 import org
.eclipse
.ui
.PartInitException
;
44 import org
.eclipse
.ui
.PlatformUI
;
45 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
46 import org
.eclipse
.ui
.part
.EditorPart
;
47 import org
.eclipse
.ui
.progress
.IProgressService
;
48 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
49 import org
.eclipse
.ui
.themes
.ITheme
;
50 import org
.eclipse
.ui
.themes
.IThemeManager
;
52 import eu
.etaxonomy
.cdm
.model
.common
.TermBase
;
53 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
54 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
55 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
56 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmDataViewer
;
57 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
58 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
59 import eu
.etaxonomy
.taxeditor
.view
.userecords
.UseRecordsViewPart
;
63 * Abstract AbstractUtility class.
70 public abstract class AbstractUtility
{
72 /** Constant <code>statusLineManager</code> */
73 protected static IStatusLineManager statusLineManager
;
82 public static boolean closeAll() {
83 return getActivePage().closeAllEditors(true);
87 * Close the given editor.
90 * The <tt>MultipageTaxonEditor</tt> to close.
91 * @return <tt>true</tt> on success
93 public static boolean close(EditorPart editor
) {
94 return getActivePage().closeEditor(editor
, true);
102 * @return a {@link org.eclipse.swt.widgets.Shell} object.
104 public static Shell
getShell() {
106 return TaxeditorStorePlugin
.getDefault().getWorkbench()
107 .getActiveWorkbenchWindow().getShell();
115 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
117 public static IWorkbenchPage
getActivePage() {
119 return TaxeditorStorePlugin
.getDefault().getWorkbench()
120 .getActiveWorkbenchWindow().getActivePage();
128 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
130 public static IWorkbenchPart
getActivePart() {
131 return getActivePage() != null ?
getActivePage().getActivePart() : null;
134 public static IWorkbench
getWorkbench() {
135 return TaxeditorStorePlugin
.getDefault().getWorkbench();
143 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
145 public static ApplicationWindow
getWorkbenchWindow() {
146 if (getWorkbench().getWorkbenchWindowCount() > 1) {
147 throw new IllegalStateException("More than one workbench window");
149 return (ApplicationWindow
) getWorkbench().getWorkbenchWindows()[0];
158 * a {@link java.lang.String} object.
159 * @return a {@link org.eclipse.ui.IViewPart} object.
161 public static IViewPart
showView(String id
) {
163 return PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
165 .showView(id
, null, IWorkbenchPage
.VIEW_VISIBLE
);
166 } catch (PartInitException e
) {
167 errorDialog("Error opening view", AbstractUtility
.class, "Could not open view: " + id
, e
);
178 * a {@link org.eclipse.ui.IViewPart} object.
180 public static void hideView(IViewPart view
) {
181 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
191 * a {@link java.lang.String} object.
194 * @return a {@link org.eclipse.ui.IViewPart} object.
196 public static IViewPart
getView(String id
, boolean restore
) {
197 IViewReference
[] references
= PlatformUI
.getWorkbench()
198 .getActiveWorkbenchWindow().getActivePage().getViewReferences();
199 for (IViewReference reference
: references
) {
200 if (reference
.getId().equals(id
)) {
201 return reference
.getView(restore
);
213 * a {@link java.lang.Class} object.
214 * @return a {@link java.lang.Object} object.
216 public static Object
getService(Class api
) {
217 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
225 * @return a {@link org.eclipse.ui.themes.ITheme} object.
227 public static ITheme
getCurrentTheme() {
228 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault()
229 .getWorkbench().getThemeManager();
230 return themeManager
.getCurrentTheme();
234 * Fonts registered to the plugin may be obtained with the Eclipse themeing
235 * functionality. Thus fonts are chooseable by the user via
236 * Preferences->General->Appearance->Colors and Fonts
238 * @return the FontRegistry for the current theme
240 public static FontRegistry
getFontRegistry() {
241 return getCurrentTheme().getFontRegistry();
249 * @param symbolicName
250 * a {@link java.lang.String} object.
251 * @return a {@link org.eclipse.swt.graphics.Font} object.
253 public static Font
getFont(String symbolicName
) {
254 return getFontRegistry().get(symbolicName
);
258 * Color registered to the plugin may be obtained with the Eclipse themeing
259 * functionality. Thus colors are editable by the user via
260 * Preferences->General->Appearance->Colors and Fonts
262 * @return the ColorRegistry for the current theme
264 public static ColorRegistry
getColorRegistry() {
265 return getCurrentTheme().getColorRegistry();
273 * @param symbolicName
274 * a {@link java.lang.String} object.
275 * @return a {@link org.eclipse.swt.graphics.Color} object.
277 public static Color
getColor(String symbolicName
) {
278 return getColorRegistry().get(symbolicName
);
282 * Open a message box that informs the user about unimplemented
283 * functionality. This method is for developer convenience.
286 * a {@link java.lang.Object} object.
288 public static void notImplementedMessage(Object source
) {
289 warningDialog("Not yet implemented", source
,
290 "This functionality is not yet implemented.");
299 * a {@link java.lang.String} object.
301 * a {@link java.lang.String} object.
303 public static void informationDialog(final String title
,
304 final String message
) {
305 Display
.getDefault().asyncExec(new Runnable() {
308 MessageDialog
.openInformation(getShell(), title
, message
);
313 public static void informationDialog(final String title
,
314 final IStatus status
) {
315 informationDialog(title
, status
.getMessage());
326 * The object where the warning was generated (used by log4j)
328 * An informative String to be presented to the user
330 public static void warningDialog(final String title
, final Object source
,
331 final String message
) {
332 Display
.getDefault().asyncExec(new Runnable() {
335 MessageDialog
.openWarning(getShell(), title
, message
);
336 Class
<?
extends Object
> clazz
= source
!= null ? source
337 .getClass() : AbstractUtility
.class;
338 warn(clazz
, message
);
348 public static void warningDialog(String title
, Object source
,
350 warningDialog(title
, source
, status
.getMessage());
361 * The object where the warning was generated (used by log4j)
363 * An informative String to be presented to the user
367 * a Throwable if one exists or null
369 public static void errorDialog(final String title
, final Object source
,
370 final String message
, final Throwable t
) {
371 Display
.getDefault().asyncExec(new Runnable() {
374 MessageDialog
.openError(getShell(), title
, message
+ getCauseRecursively(t
));
375 Class
<?
extends Object
> clazz
= source
!= null ? source
376 .getClass() : this.getClass();
377 error(clazz
, message
, t
);
380 private String
getCauseRecursively(Throwable t
) {
385 if(t
.getCause() != null){
386 return getCauseRecursively(t
.getCause());
388 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
395 public static void errorDialog(final String title
, final Object source
,
396 final String message
){
397 errorDialog(title
, source
, message
, null);
406 * a {@link java.lang.String} object.
408 * a {@link java.lang.Object} object.
410 * a {@link org.eclipse.core.runtime.IStatus} object.
412 public static void errorDialog(final String title
, final Object source
,
413 final IStatus status
) {
414 Display
.getDefault().asyncExec(new Runnable() {
417 MessageDialog
.openError(getShell(), title
, status
.getMessage());
418 Class
<?
extends Object
> clazz
= source
!= null ? source
419 .getClass() : this.getClass();
420 error(clazz
, status
.getMessage(), status
.getException());
431 * a {@link java.lang.String} object.
433 * a {@link java.lang.String} object.
436 public static boolean confirmDialog(String title
, String message
) {
437 return MessageDialog
.openQuestion(getShell(), title
, message
);
447 * {@link eu.etaxonomy.taxeditor.operation.AbstractPostOperation}
449 * @return a {@link org.eclipse.core.runtime.IStatus} object.
451 public static IStatus
executeOperation(final AbstractPostOperation operation
) {
452 if (getOperationHistory() == null) {
453 throw new IllegalArgumentException(
454 "There is no operation history for this context");
457 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
458 .getUIInfoAdapter(getShell());
460 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
462 public void run(IProgressMonitor monitor
)
463 throws InvocationTargetException
, InterruptedException
{
464 monitor
.beginTask(operation
.getLabel(), 100);
465 IStatus status
= Status
.CANCEL_STATUS
;
467 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
468 status
= getOperationHistory().execute(operation
, monitor
,
470 } catch (ExecutionException e
) {
471 errorDialog("Error executing operation", getClass(), String
.format("An error occured while executing %s.", operation
.getLabel()), e
);
476 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
478 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
484 runInUI(runnable
, null);
485 } catch (Exception e
) {
486 errorDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
489 // // Start the main progress monitor.
490 // IProgressMonitor newMonitor =
491 // startMainMonitor(getMonitor(),operation.getLabel(), 100);
493 // // Check whether operation was canceled and do some steps.
494 // workedChecked(newMonitor, 10);
497 // IStatus status = getOperationHistory().execute(operation, newMonitor,
498 // WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
500 // // Check whether operation was canceled and do some steps.
501 // workedChecked(newMonitor, 30);
503 // String statusString = status.equals(Status.OK_STATUS) ? "completed" :
505 // setStatusLine(operation.getLabel() + " " + statusString + ".");
508 // } catch (ExecutionException e) {
509 // logger.error("Error executing operation: " + operation.getLabel(),
511 // errorDialog("Error executing operation: " + operation.getLabel(),
512 // "Please refer to the error log.");
516 // // Stop the progress monitor.
517 // newMonitor.done();
520 IPostOperationEnabled postOperationEnabled
= operation
521 .getPostOperationEnabled();
522 if (postOperationEnabled
!= null) {
523 postOperationEnabled
.onComplete();
525 return Status
.OK_STATUS
;
530 * getOperationHistory
533 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory}
536 public static IOperationHistory
getOperationHistory() {
537 return getWorkbench().getOperationSupport().getOperationHistory();
546 * a {@link java.lang.String} object.
548 public static void setStatusLine(final String message
) {
549 Display
.getDefault().asyncExec(new Runnable() {
552 statusLineManager
.setMessage(message
);
564 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
566 public static IProgressMonitor
getMonitor() {
567 statusLineManager
.setCancelEnabled(false);
568 return statusLineManager
.getProgressMonitor();
572 * Starts either the given {@link IProgressMonitor} if it's not
573 * <code>null</code> or a new {@link NullProgressMonitor}.
575 * @param progressMonitor
576 * The {@link IProgressMonitor} or <code>null</code> if no
577 * progress should be reported.
579 * The name of the main task.
581 * The number of steps this task is subdivided into.
582 * @return The {@link IProgressMonitor}.
584 public static IProgressMonitor
startMainMonitor(
585 IProgressMonitor progressMonitor
, String taskName
, int steps
) {
586 IProgressMonitor newMonitor
= progressMonitor
;
587 if (newMonitor
== null) {
588 newMonitor
= new NullProgressMonitor();
590 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
591 newMonitor
.subTask(" ");
596 * Creates a {@link SubProgressMonitor} if the given
597 * {@link IProgressMonitor} is not <code>null</code> and not a
598 * {@link NullProgressMonitor}.
600 * @param progressMonitor
601 * The parent {@link IProgressMonitor} of the
602 * {@link SubProgressMonitor} to be created.
604 * The number of steps this subtask is subdivided into. Must be a
605 * positive number and must not be
606 * {@link IProgressMonitor#UNKNOWN}.
607 * @return The {@link IProgressMonitor}.
609 public static IProgressMonitor
getSubProgressMonitor(
610 IProgressMonitor progressMonitor
, int ticks
) {
611 if (progressMonitor
== null) {
612 return new NullProgressMonitor();
614 if (progressMonitor
instanceof NullProgressMonitor
) {
615 return progressMonitor
;
618 return new SubProgressMonitor(progressMonitor
, ticks
);
622 * Checks whether the user canceled this operation. If not canceled, the
623 * given number of steps are declared as done.
626 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
630 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
631 // In case the progress monitor was canceled throw an exception.
632 if (newMonitor
.isCanceled()) {
633 throw new OperationCanceledException();
635 // Otherwise declare this step as done.
636 newMonitor
.worked(steps
);
640 * Present a progress dialog to the user. This dialog will block the UI
643 * an implementation of {@link IRunnableWithProgress}
644 * @throws java.lang.InterruptedException
646 * @throws java.lang.reflect.InvocationTargetException
649 public static void busyCursorWhile(IRunnableWithProgress runnable
)
650 throws InvocationTargetException
, InterruptedException
{
651 getProgressService().busyCursorWhile(runnable
);
659 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
661 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
664 * a {@link org.eclipse.core.runtime.jobs.ISchedulingRule}
666 * @throws java.lang.reflect.InvocationTargetException
668 * @throws java.lang.InterruptedException
671 public static void runInUI(IRunnableWithProgress runnable
,
672 ISchedulingRule rule
) throws InvocationTargetException
,
673 InterruptedException
{
674 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
687 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
689 * @throws java.lang.reflect.InvocationTargetException
691 * @throws java.lang.InterruptedException
694 public static void run(boolean fork
, boolean cancelable
,
695 IRunnableWithProgress runnable
) throws InvocationTargetException
,
696 InterruptedException
{
697 getProgressService().run(fork
, cancelable
, runnable
);
705 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
707 public static IProgressService
getProgressService() {
708 IWorkbench workbench
= PlatformUI
.getWorkbench();
709 return workbench
.getProgressService();
714 * getProgressService2
717 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService}
720 public static IWorkbenchSiteProgressService
getProgressService2() {
721 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
730 * a {@link java.lang.String} object.
732 public static void info(String message
) {
733 IStatus status
= new Status(IStatus
.INFO
, getPluginId(), message
);
743 * a {@link org.eclipse.core.runtime.IStatus} object.
745 public static void info(IStatus status
) {
755 * a {@link java.lang.Class} object.
757 * a {@link java.lang.String} object.
759 public static void warn(Class source
, String message
) {
760 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), message
);
761 getLog4JLogger(source
).warn(message
);
765 public static void warn(Class source
, IStatus status
) {
766 getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
770 public static void warn(Class source
, Throwable t
) {
771 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), t
.getMessage(), t
);
772 getLog4JLogger(source
).warn(t
);
782 * a {@link java.lang.Class} object.
784 * a {@link java.lang.Throwable} object.
786 public static void error(Class source
, Throwable t
) {
787 error(source
.getClass(), t
.getMessage(), t
);
796 * a {@link java.lang.Class} object.
798 * a {@link java.lang.String} object.
800 * a {@link java.lang.Throwable} object.
802 public static void error(Class source
, String message
, Throwable t
) {
803 IStatus status
= new Status(IStatus
.ERROR
, getPluginId(), message
, t
);
804 error(source
, status
);
813 * a {@link java.lang.Class} object.
815 * a {@link org.eclipse.core.runtime.IStatus} object.
817 public static void error(Class source
, IStatus status
) {
818 getLog4JLogger(source
)
819 .error(status
.getMessage(), status
.getException());
829 * a {@link java.lang.Class} object.
830 * @return a {@link org.apache.log4j.Logger} object.
832 public static Logger
getLog4JLogger(Class clazz
) {
833 return Logger
.getLogger(clazz
);
837 * @see {@link ILog#log(IStatus)}
841 private static void log(IStatus status
) {
842 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
850 * @return a {@link java.lang.String} object.
852 public static String
getPluginId() {
853 return "eu.taxeditor";
861 * @return a {@link org.eclipse.ui.IEditorPart} object.
863 public static IEditorPart
getActiveEditor() {
864 return getActivePage() != null ?
getActivePage().getActiveEditor()
873 * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart}
876 public static DetailsViewPart
getDetailsView() {
877 return (DetailsViewPart
) getView(DetailsViewPart
.ID
, false);
882 * refreshDetailsViewer
885 public static void refreshDetailsViewer() {
886 if (getDetailsView() != null) {
887 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
893 * reflowDetailsViewer
896 public static void reflowDetailsViewer() {
897 if (getDetailsView() != null) {
898 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).reflow();
902 public static SupplementalDataViewPart
getSupplementalDataView() {
903 return (SupplementalDataViewPart
) getView(SupplementalDataViewPart
.ID
,
907 public static void reflowSupplementalViewer() {
908 if (getSupplementalDataView() != null) {
909 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer())
914 public static UseRecordsViewPart
getUseRecordsView() {
915 return (UseRecordsViewPart
) getView(UseRecordsViewPart
.ID
, false);
920 * refreshUseRecordsViewer
923 public static void refreshUseRecordsViewer() {
924 if (getUseRecordsView() != null) {
925 ((AbstractCdmDataViewer
) getUseRecordsView().getViewer()).refresh();
931 * reflowUseRecordsViewer
934 public static void reflowUseRecordsViewer() {
935 if (getUseRecordsView() != null) {
936 ((AbstractCdmDataViewer
) getUseRecordsView().getViewer()).reflow();