384f68533c9168c2b61a6367608a10469a8b80af
[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.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;
45
46 import eu.etaxonomy.cdm.model.taxon.Taxon;
47 import eu.etaxonomy.taxeditor.UiUtil;
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 IManagedForm managedForm;
76 protected ScrolledForm scrolledForm;
77 protected Composite parent;
78 protected ISelectionProvider provider;
79 protected IUndoContext undoContext;
80
81 protected IHasPropertySource selectedObject;
82
83 /* (non-Javadoc)
84 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
85 */
86 public void doSave(IProgressMonitor monitor) {}
87
88 /* (non-Javadoc)
89 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
90 */
91 public void doSaveAs() {}
92
93 /* (non-Javadoc)
94 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
95 */
96 public void init(IEditorSite site, IEditorInput input)
97 throws PartInitException {
98
99 if (!(input instanceof IEditorInput))
100 throw new PartInitException(
101 "Invalid Input: Must be IFileEditorInput");
102
103 if (input.getAdapter(Taxon.class) != null) {
104 taxon = (Taxon) input.getAdapter(Taxon.class);
105 } else {
106 throw new PartInitException(
107 "Invalid Input: Taxon cannot be null");
108 }
109
110 setSite(site);
111 setInput(input);
112
113 this.provider = new SimpleSelectionProvider();
114 this.getSite().setSelectionProvider(provider);
115 }
116
117 private void initUndoContext() {
118 undoContext = new ObjectUndoContext(this);
119 // getEditorSite returns multipageeditor, NOT this
120 // UndoActionHandler expects a IWorkbenchPartSite
121
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);
129 }
130
131 public IUndoContext getUndoContext() {
132 return undoContext;
133 }
134
135 /* (non-Javadoc)
136 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
137 */
138 public void setFocus() {
139 // setSelection(selectedObject);
140 }
141
142 /**
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>.
146 *
147 * @param selectedObject
148 */
149 protected void setSelection(IHasPropertySource selectedObject) {
150
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();
155 }
156
157 // Set the selection to this editor's selected object
158 this.selectedObject = selectedObject;
159
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();
164 }
165 if (propertySource == null) {
166 provider.setSelection(new StructuredSelection());
167 } else {
168 provider.setSelection(new StructuredSelection(propertySource));
169 }
170
171 }
172
173 /* (non-Javadoc)
174 * @see org.eclipse.ui.part.EditorPart#isDirty()
175 */
176 public boolean isDirty() {
177 return false;
178 }
179
180 /* (non-Javadoc)
181 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
182 */
183 public boolean isSaveAsAllowed() {
184 return false;
185 }
186
187 /* (non-Javadoc)
188 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
189 */
190 public void createPartControl(Composite composite) {
191
192 initUndoContext();
193
194 createManagedForm(composite);
195 }
196
197 protected void createManagedForm(Composite composite) {
198
199 managedForm = new ManagedForm(composite) {
200 public void dirtyStateChanged() {
201 firePropertyChange(PROP_DIRTY);
202 }
203 public boolean setInput(Object input) {
204 if (input instanceof IHasPropertySource) {
205 setSelection((IHasPropertySource)input);
206 }
207 return super.setInput(input);
208 }
209 };
210 scrolledForm = managedForm.getForm();
211 parent = scrolledForm.getBody();
212
213 Taxon taxon = getTaxon();
214 parent.setData(taxon);
215
216 parent.setLayout(new TableWrapLayout());
217 parent.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
218 }
219
220 @SuppressWarnings("unchecked")
221 public Object getAdapter(Class type) {
222 if (type == IPropertySheetPage.class) {
223
224 PropertySheetPage page = new PropertySheetPage() {
225 public void makeContributions(IMenuManager menuManager,
226 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
227 super.makeContributions(menuManager, toolBarManager, statusLineManager);
228
229 // Remove "Show categories", "Show advanced properties", "Restore default value"
230 toolBarManager.removeAll();
231 menuManager.removeAll();
232 }
233
234 public Control getControl() {
235 Control control = super.getControl();
236
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);
241 }
242 }
243 return control;
244 }
245 };
246
247 // Try out PartListener to intercept selections
248 IWorkbenchPage activePage = UiUtil.getActivePage();
249 // IWorkbenchPart active = activePage.getActivePart();
250 IPartListener2 partListener = new IPartListener2() {
251
252 @Override
253 public void partActivated(IWorkbenchPartReference partRef) {
254 // Fires for every part
255 // logger.warn(partRef.getTitle());
256 }
257
258 @Override
259 public void partBroughtToTop(IWorkbenchPartReference partRef) {
260 // TODO Auto-generated method stub
261
262 }
263
264 @Override
265 public void partClosed(IWorkbenchPartReference partRef) {
266 // TODO Auto-generated method stub
267
268 }
269
270 @Override
271 public void partDeactivated(IWorkbenchPartReference partRef) {
272 // TODO Auto-generated method stub
273
274 }
275
276 @Override
277 public void partHidden(IWorkbenchPartReference partRef) {
278 // TODO Auto-generated method stub
279
280 }
281
282 @Override
283 public void partInputChanged(IWorkbenchPartReference partRef) {
284 // logger.warn("part input changed");
285 }
286
287 @Override
288 public void partOpened(IWorkbenchPartReference partRef) {
289 // TODO Auto-generated method stub
290
291 }
292
293 @Override
294 public void partVisible(IWorkbenchPartReference partRef) {
295 // TODO Auto-generated method stub
296
297 }
298
299 };
300 activePage.addPartListener(partListener);
301
302 UiUtil.setPropertySheetPage(page);
303
304 CustomSortPropertySheetEntry entry = new CustomSortPropertySheetEntry();
305 page.setRootEntry(entry);
306 page.refresh();
307
308 return page;
309 }
310 return super.getAdapter(type);
311 }
312
313 protected Taxon getTaxon() {
314 return taxon;
315 }
316
317 public void setDirty() {
318 managedForm.dirtyStateChanged();
319 }
320 }