4 package eu
.etaxonomy
.taxeditor
.ui
.section
;
6 import java
.util
.Collection
;
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
;
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
;
37 * <p>Abstract AbstractEntityCollectionSection class.</p>
42 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> extends AbstractFormSection
<ENTITY
> implements IExpansionListener
{
44 protected Composite container
;
46 private Label label_empty
;
51 * <p>Constructor for AbstractEntityCollectionSection.</p>
54 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
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.
61 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
62 super(formFactory
, parentElement
, Section
.CLIENT_INDENT
| style
);
64 this.setText(getTitleString());
67 addExpansionListener(this);
70 protected Control
createToolbar() {
71 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
73 Action addAction
= new Action("add", Action
.AS_PUSH_BUTTON
){
75 * @see org.eclipse.jface.action.Action#run()
79 ELEMENT element
= createNewElement();
82 if(! getSection().isExpanded())
83 getSection().setExpanded(true);
84 internalUpdateSection(true);
88 addAction
.setImageDescriptor(new ImageDescriptor() {
91 public ImageData
getImageData() {
92 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
95 addAction
.setToolTipText(getTooltipString());
97 toolBarManager
.add(addAction
);
99 return toolBarManager
.createControl(this);
102 public void showToolbar(){
103 setTextClient(createToolbar());
106 public void removeToolbar(){
113 * @param entity a ENTITY object.
116 public void setEntity(ENTITY entity
) {
118 super.setEntity(entity
);
119 internalUpdateSection(false);
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.
129 protected void setSectionTitle() {
130 if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
131 this.setText(getTitleString() + " +");
133 this.setText(getTitleString());
138 * Removes all content from the container
140 private void destroyDynamicContent(){
141 if(label_empty
!= null){
142 label_empty
.dispose();
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.
153 * @param changed a boolean.
155 protected void internalUpdateSection(boolean changed
){
156 destroyDynamicContent();
157 if(isExpanded() || expandSectionWhenContentAvailable())
158 renderContent(isExpanded());
160 firePropertyChangeEvent(this);
164 * Whether the entities specific collection changed
166 * KLUDGE this seemingly leads to errors
171 private boolean hasCollectionChanged(ENTITY newEntity
){
175 // // return true on null
176 // if(getEntity() == null || newEntity == null) return true;
178 // // if the entities differ the collection has changed
179 // if(! getEntity().equals(newEntity)) return true;
181 // Collection<ELEMENT> oldCollection = getCollection(getEntity());
182 // Collection<ELEMENT> newCollection = getCollection(newEntity);
184 // // return true on null
185 // if(oldCollection == null || newCollection == null) return true;
187 // // if the collections are object equal, check if the content is equal, too
188 // if(oldCollection.equals(newCollection)){
190 // boolean equal = CollectionUtils.isEqualCollection(oldCollection, newCollection);
191 // // return true when collections are not equal
192 // return equal ? false : true;
198 * Create the elements to be shown in this seciton client area
200 private void renderContent(boolean forceExpansion
)
202 Collection
<ELEMENT
> elements
= getCollection(getEntity());
204 if(elements
== null || elements
.isEmpty()){
205 createEmptyContent();
207 createDynamicContents(elements
);
208 forceExpansion
= true;
211 this.setExpanded(forceExpansion
);
217 * <p>createEmptyContent</p>
219 protected void createEmptyContent(){
220 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
224 * Creates the widgets for the collection
226 * @param elements a {@link java.util.Collection} object.
228 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
231 for(final ELEMENT element
: elements
){
232 SelectionAdapter removeListener
= new SelectionAdapter(){
234 public void widgetSelected(SelectionEvent e
) {
235 removeElement(element
);
236 internalUpdateSection(true);
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
));
246 * Create the specific widget for the element
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.
252 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
253 AbstractEntityCollectionElement formElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
257 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
261 public void setBackground(Color color
) {
262 if(label_empty
!= null && !label_empty
.isDisposed()){
263 label_empty
.setBackground(color
);
265 super.setBackground(color
);
269 * <p>getTitleString</p>
271 * @return a {@link java.lang.String} object.
273 public String
getTitleString() {
274 return CdmUtils
.Nz(title
);
278 * <p>setTitleString</p>
280 * @param title a {@link java.lang.String} object.
282 public void setTitleString(String title
){
289 public void expansionStateChanging(ExpansionEvent e
) {
290 // logger.warn("Expansion State Changing");
294 public void expansionStateChanged(ExpansionEvent e
) {
296 renderContent(isExpanded());
298 destroyDynamicContent();
302 private boolean expandSectionWhenContentAvailable(){
303 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
307 * Get the specific collection of this entity
309 * @param entity a ENTITY object.
310 * @return a {@link java.util.Collection} object.
312 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
315 * Create a new Element for this collection
317 * @return a ELEMENT object.
319 public abstract ELEMENT
createNewElement();
322 * Add an element to the entities collection
324 * @param element a ELEMENT object.
326 public abstract void addElement(ELEMENT element
);
329 * Remove an element from the entities collection
331 * @param element a ELEMENT object.
333 public abstract void removeElement(ELEMENT element
);
336 * String to display when the collection is empty
338 * @return a {@link java.lang.String} object.
340 public abstract String
getEmptyString();
343 * <p>getTooltipString</p>
345 * @return String to display when hovering the add button
347 protected abstract String
getTooltipString();