Project

General

Profile

Download (9.73 KB) Statistics
| Branch: | Tag: | Revision:
1 729887cf n.hoffmann
/**
2 ac12d86b Patric Plitzner
 *
3 729887cf n.hoffmann
 */
4 f211dd28 n.hoffmann
package eu.etaxonomy.taxeditor.ui.section;
5 729887cf n.hoffmann
6
import java.util.Collection;
7 db88d819 Andreas Kohlbecker
import java.util.EnumSet;
8
import java.util.Observable;
9
import java.util.Observer;
10 729887cf n.hoffmann
11
import org.eclipse.jface.action.Action;
12 ac12d86b Patric Plitzner
import org.eclipse.jface.action.IAction;
13 729887cf n.hoffmann
import org.eclipse.jface.action.ToolBarManager;
14
import org.eclipse.jface.resource.ImageDescriptor;
15
import org.eclipse.swt.SWT;
16 db88d819 Andreas Kohlbecker
import org.eclipse.swt.events.DisposeEvent;
17
import org.eclipse.swt.events.DisposeListener;
18 729887cf n.hoffmann
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
21
import org.eclipse.swt.graphics.Color;
22
import org.eclipse.swt.graphics.ImageData;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
25
import org.eclipse.swt.widgets.Label;
26 2d9a13f7 n.hoffmann
import org.eclipse.ui.forms.events.ExpansionEvent;
27
import org.eclipse.ui.forms.events.IExpansionListener;
28 ac12d86b Patric Plitzner
import org.eclipse.ui.forms.widgets.ExpandableComposite;
29 729887cf n.hoffmann
30 2d9a13f7 n.hoffmann
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
31 729887cf n.hoffmann
import eu.etaxonomy.cdm.common.CdmUtils;
32 db88d819 Andreas Kohlbecker
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
33 ac12d86b Patric Plitzner
import eu.etaxonomy.taxeditor.model.AbstractUtility;
34 729887cf n.hoffmann
import eu.etaxonomy.taxeditor.model.ImageResources;
35 cfcb0ce6 n.hoffmann
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
36
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
import eu.etaxonomy.taxeditor.preference.Resources;
38 db88d819 Andreas Kohlbecker
import eu.etaxonomy.taxeditor.store.CdmStore;
39
import eu.etaxonomy.taxeditor.store.LoginManager;
40 09a1be0a Patric Plitzner
import eu.etaxonomy.taxeditor.store.StoreUtil;
41 78222507 n.hoffmann
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
42
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
43 dacb59c9 Patric Plitzner
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
44 729887cf n.hoffmann
45
/**
46 ac12d86b Patric Plitzner
 * This class visualizes an CDM entity of type ENTITY and additionally provides the functionality to add
47
 * other elements of type ELEMENT to them.
48
 *
49
 * @param <ENTITY> A CDM entity which should be visualized by this section.
50
 * @param <ELEMENT> An element that can be added (multiple times) to this entity.
51 729887cf n.hoffmann
 *
52 3be6ef3e n.hoffmann
 * @author n.hoffmann
53
 * @version $Id: $
54 729887cf n.hoffmann
 */
55 ac12d86b Patric Plitzner
56 db88d819 Andreas Kohlbecker
public abstract class AbstractEntityCollectionSection<ENTITY, ELEMENT> extends AbstractFormSection<ENTITY> implements IExpansionListener, Observer {
57 ac12d86b Patric Plitzner
58 db88d819 Andreas Kohlbecker
    private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
59
60
    protected Composite container;
61 ac12d86b Patric Plitzner
62 729887cf n.hoffmann
	private Label label_empty;
63
64
	private String title;
65 ac12d86b Patric Plitzner
66 da32e3d6 Patrick Plitzner
    private AbstractEntityCollectionElement entityCollectionElement;
67
68 2d9a13f7 n.hoffmann
	public AbstractEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
69 ac12d86b Patric Plitzner
		super(formFactory, parentElement, ExpandableComposite.CLIENT_INDENT | style);
70 729887cf n.hoffmann
		this.title = title;
71
		this.setText(getTitleString());
72 db88d819 Andreas Kohlbecker
		updateToolbar();
73 ac12d86b Patric Plitzner
74 2d9a13f7 n.hoffmann
		addExpansionListener(this);
75 db88d819 Andreas Kohlbecker
76
		CdmStore.getLoginManager().addObserver(this);
77
		addDisposeListener(new DisposeListener() {
78
            @Override
79
            public void widgetDisposed(DisposeEvent e) {
80
                CdmStore.getLoginManager().deleteObserver(AbstractEntityCollectionSection.this);
81
            }
82
        });
83 729887cf n.hoffmann
	}
