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
.ManagedForm
;
36 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
37 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
38 import org
.eclipse
.ui
.operations
.RedoActionHandler
;
39 import org
.eclipse
.ui
.operations
.UndoActionHandler
;
40 import org
.eclipse
.ui
.part
.EditorPart
;
41 import org
.eclipse
.ui
.views
.properties
.IPropertySheetPage
;
42 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
43 import org
.eclipse
.ui
.views
.properties
.PropertySheetPage
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
46 import eu
.etaxonomy
.taxeditor
.controller
.GlobalController
;
47 import eu
.etaxonomy
.taxeditor
.controller
.PropertySheetController
;
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 ManagedForm managedForm
;
76 protected ScrolledForm scrolledForm
;
77 protected Composite parent
;
78 protected ISelectionProvider provider
;
79 protected IUndoContext undoContext
;
81 protected IHasPropertySource selectedObject
;
83 protected Composite partComposite
;
86 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
88 public void doSave(IProgressMonitor monitor
) {}
91 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
93 public void doSaveAs() {}
96 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
98 public void init(IEditorSite site
, IEditorInput input
)
99 throws PartInitException
{
101 if (!(input
instanceof IEditorInput
))
102 throw new PartInitException(
103 "Invalid Input: Must be IFileEditorInput");
105 if (input
.getAdapter(Taxon
.class) != null) {
106 taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
108 throw new PartInitException(
109 "Invalid Input: Taxon cannot be null");
115 this.provider
= new SimpleSelectionProvider();
116 this.getSite().setSelectionProvider(provider
);
119 private void initUndoContext() {
120 undoContext
= new ObjectUndoContext(this);
121 // getEditorSite returns multipageeditor, NOT this
122 // UndoActionHandler expects a IWorkbenchPartSite
124 // UndoActionHandler undoHandler = new UndoActionHandler(getEditorSite(), undoContext);
125 // IActionBars actionBars = getEditorSite().getActionBars();
126 UndoActionHandler undoHandler
= new UndoActionHandler(getSite(), undoContext
);
127 RedoActionHandler redoHandler
= new RedoActionHandler(getSite(), undoContext
);
128 IActionBars actionBars
= getEditorSite().getActionBars();
129 actionBars
.setGlobalActionHandler(ActionFactory
.UNDO
.getId(), undoHandler
);
130 actionBars
.setGlobalActionHandler(ActionFactory
.REDO
.getId(), redoHandler
);
133 public IUndoContext
getUndoContext() {
138 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
140 public void setFocus() {
141 // setSelection(selectedObject);
145 * If there is a default property sheet object with a corresponding property source class,
146 * display it in the property sheet. Otherwise, empty the property sheet with an empty
147 * <code>StructuredSelection</code>.
149 * @param selectedObject
151 protected void setSelection(IHasPropertySource selectedObject
) {
153 // Unpaint last selection - last selection will only be unpainted
154 // when something else on this page is selected
155 if (this.selectedObject
instanceof GroupedComposite
) {
156 ((GroupedComposite
) this.selectedObject
).unpaintBorder();
159 // Set the selection to this editor's selected object
160 this.selectedObject
= selectedObject
;
162 // Get the selection's property source, pass it to the selection provider
163 IPropertySource propertySource
= null;
164 if (selectedObject
!= null) {
165 propertySource
= selectedObject
.getPropertySource();
167 if (propertySource
== null) {
168 provider
.setSelection(new StructuredSelection());
170 provider
.setSelection(new StructuredSelection(propertySource
));
176 * @see org.eclipse.ui.part.EditorPart#isDirty()
178 public boolean isDirty() {
183 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
185 public boolean isSaveAsAllowed() {
190 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
192 public void createPartControl(Composite composite
) {
194 this.partComposite
= composite
;
198 createManagedForm(composite
);
201 protected void createManagedForm(Composite composite
) {
203 managedForm
= new ManagedForm(composite
) {
204 public void dirtyStateChanged() {
205 firePropertyChange(PROP_DIRTY
);
207 public boolean setInput(Object input
) {
208 if (input
instanceof IHasPropertySource
) {
209 setSelection((IHasPropertySource
)input
);
211 return super.setInput(input
);
214 scrolledForm
= managedForm
.getForm();
215 parent
= scrolledForm
.getBody();
217 // Taxon taxon = getTaxon();
218 parent
.setData(taxon
);
220 parent
.setLayout(new TableWrapLayout());
221 parent
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
224 @SuppressWarnings("unchecked")
225 public Object
getAdapter(Class type
) {
226 if (type
== IPropertySheetPage
.class) {
228 PropertySheetPage page
= new PropertySheetPage() {
229 public void makeContributions(IMenuManager menuManager
,
230 IToolBarManager toolBarManager
, IStatusLineManager statusLineManager
) {
231 super.makeContributions(menuManager
, toolBarManager
, statusLineManager
);
233 // Remove "Show categories", "Show advanced properties", "Restore default value"
234 toolBarManager
.removeAll();
235 menuManager
.removeAll();
238 public Control
getControl() {
239 Control control
= super.getControl();
241 // Save the property sheet tree for easy access as needed
242 if (!control
.isDisposed()) {
243 if (control
instanceof Tree
) {
244 PropertySheetController
.setPropertySheetTree((Tree
) control
);
251 // Try out PartListener to intercept selections
252 IWorkbenchPage activePage
= GlobalController
.getActivePage();
253 // IWorkbenchPart active = activePage.getActivePart();
254 IPartListener2 partListener
= new IPartListener2() {
257 public void partActivated(IWorkbenchPartReference partRef
) {
258 // Fires for every part
259 // logger.warn(partRef.getTitle());
263 public void partBroughtToTop(IWorkbenchPartReference partRef
) {
264 // TODO Auto-generated method stub
269 public void partClosed(IWorkbenchPartReference partRef
) {
270 // TODO Auto-generated method stub
275 public void partDeactivated(IWorkbenchPartReference partRef
) {
276 // TODO Auto-generated method stub
281 public void partHidden(IWorkbenchPartReference partRef
) {
282 // TODO Auto-generated method stub
287 public void partInputChanged(IWorkbenchPartReference partRef
) {
288 // logger.warn("part input changed");
292 public void partOpened(IWorkbenchPartReference partRef
) {
293 // TODO Auto-generated method stub
298 public void partVisible(IWorkbenchPartReference partRef
) {
299 // TODO Auto-generated method stub
304 activePage
.addPartListener(partListener
);
306 PropertySheetController
.setPropertySheetPage(page
);
308 CustomSortPropertySheetEntry entry
= new CustomSortPropertySheetEntry();
309 page
.setRootEntry(entry
);
314 return super.getAdapter(type
);
317 protected Taxon
getTaxon() {
321 public void setDirty() {
322 managedForm
.dirtyStateChanged();
325 public boolean redraw() {
326 this.selectedObject
= null;
327 managedForm
.getForm().dispose();
328 createManagedForm(partComposite
);