p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / controller / GlobalController.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.controller;
11
12 import java.net.URL;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.SortedSet;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.operations.IOperationHistory;
20 import org.eclipse.core.commands.operations.IUndoContext;
21 import org.eclipse.core.commands.operations.IUndoableOperation;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jface.action.IStatusLineManager;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.IViewPart;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
33 import org.eclipse.ui.operations.IWorkbenchOperationSupport;
34
35 import eu.etaxonomy.cdm.model.description.Feature;
36 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
37 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
38 import eu.etaxonomy.cdm.model.name.Rank;
39 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
40 import eu.etaxonomy.taxeditor.editor.images.UrlDialog;
41 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
42
43 /**
44 * A collection of useful methods related to the UI.
45 *
46 * @author p.ciardelli
47 * @created 27.05.2008
48 * @version 1.0
49 */
50 public class GlobalController {
51 private static final Logger logger = Logger.getLogger(GlobalController.class);
52
53 private static List<Feature> preferredFeatureSet;
54 private static Set<Rank> preferredRankSet;
55
56 /**
57 * @return
58 */
59 public static IWorkbenchPage getActivePage() {
60 return TaxEditorPlugin.getDefault().getWorkbench()
61 .getActiveWorkbenchWindow().getActivePage();
62 }
63
64 /**
65 * @return
66 */
67 public static IWorkbenchPart getActivePart() {
68 return getActivePage().getActivePart();
69 }
70
71 /**
72 * @return
73 */
74 public static Shell getShell() {
75 return TaxEditorPlugin.getDefault().getWorkbench()
76 .getActiveWorkbenchWindow().getShell();
77 }
78
79 public static IViewPart getViewById(String id) {
80 return TaxEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().
81 getActivePage().findView(id);
82 }
83
84
85 public static IWorkbenchOperationSupport getOperationSupport() {
86 return TaxEditorPlugin.getDefault().getWorkbench().
87 getOperationSupport();
88 }
89
90 public static IOperationHistory getOperationHistory() {
91 // if (operationHistory == null) {
92 //// operationHistory = getOperationSupport().getOperationHistory();
93 // operationHistory = OperationHistoryFactory.getOperationHistory();
94 // }
95 // return operationHistory;
96 return getOperationSupport().getOperationHistory();
97 }
98
99 public static IUndoContext getWorkbenchUndoContext() {
100 // TODO make this more specific than GLOBAL_UNDO_CONTEXT
101 // return getOperationSupport().getUndoContext();
102 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
103 }
104
105 public static void setStatusMessage(String msg) {
106 PropertySheetController.getPropertySheet().getViewSite().getActionBars().getStatusLineManager().setMessage(msg);
107 }
108
109 /**
110 * Get the type's label out of the session's name relations vocabulary, not directly
111 * from the object.
112 *
113 * @param type
114 * @return
115 */
116 public static String getNameRelationTypeLabel(NameRelationshipType type) {
117 boolean isZoological =
118 PreferencesController.getPreferredNomenclaturalCode()
119 == NomenclaturalCode.ICZN ? true : false;
120 return getNameRelationTypeLabel(type, isZoological);
121 }
122
123 public static String getNameRelationTypeLabel(NameRelationshipType type, boolean isZoological) {
124
125 SortedSet<NameRelationshipType> vocab =
126 CdmSessionDataRepository.getDefault().getNameRelationshipTypes();
127 for (NameRelationshipType type1 : vocab) {
128 if (type1.equals(type)) {
129
130 if (isZoological &&
131 type1.equals(NameRelationshipType.BASIONYM())) {
132 return "original combination for";
133 } else {
134 return type1.getLabel();
135 }
136 }
137 }
138 return "";
139 }
140
141 /**
142 * Get the inverse representation of the type out of the session's
143 * name relations vocabulary, not directly from the object.
144 *
145 * @param type
146 * @return
147 */
148 public static String getNameRelationInverseTypeLabel(NameRelationshipType type) {
149 boolean isZoological =
150 PreferencesController.getPreferredNomenclaturalCode()
151 == NomenclaturalCode.ICZN ? true : false;
152 return getNameRelationInverseTypeLabel(type, isZoological);
153 }
154
155 public static String getNameRelationInverseTypeLabel(NameRelationshipType type, boolean isZoological) {
156 SortedSet<NameRelationshipType> vocab =
157 CdmSessionDataRepository.getDefault().getNameRelationshipTypes();
158 for (NameRelationshipType type1 : vocab) {
159 if (type1.equals(type)) {
160 return type1.getInverseLabel();
161 }
162 }
163 return "";
164 }
165
166 private static IStatusLineManager statusLineManager;
167
168 public static void setStatusLineManager(IStatusLineManager manager) {
169 statusLineManager = manager;
170 }
171
172 public static void setStatusLine(String message) {
173 statusLineManager.setMessage(message);
174 }
175
176 public static IProgressMonitor getStatusLineProgressMonitor() {
177 statusLineManager.setCancelEnabled(false);
178 return statusLineManager.getProgressMonitor();
179 }
180
181 public static List<Feature> getPreferredFeatures() {
182 return preferredFeatureSet;
183 }
184
185 public static void setPreferredFeatures(List<Feature> preferredFeatureSet) {
186 GlobalController.preferredFeatureSet = preferredFeatureSet;
187 }
188
189 public static Set<Rank> getPreferredRanks() {
190 return preferredRankSet;
191 }
192
193 public static void setPreferredRanks(Set<Rank> preferredRankSet) {
194 GlobalController.preferredRankSet = preferredRankSet;
195 }
196
197 public static void addPreferredRank(Rank rank) {
198 preferredRankSet.add(rank);
199 }
200
201 public static void addPreferredFeature(Feature feature) {
202 GlobalController.preferredFeatureSet.add(feature);
203 }
204
205 public static void executeOperation(IUndoableOperation operation){
206 try {
207 IStatus status = getOperationHistory().execute(operation, getStatusLineProgressMonitor(),
208 WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
209 String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
210 setStatusLine(operation.getLabel() + " " + statusString + ".");
211 } catch (ExecutionException e) {
212 logger.error("Error executing operation: " + operation.getLabel(), e);
213 }
214 }
215
216 /**
217 * Displays exception details in a dialog. Only shows first line in stack trace.
218 *<p>
219 *To show entire stack trace, implement following:
220 * http://rubenlaguna.com/wp/2007/07/25/eclipse-error-reporting-exception-stacktrace-details/
221 */
222 public static void openError(Exception exception) {
223 IStatus status = new Status(IStatus.ERROR, TaxEditorPlugin.PLUGIN_ID,
224 "Joe mama", exception);
225 ErrorDialog.openError(GlobalController.getShell(),
226 "Could not init application controller",
227 "Could not init application controller",
228 status);
229 }
230 }