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
.internal
.TaxeditorStorePlugin
;
55 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmDataViewer
;
56 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
57 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
60 * <p>Abstract AbstractUtility class.</p>
66 public abstract class AbstractUtility
{
68 /** Constant <code>statusLineManager</code> */
69 protected static IStatusLineManager statusLineManager
;
77 public static boolean closeAll() {
78 return getActivePage().closeAllEditors(true);
82 * Close the given editor.
84 * @param editor The <tt>MultipageTaxonEditor</tt> to close.
85 * @return <tt>true</tt> on success
87 public static boolean close(EditorPart editor
) {
88 return getActivePage().closeEditor(editor
, true);
94 * @return a {@link org.eclipse.swt.widgets.Shell} object.
96 public static Shell
getShell() {
98 return TaxeditorStorePlugin
.getDefault().getWorkbench()
99 .getActiveWorkbenchWindow().getShell();
103 * <p>getActivePage</p>
105 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
107 public static IWorkbenchPage
getActivePage(){
109 return TaxeditorStorePlugin
.getDefault().getWorkbench()
110 .getActiveWorkbenchWindow().getActivePage();
114 * <p>getActivePart</p>
116 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
118 public static IWorkbenchPart
getActivePart(){
119 return getActivePage() != null ?
getActivePage().getActivePart() : null;
122 public static IWorkbench
getWorkbench(){
123 return TaxeditorStorePlugin
.getDefault().getWorkbench();
127 * <p>getWorkbenchWindow</p>
129 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
131 public static ApplicationWindow
getWorkbenchWindow(){
132 if(getWorkbench().getWorkbenchWindowCount() > 1){
133 throw new IllegalStateException("More than one workbench window");
135 return (ApplicationWindow
) getWorkbench().getWorkbenchWindows()[0];
141 * @param id a {@link java.lang.String} object.
142 * @return a {@link org.eclipse.ui.IViewPart} object.
144 public static IViewPart
showView(String id
){
146 return PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
, null, IWorkbenchPage
.VIEW_VISIBLE
);
147 } catch (PartInitException e
) {
148 error(AbstractUtility
.class, "Could not open view: " + id
, e
);
149 throw new RuntimeException(e
);
156 * @param view a {@link org.eclipse.ui.IViewPart} object.
158 public static void hideView(IViewPart view
){
159 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view
);
166 * @param id a {@link java.lang.String} object.
167 * @param restore a boolean.
168 * @return a {@link org.eclipse.ui.IViewPart} object.
170 public static IViewPart
getView(String id
, boolean restore
){
171 IViewReference
[] references
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
172 for (IViewReference reference
: references
){
173 if(reference
.getId().equals(id
)){
174 return reference
.getView(restore
);
183 * @param api a {@link java.lang.Class} object.
184 * @return a {@link java.lang.Object} object.
186 public static Object
getService(Class api
){
187 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
191 * <p>getCurrentTheme</p>
193 * @return a {@link org.eclipse.ui.themes.ITheme} object.
195 public static ITheme
getCurrentTheme(){
196 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault().getWorkbench().getThemeManager();
197 return themeManager
.getCurrentTheme();
201 * Fonts registered to the plugin may be obtained with the Eclipse themeing functionality.
202 * Thus fonts are chooseable by the user via Preferences->General->Appearance->Colors and Fonts
204 * @return the FontRegistry for the current theme
206 public static FontRegistry
getFontRegistry(){
207 return getCurrentTheme().getFontRegistry();
213 * @param symbolicName a {@link java.lang.String} object.
214 * @return a {@link org.eclipse.swt.graphics.Font} object.
216 public static Font
getFont(String symbolicName
){
217 return getFontRegistry().get(symbolicName
);
221 * Color registered to the plugin may be obtained with the Eclipse themeing functionality.
222 * Thus colors are editable by the user via Preferences->General->Appearance->Colors and Fonts
224 * @return the ColorRegistry for the current theme
226 public static ColorRegistry
getColorRegistry(){
227 return getCurrentTheme().getColorRegistry();
233 * @param symbolicName a {@link java.lang.String} object.
234 * @return a {@link org.eclipse.swt.graphics.Color} object.
236 public static Color
getColor(String symbolicName
){
237 return getColorRegistry().get(symbolicName
);
241 * Open a message box that informs the user about unimplemented functionality.
242 * This method is for developer convenience.
244 * @param source a {@link java.lang.Object} object.
246 public static void notImplementedMessage(Object source
){
247 warningDialog("Not yet implemented", source
, "This functionality is not yet implemented.");
251 * <p>informationDialog</p>
253 * @param title a {@link java.lang.String} object.
254 * @param message a {@link java.lang.String} object.
256 public static void informationDialog(final String title
, final String message
){
257 Display
.getDefault().asyncExec(new Runnable(){
260 MessageDialog
.openInformation(getShell(), title
, message
);
266 * <p>warningDialog</p>
268 * @param title The dialogs title
269 * @param source The object where the warning was generated (used by log4j)
270 * @param message An informative String to be presented to the user
272 public static void warningDialog(final String title
, final Object source
, final String message
){
273 Display
.getDefault().asyncExec(new Runnable(){
276 MessageDialog
.openWarning(getShell(), title
, message
);
277 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : AbstractUtility
.class;
278 warn(clazz
, message
);
286 * @param title The dialogs title
287 * @param source The object where the warning was generated (used by log4j)
288 * @param message An informative String to be presented to the user
289 * @param title The dialogs title
290 * @param t a Throwable if one exists or null
292 public static void errorDialog(final String title
, final Object source
, final String message
, final Throwable t
){
293 Display
.getDefault().asyncExec(new Runnable(){
296 MessageDialog
.openError(getShell(), title
, message
);
297 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : this.getClass();
298 error(clazz
, message
, t
);
306 * @param title a {@link java.lang.String} object.
307 * @param source a {@link java.lang.Object} object.
308 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
310 public static void errorDialog(final String title
, final Object source
, final IStatus status
){
311 Display
.getDefault().asyncExec(new Runnable(){
314 MessageDialog
.openError(getShell(), title
, status
.getMessage());
315 Class
<?
extends Object
> clazz
= source
!= null ? source
.getClass() : this.getClass();
316 error(clazz
, status
.getMessage(), status
.getException());
322 * <p>confirmDialog</p>
324 * @param title a {@link java.lang.String} object.
325 * @param message a {@link java.lang.String} object.
328 public static boolean confirmDialog(String title
, String message
) {
329 return MessageDialog
.openQuestion(getShell(), title
, message
);
333 * <p>executeOperation</p>
335 * @param operation a {@link eu.etaxonomy.taxeditor.operation.AbstractPostOperation} object.
336 * @return a {@link org.eclipse.core.runtime.IStatus} object.
338 public static IStatus
executeOperation(final AbstractPostOperation operation
){
339 if(getOperationHistory() == null){
340 throw new IllegalArgumentException("There is no operation history for this context");
343 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
.getUIInfoAdapter(getShell());
347 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
349 public void run(IProgressMonitor monitor
) throws InvocationTargetException
,
350 InterruptedException
{
351 monitor
.beginTask(operation
.getLabel(), 100);
354 status
= getOperationHistory().execute(operation
, monitor
, uiInfoAdapter
);
355 } catch (ExecutionException e
) {
356 throw new RuntimeException(e
);
359 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed" : "cancelled";
360 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
366 runInUI(runnable
, null);
367 } catch (InvocationTargetException e
) {
368 throw new RuntimeException(e
);
369 } catch (InterruptedException e
) {
370 throw new RuntimeException(e
);
373 // // Start the main progress monitor.
374 // IProgressMonitor newMonitor = startMainMonitor(getMonitor(),operation.getLabel(), 100);
376 // // Check whether operation was canceled and do some steps.
377 // workedChecked(newMonitor, 10);
380 // IStatus status = getOperationHistory().execute(operation, newMonitor,
381 // WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
383 // // Check whether operation was canceled and do some steps.
384 // workedChecked(newMonitor, 30);
386 // String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
387 // setStatusLine(operation.getLabel() + " " + statusString + ".");
390 // } catch (ExecutionException e) {
391 // logger.error("Error executing operation: " + operation.getLabel(), e);
392 // errorDialog("Error executing operation: " + operation.getLabel(), "Please refer to the error log.");
396 // // Stop the progress monitor.
397 // newMonitor.done();
400 IPostOperationEnabled postOperationEnabled
= operation
.getPostOperationEnabled();
401 if(postOperationEnabled
!= null){
402 postOperationEnabled
.onComplete();
404 return Status
.OK_STATUS
;
408 * <p>getOperationHistory</p>
410 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory} object.
412 public static IOperationHistory
getOperationHistory(){
413 return TaxeditorStorePlugin
.getDefault().getWorkbench().
414 getOperationSupport().getOperationHistory();
418 * <p>setStatusLine</p>
420 * @param message a {@link java.lang.String} object.
422 public static void setStatusLine(final String message
) {
423 Display
.getDefault().asyncExec(new Runnable(){
426 statusLineManager
.setMessage(message
);
436 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
438 public static IProgressMonitor
getMonitor() {
439 statusLineManager
.setCancelEnabled(false);
440 return statusLineManager
.getProgressMonitor();
444 * Starts either the given {@link IProgressMonitor} if it's not <code>null</code> or a new {@link NullProgressMonitor}.
446 * @param progressMonitor The {@link IProgressMonitor} or <code>null</code> if no progress should be reported.
447 * @param taskName The name of the main task.
448 * @param steps The number of steps this task is subdivided into.
449 * @return The {@link IProgressMonitor}.
451 public static IProgressMonitor
startMainMonitor(IProgressMonitor progressMonitor
, String taskName
, int steps
) {
452 IProgressMonitor newMonitor
= progressMonitor
;
453 if (newMonitor
== null) {
454 newMonitor
= new NullProgressMonitor();
456 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
457 newMonitor
.subTask(" ");
462 * Creates a {@link SubProgressMonitor} if the given {@link IProgressMonitor} is not <code>null</code> and not a {@link NullProgressMonitor}.
464 * @param progressMonitor The parent {@link IProgressMonitor} of the {@link SubProgressMonitor} to be created.
465 * @param ticks The number of steps this subtask is subdivided into. Must be a positive number and must not be {@link IProgressMonitor#UNKNOWN}.
466 * @return The {@link IProgressMonitor}.
468 public static IProgressMonitor
getSubProgressMonitor(IProgressMonitor progressMonitor
, int ticks
) {
469 if (progressMonitor
== null) {
470 return new NullProgressMonitor();
472 if (progressMonitor
instanceof NullProgressMonitor
) {
473 return progressMonitor
;
476 return new SubProgressMonitor(progressMonitor
, ticks
);
480 * Checks whether the user canceled this operation. If not canceled, the given number of steps are declared as done.
482 * @param newMonitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
483 * @param steps a int.
485 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
486 // In case the progress monitor was canceled throw an exception.
487 if (newMonitor
.isCanceled()) {
488 throw new OperationCanceledException();
490 // Otherwise declare this step as done.
491 newMonitor
.worked(steps
);
495 * Present a progress dialog to the user. This dialog will block the UI
497 * @param runnable an implementation of {@link IRunnableWithProgress}
498 * @throws java.lang.InterruptedException if any.
499 * @throws java.lang.reflect.InvocationTargetException if any.
501 public static void busyCursorWhile(IRunnableWithProgress runnable
) throws InvocationTargetException
, InterruptedException
{
502 getProgressService().busyCursorWhile(runnable
);
508 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
509 * @param runnable a {@link org.eclipse.jface.operation.IRunnableWithProgress} object.
510 * @param rule a {@link org.eclipse.core.runtime.jobs.ISchedulingRule} object.
511 * @throws java.lang.reflect.InvocationTargetException if any.
512 * @throws java.lang.InterruptedException if any.
514 public static void runInUI(IRunnableWithProgress runnable
, ISchedulingRule rule
) throws InvocationTargetException
, InterruptedException
{
515 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
521 * @param fork a boolean.
522 * @param cancelable a boolean.
523 * @param runnable a {@link org.eclipse.jface.operation.IRunnableWithProgress} object.
524 * @throws java.lang.reflect.InvocationTargetException if any.
525 * @throws java.lang.InterruptedException if any.
527 public static void run(boolean fork
, boolean cancelable
, IRunnableWithProgress runnable
) throws InvocationTargetException
, InterruptedException
{
528 getProgressService().run(fork
, cancelable
, runnable
);
532 * <p>getProgressService</p>
534 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
536 public static IProgressService
getProgressService(){
537 IWorkbench workbench
= PlatformUI
.getWorkbench();
538 return workbench
.getProgressService();
542 * <p>getProgressService2</p>
544 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService} object.
546 public static IWorkbenchSiteProgressService
getProgressService2(){
547 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
553 * @param message a {@link java.lang.String} object.
555 public static void info(String message
){
556 IStatus status
= new Status(IStatus
.INFO
, getPluginId(), message
);
563 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
565 public static void info(IStatus status
){
572 * @param source a {@link java.lang.Class} object.
573 * @param message a {@link java.lang.String} object.
575 public static void warn(Class source
, String message
){
576 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), message
);
577 getLog4JLogger(source
).warn(message
);
584 * @param source a {@link java.lang.Class} object.
585 * @param t a {@link java.lang.Throwable} object.
587 public static void error(Class source
, Throwable t
){
588 error(source
.getClass(), t
.getMessage(), t
);
594 * @param source a {@link java.lang.Class} object.
595 * @param message a {@link java.lang.String} object.
596 * @param t a {@link java.lang.Throwable} object.
598 public static void error(Class source
, String message
, Throwable t
){
599 IStatus status
= new Status(IStatus
.ERROR
, getPluginId(), message
, t
);
600 error(source
, status
);
606 * @param source a {@link java.lang.Class} object.
607 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
609 public static void error(Class source
, IStatus status
){
610 getLog4JLogger(source
).error(status
.getMessage(), status
.getException());
616 * <p>getLog4JLogger</p>
618 * @param clazz a {@link java.lang.Class} object.
619 * @return a {@link org.apache.log4j.Logger} object.
621 public static Logger
getLog4JLogger(
623 return Logger
.getLogger(clazz
);
627 * @see {@link ILog#log(IStatus)}
631 private static void log(IStatus status
){
632 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
638 * @return a {@link java.lang.String} object.
640 protected static String
getPluginId(){
641 return "eu.taxeditor";
645 * <p>getActiveEditor</p>
647 * @return a {@link org.eclipse.ui.IEditorPart} object.
649 public static IEditorPart
getActiveEditor(){
650 return getActivePage() != null ?
getActivePage().getActiveEditor() : null;
654 * <p>getDetailsView</p>
656 * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart} object.
658 public static DetailsViewPart
getDetailsView(){
659 return (DetailsViewPart
) getView(DetailsViewPart
.ID
, false);
663 * <p>refreshDetailsViewer</p>
665 public static void refreshDetailsViewer(){
666 if(getDetailsView() != null){
667 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
672 * <p>reflowDetailsViewer</p>
674 public static void reflowDetailsViewer(){
675 if(getDetailsView() != null){
676 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).reflow();
680 public static SupplementalDataViewPart
getSupplementalDataView(){
681 return (SupplementalDataViewPart
) getView(SupplementalDataViewPart
.ID
, false);
684 public static void reflowSupplementalViewer(){
685 if(getSupplementalDataView() != null){
686 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer()).reflow();