started to refactor action delegation
[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.util.Set;
13 import java.util.SortedSet;
14
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.commands.operations.IUndoContext;
19 import org.eclipse.core.commands.operations.IUndoableOperation;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.action.IStatusLineManager;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
28 import org.eclipse.ui.operations.IWorkbenchOperationSupport;
29
30 import eu.etaxonomy.cdm.model.description.Feature;
31 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
32 import eu.etaxonomy.cdm.model.name.Rank;
33 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
34 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
35 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
36
37 /**
38 * A collection of useful methods related to the UI.
39 *
40 * @author p.ciardelli
41 * @created 27.05.2008
42 * @version 1.0
43 */
44 public class GlobalController {
45 private static final Logger logger = Logger.getLogger(GlobalController.class);
46
47 private static Set<Feature> preferredFeatureSet;
48 private static Set<Rank> preferredRankSet;
49
50 /**
51 * @return
52 */
53 public static IWorkbenchPage getActivePage() {
54 return TaxEditorPlugin.getDefault().getWorkbench()
55 .getActiveWorkbenchWindow().getActivePage();
56 }
57
58 /**
59 * @return
60 */
61 public static Shell getShell() {
62 return TaxEditorPlugin.getDefault().getWorkbench()
63 .getActiveWorkbenchWindow().getShell();
64 }
65
66 public static IViewPart getViewById(String id) {
67 return TaxEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().
68 getActivePage().findView(id);
69 }
70
71
72 public static IWorkbenchOperationSupport getOperationSupport() {
73 return TaxEditorPlugin.getDefault().getWorkbench().
74 getOperationSupport();
75 }
76
77 public static IOperationHistory getOperationHistory() {
78 // if (operationHistory == null) {
79 //// operationHistory = getOperationSupport().getOperationHistory();
80 // operationHistory = OperationHistoryFactory.getOperationHistory();
81 // }
82 // return operationHistory;
83 return getOperationSupport().getOperationHistory();
84 }
85
86 public static IUndoContext getWorkbenchUndoContext() {
87 // TODO make this more specific than GLOBAL_UNDO_CONTEXT
88 // return getOperationSupport().getUndoContext();
89 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
90 }
91
92 public static void setStatusMessage(String msg) {
93 PropertySheetController.getPropertySheet().getViewSite().getActionBars().getStatusLineManager().setMessage(msg);
94 }
95
96 /**
97 * Get the name out of the session's name relations vocabulary, not directly
98 * from the object.
99 *
100 * @param type
101 * @return
102 */
103 public static String getNameRelationLabelType(NameRelationshipType type) {
104 SortedSet<NameRelationshipType> vocab =
105 CdmSessionDataRepository.getDefault().getNameRelationshipTypes();
106 for (NameRelationshipType type1 : vocab) {
107 if (type1.equals(type)) {
108 return type1.getLabel();
109 }
110 }
111 return "";
112 }
113
114 private static IStatusLineManager statusLineManager;
115
116 public static void setStatusLineManager(IStatusLineManager manager) {
117 statusLineManager = manager;
118 }
119
120 public static void setStatusLine(String message) {
121 statusLineManager.setMessage(message);
122 }
123
124 public static IProgressMonitor getStatusLineProgressMonitor() {
125 statusLineManager.setCancelEnabled(false);
126 return statusLineManager.getProgressMonitor();
127 }
128
129 public static Set<Feature> getPreferredFeatures() {
130 return preferredFeatureSet;
131 }
132
133 public static void setPreferredFeatures(Set<Feature> preferredFeatureSet) {
134 GlobalController.preferredFeatureSet = preferredFeatureSet;
135 }
136
137 public static Set<Rank> getPreferredRanks() {
138 return preferredRankSet;
139 }
140
141 public static void setPreferredRanks(Set<Rank> preferredRankSet) {
142 GlobalController.preferredRankSet = preferredRankSet;
143 }
144
145 public static void addPreferredRank(Rank rank) {
146 preferredRankSet.add(rank);
147 }
148
149 public static void addPreferredFeature(Feature feature) {
150 GlobalController.preferredFeatureSet.add(feature);
151 }
152
153 public static void executeOperation(IUndoableOperation operation){
154 try {
155 IStatus status = getOperationHistory().execute(operation, getStatusLineProgressMonitor(),
156 WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
157 String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
158 setStatusLine(operation.getLabel() + " " + statusString + ".");
159 } catch (ExecutionException e) {
160 logger.error("Error executing operation: " + operation.getLabel(), e);
161 }
162 }
163 }