Project

General

Profile

Download (9.15 KB) Statistics
| Branch: | Tag: | Revision:
1 729887cf n.hoffmann
/**
2 c6fe549a Patric Plitzner
 *
3 729887cf n.hoffmann
 */
4 78222507 n.hoffmann
package eu.etaxonomy.taxeditor.ui.element;
5 729887cf n.hoffmann
6 a5d6e4c0 n.hoffmann
import java.util.ConcurrentModificationException;
7 729887cf n.hoffmann
import java.util.HashSet;
8 c7b20ed3 Patrick Plitzner
import java.util.Iterator;
9 c4c08e81 n.hoffmann
import java.util.List;
10 729887cf n.hoffmann
import java.util.Set;
11
12
import org.eclipse.core.runtime.Assert;
13
import org.eclipse.jface.util.IPropertyChangeListener;
14
import org.eclipse.jface.util.PropertyChangeEvent;
15
import org.eclipse.swt.graphics.Color;
16
import org.eclipse.swt.widgets.Composite;
17
import org.eclipse.swt.widgets.Control;
18 2d9a13f7 n.hoffmann
import org.eclipse.ui.forms.widgets.Section;
19 729887cf n.hoffmann
20 c6fe549a Patric Plitzner
import eu.etaxonomy.taxeditor.model.AbstractUtility;
21 41e2f693 Cherian Mathew
import eu.etaxonomy.taxeditor.model.MessagingUtils;
22 f211dd28 n.hoffmann
23 729887cf n.hoffmann
/**
24 3be6ef3e n.hoffmann
 * @author n.hoffmann
25
 * @version $Id: $
26 729887cf n.hoffmann
 */
27 c6fe549a Patric Plitzner
public abstract class AbstractCdmFormElement implements ICdmFormElement {
28 729887cf n.hoffmann
29
	protected CdmFormFactory formFactory;
30
31 c4c08e81 n.hoffmann
	private List<IPropertyChangeListener> propertyChangeListeners;
32 729887cf n.hoffmann
33
	private Composite layoutComposite;
34 c6fe549a Patric Plitzner
35
	private final Set<Control> controls = new HashSet<Control>();
36
37
	private final Set<ICdmFormElement> elements = new HashSet<ICdmFormElement>();
38 729887cf n.hoffmann
	private ICdmFormElement parentElement;
39 0c52f39c n.hoffmann
40
	private Color persistentBackgroundColor;
41 c6fe549a Patric Plitzner
42
43 729887cf n.hoffmann
	protected AbstractCdmFormElement(CdmFormFactory formFactory, Composite layoutComposite){
44
		this.layoutComposite = layoutComposite;
45
		this.formFactory = formFactory;
46
	}
47 c6fe549a Patric Plitzner
48 729887cf n.hoffmann
	public AbstractCdmFormElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
49
		this(formFactory, formElement.getLayoutComposite());
50
		this.parentElement = formElement;
51
//		addControl(layoutComposite);
52
	}
53 c6fe549a Patric Plitzner
54 b50ab815 Patric Plitzner
	@Override
55
    public CdmFormFactory getFormFactory() {
56 729887cf n.hoffmann
		return formFactory;
57
	}
58 c6fe549a Patric Plitzner
59 27b59ab1 n.hoffmann
	/**
60
	 * Delegates the focus to <code>this</code> elements main input control
61
	 */
62
	public void setFocus(){
63 c6fe549a Patric Plitzner
		// Override in subclasses where needed
64 27b59ab1 n.hoffmann
	}
65 c6fe549a Patric Plitzner
66 729887cf n.hoffmann
	/**
67 3be6ef3e n.hoffmann
	 * Returns all Controls that are managed by this element
68
	 *
69
	 * @return a {@link java.util.Set} object.
70 729887cf n.hoffmann
	 */
71 c6fe549a Patric Plitzner
	@Override
72
    public Set<Control> getControls(){
73 729887cf n.hoffmann
		return controls;
74
	}