84 ac12d86b Patric Plitzner
85 7cf1f544 n.hoffmann
	protected Control createToolbar() {
86 729887cf n.hoffmann
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
87 ac12d86b Patric Plitzner
88
		Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
89 729887cf n.hoffmann
			@Override
90
			public void run() {
91
				ELEMENT element = createNewElement();
92
				if(element != null){
93
					addElement(element);
94 ac12d86b Patric Plitzner
					if(! getSection().isExpanded()) {
95
                        getSection().setExpanded(true);
96
                    }
97 729887cf n.hoffmann
					internalUpdateSection(true);
98
				}
99
			}
100
		};
101
		addAction.setImageDescriptor(new ImageDescriptor() {
102 ac12d86b Patric Plitzner
103 729887cf n.hoffmann
			@Override
104
			public ImageData getImageData() {
105
				return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
106
			}
107
		});
108
		addAction.setToolTipText(getTooltipString());
109 ac12d86b Patric Plitzner
110 729887cf n.hoffmann
		toolBarManager.add(addAction);
111 ac12d86b Patric Plitzner
112 729887cf n.hoffmann
		return toolBarManager.createControl(this);
113
	}
114 ac12d86b Patric Plitzner
115 db88d819 Andreas Kohlbecker
	/**
116
	 * using this method is discouraged, use updateToolBar() instead
117
	 */
118 87f9934d n.hoffmann
	public void showToolbar(){
119
		setTextClient(createToolbar());
120
	}
121 ac12d86b Patric Plitzner
122 db88d819 Andreas Kohlbecker
    /**
123
     * using this method is discouraged, use updateToolBar() instead
124
     */
125 87f9934d n.hoffmann
	public void removeToolbar(){
126
		setTextClient(null);
127
	}
128 ac12d86b Patric Plitzner
129 87f9934d n.hoffmann
	@Override
130 729887cf n.hoffmann
	public void setEntity(ENTITY entity) {
131 c57713e2 n.hoffmann
		if(entity != null){
132 729887cf n.hoffmann
			super.setEntity(entity);
133
			internalUpdateSection(false);
134
		}
135
		setSectionTitle();
136 db88d819 Andreas Kohlbecker
		updateToolbar();
137 729887cf n.hoffmann
		layout();
138 ac12d86b Patric Plitzner
	}
139
140 729887cf n.hoffmann
	/**
141
	 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
142
	 * Override in subclasses if you want to have a different behaviour.
143
	 */
144
	protected void setSectionTitle() {
145
		if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
146
			this.setText(getTitleString() + " +");
147
		}else{
148
			this.setText(getTitleString());
149
		}
150
	}
151 ac12d86b Patric Plitzner
152 729887cf n.hoffmann
	/**
153
	 * Removes all content from the container
154
	 */
155
	private void destroyDynamicContent(){
156
		if(label_empty != null){
157
			label_empty.dispose();
158
			label_empty = null;
159
		}
160
		removeElements();
161
	}
162 ac12d86b Patric Plitzner
163 729887cf n.hoffmann
	/**
164 3be6ef3e n.hoffmann
	 * Call this method after dynamically changing the client area.
165 85260729 Patric Plitzner
	 * If the options changed is set to <code>true</code>, will also fire a state changed
166 3be6ef3e n.hoffmann
	 * event to inform the user of unsaved changes.
167
	 *
168
	 * @param changed a boolean.
169 729887cf n.hoffmann
	 */
170
	protected void internalUpdateSection(boolean changed){
171 85260729 Patric Plitzner
	    setSectionTitle();
172 cfcb0ce6 n.hoffmann
		destroyDynamicContent();
173 ac12d86b Patric Plitzner
		if(isExpanded() || expandSectionWhenContentAvailable()) {
174
            renderContent(isExpanded());
175
        }
176
		if(changed) {
177
            firePropertyChangeEvent(this);
178
        }
179 729887cf n.hoffmann
	}
