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
;
62 * Abstract AbstractUtility class.
69 public abstract class AbstractUtility
{
71 /** Constant <code>statusLineManager</code> */
72 protected static IStatusLineManager statusLineManager
;
81 public static boolean closeAll() {
82 return getActivePage().closeAllEditors(true);
86 * Close the given editor.
89 * The <tt>MultipageTaxonEditor</tt> to close.
90 * @return <tt>true</tt> on success
92 public static boolean close(EditorPart editor
) {
93 return getActivePage().closeEditor(editor
, true);
101 * @return a {@link org.eclipse.swt.widgets.Shell} object.
103 public static Shell
getShell() {
105 return TaxeditorStorePlugin
.getDefault().getWorkbench()
106 .getActiveWorkbenchWindow().getShell();
114 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
116 public static IWorkbenchPage
getActivePage() {
118 return TaxeditorStorePlugin
.getDefault().getWorkbench()
119 .getActiveWorkbenchWindow().getActivePage();
127 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
129 public static IWorkbenchPart
getActivePart() {
130 return getActivePage() != null ?
getActivePage().getActivePart() : null;
133 public static IWorkbench
getWorkbench() {
134 return TaxeditorStorePlugin
.getDefault().getWorkbench();
142 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
144 public static ApplicationWindow
getWorkbenchWindow() {
145 if (getWorkbench().getWorkbenchWindowCount() > 1) {
146 throw new IllegalStateException("More than one workbench window");
148 return (ApplicationWindow
) getWorkbench().getWorkbenchWindows()[0];
157 * a {@link java.lang.String} object.
158 * @return a {@link org.eclipse.ui.IViewPart} object.
160 public static IViewPart
showView(String id
) {
162 return PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
164 .showView(id
, null, IWorkbenchPage
.VIEW_VISIBLE
);
165 } catch (PartInitException e
) {
166 errorDialog("Error opening view", AbstractUtility
.class, "Could not open view: " + id
, e
);
177 * a {@link org.eclipse.ui.IViewPart} object.
179 public static void hideView(IViewPart view
) {
180 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
190 * a {@link java.lang.String} object.
193 * @return a {@link org.eclipse.ui.IViewPart} object.
195 public static IViewPart
getView(String id
, boolean restore
) {
196 IViewReference
[] references
= PlatformUI
.getWorkbench()
197 .getActiveWorkbenchWindow().getActivePage().getViewReferences();
198 for (IViewReference reference
: references
) {
199 if (reference
.getId().equals(id
)) {
200 return reference
.getView(restore
);
212 * a {@link java.lang.Class} object.
213 * @return a {@link java.lang.Object} object.
215 public static Object
getService(Class api
) {
216 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
224 * @return a {@link org.eclipse.ui.themes.ITheme} object.
226 public static ITheme
getCurrentTheme() {
227 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault()
228 .getWorkbench().getThemeManager();
229 return themeManager
.getCurrentTheme();
233 * Fonts registered to the plugin may be obtained with the Eclipse themeing
234 * functionality. Thus fonts are chooseable by the user via
235 * Preferences->General->Appearance->Colors and Fonts
237 * @return the FontRegistry for the current theme
239 public static FontRegistry
getFontRegistry() {
240 return getCurrentTheme().getFontRegistry();
248 * @param symbolicName
249 * a {@link java.lang.String} object.
250 * @return a {@link org.eclipse.swt.graphics.Font} object.
252 public static Font
getFont(String symbolicName
) {
253 return getFontRegistry().get(symbolicName
);
257 * Color registered to the plugin may be obtained with the Eclipse themeing
258 * functionality. Thus colors are editable by the user via
259 * Preferences->General->Appearance->Colors and Fonts
261 * @return the ColorRegistry for the current theme
263 public static ColorRegistry
getColorRegistry() {
264 return getCurrentTheme().getColorRegistry();
272 * @param symbolicName
273 * a {@link java.lang.String} object.
274 * @return a {@link org.eclipse.swt.graphics.Color} object.
276 public static Color
getColor(String symbolicName
) {
277 return getColorRegistry().get(symbolicName
);
281 * Open a message box that informs the user about unimplemented
282 * functionality. This method is for developer convenience.
285 * a {@link java.lang.Object} object.
287 public static void notImplementedMessage(Object source
) {
288 warningDialog("Not yet implemented", source
,
289 "This functionality is not yet implemented.");
298 * a {@link java.lang.String} object.
300 * a {@link java.lang.String} object.
302 public static void informationDialog(final String title
,
303 final String message
) {
304 Display
.getDefault().asyncExec(new Runnable() {
307 MessageDialog
.openInformation(getShell(), title
, message
);
312 public static void informationDialog(final String title
,
313 final IStatus status
) {
314 informationDialog(title
, status
.getMessage());
325 * The object where the warning was generated (used by log4j)
327 * An informative String to be presented to the user
329 public static void warningDialog(final String title
, final Object source
,
330 final String message
) {
331 Display
.getDefault().asyncExec(new Runnable() {
334 MessageDialog
.openWarning(getShell(), title
, message
);
335 Class
<?
extends Object
> clazz
= source
!= null ? source
336 .getClass() : AbstractUtility
.class;
337 warn(clazz
, message
);
347 public static void warningDialog(String title
, Object source
,
349 warningDialog(title
, source
, status
.getMessage());
360 * The object where the warning was generated (used by log4j)
362 * An informative String to be presented to the user
366 * a Throwable if one exists or null
368 public static void errorDialog(final String title
, final Object source
,
369 final String message
, final Throwable t
) {
370 Display
.getDefault().asyncExec(new Runnable() {
373 MessageDialog
.openError(getShell(), title
, message
+ getCauseRecursively(t
));
374 Class
<?
extends Object
> clazz
= source
!= null ? source
375 .getClass() : this.getClass();
376 error(clazz
, message
, t
);
379 private String
getCauseRecursively(Throwable t
) {
384 if(t
.getCause() != null){
385 return getCauseRecursively(t
.getCause());
387 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
394 public static void errorDialog(final String title
, final Object source
,
395 final String message
){
396 errorDialog(title
, source
, message
, null);
405 * a {@link java.lang.String} object.
407 * a {@link java.lang.Object} object.
409 * a {@link org.eclipse.core.runtime.IStatus} object.
411 public static void errorDialog(final String title
, final Object source
,
412 final IStatus status
) {
413 Display
.getDefault().asyncExec(new Runnable() {
416 MessageDialog
.openError(getShell(), title
, status
.getMessage());
417 Class
<?
extends Object
> clazz
= source
!= null ? source
418 .getClass() : this.getClass();
419 error(clazz
, status
.getMessage(), status
.getException());
430 * a {@link java.lang.String} object.
432 * a {@link java.lang.String} object.
435 public static boolean confirmDialog(String title
, String message
) {
436 return MessageDialog
.openQuestion(getShell(), title
, message
);
446 * {@link eu.etaxonomy.taxeditor.operation.AbstractPostOperation}
448 * @return a {@link org.eclipse.core.runtime.IStatus} object.
450 public static IStatus
executeOperation(final AbstractPostOperation operation
) {
451 if (getOperationHistory() == null) {
452 throw new IllegalArgumentException(
453 "There is no operation history for this context");
456 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
457 .getUIInfoAdapter(getShell());
459 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
461 public void run(IProgressMonitor monitor
)
462 throws InvocationTargetException
, InterruptedException
{
463 monitor
.beginTask(operation
.getLabel(), 100);
464 IStatus status
= Status
.CANCEL_STATUS
;
466 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
467 status
= getOperationHistory().execute(operation
, monitor
,
469 } catch (ExecutionException e
) {
470 errorDialog("Error executing operation", getClass(), String
.format("An error occured while executing %s.", operation
.getLabel()), e
);
475 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
477 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
483 runInUI(runnable
, null);
484 } catch (Exception e
) {
485 errorDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
488 // // Start the main progress monitor.
489 // IProgressMonitor newMonitor =
490 // startMainMonitor(getMonitor(),operation.getLabel(), 100);
492 // // Check whether operation was canceled and do some steps.
493 // workedChecked(newMonitor, 10);
496 // IStatus status = getOperationHistory().execute(operation, newMonitor,
497 // WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
499 // // Check whether operation was canceled and do some steps.
500 // workedChecked(newMonitor, 30);
502 // String statusString = status.equals(Status.OK_STATUS) ? "completed" :
504 // setStatusLine(operation.getLabel() + " " + statusString + ".");
507 // } catch (ExecutionException e) {
508 // logger.error("Error executing operation: " + operation.getLabel(),
510 // errorDialog("Error executing operation: " + operation.getLabel(),
511 // "Please refer to the error log.");
515 // // Stop the progress monitor.
516 // newMonitor.done();
519 IPostOperationEnabled postOperationEnabled
= operation
520 .getPostOperationEnabled();
521 if (postOperationEnabled
!= null) {
522 postOperationEnabled
.onComplete();
524 return Status
.OK_STATUS
;
529 * getOperationHistory
532 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory}
535 public static IOperationHistory
getOperationHistory() {
536 return getWorkbench().getOperationSupport().getOperationHistory();
545 * a {@link java.lang.String} object.
547 public static void setStatusLine(final String message
) {
548 Display
.getDefault().asyncExec(new Runnable() {
551 statusLineManager
.setMessage(message
);
563 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
565 public static IProgressMonitor
getMonitor() {
566 statusLineManager
.setCancelEnabled(false);
567 return statusLineManager
.getProgressMonitor();
571 * Starts either the given {@link IProgressMonitor} if it's not
572 * <code>null</code> or a new {@link NullProgressMonitor}.
574 * @param progressMonitor
575 * The {@link IProgressMonitor} or <code>null</code> if no
576 * progress should be reported.
578 * The name of the main task.
580 * The number of steps this task is subdivided into.
581 * @return The {@link IProgressMonitor}.
583 public static IProgressMonitor
startMainMonitor(
584 IProgressMonitor progressMonitor
, String taskName
, int steps
) {
585 IProgressMonitor newMonitor
= progressMonitor
;
586 if (newMonitor
== null) {
587 newMonitor
= new NullProgressMonitor();
589 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
590 newMonitor
.subTask(" ");
595 * Creates a {@link SubProgressMonitor} if the given
596 * {@link IProgressMonitor} is not <code>null</code> and not a
597 * {@link NullProgressMonitor}.
599 * @param progressMonitor
600 * The parent {@link IProgressMonitor} of the
601 * {@link SubProgressMonitor} to be created.
603 * The number of steps this subtask is subdivided into. Must be a
604 * positive number and must not be
605 * {@link IProgressMonitor#UNKNOWN}.
606 * @return The {@link IProgressMonitor}.
608 public static IProgressMonitor
getSubProgressMonitor(
609 IProgressMonitor progressMonitor
, int ticks
) {
610 if (progressMonitor
== null) {
611 return new NullProgressMonitor();
613 if (progressMonitor
instanceof NullProgressMonitor
) {
614 return progressMonitor
;
617 return new SubProgressMonitor(progressMonitor
, ticks
);
621 * Checks whether the user canceled this operation. If not canceled, the
622 * given number of steps are declared as done.
625 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
629 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
630 // In case the progress monitor was canceled throw an exception.
631 if (newMonitor
.isCanceled()) {
632 throw new OperationCanceledException();
634 // Otherwise declare this step as done.
635 newMonitor
.worked(steps
);
639 * Present a progress dialog to the user. This dialog will block the UI
642 * an implementation of {@link IRunnableWithProgress}
643 * @throws java.lang.InterruptedException
645 * @throws java.lang.reflect.InvocationTargetException
648 public static void busyCursorWhile(IRunnableWithProgress runnable
)
649 throws InvocationTargetException
, InterruptedException
{
650 getProgressService().busyCursorWhile(runnable
);
658 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
660 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
663 * a {@link org.eclipse.core.runtime.jobs.ISchedulingRule}
665 * @throws java.lang.reflect.InvocationTargetException
667 * @throws java.lang.InterruptedException
670 public static void runInUI(IRunnableWithProgress runnable
,
671 ISchedulingRule rule
) throws InvocationTargetException
,
672 InterruptedException
{
673 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
686 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
688 * @throws java.lang.reflect.InvocationTargetException
690 * @throws java.lang.InterruptedException
693 public static void run(boolean fork
, boolean cancelable
,
694 IRunnableWithProgress runnable
) throws InvocationTargetException
,
695 InterruptedException
{
696 getProgressService().run(fork
, cancelable
, runnable
);
704 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
706 public static IProgressService
getProgressService() {
707 IWorkbench workbench
= PlatformUI
.getWorkbench();
708 return workbench
.getProgressService();
713 * getProgressService2
716 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService}
719 public static IWorkbenchSiteProgressService
getProgressService2() {
720 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
729 * a {@link java.lang.String} object.
731 public static void info(String message
) {
732 IStatus status
= new Status(IStatus
.INFO
, getPluginId(), message
);
742 * a {@link org.eclipse.core.runtime.IStatus} object.
744 public static void info(IStatus status
) {
754 * a {@link java.lang.Class} object.
756 * a {@link java.lang.String} object.
758 public static void warn(Class source
, String message
) {
759 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), message
);
760 getLog4JLogger(source
).warn(message
);
764 public static void warn(Class source
, IStatus status
) {
765 getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
769 public static void warn(Class source
, Throwable t
) {
770 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), t
.getMessage(), t
);
771 getLog4JLogger(source
).warn(t
);
781 * a {@link java.lang.Class} object.
783 * a {@link java.lang.Throwable} object.
785 public static void error(Class source
, Throwable t
) {
786 error(source
.getClass(), t
.getMessage(), t
);
795 * a {@link java.lang.Class} object.
797 * a {@link java.lang.String} object.
799 * a {@link java.lang.Throwable} object.
801 public static void error(Class source
, String message
, Throwable t
) {
802 IStatus status
= new Status(IStatus
.ERROR
, getPluginId(), message
, t
);
803 error(source
, status
);
812 * a {@link java.lang.Class} object.
814 * a {@link org.eclipse.core.runtime.IStatus} object.
816 public static void error(Class source
, IStatus status
) {
817 getLog4JLogger(source
)
818 .error(status
.getMessage(), status
.getException());
828 * a {@link java.lang.Class} object.
829 * @return a {@link org.apache.log4j.Logger} object.
831 public static Logger
getLog4JLogger(Class clazz
) {
832 return Logger
.getLogger(clazz
);
836 * @see {@link ILog#log(IStatus)}
840 private static void log(IStatus status
) {
841 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
849 * @return a {@link java.lang.String} object.
851 public static String
getPluginId() {
852 return "eu.taxeditor";
860 * @return a {@link org.eclipse.ui.IEditorPart} object.
862 public static IEditorPart
getActiveEditor() {
863 return getActivePage() != null ?
getActivePage().getActiveEditor()
872 * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart}
875 public static DetailsViewPart
getDetailsView() {
876 return (DetailsViewPart
) getView(DetailsViewPart
.ID
, false);
881 * refreshDetailsViewer
884 public static void refreshDetailsViewer() {
885 if (getDetailsView() != null) {
886 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
892 * reflowDetailsViewer
895 public static void reflowDetailsViewer() {
896 if (getDetailsView() != null) {
897 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).reflow();
901 public static SupplementalDataViewPart
getSupplementalDataView() {
902 return (SupplementalDataViewPart
) getView(SupplementalDataViewPart
.ID
,
906 public static void reflowSupplementalViewer() {
907 if (getSupplementalDataView() != null) {
908 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer())