DescriptionLabelComponent's ContextMenu now rebuilt every time it is called to reflec...
[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.core.runtime.Assert;
20 import org.eclipse.jface.preference.IPreferenceStore;
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 /**
174 * Returns the <code>TaxonomicTreeViewer</code> used for navigation. Not to
175 * be confused with the window that contains it, <code>TaxonomicTreeView</code>.
176 *
177 * @return
178 */
179 public static TaxonomicTreeViewer getTreeViewer() {
180 TaxonomicTreeView view = getTreeView();
181 if (view == null) {
182 return null;
183 }
184 return view.getTreeViewer();
185 }
186
187 /**
188 * @see eu.etaxonomy.taxeditor.navigation.TaxonomicTreeView#createTreeViewer()
189 *
190 * @return
191 */
192 public static TaxonomicTreeViewer createTreeViewer() {
193 TaxonomicTreeView view = getTreeView();
194 if (view == null) {
195 return null;
196 }
197 return view.createTreeViewer();
198 }
199
200 /**
201 * Returns the <code>TaxonomicTreeView</code> containing the
202 * <code>TaxonomicTreeViewer</code> used for navigation.
203 *
204 * @return
205 */
206 public static TaxonomicTreeView getTreeView() {
207 IViewPart view = getViewById(TaxonomicTreeView.ID);
208 if (view == null || !(view instanceof TaxonomicTreeView)) {
209 return null;
210 }
211 return (TaxonomicTreeView) view;
212 }
213
214
215 public static IWorkbenchOperationSupport getOperationSupport() {
216 IWorkbench workbench = TaxEditorPlugin.getDefault().getWorkbench();
217 return workbench.getOperationSupport();
218 }
219
220 public static IOperationHistory getOperationHistory() {
221 return OperationHistoryFactory.getOperationHistory();
222 // return getOperationSupport().getOperationHistory();
223 }
224
225 public static IUndoContext getWorkbenchUndoContext() {
226 return getOperationSupport().getUndoContext();
227 }
228
229 public static void doEditorSave(Taxon taxon, boolean confirm) {
230 IEditorPart editor = null;
231 try {
232 editor = getEditorByTaxon(taxon);
233 } catch (PartInitException e) {
234 // TODO Auto-generated catch block
235 e.printStackTrace();
236 }
237 if (editor != null) {
238 getActivePage().saveEditor(editor, confirm);
239 }
240 }
241
242
243 public static EditorGroupComposite getMisappliedNameGroupComposite(
244 IManagedForm managedForm) {
245
246 // Iterate through parent's children until we find a composite which has a data field
247 // for MISAPPLIED_NAME
248 Composite parent = managedForm.getForm().getBody();
249 for (Control groupComposite : parent.getChildren()) {
250 if (groupComposite instanceof EditorGroupComposite) {
251 if (groupComposite.getData(ITaxEditorConstants.MISAPPLIED_NAME) != null) {
252 return (EditorGroupComposite) groupComposite;
253 }
254 }
255 }
256 return null;
257 }
258
259 public static EditorGroupComposite createMisappliedNameGroupComposite(
260 IManagedForm managedForm) {
261
262 Composite parent = managedForm.getForm().getBody();
263
264 EditorGroupComposite composite = new EditorGroupComposite(parent, managedForm);
265 composite.setData(ITaxEditorConstants.MISAPPLIED_NAME,"");
266 new CompositeBorderDecorator(composite, managedForm);
267 return composite;
268 }
269
270 public static IPreferenceStore getPrefStore() {
271 return TaxEditorPlugin.getDefault().getPreferenceStore();
272 }
273
274 /**
275 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
276 * to have shown in preferences.
277 * </p>
278 * <p>
279 * <code>Feature</code>s are shown unless otherwise specified.
280 * </p>
281 * @return
282 */
283 public static Set<Feature> getPreferredFeatures() {
284
285 // Initialize preferredFeatureSet as necessary
286 if (preferredFeatureSet == null) {
287
288 preferredFeatureSet = new HashSet<Feature>();
289
290 TermVocabulary<Feature> features = CdmUtil.getDescriptionService().
291 getDefaultFeatureVocabulary();
292 for (Feature feature : features) {
293
294 // If the feature is set to show, add it to preferredFeatureSet
295 if (getFeaturePreference(feature)) {
296 preferredFeatureSet.add(feature);
297 }
298 }
299
300 }
301 return preferredFeatureSet;
302 }
303
304 /**
305 * True if <code>feature</code> is set to "show" in preferences.
306 *
307 * @param feature
308 * @return
309 */
310 public static boolean getFeaturePreference(Feature feature) {
311
312 String preferenceKey = getPreferenceKey(feature);
313
314 // If feature does not yet have a pref, set it to true
315 if (!getPrefStore().contains(preferenceKey)) {
316 getPrefStore().setDefault(preferenceKey, true);
317 }
318
319 return getPrefStore().getBoolean(preferenceKey);
320 }
321
322 /**
323 * Set the show state of a <code>Feature</code> in the
324 * <code>PreferenceStore</code>.
325 * <p>
326 * Also sets <code>preferredFeatureSet</code> to null to force it be
327 * re-populated the next time {@link #getPreferredFeatures()} is called.
328 *
329 * @param feature
330 * @param show
331 */
332 public static void setFeaturePreference(Feature feature, boolean show) {
333 preferredFeatureSet = null;
334 getPrefStore().setValue(getPreferenceKey(feature), show);
335 }
336
337 /**
338 * Construct a unique key using feature's Uuid
339 *
340 * @param feature
341 * @return
342 */
343 private static String getPreferenceKey(Feature feature) {
344 return ITaxEditorConstants.FEATURE_PREFERENCE
345 . concat(".")
346 . concat(feature.getUuid().toString());
347 }
348
349 /**
350 * @return
351 */
352 public static NonViralName getPreferredNameClassInstance() {
353 String nameCodePreference = TaxEditorPlugin.getDefault().getPreferenceStore().getString(ITaxEditorConstants.CODE_PREFERENCE);
354
355 // Check whether name code preference needs to be initialized
356 if (nameCodePreference == null || nameCodePreference.equals("")) {
357 nameCodePreference = ITaxEditorConstants.DEFAULT_CODE_PREFERENCE;
358 UiUtil.getPrefStore().setValue(ITaxEditorConstants.CODE_PREFERENCE,
359 nameCodePreference);
360 }
361
362 if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICBN)) {
363 return BotanicalName.NewInstance(null);
364 } else if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICZN)) {
365 return ZoologicalName.NewInstance(null);
366 }
367 return NonViralName.NewInstance(null);
368 }
369
370 /**
371 * @return
372 */
373 public static NomenclaturalCode getPreferredNomenclaturalCode() {
374
375 String nameCodePreference = TaxEditorPlugin.getDefault().getPreferenceStore().getString(ITaxEditorConstants.CODE_PREFERENCE);
376
377 // Check whether name code preference needs to be initialized
378 if (nameCodePreference == null || nameCodePreference.equals("")) {
379 nameCodePreference = ITaxEditorConstants.DEFAULT_CODE_PREFERENCE;
380 TaxEditorPlugin.getDefault().getPreferenceStore().setValue(ITaxEditorConstants.CODE_PREFERENCE,
381 nameCodePreference);
382 }
383
384 if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICBN)) {
385 return NomenclaturalCode.ICBN();
386 } else if (nameCodePreference.equals(ITaxEditorConstants.CODE_PREFERENCE_ICZN)) {
387 return NomenclaturalCode.ICZN();
388 }
389 return null;
390 }
391
392 }