Project

General

Profile

Download (5.95 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.ui.section;
11

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

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

    
33
/**
34
 * Visualizes an element of type ENTITY in an {@link AbstractEntityCollectionSection}
35
 *  and links listener functionalities to it.
36
 *
37
 *  @param ENTITY the type of the element which is visualized by this class
38
 *
39
 * @author n.hoffmann
40
 * @created Nov 16, 2009
41
 */
42
public abstract class AbstractEntityCollectionElement<ENTITY> extends
43
		AbstractCdmFormElement implements IEntityElement<ENTITY>,
44
		SelectionListener, IConversationEnabled {
45

    
46
	protected ENTITY entity;
47

    
48
	private final Composite container;
49

    
50
	/**
51
	 * Composite "around" the actual content. Is used for control action like e.g. remove button
52
	 */
53
	private final Composite box;
54

    
55
	private Button btnRemove;
56
    protected Button btnChooseEntity;
57

    
58
	private Color backgroundColor;
59

    
60
	public AbstractEntityCollectionElement(CdmFormFactory formFactory,
61
			AbstractFormSection section, ENTITY entity, SelectionListener removeListener,
62
			Color backgroundColor, int style) {
63
		super(formFactory, (ICdmFormElement) section);
64
		this.entity = entity;
65

    
66
		init();
67

    
68
		formFactory.addPropertyChangeListener(this);
69

    
70
		box = formFactory.createComposite(section.getLayoutComposite());
71
		box.setBackgroundMode(SWT.INHERIT_DEFAULT);
72
		addControl(box);
73

    
74
		TableWrapLayout boxLayout = LayoutConstants.LAYOUT(4, false);
75
		boxLayout.topMargin = 4;
76
		boxLayout.bottomMargin = 4;
77
		box.setLayout(boxLayout);
78

    
79
		box.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
80

    
81
		container = formFactory.createComposite(box);
82
		container.setBackgroundMode(SWT.INHERIT_DEFAULT);
83

    
84
		setLayoutComposite(container);
85

    
86
		addControl(container);
87
		TableWrapLayout containerLayout = LayoutConstants.LAYOUT(2, false);
88
		containerLayout.horizontalSpacing = 5;
89

    
90
		container.setLayout(containerLayout);
91
		container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
92

    
93
		if (removeListener != null) {
94
			btnRemove = formFactory.createButton(box, null, SWT.PUSH);
95
			addControl(btnRemove);
96
			btnRemove.setLayoutData(LayoutConstants.RIGHT());
97
			btnRemove.setImage(ImageResources
98
					.getImage(ImageResources.TRASH_ICON));
99
			btnRemove.setToolTipText("Remove");
100

    
101
			btnRemove.addSelectionListener(removeListener);
102
		}
103

    
104
		createControls(this, style);
105

    
106
		setEntity(entity);
107
	}
108

    
109
	/**
110
	 * Init gets executed before any other setup of the section takes place
111
	 *
112
	 * Implement this if you want to configure the section
113
	 */
114
	public void init() {
115
		// default implementation is empty
116
	}
117

    
118
	public abstract void setEntity(ENTITY entity);
119

    
120
	@Override
121
    public ENTITY getEntity() {
122
		return entity;
123
	}
124

    
125
	/**
126
	 * Sub classes should override to provide the functionality to choose the
127
	 * entity from existing ones from the data source.<br>
128
	 * <b>Note:</b> to enable this functionality sub classes have to set
129
	 * the corresponding flag in the super constructor
130
	 * @return an existing entity from the data source
131
	 */
132
	protected ENTITY selectFromDialog(){
133
	    return null;
134
	}
135

    
136
	public abstract void createControls(ICdmFormElement element, int style);
137

    
138
	/**
139
	 * Mark <code>this</code> element as selected.
140
	 */
141
	@Override
142
    public void setSelected(boolean selected) {
143

    
144
		for (ICdmFormElement element : getElements()) {
145
			if (element instanceof ISelectable) {
146
				((ISelectable) element).setSelected(selected);
147
			}
148
		}
149
		setBackground(selected ? SELECTED : getPersistentBackground());
150
	}
151

    
152
	/** {@inheritDoc} */
153
	@Override
154
	public void propertyChange(PropertyChangeEvent event) {
155
		if (event == null) {
156
			return;
157
		}
158
		Object eventSource = event.getSource();
159
		if (getElements().contains(eventSource) || getControls().contains(eventSource)) {
160
			handleEvent(eventSource);
161
		}
162
	}
163

    
164
	public abstract void handleEvent(Object eventSource);
165

    
166
	/** {@inheritDoc} */
167
	@Override
168
    public void setBackground(Color color) {
169
	    if(box.isDisposed() || container.isDisposed()){
170
	        return;
171
	    }
172
		backgroundColor = color;
173
		super.setBackground(color);
174
		box.setBackground(color);
175
		container.setBackground(color);
176
	}
177

    
178
	/**
179
	 * {@inheritDoc}
180
	 *
181
	 * React when selection occurs
182
	 */
183
	@Override
184
    public void widgetSelected(SelectionEvent e) {
185

    
186
	}
187

    
188
	/** {@inheritDoc} */
189
	@Override
190
    public void widgetDefaultSelected(SelectionEvent e) {
191
	}
192

    
193
	/** {@inheritDoc} */
194
	@Override
195
	public Composite getLayoutComposite() {
196
		return container;
197
	}
198

    
199
	public Color getBackgroundColor() {
200
		return backgroundColor;
201
	}
202

    
203
    public Composite getBox() {
204
        return box;
205
    }
206

    
207
	@Override
208
    public ConversationHolder getConversationHolder() {
209
		if (getParentElement() instanceof IConversationEnabled) {
210
			return ((IConversationEnabled) getParentElement())
211
					.getConversationHolder();
212
		}
213
		throw new IllegalArgumentException(
214
				"Parent element should be IConversationEnabled");
215
	}
216

    
217
	/** {@inheritDoc} */
218
	@Override
219
    public void update(CdmDataChangeMap changeEvents) {
220
	}
221
}
(3-3/9)