Project

General

Profile

Download (7.61 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
package eu.etaxonomy.taxeditor.ui.section;
10

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

    
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.taxeditor.model.ImageResources;
24
import eu.etaxonomy.taxeditor.store.CdmStore;
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.IEnableableFormElement;
30
import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
31
import eu.etaxonomy.taxeditor.ui.element.ISelectable;
32
import eu.etaxonomy.taxeditor.ui.element.LabelElement;
33
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
34

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

    
48
	protected ENTITY entity;
49

    
50
	private final Composite container;
51

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

    
57
	private boolean isEnabled;
58

    
59
	private Button btnRemove;
60

    
61
	private Button btnChooseEntity;
62

    
63
	private Color backgroundColor;
64

    
65
	private LabelElement warnForReferencedObjects;
66

    
67
	public AbstractEntityCollectionElement(CdmFormFactory formFactory,
68
			AbstractFormSection section, ENTITY entity, SelectionListener removeListener,
69
			Color backgroundColor, int style) {
70
		super(formFactory, (ICdmFormElement) section);
71
		this.entity = entity;
72

    
73
		init();
74

    
75
		formFactory.addPropertyChangeListener(this);
76

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

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

    
86
		box.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
87

    
88
		container = formFactory.createComposite(box);
89
		container.setBackgroundMode(SWT.INHERIT_DEFAULT);
90

    
91
		setLayoutComposite(container);
92

    
93
		addControl(container);
94
		TableWrapLayout containerLayout = LayoutConstants.LAYOUT(2, false);
95
		containerLayout.horizontalSpacing = 5;
96

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

    
100
		if (removeListener != null) {
101
			btnRemove = formFactory.createButton(box, null, SWT.PUSH);
102
			addControl(btnRemove);
103
			btnRemove.setLayoutData(LayoutConstants.RIGHT());
104
			btnRemove.setImage(ImageResources
105
					.getImage(ImageResources.TRASH_ICON));
106
			btnRemove.setToolTipText("Remove");
107

    
108
			btnRemove.addSelectionListener(removeListener);
109
			btnRemove.setEnabled(isEnabled);
110
		}
111

    
112

    
113

    
114
		setEntity(entity);
115
		createControls(this, style);
116
	}
117

    
118
	/**
119
	 * Init gets executed before any other setup of the section takes place
120
	 *
121
	 * Implement this if you want to configure the section
122
	 */
123
	public void init() {
124
		// default implementation is empty
125
	}
126

    
127
	public abstract void setEntity(ENTITY entity);
128

    
129
	@Override
130
    public ENTITY getEntity() {
131
		return entity;
132
	}
133

    
134
	/**
135
	 * Sub classes should override to provide the functionality to choose the
136
	 * entity from existing ones from the data source.<br>
137
	 * <b>Note:</b> to enable this functionality sub classes have to set
138
	 * the corresponding flag in the super constructor
139
	 * @return an existing entity from the data source
140
	 */
141
	protected ENTITY selectFromDialog(){
142
	    return null;
143
	}
144

    
145
	public abstract void createControls(ICdmFormElement element, int style);
146

    
147
	/**
148
	 * Mark <code>this</code> element as selected.
149
	 */
150
	@Override
151
    public void setSelected(boolean selected) {
152

    
153
		for (ICdmFormElement element : getElements()) {
154
			if (element instanceof ISelectable) {
155
				((ISelectable) element).setSelected(selected);
156
			}
157
		}
158
		setBackground(selected ? SELECTED : getPersistentBackground());
159
	}
160

    
161
	@Override
162
	public void propertyChange(PropertyChangeEvent event) {
163
		if (event == null) {
164
			return;
165
		}
166
		Object eventSource = event.getSource();
167
		if (getElements().contains(eventSource) || getControls().contains(eventSource)) {
168
			handleEvent(eventSource);
169
		}
170
	}
171

    
172
	public abstract void handleEvent(Object eventSource);
173

    
174
	@Override
175
    public void setBackground(Color color) {
176
	    if(box.isDisposed() || container.isDisposed()){
177
	        return;
178
	    }
179
		backgroundColor = color;
180
		super.setBackground(color);
181
		box.setBackground(color);
182
		container.setBackground(color);
183
	}
184

    
185
	/**
186
	 * {@inheritDoc}
187
	 *
188
	 * React when selection occurs
189
	 */
190
	@Override
191
    public void widgetSelected(SelectionEvent e) {
192

    
193
	}
194

    
195
	@Override
196
    public void widgetDefaultSelected(SelectionEvent e) {
197
	}
198

    
199
	@Override
200
	public Composite getLayoutComposite() {
201
		return container;
202
	}
203

    
204
	public Color getBackgroundColor() {
205
		return backgroundColor;
206
	}
207

    
208
    public Composite getBox() {
209
        return box;
210
    }
211

    
212
    public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects) {
213
        this.warnForReferencedObjects = warnForReferencedObjects;
214
    }
215

    
216
    public void setWarnForReferencingObjects(ICdmFormElement formElement, int defaultReferencingObjects){
217
        if (entity instanceof CdmBase){
218
            CdmBase cdmBase = (CdmBase)entity;
219
            if (cdmBase.getId() != 0){
220
                long referencingObjectsCount = CdmStore.getCommonService().getReferencingObjectsCount(cdmBase);
221

    
222
                if (referencingObjectsCount > defaultReferencingObjects){
223
                    setWarnForReferencedObjects(formFactory.createLabel(formElement, CdmUtils.Nz("The "+ cdmBase.getUserFriendlyTypeName()+" is referenced by more than one object, if you change it, it is changed for all these objects")));
224
                    warnForReferencedObjects.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
225
                    warnForReferencedObjects.setLayout(LayoutConstants.FILL(2, 3));
226
                    warnForReferencedObjects.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
227
                }
228
            }
229
        }
230
    }
231

    
232
    public void setWarnForReferencingObjects(ICdmFormElement formElement){
233
        setWarnForReferencingObjects(formElement, 1);
234
    }
235

    
236
    public void setWarnForReferencingObjectsVisible(boolean isVisible){
237
        if (warnForReferencedObjects != null){
238
            warnForReferencedObjects.setVisible(isVisible);
239
        }
240
    }
241

    
242
    @Override
243
	public void setEnabled(boolean enabled) {
244
		this.isEnabled = enabled;
245
		if (btnRemove != null){
246
			btnRemove.setEnabled(enabled);
247
		}
248
		if (btnChooseEntity != null){
249
			btnChooseEntity.setEnabled(enabled);
250
		}
251

    
252
		for (ICdmFormElement element:this.getElements()){
253
			if (element instanceof IEnableableFormElement){
254
				((IEnableableFormElement)element).setEnabled(enabled);
255
			}
256
		}
257
	}
258

    
259
	@Override
260
	public boolean isEnabled() {
261
		return isEnabled;
262
	}
263
}
(3-3/9)