Refactored description editor per #577
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / AbstractTaxonEditor.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.editor;
11
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;
44
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;
49
50 /**
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.
54 * <p>
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>.
58 * </p>
59 * @author p.ciardelli
60 * @created 10.09.2008
61 * @version 1.0
62 */
63 public abstract class AbstractTaxonEditor extends EditorPart implements
64 IAdaptable {
65 private static final Logger logger = Logger
66 .getLogger(AbstractTaxonEditor.class);
67
68 private Taxon taxon;
69
70 /**
71 * When this <code>EditorPart</code> gets focus, the data structure of
72 * <code>defaultPropertyObject</code> is displayed in the property sheet.
73 */
74
75 protected ManagedForm managedForm;
76 protected ScrolledForm scrolledForm;
77 protected Composite parent;
78 protected ISelectionProvider provider;
79 protected IUndoContext undoContext;
80
81 protected IHasPropertySource selectedObject;
82
83 protected Composite partComposite;
84
85 /* (non-Javadoc)
86 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
87 */
88 public void doSave(IProgressMonitor monitor) {}
89
90 /* (non-Javadoc)
91 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
92 */
93 public void doSaveAs() {}
94
95 /* (non-Javadoc)
96 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
97 */
98 public void init(IEditorSite site, IEditorInput input)
99 throws PartInitException {
100
101 if (!(input instanceof IEditorInput))
102 throw new PartInitException(
103 "Invalid Input: Must be IFileEditorInput");
104
105 if (input.getAdapter(Taxon.class) != null) {
106 taxon = (Taxon) input.getAdapter(Taxon.class);
107 } else {
108 throw new PartInitException(
109 "Invalid Input: Taxon cannot be null");
110 }
111
112 setSite(site);
113 setInput(input);
114
115 this.provider = new SimpleSelectionProvider();
116 this.getSite().setSelectionProvider(provider);
117 }
118
119 private void initUndoContext() {
120 undoContext = new ObjectUndoContext(this);
121 // getEditorSite returns multipageeditor, NOT this
122 // UndoActionHandler expects a IWorkbenchPartSite
123
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);
131 }
132
133 public IUndoContext getUndoContext() {
134 return undoContext;
135 }
136
137 /* (non-Javadoc)
138 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
139 */
140 public void setFocus() {
141 // setSelection(selectedObject);
142 }
143
144 /**
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>.
148 *
149 * @param selectedObject
150 */
151 protected void setSelection(IHasPropertySource selectedObject) {
152
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();
157 }
158
159 // Set the selection to this editor's selected object
160 this.selectedObject = selectedObject;
161
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();
166 }
167 if (propertySource == null) {
168 provider.setSelection(new StructuredSelection());
169 } else {
170 provider.setSelection(new StructuredSelection(propertySource));
171 }
172
173 }
174
175 /* (non-Javadoc)
176 * @see org.eclipse.ui.part.EditorPart#isDirty()
177 */
178 public boolean isDirty() {
179 return false;
180 }
181
182 /* (non-Javadoc)
183 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
184 */
185 public boolean isSaveAsAllowed() {
186 return false;
187 }
188
189 /* (non-Javadoc)
190 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
191 */
192 public void createPartControl(Composite composite) {
193
194 this.partComposite = composite;
195
196 initUndoContext();
197
198 createManagedForm(composite);
199 }
200
201 protected void createManagedForm(Composite composite) {
202
203 managedForm = new ManagedForm(composite) {
204 public void dirtyStateChanged() {
205 firePropertyChange(PROP_DIRTY);
206 }
207 public boolean setInput(Object input) {
208 if (input instanceof IHasPropertySource) {
209 setSelection((IHasPropertySource)input);
210 }
211 return super.setInput(input);
212 }
213 };
214 scrolledForm = managedForm.getForm();
215 parent = scrolledForm.getBody();
216
217 // Taxon taxon = getTaxon();
218 parent.setData(taxon);
219
220 parent.setLayout(new TableWrapLayout());
221 parent.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
222 }
223
224 @SuppressWarnings("unchecked")
225 public Object getAdapter(Class type) {
226 if (type == IPropertySheetPage.class) {
227
228 PropertySheetPage page = new PropertySheetPage() {
229 public void makeContributions(IMenuManager menuManager,
230 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
231 super.makeContributions(menuManager, toolBarManager, statusLineManager);
232
233 // Remove "Show categories", "Show advanced properties", "Restore default value"
234 toolBarManager.removeAll();
235 menuManager.removeAll();
236 }
237
238 public Control getControl() {
239 Control control = super.getControl();
240
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);
245 }
246 }
247 return control;
248 }
249 };
250
251 // Try out PartListener to intercept selections
252 IWorkbenchPage activePage = GlobalController.getActivePage();
253 // IWorkbenchPart active = activePage.getActivePart();
254 IPartListener2 partListener = new IPartListener2() {
255
256 @Override
257 public void partActivated(IWorkbenchPartReference partRef) {
258 // Fires for every part
259 // logger.warn(partRef.getTitle());
260 }
261
262 @Override
263 public void partBroughtToTop(IWorkbenchPartReference partRef) {
264 // TODO Auto-generated method stub
265
266 }
267
268 @Override
269 public void partClosed(IWorkbenchPartReference partRef) {
270 // TODO Auto-generated method stub
271
272 }
273
274 @Override
275 public void partDeactivated(IWorkbenchPartReference partRef) {
276 // TODO Auto-generated method stub
277
278 }
279
280 @Override
281 public void partHidden(IWorkbenchPartReference partRef) {
282 // TODO Auto-generated method stub
283
284 }
285
286 @Override
287 public void partInputChanged(IWorkbenchPartReference partRef) {
288 // logger.warn("part input changed");
289 }
290
291 @Override
292 public void partOpened(IWorkbenchPartReference partRef) {
293 // TODO Auto-generated method stub
294
295 }
296
297 @Override
298 public void partVisible(IWorkbenchPartReference partRef) {
299 // TODO Auto-generated method stub
300
301 }
302
303 };
304 activePage.addPartListener(partListener);
305
306 PropertySheetController.setPropertySheetPage(page);
307
308 CustomSortPropertySheetEntry entry = new CustomSortPropertySheetEntry();
309 page.setRootEntry(entry);
310 page.refresh();
311
312 return page;
313 }
314 return super.getAdapter(type);
315 }
316
317 protected Taxon getTaxon() {
318 return taxon;
319 }
320
321 public void setDirty() {
322 managedForm.dirtyStateChanged();
323 }
324
325 public boolean redraw() {
326 this.selectedObject = null;
327 managedForm.getForm().dispose();
328 createManagedForm(partComposite);
329 return true;
330 }
331 }