75 c6fe549a Patric Plitzner
76 729887cf n.hoffmann
	/**
77
	 * adds the control to the set of controls that are managed by this element
78 3be6ef3e n.hoffmann
	 *
79
	 * @param child a {@link org.eclipse.swt.widgets.Control} object.
80 729887cf n.hoffmann
	 */
81
	protected void addControl(Control child){
82
		controls.add(child);
83
	}
84 c6fe549a Patric Plitzner
85 729887cf n.hoffmann
	protected void removeControl(Control child){
86
		controls.remove(child);
87
	}
88 c6fe549a Patric Plitzner
89 3be6ef3e n.hoffmann
	/**
90
	 * <p>Getter for the field <code>elements</code>.</p>
91
	 *
92
	 * @return a {@link java.util.Set} object.
93
	 */
94 c6fe549a Patric Plitzner
	@Override
95
    public Set<ICdmFormElement> getElements(){
96 729887cf n.hoffmann
		return elements;
97
	}
98 c6fe549a Patric Plitzner
99 3be6ef3e n.hoffmann
	/**
100
	 * <p>Getter for the field <code>parentElement</code>.</p>
101
	 *
102 78222507 n.hoffmann
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
103 3be6ef3e n.hoffmann
	 */
104 c6fe549a Patric Plitzner
	@Override
105
    public ICdmFormElement getParentElement(){
106 729887cf n.hoffmann
		return parentElement;
107
	}
108 c6fe549a Patric Plitzner
109 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
110 c6fe549a Patric Plitzner
	@Override
111
    public void addElement(ICdmFormElement element){
112 729887cf n.hoffmann
		elements.add(element);
113
	}
114 c6fe549a Patric Plitzner
115 3be6ef3e n.hoffmann
	/**
116 1987e065 Patric Plitzner
	 * Remove all child {@link ICdmFormElement}s and child {@link Control}s
117
	 * of the given and the element itself.
118
	 * @param formElement The element to remove
119
	 */
120
	public void removeElementsAndControls(ICdmFormElement formElement){
121
	    for(ICdmFormElement childElement : formElement.getElements()){
122
	        // recursion
123
	        childElement.removeElements();
124
125
	        // unregister selection arbitrator
126
	        if(childElement instanceof ISelectableElement){
127
	            SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
128
	            if(selectionArbitrator != null){
129
	                formFactory.destroySelectionArbitrator(selectionArbitrator);
130
	            }
131
	        }
132
133
	        // unregister from property changes
134
	        formFactory.removePropertyChangeListener(childElement);
135
136
	        // dispose of the controls
137
	        removeControls(childElement);
138
	    }
139
	    removeControls(formElement);
140
	}
141
142
	/**
143
	 * Removes all child {@link ICdmFormElement}s and child {@link Control}s
144
	 * and the element itself.
145 3be6ef3e n.hoffmann
	 */
146 c6fe549a Patric Plitzner
	@Override
147
    public void removeElements(){
148 c7b20ed3 Patrick Plitzner
        for (Iterator<ICdmFormElement> iterator = getElements().iterator();iterator.hasNext();) {
149
            ICdmFormElement childElement = iterator.next();
150 729887cf n.hoffmann
			// recursion
151
			childElement.removeElements();
152 c6fe549a Patric Plitzner
153 729887cf n.hoffmann
			// unregister selection arbitrator
154
			if(childElement instanceof ISelectableElement){
155
				SelectionArbitrator selectionArbitrator = ((ISelectableElement) childElement).getSelectionArbitrator();
156
				if(selectionArbitrator != null){
157
					formFactory.destroySelectionArbitrator(selectionArbitrator);
158
				}
159
			}
160 c6fe549a Patric Plitzner
161 30140758 n.hoffmann
			// unregister from property changes
162 c6fe549a Patric Plitzner
			formFactory.removePropertyChangeListener(childElement);
163
164 729887cf n.hoffmann
			// dispose of the controls
165
			removeControls(childElement);
166
		}
167 30c956e4 Katja Luther
		if(this instanceof ISelectableElement){
168
            SelectionArbitrator selectionArbitrator = ((ISelectableElement) this).getSelectionArbitrator();
169
            if(selectionArbitrator != null){
170
                formFactory.destroySelectionArbitrator(selectionArbitrator);
171
            }
172
        }
173 cd074e7b n.hoffmann
		removeControls(this);
174 729887cf n.hoffmann
		elements.clear();
175
	}
