Project

General

Profile

Download (7.42 KB) Statistics
| Branch: | Tag: | Revision:
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.runtime.IAdaptable;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.jface.action.IMenuManager;
16
import org.eclipse.jface.action.IStatusLineManager;
17
import org.eclipse.jface.action.IToolBarManager;
18
import org.eclipse.jface.viewers.ISelectionProvider;
19
import org.eclipse.jface.viewers.StructuredSelection;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.graphics.Color;
22
import org.eclipse.swt.widgets.Composite;
23
import org.eclipse.swt.widgets.Control;
24
import org.eclipse.swt.widgets.Display;
25
import org.eclipse.swt.widgets.Tree;
26
import org.eclipse.ui.IEditorInput;
27
import org.eclipse.ui.IEditorSite;
28
import org.eclipse.ui.PartInitException;
29
import org.eclipse.ui.forms.IManagedForm;
30
import org.eclipse.ui.forms.ManagedForm;
31
import org.eclipse.ui.forms.widgets.ScrolledForm;
32
import org.eclipse.ui.forms.widgets.TableWrapLayout;
33
import org.eclipse.ui.part.EditorPart;
34
import org.eclipse.ui.views.properties.IPropertySheetPage;
35
import org.eclipse.ui.views.properties.IPropertySource;
36
import org.eclipse.ui.views.properties.PropertySheetPage;
37

    
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.taxeditor.UiUtil;
40
import eu.etaxonomy.taxeditor.propertysheet.CustomSortPropertySheetEntry;
41
import eu.etaxonomy.taxeditor.propertysheet.PropertySourceAdapter;
42

    
43
/**
44
 * The abstract editor for displaying a category of <code>Taxon</code> data, corresponding
45
 * to the tabs ("Name", "Descriptive", etc.) at the bottom of a <code>Taxon</code> view. Implements
46
 * <code>IAdaptable</code> in order to display properties of the objects whose UI elements have focus. 
47
 * <p>
48
 * Implementing classes can choose to show an object in the property sheet when the
49
 * <code>AbstractTaxonEditorView</code> gets focus, by passing the object to the method
50
 * <code>setDefaultPropertySheetObject</code>, for instance, in the method<code>init</code>.
51
 * </p>
52
 * @author p.ciardelli
53
 * @created 10.09.2008
54
 * @version 1.0
55
 */
