Project

General

Profile

Download (7.24 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.Event;
21
import org.eclipse.swt.widgets.Layout;
22
import org.eclipse.swt.widgets.Listener;
23
import org.eclipse.ui.forms.widgets.TableWrapLayout;
24

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

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

    
51
	protected ENTITY entity;
52

    
53
	private final Composite container;
54

    
55
	/**
56
	 * Composite "around" the actual content. Is used for control action like e.g. remove button
57
	 */
58
	private final Composite box;
59

    
60
	private Button btnRemove;
61
    private Button btnChooseEntity;
62

    
63
	private Color backgroundColor;
64

    
65
	public AbstractEntityCollectionElement(CdmFormFactory formFactory,
66
	        AbstractFormSection section, ENTITY entity,
67
	        SelectionListener removeListener, Color backgroundColor, int style) {
68
	    this(formFactory, section, entity, removeListener, false, backgroundColor, style);
69
	}
70
	public AbstractEntityCollectionElement(CdmFormFactory formFactory,
71
			AbstractFormSection section, ENTITY entity, SelectionListener removeListener,
72
			boolean isChoosableEntity, Color backgroundColor, int style) {
73
		super(formFactory, (ICdmFormElement) section);
74

    
75
		init();
76

    
77
		formFactory.addPropertyChangeListener(this);
78

    
79
		box = formFactory.createComposite(section.getLayoutComposite());
80
		box.setBackgroundMode(SWT.INHERIT_DEFAULT);
81
		addControl(box);
82

    
83
		TableWrapLayout boxLayout = LayoutConstants.LAYOUT(4, false);
84
		boxLayout.topMargin = 4;
85
		boxLayout.bottomMargin = 4;
86
		box.setLayout(boxLayout);
87

    
88
		box.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
89

    
90
		container = formFactory.createComposite(box);
91
		container.setBackgroundMode(SWT.INHERIT_DEFAULT);
92

    
93
		setLayoutComposite(container);
94

    
95
		addControl(container);
96
		Layout containerLayout = LayoutConstants.LAYOUT(2, false);
97

    
98
		container.setLayout(containerLayout);
99
		container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
100

    
101
		if(isChoosableEntity){
102
		    btnChooseEntity = formFactory.createButton(box, null, SWT.PUSH);
103
		    addControl(btnChooseEntity);
104
		    btnChooseEntity.setLayoutData(LayoutConstants.RIGHT());
105
		    btnChooseEntity.setImage(ImageResources.getImage(ImageResources.BROWSE_ICON));
106
		    btnChooseEntity.setToolTipText("Browse");
107
		    btnChooseEntity.addListener(SWT.Selection, new Listener() {
108

    
109
		        @Override
110
		        public void handleEvent(Event event) {
111
		            ENTITY entity = selectFromDialog();
112
		            if(entity!=null){
113
		                if(getParentElement() instanceof AbstractEntityCollectionSection){
114
		                    ((AbstractEntityCollectionSection)getParentElement()).removeElement(getEntity());
115
		                    setEntity(entity);
116
		                    ((AbstractEntityCollectionSection)getParentElement()).addElement(entity);
117
		                    ((AbstractEntityCollectionSection)getParentElement()).firePropertyChangeEvent(getParentElement());
118
		                }
119
		            }
120
		        }
121
		    });
122
		}
123

    
124
		if (removeListener != null) {
125
			btnRemove = formFactory.createButton(box, null, SWT.PUSH);
126
			addControl(btnRemove);
127
			btnRemove.setLayoutData(LayoutConstants.RIGHT());
128
			btnRemove.setImage(ImageResources
129
					.getImage(ImageResources.TRASH_ICON));
130
			btnRemove.setToolTipText("Remove");
131

    
132
			btnRemove.addSelectionListener(removeListener);
133
		}
134

    
135
		createControls(this, style);
136

    
137
		setEntity(entity);
138
	}
139

    
140
	/**
141
	 * Init gets executed before any other setup of the section takes place
142
	 *
143
	 * Implement this if you want to configure the section
144
	 */
145
	public void init() {
146
		// default implementation is empty
147
	}
148

    
149
	public abstract void setEntity(ENTITY entity);
150

    
151
	@Override
152
    public ENTITY getEntity() {
153
		return entity;
154
	}
155

    
156
	/**
157
	 * Sub classes should override to provide the functionality to choose the
158
	 * entity from existing ones from the data source.<br>
159
	 * <b>Note:</b> to enable this functionality sub classes have to set
160
	 * the corresponding flag in the super constructor
161
	 * @return an existing entity from the data source
162
	 */
163
	protected ENTITY selectFromDialog(){
164
	    return null;
165
	}
166

    
167
	public abstract void createControls(ICdmFormElement element, int style);
168

    
169
	/**
170
	 * Mark <code>this</code> element as selected.
171
	 */
172
	@Override
173
    public void setSelected(boolean selected) {
174

    
175
		for (ICdmFormElement element : getElements()) {
176
			if (element instanceof ISelectable) {
177
				((ISelectable) element).setSelected(selected);
178
			}
179
		}
180
		setBackground(selected ? SELECTED : getPersistentBackground());
181
	}
182

    
183
	/** {@inheritDoc} */
184
	@Override
185
	public void propertyChange(PropertyChangeEvent event) {
186
		if (event == null) {
187
			return;
188
		}
189
		Object eventSource = event.getSource();
190
		if (getElements().contains(eventSource)) {
191
			handleEvent(eventSource);
192
		}
193
	}
194

    
195
	public abstract void handleEvent(Object eventSource);
196

    
197
	/** {@inheritDoc} */
198
	@Override
199
    public void setBackground(Color color) {
200
		backgroundColor = color;
201
		super.setBackground(color);
202
		box.setBackground(color);
203
		container.setBackground(color);
204
	}
205

    
206
	/**
207
	 * {@inheritDoc}
208
	 *
209
	 * React when selection occurs
210
	 */
211
	@Override
212
    public void widgetSelected(SelectionEvent e) {
213

    
214
	}
215

    
216
	/** {@inheritDoc} */
217
	@Override
218
    public void widgetDefaultSelected(SelectionEvent e) {
219
	}
220

    
221
	/** {@inheritDoc} */
222
	@Override
223
	public Composite getLayoutComposite() {
224
		return container;
225
	}
226

    
227
	public Color getBackgroundColor() {
228
		return backgroundColor;
229
	}
230

    
231
    public Composite getBox() {
232
        return box;
233
    }
234

    
235
	@Override
236
    public ConversationHolder getConversationHolder() {
237
		if (getParentElement() instanceof IConversationEnabled) {
238
			return ((IConversationEnabled) getParentElement())
239
					.getConversationHolder();
240
		}
241
		throw new IllegalArgumentException(
242
				"Parent element should be IConversationEnabled");
243
	}
244

    
245
	/** {@inheritDoc} */
246
	@Override
247
    public void update(CdmDataChangeMap changeEvents) {
248
	}
249
}
(3-3/8)