176 c6fe549a Patric Plitzner
177 729887cf n.hoffmann
	private void removeControls(ICdmFormElement element){
178 2d9a13f7 n.hoffmann
		if(element instanceof Section){
179
			((Section) element).dispose();
180
			element = null;
181
		}else{
182
			for(Control control : element.getControls()){
183
				// we added the layoutComposite of the parental element as the layout composite to this formElement
184
				// but we do not want to destroy it.
185
				if(control.equals(element.getLayoutComposite())){
186
					continue;
187
				}else{
188
					control.dispose();
189
					control = null;
190
				}
191 729887cf n.hoffmann
			}
192
		}
193
	}
194 c6fe549a Patric Plitzner
195 3be6ef3e n.hoffmann
	/**
196
	 * <p>Getter for the field <code>layoutComposite</code>.</p>
197
	 *
198
	 * @return a {@link org.eclipse.swt.widgets.Composite} object.
199
	 */
200 c6fe549a Patric Plitzner
	@Override
201
    public Composite getLayoutComposite() {
202 729887cf n.hoffmann
		return layoutComposite;
203
	}
204 c6fe549a Patric Plitzner
205 3be6ef3e n.hoffmann
	/**
206
	 * <p>Setter for the field <code>layoutComposite</code>.</p>
207
	 *
208
	 * @param layoutComposite a {@link org.eclipse.swt.widgets.Composite} object.
209
	 */
210 729887cf n.hoffmann
	public void setLayoutComposite(Composite layoutComposite){
211
		this.layoutComposite = layoutComposite;
212
	}
213 c6fe549a Patric Plitzner
214 3be6ef3e n.hoffmann
	/**
215
	 * <p>Getter for the field <code>propertyChangeListeners</code>.</p>
216
	 *
217
	 * @return a {@link java.util.Set} object.
218
	 */
219 c6fe549a Patric Plitzner
	@Override
220
    public List<IPropertyChangeListener> getPropertyChangeListeners() {
221 729887cf n.hoffmann
		return propertyChangeListeners;
222
	}
223 c6fe549a Patric Plitzner
224 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
225 c6fe549a Patric Plitzner
	@Override
226
    public void setPropertyChangeListeners(List<IPropertyChangeListener> propertyChangeListeners){
227 729887cf n.hoffmann
		this.propertyChangeListeners = propertyChangeListeners;
228
	}
229 c6fe549a Patric Plitzner
230 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
231 c6fe549a Patric Plitzner
	@Override
232
    public void firePropertyChangeEvent(CdmPropertyChangeEvent event) {
233 d089cce7 Patric Plitzner
	    //TODO: replace propertyChangeListeners with formFactory.getPropertyChangeListeners() and remove member propertyChangeListeners from AbstractCdmFormElement
234 729887cf n.hoffmann
		Assert.isNotNull(propertyChangeListeners, "Property change listeners are not present");
235 a5d6e4c0 n.hoffmann
236
		try{
237
			for(Object listener : propertyChangeListeners){
238
				((IPropertyChangeListener)listener).propertyChange(event);
239
			}
240
		}catch(ConcurrentModificationException e){
241 c6fe549a Patric Plitzner
			// There are two cases that produce a CME.
242 a5d6e4c0 n.hoffmann
			// Described here: http://dev.e-taxonomy.eu/trac/ticket/2363#comment:2
243
			// and here: http://dev.e-taxonomy.eu/trac/ticket/2438
244
			// Ignoring the CME because nothing bad is happening
245 41e2f693 Cherian Mathew
			MessagingUtils.warn(getClass(), "ConcurrentModificationException. Can be ignored.");
246 729887cf n.hoffmann
		}
247
	}