56
public abstract class AbstractTaxonEditorView extends EditorPart implements
57
		IAdaptable {
58
	private static final Logger logger = Logger
59
			.getLogger(AbstractTaxonEditorView.class);
60

    
61
	private Taxon taxon;
62
	
63
	/**
64
	 * When this <code>EditorPart</code> gets focus, the data structure of 
65
	 * <code>defaultPropertyObject</code> is displayed in the property sheet.
66
	 */
67
	protected Object defaultPropertySheetObject = null;
68
	
69
	protected IManagedForm managedForm;
70
	protected ScrolledForm scrolledForm; 
71
	protected Composite parent;
72
	protected ISelectionProvider provider;
73
	
74
	/* (non-Javadoc)
75
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
76
	 */
77
	@Override
78
	public void doSave(IProgressMonitor monitor) {
79
		// TODO Auto-generated method stub
80

    
81
	}
82

    
83
	/* (non-Javadoc)
84
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
85
	 */
86
	@Override
87
	public void doSaveAs() {
88
		// TODO Auto-generated method stub
89

    
90
	}
91

    
92
	/* (non-Javadoc)
93
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
94
	 */
95
	@Override
96
	public void init(IEditorSite site, IEditorInput input)
97
			throws PartInitException {
98
		if (!(input instanceof IEditorInput))
99
			throw new PartInitException(
100
				"Invalid Input: Must be IFileEditorInput");
101
		
102
		if (input.getAdapter(Taxon.class) != null) {
103
			taxon = (Taxon) input.getAdapter(Taxon.class);
104
		} else {
105
			throw new PartInitException(
106
				"Invalid Input: Taxon cannot be null");
107
		}
108

    
109
		setSite(site);
110
		setInput(input);
111
		
112
		this.provider = new SimpleSelectionProvider();
113
		this.getSite().setSelectionProvider(provider);
114
	}
115

    
116
	/**
117
	 * When this <code>EditorPart</code> gets focus, the data structure of 
118
	 * <code>defaultPropertyObject</code> is displayed in the property sheet.
119
	 * 
120
	 * @param object
121
	 */
122
	protected void setDefaultPropertySheetObject(Object object) {
123
		this.defaultPropertySheetObject = object;
124
	}
125

    
126
	/* (non-Javadoc)
127
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
128
	 */
129
	public void setFocus() {
130
		setSelection(defaultPropertySheetObject);
131
	}
132
	
133
	/**
134
	 * If there is a default property sheet object with a corresponding property source class, 
135
	 * display it in the property sheet. Otherwise, empty the property sheet with an empty
136
	 * <code>StructuredSelection</code>.
137
	 * 
138
	 * @param selectedObject
139
	 */
140
	protected void setSelection(Object selectedObject) {
141
		IPropertySource propertySource = null;
142
		if (selectedObject != null) {
143
			propertySource = new PropertySourceAdapter(selectedObject).getPropertySource();
144
		}
145
		
146
		if (propertySource == null) {
147
			provider.setSelection(new StructuredSelection());
148
		} else {
149
			provider.setSelection(new StructuredSelection(propertySource));
150
		}		
151
	}
152
	
153
	/* (non-Javadoc)
154
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
155
	 */
156
	@Override
157
	public boolean isDirty() {
158
		// TODO Auto-generated method stub
159
		return false;
160
	}
161

    
162
	/* (non-Javadoc)
163
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
164
	 */
165
	@Override
166
	public boolean isSaveAsAllowed() {
167
		// TODO Auto-generated method stub
168
		return false;
169
	}
170

    
171
	/* (non-Javadoc)
172
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
173
	 */
174
	@Override
175
	public void createPartControl(Composite composite) {
176
		
177
		managedForm = new ManagedForm(composite) {
178
			public void dirtyStateChanged() {
179
				firePropertyChange(PROP_DIRTY);
180
			}
181
			public boolean setInput(Object input) {
182
				setSelection(input);
183
				return super.setInput(input); 
184
			} 
185
		};
186
		scrolledForm = managedForm.getForm();
187
		parent = scrolledForm.getBody();
188
				
189
		Taxon taxon = getTaxon();
190
		parent.setData(taxon);
191
		
192
		parent.setLayout(new TableWrapLayout());		
193
		parent.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
194
	}
195
	
196
	@SuppressWarnings("unchecked")
197
	public Object getAdapter(Class type) {
198
	    if (type == IPropertySheetPage.class) {
199
	    	
200
	        PropertySheetPage page = new PropertySheetPage() {
201
	            public void makeContributions(IMenuManager menuManager,
202
	                    IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
203
	            	super.makeContributions(menuManager, toolBarManager, statusLineManager);
204
	            
205
	            	// Remove "Show categories", "Show advanced properties", "Restore default value"
206
	            	toolBarManager.removeAll();
207
	            	menuManager.removeAll();
208
	            }
209
	            
210
	            public Control getControl() {
211
	            	Control control = super.getControl();
212
	            	if (!control.isDisposed()) {
213
	            		if (control instanceof Tree) {
214
	            			UiUtil.setPropertySheetTree((Tree) control);
215
	            		}
216
	            	}
217
	            	return control;
218
	            }
219
	        };	        
220
	        UiUtil.setPropertySheetPage(page);
221
	        
222
	        CustomSortPropertySheetEntry entry = new CustomSortPropertySheetEntry();
223
	        page.setRootEntry(entry);
224
	        page.refresh();
225
	        
226
	        return page;
227
	    }
228
	    return super.getAdapter(type);
229
	}
230
	
231
	protected Taxon getTaxon() {
232
		return taxon;
233
	}
234
}
(1-1/26)