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
.taxeditor
.operation
.AbstractPostOperation
;
53 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
54 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
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
);
320 * The object where the warning was generated (used by log4j)
322 * An informative String to be presented to the user
324 public static void warningDialog(final String title
, final Object source
,
325 final String message
) {
326 Display
.getDefault().asyncExec(new Runnable() {
329 MessageDialog
.openWarning(getShell(), title
, message
);
330 Class
<?
extends Object
> clazz
= source
!= null ? source
331 .getClass() : AbstractUtility
.class;
332 warn(clazz
, message
);
345 * The object where the warning was generated (used by log4j)
347 * An informative String to be presented to the user
351 * a Throwable if one exists or null
353 public static void errorDialog(final String title
, final Object source
,
354 final String message
, final Throwable t
) {
355 Display
.getDefault().asyncExec(new Runnable() {
358 MessageDialog
.openError(getShell(), title
, message
+ getCauseRecursively(t
));
359 Class
<?
extends Object
> clazz
= source
!= null ? source
360 .getClass() : this.getClass();
361 error(clazz
, message
, t
);
364 private String
getCauseRecursively(Throwable t
) {
369 if(t
.getCause() != null){
370 return getCauseRecursively(t
.getCause());
372 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
385 * a {@link java.lang.String} object.
387 * a {@link java.lang.Object} object.
389 * a {@link org.eclipse.core.runtime.IStatus} object.
391 public static void errorDialog(final String title
, final Object source
,
392 final IStatus status
) {
393 Display
.getDefault().asyncExec(new Runnable() {
396 MessageDialog
.openError(getShell(), title
, status
.getMessage());
397 Class
<?
extends Object
> clazz
= source
!= null ? source
398 .getClass() : this.getClass();
399 error(clazz
, status
.getMessage(), status
.getException());
410 * a {@link java.lang.String} object.
412 * a {@link java.lang.String} object.
415 public static boolean confirmDialog(String title
, String message
) {
416 return MessageDialog
.openQuestion(getShell(), title
, message
);
426 * {@link eu.etaxonomy.taxeditor.operation.AbstractPostOperation}
428 * @return a {@link org.eclipse.core.runtime.IStatus} object.
430 public static IStatus
executeOperation(final AbstractPostOperation operation
) {
431 if (getOperationHistory() == null) {
432 throw new IllegalArgumentException(
433 "There is no operation history for this context");
436 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
437 .getUIInfoAdapter(getShell());
439 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
441 public void run(IProgressMonitor monitor
)
442 throws InvocationTargetException
, InterruptedException
{
443 monitor
.beginTask(operation
.getLabel(), 100);
444 IStatus status
= Status
.CANCEL_STATUS
;
446 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
447 status
= getOperationHistory().execute(operation
, monitor
,
449 } catch (ExecutionException e
) {
450 errorDialog("Error executing operation", getClass(), String
.format("An error occured while executing %s.", operation
.getLabel()), e
);
455 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
457 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
463 runInUI(runnable
, null);
464 } catch (Exception e
) {
465 errorDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
468 // // Start the main progress monitor.
469 // IProgressMonitor newMonitor =
470 // startMainMonitor(getMonitor(),operation.getLabel(), 100);
472 // // Check whether operation was canceled and do some steps.
473 // workedChecked(newMonitor, 10);
476 // IStatus status = getOperationHistory().execute(operation, newMonitor,
477 // WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
479 // // Check whether operation was canceled and do some steps.
480 // workedChecked(newMonitor, 30);
482 // String statusString = status.equals(Status.OK_STATUS) ? "completed" :
484 // setStatusLine(operation.getLabel() + " " + statusString + ".");
487 // } catch (ExecutionException e) {
488 // logger.error("Error executing operation: " + operation.getLabel(),
490 // errorDialog("Error executing operation: " + operation.getLabel(),
491 // "Please refer to the error log.");
495 // // Stop the progress monitor.
496 // newMonitor.done();
499 IPostOperationEnabled postOperationEnabled
= operation
500 .getPostOperationEnabled();
501 if (postOperationEnabled
!= null) {
502 postOperationEnabled
.onComplete();
504 return Status
.OK_STATUS
;
509 * getOperationHistory
512 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory}
515 public static IOperationHistory
getOperationHistory() {
516 return getWorkbench().getOperationSupport().getOperationHistory();
525 * a {@link java.lang.String} object.
527 public static void setStatusLine(final String message
) {
528 Display
.getDefault().asyncExec(new Runnable() {
531 statusLineManager
.setMessage(message
);
543 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
545 public static IProgressMonitor
getMonitor() {
546 statusLineManager
.setCancelEnabled(false);
547 return statusLineManager
.getProgressMonitor();
551 * Starts either the given {@link IProgressMonitor} if it's not
552 * <code>null</code> or a new {@link NullProgressMonitor}.
554 * @param progressMonitor
555 * The {@link IProgressMonitor} or <code>null</code> if no
556 * progress should be reported.
558 * The name of the main task.
560 * The number of steps this task is subdivided into.
561 * @return The {@link IProgressMonitor}.
563 public static IProgressMonitor
startMainMonitor(
564 IProgressMonitor progressMonitor
, String taskName
, int steps
) {
565 IProgressMonitor newMonitor
= progressMonitor
;
566 if (newMonitor
== null) {
567 newMonitor
= new NullProgressMonitor();
569 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
570 newMonitor
.subTask(" ");
575 * Creates a {@link SubProgressMonitor} if the given
576 * {@link IProgressMonitor} is not <code>null</code> and not a
577 * {@link NullProgressMonitor}.
579 * @param progressMonitor
580 * The parent {@link IProgressMonitor} of the
581 * {@link SubProgressMonitor} to be created.
583 * The number of steps this subtask is subdivided into. Must be a
584 * positive number and must not be
585 * {@link IProgressMonitor#UNKNOWN}.
586 * @return The {@link IProgressMonitor}.
588 public static IProgressMonitor
getSubProgressMonitor(
589 IProgressMonitor progressMonitor
, int ticks
) {
590 if (progressMonitor
== null) {
591 return new NullProgressMonitor();
593 if (progressMonitor
instanceof NullProgressMonitor
) {
594 return progressMonitor
;
597 return new SubProgressMonitor(progressMonitor
, ticks
);
601 * Checks whether the user canceled this operation. If not canceled, the
602 * given number of steps are declared as done.
605 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
609 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
610 // In case the progress monitor was canceled throw an exception.
611 if (newMonitor
.isCanceled()) {
612 throw new OperationCanceledException();
614 // Otherwise declare this step as done.
615 newMonitor
.worked(steps
);
619 * Present a progress dialog to the user. This dialog will block the UI
622 * an implementation of {@link IRunnableWithProgress}
623 * @throws java.lang.InterruptedException
625 * @throws java.lang.reflect.InvocationTargetException
628 public static void busyCursorWhile(IRunnableWithProgress runnable
)
629 throws InvocationTargetException
, InterruptedException
{
630 getProgressService().busyCursorWhile(runnable
);
638 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
640 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
643 * a {@link org.eclipse.core.runtime.jobs.ISchedulingRule}
645 * @throws java.lang.reflect.InvocationTargetException
647 * @throws java.lang.InterruptedException
650 public static void runInUI(IRunnableWithProgress runnable
,
651 ISchedulingRule rule
) throws InvocationTargetException
,
652 InterruptedException
{
653 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
666 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
668 * @throws java.lang.reflect.InvocationTargetException
670 * @throws java.lang.InterruptedException
673 public static void run(boolean fork
, boolean cancelable
,
674 IRunnableWithProgress runnable
) throws InvocationTargetException
,
675 InterruptedException
{
676 getProgressService().run(fork
, cancelable
, runnable
);
684 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
686 public static IProgressService
getProgressService() {
687 IWorkbench workbench
= PlatformUI
.getWorkbench();
688 return workbench
.getProgressService();
693 * getProgressService2
696 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService}
699 public static IWorkbenchSiteProgressService
getProgressService2() {
700 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
709 * a {@link java.lang.String} object.
711 public static void info(String message
) {
712 IStatus status
= new Status(IStatus
.INFO
, getPluginId(), message
);
722 * a {@link org.eclipse.core.runtime.IStatus} object.
724 public static void info(IStatus status
) {
734 * a {@link java.lang.Class} object.
736 * a {@link java.lang.String} object.
738 public static void warn(Class source
, String message
) {
739 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), message
);
740 getLog4JLogger(source
).warn(message
);
750 * a {@link java.lang.Class} object.
752 * a {@link java.lang.Throwable} object.
754 public static void error(Class source
, Throwable t
) {
755 error(source
.getClass(), t
.getMessage(), t
);
764 * a {@link java.lang.Class} object.
766 * a {@link java.lang.String} object.
768 * a {@link java.lang.Throwable} object.
770 public static void error(Class source
, String message
, Throwable t
) {
771 IStatus status
= new Status(IStatus
.ERROR
, getPluginId(), message
, t
);
772 error(source
, status
);
781 * a {@link java.lang.Class} object.
783 * a {@link org.eclipse.core.runtime.IStatus} object.
785 public static void error(Class source
, IStatus status
) {
786 getLog4JLogger(source
)
787 .error(status
.getMessage(), status
.getException());
797 * a {@link java.lang.Class} object.
798 * @return a {@link org.apache.log4j.Logger} object.
800 public static Logger
getLog4JLogger(Class clazz
) {
801 return Logger
.getLogger(clazz
);
805 * @see {@link ILog#log(IStatus)}
809 private static void log(IStatus status
) {
810 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
818 * @return a {@link java.lang.String} object.
820 protected static String
getPluginId() {
821 return "eu.taxeditor";
829 * @return a {@link org.eclipse.ui.IEditorPart} object.
831 public static IEditorPart
getActiveEditor() {
832 return getActivePage() != null ?
getActivePage().getActiveEditor()
841 * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart}
844 public static DetailsViewPart
getDetailsView() {
845 return (DetailsViewPart
) getView(DetailsViewPart
.ID
, false);
850 * refreshDetailsViewer
853 public static void refreshDetailsViewer() {
854 if (getDetailsView() != null) {
855 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
861 * reflowDetailsViewer
864 public static void reflowDetailsViewer() {
865 if (getDetailsView() != null) {
866 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).reflow();
870 public static SupplementalDataViewPart
getSupplementalDataView() {
871 return (SupplementalDataViewPart
) getView(SupplementalDataViewPart
.ID
,
875 public static void reflowSupplementalViewer() {
876 if (getSupplementalDataView() != null) {
877 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer())