Project

General

Profile

Download (8.22 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.api.conversation.ConversationHolder;
22
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23
import eu.etaxonomy.cdm.common.CdmUtils;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
26
import eu.etaxonomy.taxeditor.model.ImageResources;
27
import eu.etaxonomy.taxeditor.store.CdmStore;
28
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
29
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
30
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
31
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
33
import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
34
import eu.etaxonomy.taxeditor.ui.element.ISelectable;
35
import eu.etaxonomy.taxeditor.ui.element.LabelElement;
36
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
37

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

    
52
	protected ENTITY entity;
53

    
54
	private final Composite container;
55

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

    
63
	
64

    
65
	private Button btnRemove;
66
    protected Button btnChooseEntity;
67

    
68
	private Color backgroundColor;
69

    
70
	private LabelElement warnForReferencedObjects;
71

    
72
	public AbstractEntityCollectionElement(CdmFormFactory formFactory,
73
			AbstractFormSection section, ENTITY entity, SelectionListener removeListener,
74
			Color backgroundColor, int style) {
75
		super(formFactory, (ICdmFormElement) section);
76
		this.entity = entity;
77

    
78
		init();
79

    
80
		formFactory.addPropertyChangeListener(this);
81

    
82
		box = formFactory.createComposite(section.getLayoutComposite());
83
		box.setBackgroundMode(SWT.INHERIT_DEFAULT);
84
		addControl(box);
85

    
86
		TableWrapLayout boxLayout = LayoutConstants.LAYOUT(4, false);
87
		boxLayout.topMargin = 4;
88
		boxLayout.bottomMargin = 4;
89
		box.setLayout(boxLayout);
90

    
91
		box.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
92

    
93
		container = formFactory.createComposite(box);
94
		container.setBackgroundMode(SWT.INHERIT_DEFAULT);
95

    
96
		setLayoutComposite(container);
97

    
98
		addControl(container);
99
		TableWrapLayout containerLayout = LayoutConstants.LAYOUT(2, false);
100
		containerLayout.horizontalSpacing = 5;
101

    
102
		container.setLayout(containerLayout);
103
		container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
104

    
105
		if (removeListener != null) {
106
			btnRemove = formFactory.createButton(box, null, SWT.PUSH);
107
			addControl(btnRemove);
108
			btnRemove.setLayoutData(LayoutConstants.RIGHT());
109
			btnRemove.setImage(ImageResources
110
					.getImage(ImageResources.TRASH_ICON));
111
			btnRemove.setToolTipText("Remove");
112

    
113
			btnRemove.addSelectionListener(removeListener);
114
			btnRemove.setEnabled(isEnabled);
115
		}
116

    
117
		
118

    
119
		setEntity(entity);
120
		createControls(this, style);
121
	}
122

    
123
	/**
124
	 * Init gets executed before any other setup of the section takes place
125
	 *
126
	 * Implement this if you want to configure the section
127
	 */
128
	public void init() {
129
		// default implementation is empty
130
	}
131

    
132
	public abstract void setEntity(ENTITY entity);
133

    
134
	@Override
135
    public ENTITY getEntity() {
136
		return entity;
137
	}
138

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

    
150
	public abstract void createControls(ICdmFormElement element, int style);
151

    
152
	/**
153
	 * Mark <code>this</code> element as selected.
154
	 */
155
	@Override
156
    public void setSelected(boolean selected) {
157

    
158
		for (ICdmFormElement element : getElements()) {
159
			if (element instanceof ISelectable) {
160
				((ISelectable) element).setSelected(selected);
161
			}
162
		}
163
		setBackground(selected ? SELECTED : getPersistentBackground());
164
	}
165

    
166
	@Override
167
	public void propertyChange(PropertyChangeEvent event) {
168
		if (event == null) {
169
			return;
170
		}
171
		Object eventSource = event.getSource();
172
		if (getElements().contains(eventSource) || getControls().contains(eventSource)) {
173
			handleEvent(eventSource);
174
		}
175
	}
176

    
177
	public abstract void handleEvent(Object eventSource);
178

    
179
	@Override
180
    public void setBackground(Color color) {
181
	    if(box.isDisposed() || container.isDisposed()){
182
	        return;
183
	    }
184
		backgroundColor = color;
185
		super.setBackground(color);
186
		box.setBackground(color);
187
		container.setBackground(color);
188
	}
189

    
190
	/**
191
	 * {@inheritDoc}
192
	 *
193
	 * React when selection occurs
194
	 */
195
	@Override
196
    public void widgetSelected(SelectionEvent e) {
197

    
198
	}
199

    
200
	@Override
201
    public void widgetDefaultSelected(SelectionEvent e) {
202
	}
203

    
204
	@Override
205
	public Composite getLayoutComposite() {
206
		return container;
207
	}
208

    
209
	public Color getBackgroundColor() {
210
		return backgroundColor;
211
	}
212

    
213
    public Composite getBox() {
214
        return box;
215
    }
216

    
217
	@Override
218
    public ConversationHolder getConversationHolder() {
219
		if (getParentElement() instanceof IConversationEnabled) {
220
			return ((IConversationEnabled) getParentElement())
221
					.getConversationHolder();
222
		}
223
		throw new IllegalArgumentException(
224
				"Parent element should be IConversationEnabled");
225
	}
226

    
227
	@Override
228
    public void update(CdmDataChangeMap changeEvents) {
229
	}
230

    
231
    public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects) {
232
        this.warnForReferencedObjects = warnForReferencedObjects;
233
    }
234

    
235
    public void setWarnForReferencingObjects(ICdmFormElement formElement, int defaultReferencingObjects){
236
        if (entity instanceof CdmBase){
237
            CdmBase cdmBase = (CdmBase)entity;
238
            if (cdmBase.getId() != 0){
239
                long referencingObjectsCount = CdmStore.getCommonService().getReferencingObjectsCount(cdmBase);
240

    
241
                if (referencingObjectsCount > defaultReferencingObjects){
242
                    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")));
243
                    warnForReferencedObjects.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
244
                    warnForReferencedObjects.setLayout(LayoutConstants.FILL(2, 3));
245
                    warnForReferencedObjects.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
246
                }
247
            }
248
        }
249
    }
250

    
251
    public void setWarnForReferencingObjects(ICdmFormElement formElement){
252
        setWarnForReferencingObjects(formElement, 1);
253
    }
254

    
255
    public void setWarnForReferencingObjectsVisible(boolean isVisible){
256
        if (warnForReferencedObjects != null){
257
            warnForReferencedObjects.setVisible(isVisible);
258
        }
259
    }
260
    
261
    @Override
262
	public void setEnabled(boolean enabled) {
263
		this.isEnabled = enabled;
264
		if (btnRemove != null){
265
			btnRemove.setEnabled(enabled);
266
		}
267
		if (btnChooseEntity != null){
268
			btnChooseEntity.setEnabled(enabled);
269
		}
270
		
271
		for (ICdmFormElement element:this.getElements()){
272
			if (element instanceof IEnableableFormElement){
273
				((IEnableableFormElement)element).setEnabled(enabled);
274
			}
275
		}
276
	}
277

    
278
	@Override
279
	public boolean isEnabled() {
280
		return isEnabled;
281
	}
282
}
(3-3/9)