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