248 c6fe549a Patric Plitzner
249 caf6ce95 n.hoffmann
	/**
250
	 * Fires a {@link CdmPropertyChangeEvent} with the given object as source.
251 c6fe549a Patric Plitzner
	 *
252 caf6ce95 n.hoffmann
	 * @param object the object on which the property changed
253
	 */
254 e0b35bf1 n.hoffmann
	public void firePropertyChangeEvent(Object object){
255
		firePropertyChangeEvent(object, null);
256
	}
257 c6fe549a Patric Plitzner
258 caf6ce95 n.hoffmann
	/**
259
	 * Fires a {@link CdmPropertyChangeEvent} with the given object as source also containing the
260
	 * originating event
261 c6fe549a Patric Plitzner
	 *
262 caf6ce95 n.hoffmann
	 * @param object the object on which the property changed
263
	 * @param originatingEvent the originating event
264
	 */
265 e0b35bf1 n.hoffmann
	public void firePropertyChangeEvent(Object object, PropertyChangeEvent originatingEvent){
266
		firePropertyChangeEvent(new CdmPropertyChangeEvent(object, originatingEvent));
267
	}
268 c6fe549a Patric Plitzner
269 729887cf n.hoffmann
270
	/**
271 3be6ef3e n.hoffmann
	 * {@inheritDoc}
272
	 *
273 729887cf n.hoffmann
	 * This method gets called whenever the toolkit this composite was created with gets a property change notification.
274 3be6ef3e n.hoffmann
	 *
275 729887cf n.hoffmann
	 * It is good advice to check whether the PropertyChangeEvent is destined for the implementing composite.
276
	 * Implementations should also check for null PropertyChangeEvents and return immediately in that case.
277 78222507 n.hoffmann
	 * @see eu.etaxonomy.taxeditor.ui.element.ICdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
278 729887cf n.hoffmann
	 */
279 c6fe549a Patric Plitzner
	@Override
280
    public void propertyChange(PropertyChangeEvent event) {
281 729887cf n.hoffmann
		// implement in subclasses
282
	}
283 c6fe549a Patric Plitzner
284 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
285 c6fe549a Patric Plitzner
	@Override
286
    public boolean containsFormElement(ICdmFormElement formElement){
287 729887cf n.hoffmann
		if(formElement == this){
288
			return true;
289
		}else{
290
			for(ICdmFormElement element : getElements()){
291
				boolean contains = element.containsFormElement(formElement);
292
				if(contains == true){
293
					return true;
294
				}
295
			}
296
			return false;
297
		}
298
	}
299 c6fe549a Patric Plitzner
300
	@Override
301
    public void refresh() {
302
		// empty default implementation
303 729887cf n.hoffmann
	}
304 c6fe549a Patric Plitzner
305 0c52f39c n.hoffmann
306 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
307 cfcb0ce6 n.hoffmann
	@Override
308
	public void setBackground(Color color) {
309 1d9ed6ce n.hoffmann
		for(ICdmFormElement element : getElements()){
310
			element.setBackground(color);
311
		}
312 cfcb0ce6 n.hoffmann
	}
313 c6fe549a Patric Plitzner
314 0c52f39c n.hoffmann
	@Override
315
	public void setPersistentBackground(Color color) {
316
		persistentBackgroundColor = color;
317
		setBackground(color);
318
	}
319 c6fe549a Patric Plitzner
320 0c52f39c n.hoffmann
	@Override
321
	public Color getPersistentBackground() {
322
		return persistentBackgroundColor;
323
	}
324 c6fe549a Patric Plitzner
325 f211dd28 n.hoffmann
	public Color getColor(String colorId){
326 c6fe549a Patric Plitzner
		return AbstractUtility.getColor(colorId);
327 f211dd28 n.hoffmann
	}
328 729887cf n.hoffmann
}