Project

General

Profile

Download (6.96 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.ui.section;
12

    
13
import org.eclipse.jface.util.PropertyChangeEvent;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.SelectionEvent;
16
import org.eclipse.swt.events.SelectionListener;
17
import org.eclipse.swt.graphics.Color;
18
import org.eclipse.swt.widgets.Button;
19
import org.eclipse.swt.widgets.Composite;
20
import org.eclipse.swt.widgets.Layout;
21
import org.eclipse.ui.forms.widgets.TableWrapLayout;
22

    
23
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
25
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
26
import eu.etaxonomy.taxeditor.model.ImageResources;
27
import eu.etaxonomy.taxeditor.ui.forms.AbstractCdmFormElement;
28
import eu.etaxonomy.taxeditor.ui.forms.AbstractFormSection;
29
import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
30
import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
31
import eu.etaxonomy.taxeditor.ui.forms.IEntityElement;
32
import eu.etaxonomy.taxeditor.ui.forms.ISelectable;
33

    
34
/**
35
 * <p>Abstract AbstractEntityCollectionElement class.</p>
36
 *
37
 * @author n.hoffmann
38
 * @created Nov 16, 2009
39
 * @version 1.0
40
 */
41
public abstract class AbstractEntityCollectionElement<ENTITY> extends AbstractCdmFormElement implements IEntityElement<ENTITY>, SelectionListener, IConversationEnabled{
42
	
43
	protected ENTITY entity;
44
	
45
	private Composite container;
46

    
47
	private Composite box;
48
	
49
	private Button button_remove;
50
	
51
	private Color backgroundColor;
52
	
53
	/**
54
	 * <p>Constructor for AbstractEntityCollectionElement.</p>
55
	 *
56
	 * @param style a int.
57
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
58
	 * @param section a {@link eu.etaxonomy.taxeditor.ui.forms.AbstractFormSection} object.
59
	 * @param entity a ENTITY object.
60
	 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
61
	 * @param <ENTITY> a ENTITY object.
62
	 */
63
	public AbstractEntityCollectionElement(CdmFormFactory formFactory, AbstractFormSection section, ENTITY entity, SelectionListener removeListener,
64
			Color backgroundColor, int style) {
65
		super(formFactory, (ICdmFormElement) section);
66

    
67
		init();
68
		
69
		formFactory.addPropertyChangeListener(this);
70
		
71
//		section.getLayoutComposite().setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));
72
		
73
		box = formFactory.createComposite(section.getLayoutComposite());
74
		box.setBackgroundMode(SWT.INHERIT_DEFAULT);
75
		addControl(box);
76
		
77

    
78
		TableWrapLayout boxLayout = CdmFormFactory.LAYOUT(2, false);		
79
		boxLayout.topMargin = 4;
80
		boxLayout.bottomMargin = 4;
81
		box.setLayout(boxLayout);
82
		
83
		box.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
84
		
85
//		box.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
86
		
87
		container = formFactory.createComposite(box);
88
		container.setBackgroundMode(SWT.INHERIT_DEFAULT);
89
		
90
//		container.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
91
		setLayoutComposite(container);
92
		
93
		addControl(container);
94
		Layout containerLayout = CdmFormFactory.LAYOUT(2, false);
95
		
96
		container.setLayout(containerLayout);
97
		container.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
98
		
99
		if(removeListener != null){
100
			button_remove = formFactory.createButton(box, null, SWT.PUSH);
101
			addControl(button_remove);
102
			button_remove.setLayoutData(CdmFormFactory.RIGHT());
103
			button_remove.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
104
			button_remove.setToolTipText("Remove");
105
			
106
			button_remove.addSelectionListener(removeListener);
107
		}
108
		
109
		
110
		createControls(this, style);
111
				
112
		setEntity(entity);
113
	}
114
	
115
	/**
116
	 * Init gets executed before any other setup of the section takes place
117
	 *
118
	 * Implement this if you want to configure the section
119
	 */
120
	public void init() {
121
		// default implementation is empty
122
	}
123

    
124
	/**
125
	 * <p>Setter for the field <code>entity</code>.</p>
126
	 *
127
	 * @param entity a ENTITY object.
128
	 */
129
	public abstract void setEntity(ENTITY entity);
130
	
131
	/**
132
	 * <p>Getter for the field <code>entity</code>.</p>
133
	 *
134
	 * @return a ENTITY object.
135
	 */
136
	public ENTITY getEntity(){
137
		return entity;
138
	}
139
	
140
	/**
141
	 * <p>createControls</p>
142
	 *
143
	 * @param element a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
144
	 * @param style a int.
145
	 */
146
	public abstract void createControls(ICdmFormElement element, int style);
147

    
148
	/** {@inheritDoc} */
149
	public void setSelected(boolean selected) {
150
		for(ICdmFormElement element : getElements()){
151
			if(element instanceof ISelectable){
152
				((ISelectable)element).setSelected(selected);
153
			}
154
		}
155
		this.setBackground(getColor(selected));
156
	}
157
	
158
	/*
159
	 * (non-Javadoc)
160
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
161
	 */
162
	/** {@inheritDoc} */
163
	@Override
164
	public void propertyChange(PropertyChangeEvent event) {
165
		if(event == null){
166
			return;
167
		}
168
		Object eventSource = event.getSource();
169
		if(getElements().contains(eventSource)){
170
			handleEvent(eventSource);
171
		}
172
	}
173
	
174
	/**
175
	 * <p>handleEvent</p>
176
	 *
177
	 * @param eventSource a {@link java.lang.Object} object.
178
	 */
179
	public abstract void handleEvent(Object eventSource);
180

    
181
	/*
182
	 * (non-Javadoc)
183
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#getColor(boolean)
184
	 */
185
	/** {@inheritDoc} */
186
	@Override
187
	public Color getColor(boolean selected) {
188
		return selected ? SELECTED : backgroundColor;
189
	}
190
	
191
	/** {@inheritDoc} */
192
	public void setBackground(Color color){
193
		container.setBackground(color);
194
		box.setBackground(color);
195
		for(ICdmFormElement element : getElements()){
196
			element.setBackground(color);
197
		}
198
	}
199
	
200
	/**
201
	 * {@inheritDoc}
202
	 *
203
	 * React when selection occurs
204
	 */
205
	public void widgetSelected(SelectionEvent e) {
206
		
207
	}
208
	
209
	/** {@inheritDoc} */
210
	public void widgetDefaultSelected(SelectionEvent e) {}
211
	
212
	/** {@inheritDoc} */
213
	@Override
214
	public Composite getLayoutComposite() {
215
		return container;
216
	}
217

    
218
	/**
219
	 * <p>Setter for the field <code>backgroundColor</code>.</p>
220
	 *
221
	 * @param backgroundColor the backgroundColor to set
222
	 */
223
	public void setBackgroundColor(Color backgroundColor) {
224
		this.backgroundColor = backgroundColor;
225
		getLayoutComposite().setBackground(backgroundColor);
226
		setBackground(backgroundColor);
227
	}
228

    
229
	/**
230
	 * <p>Getter for the field <code>backgroundColor</code>.</p>
231
	 *
232
	 * @return the backgroundColor
233
	 */
234
	public Color getBackgroundColor() {
235
		return backgroundColor;
236
	}
237
	
238
	/**
239
	 * <p>getConversationHolder</p>
240
	 *
241
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
242
	 */
243
	public ConversationHolder getConversationHolder() {
244
		if(getParentElement() instanceof IConversationEnabled){
245
			return ((IConversationEnabled) getParentElement()).getConversationHolder();
246
		}
247
		throw new IllegalArgumentException("Parent element should be IConversationEnabled");
248
	}
249
	
250
	/** {@inheritDoc} */
251
	public void update(CdmDataChangeMap changeEvents) {}
252
}
(3-3/6)