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
;
12 import java
.util
.HashSet
;
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
;
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
;
52 * A collection of useful methods related to the UI.
59 private static final Logger logger
= Logger
.getLogger(UiUtil
.class);
61 private static Set
<Feature
> preferredFeatureSet
;
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);
69 // PropertySheet ps = new PropertySheet();
74 * The property sheet listener ensures only property sheets
75 * with data cause the Property Sheet to be updated.
77 public static void addPropertySheetInputListener() {
78 IViewPart propertySheet
= getPropertySheet();
80 PropertySheet ps
= (PropertySheet
) propertySheet
;
81 // ps.addPartPropertyListener(listener)
82 // ps.addPropertyListener(l)
88 * @throws PartInitException
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);
101 public static IEditorPart
getEditorByTaxon(Taxon taxon
)
102 throws PartInitException
{
103 IEditorInput input
= new NameEditorInput(taxon
);
104 return getEditorByInput(input
);
110 public static IWorkbenchPage
getActivePage() {
111 return TaxEditorPlugin
.getDefault().getWorkbench()
112 .getActiveWorkbenchWindow().getActivePage();
118 public static Shell
getShell() {
119 return TaxEditorPlugin
.getDefault().getWorkbench()
120 .getActiveWorkbenchWindow().getShell();
126 * @throws PartInitException
128 public static void openEditor(IEditorInput input
, String editorId
)
129 throws PartInitException
{
130 getActivePage().openEditor(input
, editorId
);
136 * @throws PartInitException
138 public static void closeEditor(Taxon taxon
, boolean save
)
139 throws PartInitException
{
140 IEditorPart editor
= getEditorByTaxon(taxon
);
141 closeEditor(editor
, save
);
147 * @throws PartInitException
149 public static void closeEditor(IEditorInput input
, boolean save
)
150 throws PartInitException
{
151 IEditorPart editor
= getEditorByInput(input
);
152 closeEditor(editor
, save
);
158 * @throws PartInitException
160 public static void closeEditor(IEditorPart editor
, boolean save
)
161 throws PartInitException
{
162 if (editor
!= null) {
163 getActivePage().closeEditor(editor
, save
);
167 public static IViewPart
getViewById(String id
) {
168 return TaxEditorPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().
169 getActivePage().findView(id
);
174 * Returns the <code>TaxonomicTreeViewer</code> used for navigation. Not to
175 * be confused with the window that contains it, <code>TaxonomicTreeView</code>.
179 public static TaxonomicTreeViewer
getTreeViewer() {
180 TaxonomicTreeView view
= getTreeView();
184 return view
.getTreeViewer();
188 * @see eu.etaxonomy.taxeditor.navigation.TaxonomicTreeView#createTreeViewer()
192 public static TaxonomicTreeViewer
createTreeViewer() {
193 TaxonomicTreeView view
= getTreeView();
197 return view
.createTreeViewer();
201 * Returns the <code>TaxonomicTreeView</code> containing the
202 * <code>TaxonomicTreeViewer</code> used for navigation.
206 public static TaxonomicTreeView
getTreeView() {
207 IViewPart view
= getViewById(TaxonomicTreeView
.ID
);
208 if (view
== null || !(view
instanceof TaxonomicTreeView
)) {
211 return (TaxonomicTreeView
) view
;
215 public static IWorkbenchOperationSupport
getOperationSupport() {
216 IWorkbench workbench
= TaxEditorPlugin
.getDefault().getWorkbench();
217 return workbench
.getOperationSupport();
220 public static IOperationHistory
getOperationHistory() {
221 return OperationHistoryFactory
.getOperationHistory();
222 // return getOperationSupport().getOperationHistory();
225 public static IUndoContext
getWorkbenchUndoContext() {
226 return getOperationSupport().getUndoContext();
229 public static void doEditorSave(Taxon taxon
, boolean confirm
) {
230 IEditorPart editor
= null;
232 editor
= getEditorByTaxon(taxon
);
233 } catch (PartInitException e
) {
234 // TODO Auto-generated catch block
237 if (editor
!= null) {
238 getActivePage().saveEditor(editor
, confirm
);
243 public static EditorGroupComposite
getMisappliedNameGroupComposite(
244 IManagedForm managedForm
) {
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
;
259 public static EditorGroupComposite
createMisappliedNameGroupComposite(
260 IManagedForm managedForm
) {
262 Composite parent
= managedForm
.getForm().getBody();
264 EditorGroupComposite composite
= new EditorGroupComposite(parent
, managedForm
);
265 composite
.setData(ITaxEditorConstants
.MISAPPLIED_NAME
,"");
266 new CompositeBorderDecorator(composite
, managedForm
);
270 public static IPreferenceStore
getPrefStore() {
271 return TaxEditorPlugin
.getDefault().getPreferenceStore();
275 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
276 * to have shown in preferences.
279 * <code>Feature</code>s are shown unless otherwise specified.
283 public static Set
<Feature
> getPreferredFeatures() {
285 // Initialize preferredFeatureSet as necessary
286 if (preferredFeatureSet
== null) {
288 preferredFeatureSet
= new HashSet
<Feature
>();
290 TermVocabulary
<Feature
> features
= CdmUtil
.getDescriptionService().
291 getDefaultFeatureVocabulary();
292 for (Feature feature
: features
) {
294 // If the feature is set to show, add it to preferredFeatureSet
295 if (getFeaturePreference(feature
)) {
296 preferredFeatureSet
.add(feature
);
301 return preferredFeatureSet
;
305 * True if <code>feature</code> is set to "show" in preferences.
310 public static boolean getFeaturePreference(Feature feature
) {
312 String preferenceKey
= getPreferenceKey(feature
);
314 // If feature does not yet have a pref, set it to true
315 if (!getPrefStore().contains(preferenceKey
)) {
316 getPrefStore().setDefault(preferenceKey
, true);
319 return getPrefStore().getBoolean(preferenceKey
);
323 * Set the show state of a <code>Feature</code> in the
324 * <code>PreferenceStore</code>.
326 * Also sets <code>preferredFeatureSet</code> to null to force it be
327 * re-populated the next time {@link #getPreferredFeatures()} is called.
332 public static void setFeaturePreference(Feature feature
, boolean show
) {
333 preferredFeatureSet
= null;
334 getPrefStore().setValue(getPreferenceKey(feature
), show
);
338 * Construct a unique key using feature's Uuid
343 private static String
getPreferenceKey(Feature feature
) {
344 return ITaxEditorConstants
.FEATURE_PREFERENCE
346 . concat(feature
.getUuid().toString());
352 public static NonViralName
getPreferredNameClassInstance() {
353 String nameCodePreference
= TaxEditorPlugin
.getDefault().getPreferenceStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
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
,
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);
367 return NonViralName
.NewInstance(null);
373 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
375 String nameCodePreference
= TaxEditorPlugin
.getDefault().getPreferenceStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
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
,
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();