2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.controller
;
13 import java
.util
.List
;
15 import java
.util
.SortedSet
;
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
;
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
;
44 * A collection of useful methods related to the UI.
50 public class GlobalController
{
51 private static final Logger logger
= Logger
.getLogger(GlobalController
.class);
53 private static List
<Feature
> preferredFeatureSet
;
54 private static Set
<Rank
> preferredRankSet
;
59 public static IWorkbenchPage
getActivePage() {
60 return TaxEditorPlugin
.getDefault().getWorkbench()
61 .getActiveWorkbenchWindow().getActivePage();
67 public static IWorkbenchPart
getActivePart() {
68 return getActivePage().getActivePart();
74 public static Shell
getShell() {
75 return TaxEditorPlugin
.getDefault().getWorkbench()
76 .getActiveWorkbenchWindow().getShell();
79 public static IViewPart
getViewById(String id
) {
80 return TaxEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().
81 getActivePage().findView(id
);
85 public static IWorkbenchOperationSupport
getOperationSupport() {
86 return TaxEditorPlugin
.getDefault().getWorkbench().
87 getOperationSupport();
90 public static IOperationHistory
getOperationHistory() {
91 // if (operationHistory == null) {
92 //// operationHistory = getOperationSupport().getOperationHistory();
93 // operationHistory = OperationHistoryFactory.getOperationHistory();
95 // return operationHistory;
96 return getOperationSupport().getOperationHistory();
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
;
105 public static void setStatusMessage(String msg
) {
106 PropertySheetController
.getPropertySheet().getViewSite().getActionBars().getStatusLineManager().setMessage(msg
);
110 * Get the type's label out of the session's name relations vocabulary, not directly
116 public static String
getNameRelationTypeLabel(NameRelationshipType type
) {
117 boolean isZoological
=
118 PreferencesController
.getPreferredNomenclaturalCode()
119 == NomenclaturalCode
.ICZN ?
true : false;
120 return getNameRelationTypeLabel(type
, isZoological
);
123 public static String
getNameRelationTypeLabel(NameRelationshipType type
, boolean isZoological
) {
125 SortedSet
<NameRelationshipType
> vocab
=
126 CdmSessionDataRepository
.getDefault().getNameRelationshipTypes();
127 for (NameRelationshipType type1
: vocab
) {
128 if (type1
.equals(type
)) {
131 type1
.equals(NameRelationshipType
.BASIONYM())) {
132 return "original combination for";
134 return type1
.getLabel();
142 * Get the inverse representation of the type out of the session's
143 * name relations vocabulary, not directly from the object.
148 public static String
getNameRelationInverseTypeLabel(NameRelationshipType type
) {
149 boolean isZoological
=
150 PreferencesController
.getPreferredNomenclaturalCode()
151 == NomenclaturalCode
.ICZN ?
true : false;
152 return getNameRelationInverseTypeLabel(type
, isZoological
);
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();
166 private static IStatusLineManager statusLineManager
;
168 public static void setStatusLineManager(IStatusLineManager manager
) {
169 statusLineManager
= manager
;
172 public static void setStatusLine(String message
) {
173 statusLineManager
.setMessage(message
);
176 public static IProgressMonitor
getStatusLineProgressMonitor() {
177 statusLineManager
.setCancelEnabled(false);
178 return statusLineManager
.getProgressMonitor();
181 public static List
<Feature
> getPreferredFeatures() {
182 return preferredFeatureSet
;
185 public static void setPreferredFeatures(List
<Feature
> preferredFeatureSet
) {
186 GlobalController
.preferredFeatureSet
= preferredFeatureSet
;
189 public static Set
<Rank
> getPreferredRanks() {
190 return preferredRankSet
;
193 public static void setPreferredRanks(Set
<Rank
> preferredRankSet
) {
194 GlobalController
.preferredRankSet
= preferredRankSet
;
197 public static void addPreferredRank(Rank rank
) {
198 preferredRankSet
.add(rank
);
201 public static void addPreferredFeature(Feature feature
) {
202 GlobalController
.preferredFeatureSet
.add(feature
);
205 public static void executeOperation(IUndoableOperation operation
){
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
);
217 * Displays exception details in a dialog. Only shows first line in stack trace.
219 *To show entire stack trace, implement following:
220 * http://rubenlaguna.com/wp/2007/07/25/eclipse-error-reporting-exception-stacktrace-details/
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",