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
.editor
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
14 import org
.eclipse
.core
.commands
.operations
.ObjectUndoContext
;
15 import org
.eclipse
.core
.runtime
.IAdaptable
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.jface
.action
.IMenuManager
;
18 import org
.eclipse
.jface
.action
.IStatusLineManager
;
19 import org
.eclipse
.jface
.action
.IToolBarManager
;
20 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
21 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.Tree
;
27 import org
.eclipse
.ui
.IActionBars
;
28 import org
.eclipse
.ui
.IEditorInput
;
29 import org
.eclipse
.ui
.IEditorSite
;
30 import org
.eclipse
.ui
.IPartListener2
;
31 import org
.eclipse
.ui
.IWorkbenchPage
;
32 import org
.eclipse
.ui
.IWorkbenchPartReference
;
33 import org
.eclipse
.ui
.PartInitException
;
34 import org
.eclipse
.ui
.actions
.ActionFactory
;
35 import org
.eclipse
.ui
.forms
.IManagedForm
;
36 import org
.eclipse
.ui
.forms
.ManagedForm
;
37 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
38 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
39 import org
.eclipse
.ui
.operations
.RedoActionHandler
;
40 import org
.eclipse
.ui
.operations
.UndoActionHandler
;
41 import org
.eclipse
.ui
.part
.EditorPart
;
42 import org
.eclipse
.ui
.views
.properties
.IPropertySheetPage
;
43 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
44 import org
.eclipse
.ui
.views
.properties
.PropertySheetPage
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
47 import eu
.etaxonomy
.taxeditor
.UiUtil
;
48 import eu
.etaxonomy
.taxeditor
.propertysheet
.CustomSortPropertySheetEntry
;
51 * The abstract editor for displaying a category of <code>Taxon</code> data, corresponding
52 * to the tabs ("Name", "Descriptive", etc.) at the bottom of a <code>Taxon</code> view. Implements
53 * <code>IAdaptable</code> in order to display properties of the objects whose UI elements have focus.
55 * Implementing classes can choose to show an object in the property sheet when the
56 * <code>AbstractTaxonEditor</code> gets focus, by passing the object to the method
57 * <code>setDefaultPropertySheetObject</code>, for instance, in the method<code>init</code>.
63 public abstract class AbstractTaxonEditor
extends EditorPart
implements
65 private static final Logger logger
= Logger
66 .getLogger(AbstractTaxonEditor
.class);
71 * When this <code>EditorPart</code> gets focus, the data structure of
72 * <code>defaultPropertyObject</code> is displayed in the property sheet.
75 protected IManagedForm managedForm
;
76 protected ScrolledForm scrolledForm
;
77 protected Composite parent
;
78 protected ISelectionProvider provider
;
79 protected IUndoContext undoContext
;
81 protected IHasPropertySource selectedObject
;
84 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
86 public void doSave(IProgressMonitor monitor
) {}
89 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
91 public void doSaveAs() {}
94 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
96 public void init(IEditorSite site
, IEditorInput input
)
97 throws PartInitException
{
99 if (!(input
instanceof IEditorInput
))
100 throw new PartInitException(
101 "Invalid Input: Must be IFileEditorInput");
103 if (input
.getAdapter(Taxon
.class) != null) {
104 taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
106 throw new PartInitException(
107 "Invalid Input: Taxon cannot be null");
113 this.provider
= new SimpleSelectionProvider();
114 this.getSite().setSelectionProvider(provider
);
117 private void initUndoContext() {
118 undoContext
= new ObjectUndoContext(this);
119 // getEditorSite returns multipageeditor, NOT this
120 // UndoActionHandler expects a IWorkbenchPartSite
122 // UndoActionHandler undoHandler = new UndoActionHandler(getEditorSite(), undoContext);
123 // IActionBars actionBars = getEditorSite().getActionBars();
124 UndoActionHandler undoHandler
= new UndoActionHandler(getSite(), undoContext
);
125 RedoActionHandler redoHandler
= new RedoActionHandler(getSite(), undoContext
);
126 IActionBars actionBars
= getEditorSite().getActionBars();
127 actionBars
.setGlobalActionHandler(ActionFactory
.UNDO
.getId(), undoHandler
);
128 actionBars
.setGlobalActionHandler(ActionFactory
.REDO
.getId(), redoHandler
);
131 public IUndoContext
getUndoContext() {
136 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
138 public void setFocus() {
139 // setSelection(selectedObject);
143 * If there is a default property sheet object with a corresponding property source class,
144 * display it in the property sheet. Otherwise, empty the property sheet with an empty
145 * <code>StructuredSelection</code>.
147 * @param selectedObject
149 protected void setSelection(IHasPropertySource selectedObject
) {
151 // Unpaint last selection - last selection will only be unpainted
152 // when something else on this page is selected
153 if (this.selectedObject
instanceof EditorGroupedComposite
) {
154 ((EditorGroupedComposite
) this.selectedObject
).unpaintBorder();
157 // Set the selection to this editor's selected object
158 this.selectedObject
= selectedObject
;
160 // Get the selection's property source, pass it to the selection provider
161 IPropertySource propertySource
= null;
162 if (selectedObject
!= null) {
163 propertySource
= selectedObject
.getPropertySource();
165 if (propertySource
== null) {
166 provider
.setSelection(new StructuredSelection());
168 provider
.setSelection(new StructuredSelection(propertySource
));
174 * @see org.eclipse.ui.part.EditorPart#isDirty()
176 public boolean isDirty() {
181 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
183 public boolean isSaveAsAllowed() {
188 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
190 public void createPartControl(Composite composite
) {
194 createManagedForm(composite
);
197 protected void createManagedForm(Composite composite
) {
199 managedForm
= new ManagedForm(composite
) {
200 public void dirtyStateChanged() {
201 firePropertyChange(PROP_DIRTY
);
203 public boolean setInput(Object input
) {
204 if (input
instanceof IHasPropertySource
) {
205 setSelection((IHasPropertySource
)input
);
207 return super.setInput(input
);
210 scrolledForm
= managedForm
.getForm();
211 parent
= scrolledForm
.getBody();
213 Taxon taxon
= getTaxon();
214 parent
.setData(taxon
);
216 parent
.setLayout(new TableWrapLayout());
217 parent
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
220 @SuppressWarnings("unchecked")
221 public Object
getAdapter(Class type
) {
222 if (type
== IPropertySheetPage
.class) {
224 PropertySheetPage page
= new PropertySheetPage() {
225 public void makeContributions(IMenuManager menuManager
,
226 IToolBarManager toolBarManager
, IStatusLineManager statusLineManager
) {
227 super.makeContributions(menuManager
, toolBarManager
, statusLineManager
);
229 // Remove "Show categories", "Show advanced properties", "Restore default value"
230 toolBarManager
.removeAll();
231 menuManager
.removeAll();
234 public Control
getControl() {
235 Control control
= super.getControl();
237 // Save the property sheet tree for easy access as needed
238 if (!control
.isDisposed()) {
239 if (control
instanceof Tree
) {
240 UiUtil
.setPropertySheetTree((Tree
) control
);
247 // Try out PartListener to intercept selections
248 IWorkbenchPage activePage
= UiUtil
.getActivePage();
249 // IWorkbenchPart active = activePage.getActivePart();
250 IPartListener2 partListener
= new IPartListener2() {
253 public void partActivated(IWorkbenchPartReference partRef
) {
254 // Fires for every part
255 // logger.warn(partRef.getTitle());
259 public void partBroughtToTop(IWorkbenchPartReference partRef
) {
260 // TODO Auto-generated method stub
265 public void partClosed(IWorkbenchPartReference partRef
) {
266 // TODO Auto-generated method stub
271 public void partDeactivated(IWorkbenchPartReference partRef
) {
272 // TODO Auto-generated method stub
277 public void partHidden(IWorkbenchPartReference partRef
) {
278 // TODO Auto-generated method stub
283 public void partInputChanged(IWorkbenchPartReference partRef
) {
284 // logger.warn("part input changed");
288 public void partOpened(IWorkbenchPartReference partRef
) {
289 // TODO Auto-generated method stub
294 public void partVisible(IWorkbenchPartReference partRef
) {
295 // TODO Auto-generated method stub
300 activePage
.addPartListener(partListener
);
302 UiUtil
.setPropertySheetPage(page
);
304 CustomSortPropertySheetEntry entry
= new CustomSortPropertySheetEntry();
305 page
.setRootEntry(entry
);
310 return super.getAdapter(type
);
313 protected Taxon
getTaxon() {
317 public void setDirty() {
318 managedForm
.dirtyStateChanged();