Project

General

Profile

Download (7.6 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>
47
        extends AbstractCdmFormElement
48
        implements IEntityElement<ENTITY>, SelectionListener,
49
              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
	@Override
160
	public void propertyChange(PropertyChangeEvent event) {
161
		if (event == null) {
162
			return;
163
		}
164
		Object eventSource = event.getSource();
165
		if (getElements().contains(eventSource) || getControls().contains(eventSource)) {
166
			handleEvent(eventSource);
167
		}
168
	}
169

    
170
	public abstract void handleEvent(Object eventSource);
171

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

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

    
191
	}
192

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

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

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

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

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

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

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

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

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

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

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