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
.IViewPart
;
38 import org
.eclipse
.ui
.IViewReference
;
39 import org
.eclipse
.ui
.IWorkbench
;
40 import org
.eclipse
.ui
.IWorkbenchPage
;
41 import org
.eclipse
.ui
.IWorkbenchPart
;
42 import org
.eclipse
.ui
.PartInitException
;
43 import org
.eclipse
.ui
.PlatformUI
;
44 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
45 import org
.eclipse
.ui
.progress
.IProgressService
;
46 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
47 import org
.eclipse
.ui
.themes
.ITheme
;
48 import org
.eclipse
.ui
.themes
.IThemeManager
;
50 import eu
.etaxonomy
.taxeditor
.operations
.AbstractPostOperation
;
51 import eu
.etaxonomy
.taxeditor
.operations
.IPostOperationEnabled
;
52 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
55 * <p>Abstract AbstractUtility class.</p>
61 public abstract class AbstractUtility
{
63 /** Constant <code>statusLineManager</code> */
64 protected static IStatusLineManager statusLineManager
;
69 * @return a {@link org.eclipse.swt.widgets.Shell} object.
71 public static Shell
getShell() {
73 return TaxeditorStorePlugin
.getDefault().getWorkbench()
74 .getActiveWorkbenchWindow().getShell();
78 * <p>getActivePage</p>
80 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
82 public static IWorkbenchPage
getActivePage(){
84 return TaxeditorStorePlugin
.getDefault().getWorkbench()
85 .getActiveWorkbenchWindow().getActivePage();
89 * <p>getActivePart</p>
91 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
93 public static IWorkbenchPart
getActivePart(){
94 return getActivePage() != null ?
getActivePage().getActivePart() : null;
98 * <p>getWorkbenchWindow</p>
100 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
102 public static ApplicationWindow
getWorkbenchWindow(){
103 if(TaxeditorStorePlugin
.getDefault().getWorkbench().getWorkbenchWindowCount() > 1){
104 throw new IllegalStateException("More than one workbench window");
106 return (ApplicationWindow
) TaxeditorStorePlugin
.getDefault().getWorkbench().getWorkbenchWindows()[0];
112 * @param id a {@link java.lang.String} object.
113 * @return a {@link org.eclipse.ui.IViewPart} object.
115 public static IViewPart
showView(String id
){
117 return PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id
);
118 } catch (PartInitException e
) {
119 error(AbstractUtility
.class, "Could not open view: " + id
, e
);
120 throw new RuntimeException(e
);
127 * @param view a {@link org.eclipse.ui.IViewPart} object.
129 public static void hideView(IViewPart view
){
130 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view
);
137 * @param id a {@link java.lang.String} object.
138 * @param restore a boolean.
139 * @return a {@link org.eclipse.ui.IViewPart} object.
141 public static IViewPart
getView(String id
, boolean restore
){
142 IViewReference
[] references
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
143 for (IViewReference reference
: references
){
144 if(reference
.getId().equals(id
)){
145 return reference
.getView(restore
);
156 public static boolean closeAll() {
157 return getActivePage().closeAllEditors(true);
163 * @param api a {@link java.lang.Class} object.
164 * @return a {@link java.lang.Object} object.
166 public static Object
getService(Class api
){
167 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
171 * <p>getCurrentTheme</p>
173 * @return a {@link org.eclipse.ui.themes.ITheme} object.
175 public static ITheme
getCurrentTheme(){
176 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault().getWorkbench().getThemeManager();
177 return themeManager
.getCurrentTheme();
181 * Fonts registered to the plugin may be obtained with the Eclipse themeing functionality.
182 * Thus fonts are chooseable by the user via Preferences->General->Appearance->Colors and Fonts
184 * @return the FontRegistry for the current theme
186 public static FontRegistry
getFontRegistry(){
187 return getCurrentTheme().getFontRegistry();
193 * @param symbolicName a {@link java.lang.String} object.
194 * @return a {@link org.eclipse.swt.graphics.Font} object.
196 public static Font
getFont(String symbolicName
){
197 return getFontRegistry().get(symbolicName
);
201 * Color registered to the plugin may be obtained with the Eclipse themeing functionality.
202 * Thus colors are editable by the user via Preferences->General->Appearance->Colors and Fonts
204 * @return the ColorRegistry for the current theme
206 public static ColorRegistry
getColorRegistry(){
207 return getCurrentTheme().getColorRegistry();
213 * @param symbolicName a {@link java.lang.String} object.
214 * @return a {@link org.eclipse.swt.graphics.Color} object.
216 public static Color
getColor(String symbolicName
){
217 return getColorRegistry().get(symbolicName
);
221 * Open a message box that informs the user about unimplemented functionality.
222 * This method is for developer convenience.
224 * @param source a {@link java.lang.Object} object.
226 public static void notImplementedMessage(Object source
){
227 warningDialog("Not yet implemented", source
, "This functionality is not yet implemented.");
231 * <p>informationDialog</p>
233 * @param title a {@link java.lang.String} object.
234 * @param message a {@link java.lang.String} object.
236 public static void informationDialog(final String title
, final String message
){
237 Display
.getDefault().asyncExec(new Runnable(){
240 MessageDialog
.openInformation(getShell(), title
, message
);
246 * <p>warningDialog</p>
248 * @param title The dialogs title
249 * @param source The object where the warning was generated (used by log4j)
250 * @param message An informative String to be presented to the user
252 public static void warningDialog(final String title
, final Object source
, final String message
){
253 Display
.getDefault().asyncExec(new Runnable(){
256 MessageDialog
.openWarning(getShell(), title
, message
);
257 warn(AbstractUtility
.class, message
);
265 * @param title The dialogs title
266 * @param source The object where the warning was generated (used by log4j)
267 * @param message An informative String to be presented to the user
268 * @param title The dialogs title
269 * @param t a Throwable if one exists or null
271 public static void errorDialog(final String title
, final Object source
, final String message
, final Throwable t
){
272 Display
.getDefault().asyncExec(new Runnable(){
275 MessageDialog
.openError(getShell(), title
, message
);
276 error(AbstractUtility
.class, message
, t
);
284 * @param title a {@link java.lang.String} object.
285 * @param source a {@link java.lang.Object} object.
286 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
288 public static void errorDialog(final String title
, final Object source
, final IStatus status
){
293 * <p>confirmDialog</p>
295 * @param title a {@link java.lang.String} object.
296 * @param message a {@link java.lang.String} object.
299 public static boolean confirmDialog(String title
, String message
) {
300 return MessageDialog
.openQuestion(getShell(), title
, message
);
304 * <p>executeOperation</p>
306 * @param operation a {@link eu.etaxonomy.taxeditor.operations.AbstractPostOperation} object.
307 * @return a {@link org.eclipse.core.runtime.IStatus} object.
309 public static IStatus
executeOperation(final AbstractPostOperation operation
){
310 if(getOperationHistory() == null){
311 throw new IllegalArgumentException("There is no operation history for this context");
314 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
.getUIInfoAdapter(getShell());
318 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
320 public void run(IProgressMonitor monitor
) throws InvocationTargetException
,
321 InterruptedException
{
322 monitor
.beginTask(operation
.getLabel(), 100);
325 status
= getOperationHistory().execute(operation
, monitor
, uiInfoAdapter
);
326 } catch (ExecutionException e
) {
327 throw new RuntimeException(e
);
330 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed" : "cancelled";
331 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
337 runInUI(runnable
, null);
338 } catch (InvocationTargetException e
) {
339 throw new RuntimeException(e
);
340 } catch (InterruptedException e
) {
341 throw new RuntimeException(e
);
344 // // Start the main progress monitor.
345 // IProgressMonitor newMonitor = startMainMonitor(getMonitor(),operation.getLabel(), 100);
347 // // Check whether operation was canceled and do some steps.
348 // workedChecked(newMonitor, 10);
351 // IStatus status = getOperationHistory().execute(operation, newMonitor,
352 // WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
354 // // Check whether operation was canceled and do some steps.
355 // workedChecked(newMonitor, 30);
357 // String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
358 // setStatusLine(operation.getLabel() + " " + statusString + ".");
361 // } catch (ExecutionException e) {
362 // logger.error("Error executing operation: " + operation.getLabel(), e);
363 // errorDialog("Error executing operation: " + operation.getLabel(), "Please refer to the error log.");
367 // // Stop the progress monitor.
368 // newMonitor.done();
371 IPostOperationEnabled postOperationEnabled
= operation
.getPostOperationEnabled();
372 postOperationEnabled
.onComplete();
373 return Status
.OK_STATUS
;
377 * <p>getOperationHistory</p>
379 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory} object.
381 public static IOperationHistory
getOperationHistory(){
382 return TaxeditorStorePlugin
.getDefault().getWorkbench().
383 getOperationSupport().getOperationHistory();
387 * <p>setStatusLine</p>
389 * @param message a {@link java.lang.String} object.
391 public static void setStatusLine(final String message
) {
392 Display
.getDefault().asyncExec(new Runnable(){
395 statusLineManager
.setMessage(message
);
405 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
407 public static IProgressMonitor
getMonitor() {
408 statusLineManager
.setCancelEnabled(false);
409 return statusLineManager
.getProgressMonitor();
413 * Starts either the given {@link IProgressMonitor} if it's not <code>null</code> or a new {@link NullProgressMonitor}.
415 * @param progressMonitor The {@link IProgressMonitor} or <code>null</code> if no progress should be reported.
416 * @param taskName The name of the main task.
417 * @param steps The number of steps this task is subdivided into.
418 * @return The {@link IProgressMonitor}.
420 public static IProgressMonitor
startMainMonitor(IProgressMonitor progressMonitor
, String taskName
, int steps
) {
421 IProgressMonitor newMonitor
= progressMonitor
;
422 if (newMonitor
== null) {
423 newMonitor
= new NullProgressMonitor();
425 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
426 newMonitor
.subTask(" ");
431 * Creates a {@link SubProgressMonitor} if the given {@link IProgressMonitor} is not <code>null</code> and not a {@link NullProgressMonitor}.
433 * @param progressMonitor The parent {@link IProgressMonitor} of the {@link SubProgressMonitor} to be created.
434 * @param ticks The number of steps this subtask is subdivided into. Must be a positive number and must not be {@link IProgressMonitor#UNKNOWN}.
435 * @return The {@link IProgressMonitor}.
437 public static IProgressMonitor
getSubProgressMonitor(IProgressMonitor progressMonitor
, int ticks
) {
438 if (progressMonitor
== null) {
439 return new NullProgressMonitor();
441 if (progressMonitor
instanceof NullProgressMonitor
) {
442 return progressMonitor
;
445 return new SubProgressMonitor(progressMonitor
, ticks
);
449 * Checks whether the user canceled this operation. If not canceled, the given number of steps are declared as done.
451 * @param newMonitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
452 * @param steps a int.
454 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
455 // In case the progress monitor was canceled throw an exception.
456 if (newMonitor
.isCanceled()) {
457 throw new OperationCanceledException();
459 // Otherwise declare this step as done.
460 newMonitor
.worked(steps
);
464 * Present a progress dialog to the user. This dialog will block the UI
466 * @param runnable an implementation of {@link IRunnableWithProgress}
467 * @throws java.lang.InterruptedException if any.
468 * @throws java.lang.reflect.InvocationTargetException if any.
470 public static void busyCursorWhile(IRunnableWithProgress runnable
) throws InvocationTargetException
, InterruptedException
{
471 getProgressService().busyCursorWhile(runnable
);
477 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
478 * @param runnable a {@link org.eclipse.jface.operation.IRunnableWithProgress} object.
479 * @param rule a {@link org.eclipse.core.runtime.jobs.ISchedulingRule} object.
480 * @throws java.lang.reflect.InvocationTargetException if any.
481 * @throws java.lang.InterruptedException if any.
483 public static void runInUI(IRunnableWithProgress runnable
, ISchedulingRule rule
) throws InvocationTargetException
, InterruptedException
{
484 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
490 * @param fork a boolean.
491 * @param cancelable a boolean.
492 * @param runnable a {@link org.eclipse.jface.operation.IRunnableWithProgress} object.
493 * @throws java.lang.reflect.InvocationTargetException if any.
494 * @throws java.lang.InterruptedException if any.
496 public static void run(boolean fork
, boolean cancelable
, IRunnableWithProgress runnable
) throws InvocationTargetException
, InterruptedException
{
497 getProgressService().run(fork
, cancelable
, runnable
);
501 * <p>getProgressService</p>
503 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
505 public static IProgressService
getProgressService(){
506 IWorkbench workbench
= PlatformUI
.getWorkbench();
507 return workbench
.getProgressService();
511 * <p>getProgressService2</p>
513 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService} object.
515 public static IWorkbenchSiteProgressService
getProgressService2(){
516 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
522 * @param message a {@link java.lang.String} object.
524 public static void info(String message
){
525 IStatus status
= new Status(IStatus
.INFO
, getPluginId(), message
);
532 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
534 public static void info(IStatus status
){
541 * @param source a {@link java.lang.Class} object.
542 * @param message a {@link java.lang.String} object.
544 public static void warn(Class source
, String message
){
545 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), message
);
546 getLog4JLogger(source
).warn(message
);
553 * @param source a {@link java.lang.Class} object.
554 * @param t a {@link java.lang.Throwable} object.
556 public static void error(Class source
, Throwable t
){
557 error(source
.getClass(), t
.getMessage(), t
);
563 * @param source a {@link java.lang.Class} object.
564 * @param message a {@link java.lang.String} object.
565 * @param t a {@link java.lang.Throwable} object.
567 public static void error(Class source
, String message
, Throwable t
){
568 IStatus status
= new Status(IStatus
.ERROR
, getPluginId(), message
, t
);
569 error(source
, status
);
575 * @param source a {@link java.lang.Class} object.
576 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
578 public static void error(Class source
, IStatus status
){
579 getLog4JLogger(source
).error(status
.getMessage(), status
.getException());
585 * <p>getLog4JLogger</p>
587 * @param clazz a {@link java.lang.Class} object.
588 * @return a {@link org.apache.log4j.Logger} object.
590 public static Logger
getLog4JLogger(
592 return Logger
.getLogger(clazz
);
596 * @see {@link ILog#log(IStatus)}
600 private static void log(IStatus status
){
601 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
607 * @return a {@link java.lang.String} object.
609 protected static String
getPluginId(){
610 return "eu.taxeditor";