Project

General

Profile

Download (7.69 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.swt.widgets.Display;
20
import org.eclipse.ui.forms.widgets.TableWrapLayout;
21

    
22
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
23
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
27
import eu.etaxonomy.taxeditor.model.ImageResources;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
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.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> 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
    protected 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
		}
110

    
111
		createControls(this, style);
112

    
113
		setEntity(entity);
114
	}
115

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

    
125
	public abstract void setEntity(ENTITY entity);
126

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

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

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

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

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

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

    
171
	public abstract void handleEvent(Object eventSource);
172

    
173
	/** {@inheritDoc} */
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
	/** {@inheritDoc} */
196
	@Override
197
    public void widgetDefaultSelected(SelectionEvent e) {
198
	}
199

    
200
	/** {@inheritDoc} */
201
	@Override
202
	public Composite getLayoutComposite() {
203
		return container;
204
	}
205

    
206
	public Color getBackgroundColor() {
207
		return backgroundColor;
208
	}
209

    
210
    public Composite getBox() {
211
        return box;
212
    }
213

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

    
224
	/** {@inheritDoc} */
225
	@Override
226
    public void update(CdmDataChangeMap changeEvents) {
227
	}
228

    
229
    public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects) {
230
        this.warnForReferencedObjects = warnForReferencedObjects;
231
    }
232

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

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

    
249
    public void setWarnForReferencingObjects(ICdmFormElement formElement){
250
        setWarnForReferencingObjects(formElement, 1);
251
    }
252

    
253
    public void setWarnForReferencingObjectsVisible(boolean isVisible){
254
        if (warnForReferencedObjects != null){
255
            warnForReferencedObjects.setVisible(isVisible);
256
        }
257
    }
258
}
(3-3/9)