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
.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
;
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
);
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
;
186 public static IWorkbenchOperationSupport
getOperationSupport() {
187 IWorkbench workbench
= TaxEditorPlugin
.getDefault().getWorkbench();
188 return workbench
.getOperationSupport();
191 public static IOperationHistory
getOperationHistory() {
192 return OperationHistoryFactory
.getOperationHistory();
193 // return getOperationSupport().getOperationHistory();
196 public static IUndoContext
getWorkbenchUndoContext() {
197 return getOperationSupport().getUndoContext();
200 public static void doEditorSave(Taxon taxon
, boolean confirm
) {
201 IEditorPart editor
= null;
203 editor
= getEditorByTaxon(taxon
);
204 } catch (PartInitException e
) {
205 // TODO Auto-generated catch block
208 if (editor
!= null) {
209 getActivePage().saveEditor(editor
, confirm
);
213 public static void logSessionCollectionMembership(Taxon taxon
) {
214 String dashes
= "---------------------------------------";
216 logger
.warn(CdmUtil
.getDisplayName(taxon
));
218 logger
.warn("In root taxa? " + TaxEditorPlugin
.getDefault().getSessionRootTaxa().contains(taxon
));
222 public static EditorGroupComposite
getMisappliedNameGroupComposite(
223 IManagedForm managedForm
) {
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
;
238 public static EditorGroupComposite
createMisappliedNameGroupComposite(
239 IManagedForm managedForm
) {
241 Composite parent
= managedForm
.getForm().getBody();
243 EditorGroupComposite composite
= new EditorGroupComposite(parent
, managedForm
);
244 composite
.setData(ITaxEditorConstants
.MISAPPLIED_NAME
,"");
245 new CompositeBorderDecorator(composite
, managedForm
);
249 public static IPreferenceStore
getPrefStore() {
250 return TaxEditorPlugin
.getDefault().getPreferenceStore();
254 * Returns a <code>Set</code> of the <code>Feature</code>s that the user has chosen
255 * to have shown in preferences.
258 * <code>Feature</code>s are shown unless otherwise specified.
262 public static Set
<Feature
> getPreferredFeatures() {
264 // Initialize preferredFeatureSet as necessary
265 if (preferredFeatureSet
== null) {
267 preferredFeatureSet
= new HashSet
<Feature
>();
269 TermVocabulary
<Feature
> features
= CdmUtil
.getDescriptionService().
270 getDefaultFeatureVocabulary();
271 for (Feature feature
: features
) {
273 // If the feature is set to show, add it to preferredFeatureSet
274 if (getFeaturePreference(feature
)) {
275 preferredFeatureSet
.add(feature
);
280 return preferredFeatureSet
;
284 * True if <code>feature</code> is set to "show" in preferences.
289 public static boolean getFeaturePreference(Feature feature
) {
291 String preferenceKey
= getPreferenceKey(feature
);
293 // If feature does not yet have a pref, set it to true
294 if (!getPrefStore().contains(preferenceKey
)) {
295 getPrefStore().setDefault(preferenceKey
, true);
298 return getPrefStore().getBoolean(preferenceKey
);
301 public static void setFeaturePreference(Feature feature
, boolean show
) {
302 getPrefStore().setValue(getPreferenceKey(feature
), show
);
306 * Construct a unique key using feature's Uuid
311 private static String
getPreferenceKey(Feature feature
) {
312 return ITaxEditorConstants
.FEATURE_PREFERENCE
314 . concat(feature
.getUuid().toString());
320 public static NonViralName
getPreferredNameClassInstance() {
321 String nameCodePreference
= TaxEditorPlugin
.getDefault().getPreferenceStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
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
,
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);
335 return NonViralName
.NewInstance(null);
341 public static NomenclaturalCode
getPreferredNomenclaturalCode() {
343 String nameCodePreference
= TaxEditorPlugin
.getDefault().getPreferenceStore().getString(ITaxEditorConstants
.CODE_PREFERENCE
);
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
,
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();