(no commit message)
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / model / AbstractUtility.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.model;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.core.commands.operations.IOperationHistory;
16 import org.eclipse.core.commands.operations.IUndoableOperation;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jface.action.IStatusLineManager;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.resource.ColorRegistry;
23 import org.eclipse.jface.resource.FontRegistry;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.themes.ITheme;
30 import org.eclipse.ui.themes.IThemeManager;
31 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
32
33 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
34
35 /**
36 * @author n.hoffmann
37 * @created 11.05.2009
38 * @version 1.0
39 */
40 public abstract class AbstractUtility {
41 private static final Logger logger = Logger.getLogger(AbstractUtility.class);
42
43 protected static IStatusLineManager statusLineManager;
44
45 /**
46 * @return
47 */
48 public static Shell getShell() {
49 return TaxeditorStorePlugin.getDefault().getWorkbench()
50 .getActiveWorkbenchWindow().getShell();
51 }
52
53 public static IWorkbenchPage getActivePage(){
54 return TaxeditorStorePlugin.getDefault().getWorkbench()
55 .getActiveWorkbenchWindow().getActivePage();
56 }
57
58 public static IViewPart getView(String id) throws PartInitException{
59 return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
60 }
61
62 public static boolean closeAll() {
63 return getActivePage().closeAllEditors(true);
64 }
65
66 /**
67 *
68 * @param api
69 * @return
70 */
71 public static Object getService(Class api){
72 return TaxeditorStorePlugin.getDefault().getWorkbench().getService(api);
73 }
74
75 public static ITheme getCurrentTheme(){
76 IThemeManager themeManager = TaxeditorStorePlugin.getDefault().getWorkbench().getThemeManager();
77 return themeManager.getCurrentTheme();
78 }
79
80 /**
81 * Fonts registered to the plugin may be obtained with the Eclipse themeing functionality.
82 * Thus fonts are chooseable by the user via Preferences->General->Appearance->Colors and Fonts
83 *
84 * @return the FontRegistry for the current theme
85 */
86 public static FontRegistry getFontRegistry(){
87 return getCurrentTheme().getFontRegistry();
88 }
89
90 /**
91 * Color registered to the plugin may be obtained with the Eclipse themeing functionality.
92 * Thus colors are editable by the user via Preferences->General->Appearance->Colors and Fonts
93 *
94 * @return the ColorRegistry for the current theme
95 */
96 public static ColorRegistry getColorRegistry(){
97 return getCurrentTheme().getColorRegistry();
98 }
99
100 /**
101 * Open a message box that informs the user about unimplemented functionality.
102 * This method is for developer convenience.
103 */
104 public static void notImplementedMessage(){
105 warningDialog("Not yet implemented", "This functionality is not yet implemented.");
106 }
107
108 public static void informationDialog(String title, String message){
109 MessageDialog.openInformation(getShell(), title, message);
110 }
111
112 public static void warningDialog(String title, String message){
113 MessageDialog.openWarning(getShell(), title, message);
114 }
115
116 public static void errorDialog(String title, String message){
117 MessageDialog.openError(getShell(), title, message);
118 }
119
120 public static IStatus executeOperation(IUndoableOperation operation){
121 if(getOperationHistory() == null){
122 throw new IllegalArgumentException("Ther is no operation history for this context");
123 }
124
125 try {
126 IStatus status = getOperationHistory().execute(operation, getMonitor(),
127 WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
128 String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
129 setStatusLine(operation.getLabel() + " " + statusString + ".");
130 return status;
131 } catch (ExecutionException e) {
132 logger.error("Error executing operation: " + operation.getLabel(), e);
133 }
134 return null;
135 }
136
137 public static IOperationHistory getOperationHistory(){
138 return TaxeditorStorePlugin.getDefault().getWorkbench().
139 getOperationSupport().getOperationHistory();
140 }
141
142 public static void setStatusLine(String message) {
143 statusLineManager.setMessage(message);
144 }
145
146 protected static IProgressMonitor getMonitor() {
147 statusLineManager.setCancelEnabled(false);
148 return statusLineManager.getProgressMonitor();
149 }
150 }