Added FeaturePreferences to enable user to choose which Features to display in the...
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / UiUtil.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;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.core.commands.operations.IOperationHistory;
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.core.commands.operations.OperationHistoryFactory;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.jface.viewers.TreeViewer;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IEditorReference;
27 import org.eclipse.ui.IPageLayout;
28 import org.eclipse.ui.IViewPart;
29 import org.eclipse.ui.IViewReference;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.PartInitException;
33 import org.eclipse.ui.forms.IManagedForm;
34 import org.eclipse.ui.operations.IWorkbenchOperationSupport;
35 import org.eclipse.ui.views.properties.PropertySheet;
36
37 import eu.etaxonomy.cdm.model.common.TermVocabulary;
38 import eu.etaxonomy.cdm.model.description.Feature;
39 import eu.etaxonomy.cdm.model.name.BotanicalName;
40 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
41 import eu.etaxonomy.cdm.model.name.NonViralName;
42 import eu.etaxonomy.cdm.model.name.ZoologicalName;
43 import eu.etaxonomy.cdm.model.taxon.Taxon;
44 import eu.etaxonomy.taxeditor.editor.CompositeBorderDecorator;
45 import eu.etaxonomy.taxeditor.editor.EditorGroupComposite;
46 import eu.etaxonomy.taxeditor.model.CdmUtil;
47 import eu.etaxonomy.taxeditor.model.NameEditorInput;
48 import eu.etaxonomy.taxeditor.navigation.TaxonomicTreeView;
49 import eu.etaxonomy.taxeditor.navigation.TaxonomicTreeViewer;
50
51 /**
52 * A collection of useful methods related to the UI.
53 *
54 * @author p.ciardelli
55 * @created 27.05.2008
56 * @version 1.0
57 */
58 public class UiUtil {
59 private static final Logger logger = Logger.getLogger(UiUtil.class);
60
61 private static Set<Feature> preferredFeatureSet;
62
63 public static IViewPart getPropertySheet() {
64 for (IViewReference reference : getActivePage().getViewReferences()) {
65 if (reference.getId().equals(IPageLayout.ID_PROP_SHEET)) {
66 return reference.getView(false);
67 }
68 }
69 // PropertySheet ps = new PropertySheet();
70 return null;
71 }
72
73 /**
74 * The property sheet listener ensures only property sheets
75 * with data cause the Property Sheet to be updated.
76 */
77 public static void addPropertySheetInputListener() {
78 IViewPart propertySheet = getPropertySheet();
79 // propertySheet.get
80 PropertySheet ps = (PropertySheet) propertySheet;
81 // ps.addPartPropertyListener(listener)
82 // ps.addPropertyListener(l)
83 }
84
85 /**
86 * @param input
87 * @return
88 * @throws PartInitException
89 */
90 public static IEditorPart getEditorByInput(IEditorInput input)
91 throws PartInitException {
92 for (IEditorReference reference : getActivePage().getEditorReferences()) {
93 if (reference.getEditorInput().equals(input)) {
94 IEditorPart editor = reference.getEditor(false);
95 return editor;
96 }
97 }
98 return null;
99 }
100
101 public static IEditorPart getEditorByTaxon(Taxon taxon)
102 throws PartInitException {
103 IEditorInput input = new NameEditorInput(taxon);
104 return getEditorByInput(input);
105 }
106
107 /**
108 * @return
109 */
110 public static IWorkbenchPage getActivePage() {
111 return TaxEditorPlugin.getDefault().getWorkbench()
112 .getActiveWorkbenchWindow().getActivePage();
113 }
114
115 /**
116 * @return
117 */
118 public static Shell getShell() {
119 return TaxEditorPlugin.getDefault().getWorkbench()
120 .getActiveWorkbenchWindow().getShell();
121 }
122
123 /**
124 * @param input
125 * @param editorId
126 * @throws PartInitException
127 */
128 public static void openEditor(IEditorInput input, String editorId)
129 throws PartInitException {
130 getActivePage().openEditor(input, editorId);
131 }
132
133 /**
134 * @param taxon
135 * @param save
136 * @throws PartInitException
137 */
138 public static void closeEditor(Taxon taxon, boolean save)
139 throws PartInitException {
140 IEditorPart editor = getEditorByTaxon(taxon);
141 closeEditor(editor, save);
142 }
143
144 /**
145 * @param input
146 * @param save
147 * @throws PartInitException
148 */
149 public static void closeEditor(IEditorInput input, boolean save)
150 throws PartInitException {
151 IEditorPart editor = getEditorByInput(input);
152 closeEditor(editor, save);
153 }
154
155 /**
156 * @param editor
157 * @param save
158 * @throws PartInitException
159 */
160 public static void closeEditor(IEditorPart editor, boolean save)
161 throws PartInitException {
162 if (editor != null) {
163 getActivePage().closeEditor(editor, save);
164 }
165 }
166
167 public static IViewPart getViewById(String id) {
168 return TaxEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().
169 getActivePage().findView(id);
170 }
171
172 /**
173 * @return
174 */
175 public static TaxonomicTreeViewer getTreeViewer() {
176 IViewPart view = getViewById(TaxonomicTreeView.ID);
177 if (view instanceof TaxonomicTreeView) {
178 TreeViewer viewer = ((TaxonomicTreeView) view).getTreeViewer();
179 if (viewer instanceof TaxonomicTreeViewer) {
180 return (TaxonomicTreeViewer) viewer;
181 }
182 }
183 return null;
184 }
185
186 public static IWorkbenchOperationSupport getOperationSupport() {
187 IWorkbench workbench = TaxEditorPlugin.getDefault().getWorkbench();
188 return workbench.getOperationSupport();
189 }
190
191 public static IOperationHistory getOperationHistory() {
192 return OperationHistoryFactory.getOperationHistory();
193 // return getOperationSupport().getOperationHistory();
194 }
195
196 public static IUndoContext getWorkbenchUndoContext() {
197 return getOperationSupport().getUndoContext();
198 }
199
200 public static void doEditorSave(Taxon taxon, boolean confirm) {
201 IEditorPart editor = null;
202 try {
203 editor = getEditorByTaxon(taxon);
204 } catch (PartInitException e) {
205 // TODO Auto-generated catch block
206 e.printStackTrace();
207 }
208 if (editor != null) {
209 getActivePage().saveEditor(editor, confirm);
210 }
211 }
212
213 public static void logSessionCollectionMembership(Taxon taxon) {
214 String dashes = "---------------------------------------";
215 logger.warn(dashes);
216 logger.warn(CdmUtil.getDisplayName(taxon));
217 logger.warn(dashes);
218 logger.warn("In root taxa? " + TaxEditorPlugin.getDefault().getSessionRootTaxa().contains(taxon));
219 logger.warn(dashes);
220 }
221
222 public static EditorGroupComposite getMisappliedNameGroupComposite(
223 IManagedForm managedForm) {
224
225 // Iterate through parent's children until we find a composite which has a data field
226 // for MISAPPLIED_NAME
227 Composite parent = managedForm.getForm().getBody();
228 for (Control groupComposite : parent.getChildren()) {
229 if (groupComposite instanceof EditorGroupComposite) {
230 if (groupComposite.getData(ITaxEditorConstants.MISAPPLIED_NAME) != null) {
231 return (EditorGroupComposite) groupComposite;
232 }
233 }
234 }
235 return null;
236 }
237
238 public static EditorGroupComposite createMisappliedNameGroupComposite(
239 IManagedForm managedForm) {
240
241 Composite parent = managedForm.getForm().getBody();
242
243 EditorGroupComposite composite = new EditorGroupComposite(parent, managedForm);
244 composite.setData(ITaxEditorConstants.MISAPPLIED_NAME,"");
245 new CompositeBorderDecorator(composite, managedForm);
246 return composite;
247 }
248
249 public static IPreferenceStore getPrefStore() {
250 return TaxEditorPlugin.getDefault().getPreferenceStore();
251 }
252
253 /**
254 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
255 * to have shown in preferences.
256 * </p>
257 * <p>
258 * <code>Feature</code>s are shown unless otherwise specified.
259 * </p>
260 * @return
261 */
262 public static Set<Feature> getPreferredFeatures() {
263
264 // Initialize preferredFeatureSet as necessary
265 if (preferredFeatureSet == null) {
266
267 preferredFeatureSet = new HashSet<Feature>();
268
269 TermVocabulary<Feature> features = CdmUtil.getDescriptionService().
270 getDefaultFeatureVocabulary();
271 for (Feature feature : features) {
272
273 // If the feature is set to show, add it to preferredFeatureSet
274 if (getFeaturePreference(feature)) {
275 preferredFeatureSet.add(feature);
276 }
277 }
278
279 }
280 return preferredFeatureSet;
281 }
282
283 /**
284 * True if <code>feature</code> is set to "show" in preferences.
285 *
286 * @param feature
287 * @return
288 */
289 public static boolean getFeaturePreference(Feature feature) {
290
291 String preferenceKey = getPreferenceKey(feature);
292
293 // If feature does not yet have a pref, set it to true
294 if (!getPrefStore().contains(preferenceKey)) {
295 getPrefStore().setDefault(preferenceKey, true);
296 }
297
298 return getPrefStore().getBoolean(preferenceKey);
299 }
300
301 public static void setFeaturePreference(Feature feature, boolean show) {
302 getPrefStore().setValue(getPreferenceKey(feature), show);
303 }
304
305 /**
306 * Construct a unique key using feature's Uuid
307 *
308 * @param feature
309 * @return
310 */
311 private static String getPreferenceKey(Feature feature) {
312 return ITaxEditorConstants.FEATURE_PREFERENCE
313 . concat(".")
314 . concat(feature.getUuid().toString());
315 }
316
317 /**
318 * @return
319 */
320 public static NonViralName getPreferredNameClassInstance() {
321 String nameCodePreference = TaxEditorPlugin.getDefault().getPreferenceStore().getString(ITaxEditorConstants.CODE_PREFERENCE);
322
323 // Check whether name code preference needs to be initialized
324 if (nameCodePreference == null || nameCodePreference.equals("")) {
325 nameCodePreference = ITaxEditorConstants.DEFAULT_CODE_PREFERENCE;
326 UiUtil.getPrefStore().setValue(ITaxEditorConstants.CODE_PREFERENCE,
327 nameCodePreference);
328 }
329
330 if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICBN)) {
331 return BotanicalName.NewInstance(null);
332 } else if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICZN)) {
333 return ZoologicalName.NewInstance(null);
334 }
335 return NonViralName.NewInstance(null);
336 }
337
338 /**
339 * @return
340 */
341 public static NomenclaturalCode getPreferredNomenclaturalCode() {
342
343 String nameCodePreference = TaxEditorPlugin.getDefault().getPreferenceStore().getString(ITaxEditorConstants.CODE_PREFERENCE);
344
345 // Check whether name code preference needs to be initialized
346 if (nameCodePreference == null || nameCodePreference.equals("")) {
347 nameCodePreference = ITaxEditorConstants.DEFAULT_CODE_PREFERENCE;
348 TaxEditorPlugin.getDefault().getPreferenceStore().setValue(ITaxEditorConstants.CODE_PREFERENCE,
349 nameCodePreference);
350 }
351
352 if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICBN)) {
353 return NomenclaturalCode.ICBN();
354 } else if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICZN)) {
355 return NomenclaturalCode.ICZN();
356 }
357 return null;
358 }
359
360 }