180
181
	/**
182 ac12d86b Patric Plitzner
	 * Create the elements to be shown in this section client area
183 729887cf n.hoffmann
	 */
184 cfcb0ce6 n.hoffmann
	private void renderContent(boolean forceExpansion)
185
	{
186
		Collection<ELEMENT> elements = getCollection(getEntity());
187 ac12d86b Patric Plitzner
188 87f9934d n.hoffmann
		if(elements == null || elements.isEmpty()){
189 cfcb0ce6 n.hoffmann
			createEmptyContent();
190 729887cf n.hoffmann
		}else{
191 cfcb0ce6 n.hoffmann
			createDynamicContents(elements);
192
			forceExpansion = true;
193 729887cf n.hoffmann
		}
194 ac12d86b Patric Plitzner
195 cfcb0ce6 n.hoffmann
		this.setExpanded(forceExpansion);
196 ac12d86b Patric Plitzner
197 cfcb0ce6 n.hoffmann
		reflow();
198 729887cf n.hoffmann
	}
199 ac12d86b Patric Plitzner
200 cfcb0ce6 n.hoffmann
	protected void createEmptyContent(){
201
		label_empty = formFactory.createLabel(getLayoutComposite(), getEmptyString());
202
	}
203 ac12d86b Patric Plitzner
204 cfcb0ce6 n.hoffmann
	/**
205
	 * Creates the widgets for the collection
206 3be6ef3e n.hoffmann
	 *
207
	 * @param elements a {@link java.util.Collection} object.
208 cfcb0ce6 n.hoffmann
	 */
209
	protected void createDynamicContents(Collection<ELEMENT> elements)
210 ac12d86b Patric Plitzner
	{
211 cfcb0ce6 n.hoffmann
		int i = 0;
212
		for(final ELEMENT element : elements){
213
			SelectionAdapter removeListener = new SelectionAdapter(){
214
				@Override
215
				public void widgetSelected(SelectionEvent e) {
216 ac12d86b Patric Plitzner
					removeElement(element);
217 cfcb0ce6 n.hoffmann
					internalUpdateSection(true);
218
				}
219
			};
220
			boolean modulo = i++%2 == 0;
221
			String colorResource = modulo ? Resources.COLOR_LIST_EVEN : Resources.COLOR_LIST_ODD;
222 ac12d86b Patric Plitzner
			createElementComposite(element, removeListener, AbstractUtility.getColor(colorResource));
223 cfcb0ce6 n.hoffmann
		}
224 729887cf n.hoffmann
	}
225
226
	/**
227
	 * Create the specific widget for the element
228 3be6ef3e n.hoffmann
	 *
229
	 * @param element a ELEMENT object.
230
	 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
231
	 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
232 729887cf n.hoffmann
	 */
233
	protected void createElementComposite(ELEMENT element, SelectionListener removeListener, Color backgroundColor){
234 da32e3d6 Patrick Plitzner
		entityCollectionElement = formFactory.createEntityCollectionElement(this, element, removeListener, backgroundColor, SWT.NULL);
235 729887cf n.hoffmann
	}
236 ac12d86b Patric Plitzner
237 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
238 729887cf n.hoffmann
	@Override
239
	public void setBackground(Color color) {
240
		if(label_empty != null && !label_empty.isDisposed()){
241
			label_empty.setBackground(color);
242
		}
243
		super.setBackground(color);
244
	}
245
246 cfcb0ce6 n.hoffmann
	/**
247 3be6ef3e n.hoffmann
	 * <p>getTitleString</p>
248
	 *
249
	 * @return a {@link java.lang.String} object.
250 cfcb0ce6 n.hoffmann
	 */
251
	public String getTitleString() {
252
		return CdmUtils.Nz(title);
253
	}
254 ac12d86b Patric Plitzner
255 3be6ef3e n.hoffmann
	/**
256
	 * <p>setTitleString</p>
257
	 *
258
	 * @param title a {@link java.lang.String} object.
259
	 */
