Project

General

Profile

Download (7.58 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.IEntityElement;
33
import eu.etaxonomy.taxeditor.ui.element.ISelectable;
34
import eu.etaxonomy.taxeditor.ui.element.LabelElement;
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
 */
46
public abstract class AbstractEntityCollectionElement<ENTITY> extends
47
		AbstractCdmFormElement implements IEntityElement<ENTITY>,
48
		SelectionListener, IConversationEnabled {
49

    
50
	protected ENTITY entity;
51

    
52
	private final Composite container;
53

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

    
59
	private Button btnRemove;
60
    protected Button btnChooseEntity;
61

    
62
	private Color backgroundColor;
63

    
64
	private LabelElement warnForReferencedObjects;
65

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

    
72
		init();
73

    
74
		formFactory.addPropertyChangeListener(this);
75

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

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

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

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

    
90
		setLayoutComposite(container);
91

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

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

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

    
107
			btnRemove.addSelectionListener(removeListener);
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
	public abstract void setEntity(ENTITY entity);
125

    
126
	@Override
127
    public ENTITY getEntity() {
128
		return entity;
129
	}
130

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

    
142
	public abstract void createControls(ICdmFormElement element, int style);
143

    
144
	/**
145
	 * Mark <code>this</code> element as selected.
146
	 */
147
	@Override
148
    public void setSelected(boolean selected) {
149

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

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

    
169
	public abstract void handleEvent(Object eventSource);
170

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

    
182
	/**
183
	 * {@inheritDoc}
184
	 *
185
	 * React when selection occurs
186
	 */
187
	@Override
188
    public void widgetSelected(SelectionEvent e) {
189

    
190
	}
191

    
192
	@Override
193
    public void widgetDefaultSelected(SelectionEvent e) {
194
	}
195

    
196
	@Override
197
	public Composite getLayoutComposite() {
198
		return container;
199
	}
200

    
201
	public Color getBackgroundColor() {
202
		return backgroundColor;
203
	}
204

    
205
    public Composite getBox() {
206
        return box;
207
    }
208

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

    
219
	@Override
220
    public void update(CdmDataChangeMap changeEvents) {
221
	}
222

    
223
    public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects) {
224
        this.warnForReferencedObjects = warnForReferencedObjects;
225
    }
226

    
227
    public void setWarnForReferencingObjects(ICdmFormElement formElement, int defaultReferencingObjects){
228
        if (entity instanceof CdmBase){
229
            CdmBase cdmBase = (CdmBase)entity;
230
            if (cdmBase.getId() != 0){
231
                long referencingObjectsCount = CdmStore.getCommonService().getReferencingObjectsCount(cdmBase);
232

    
233
                if (referencingObjectsCount > defaultReferencingObjects){
234
                    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")));
235
                    warnForReferencedObjects.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
236
                    warnForReferencedObjects.setLayout(LayoutConstants.FILL(2, 3));
237
                    warnForReferencedObjects.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
238
                }
239
            }
240
        }
241
    }
242

    
243
    public void setWarnForReferencingObjects(ICdmFormElement formElement){
244
        setWarnForReferencingObjects(formElement, 1);
245
    }
246

    
247
    public void setWarnForReferencingObjectsVisible(boolean isVisible){
248
        if (warnForReferencedObjects != null){
249
            warnForReferencedObjects.setVisible(isVisible);
250
        }
251
    }
252
}
(3-3/9)