260 cfcb0ce6 n.hoffmann
	public void setTitleString(String title){
261
		this.title = title;
262 87f9934d n.hoffmann
		setSectionTitle();
263
		layout();
264 cfcb0ce6 n.hoffmann
	}
265 ac12d86b Patric Plitzner
266 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
267 ac12d86b Patric Plitzner
	@Override
268
    public void expansionStateChanging(ExpansionEvent e) {
269 cfcb0ce6 n.hoffmann
//		logger.warn("Expansion State Changing");
270
	}
271 ac12d86b Patric Plitzner
272 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
273 ac12d86b Patric Plitzner
	@Override
274
    public void expansionStateChanged(ExpansionEvent e) {
275 c10e4c27 n.hoffmann
		if(isExpanded()){
276
			renderContent(isExpanded());
277
		}else{
278
			destroyDynamicContent();
279
		}
280 cfcb0ce6 n.hoffmann
	}
281 ac12d86b Patric Plitzner
282 cfcb0ce6 n.hoffmann
	private boolean expandSectionWhenContentAvailable(){
283
		return PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE);
284
	}
285 ac12d86b Patric Plitzner
286 7c5ba8e0 Patric Plitzner
	/**
287 a14f5d39 Patric Plitzner
	 * Remove an element from the entities collection and update the section
288 7c5ba8e0 Patric Plitzner
	 *
289
	 * @param element a ELEMENT object.
290
	 */
291
	public void removeElementAndUpdate(ELEMENT element) {
292
		removeElement(element);
293
		internalUpdateSection(true);
294
	}
295 a14f5d39 Patric Plitzner
296 db88d819 Andreas Kohlbecker
	@Override
297
    public void update(Observable o, Object arg){
298
	    if(o instanceof LoginManager){
299
	        updateToolbar();
300
	    }
301
	}
302
303
    private void updateToolbar() {
304 09a1be0a Patric Plitzner
        if(getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), UPDATE)){
305 db88d819 Andreas Kohlbecker
            showToolbar();
306
        } else {
307
            removeToolbar();
308
        }
309
    }
310
311 da32e3d6 Patrick Plitzner
    public AbstractEntityCollectionElement getEntityCollectionElement() {
312
        return entityCollectionElement;
313
    }
314
315 729887cf n.hoffmann
	/**
316
	 * Get the specific collection of this entity
317 3be6ef3e n.hoffmann
	 *
318
	 * @param entity a ENTITY object.
319
	 * @return a {@link java.util.Collection} object.
320 729887cf n.hoffmann
	 */
321
	public abstract Collection<ELEMENT> getCollection(ENTITY entity);
322 ac12d86b Patric Plitzner
323 729887cf n.hoffmann
	/**
324
	 * Create a new Element for this collection
325 3be6ef3e n.hoffmann
	 *
326
	 * @return a ELEMENT object.
327 729887cf n.hoffmann
	 */
328
	public abstract ELEMENT createNewElement();
329 ac12d86b Patric Plitzner
330 729887cf n.hoffmann
	/**
331
	 * Add an element to the entities collection
332 3be6ef3e n.hoffmann
	 *
333
	 * @param element a ELEMENT object.
334 729887cf n.hoffmann
	 */
335
	public abstract void addElement(ELEMENT element);
336 ac12d86b Patric Plitzner
337 729887cf n.hoffmann
	/**
338
	 * Remove an element from the entities collection
339 3be6ef3e n.hoffmann
	 *
340
	 * @param element a ELEMENT object.
341 729887cf n.hoffmann
	 */
342
	public abstract void removeElement(ELEMENT element);
343 ac12d86b Patric Plitzner
344 729887cf n.hoffmann
	/**
345
	 * String to display when the collection is empty
346 3be6ef3e n.hoffmann
	 *
347
	 * @return a {@link java.lang.String} object.
348 729887cf n.hoffmann
	 */
349
	public abstract String getEmptyString();
350 ac12d86b Patric Plitzner
351 729887cf n.hoffmann
	/**
352 3be6ef3e n.hoffmann
	 * <p>getTooltipString</p>
353
	 *
354 729887cf n.hoffmann
	 * @return String to display when hovering the add button
355
	 */
356 ac12d86b Patric Plitzner
	protected abstract String getTooltipString();
357 729887cf n.hoffmann
}