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
.CdmPropertyChangeEvent
;
35 import eu
.etaxonomy
.taxeditor
.ui
.forms
.ICdmFormElement
;
38 * <p>Abstract AbstractEntityCollectionSection class.</p>
43 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> extends AbstractFormSection
<ENTITY
> implements IExpansionListener
{
45 protected Composite container
;
47 private Label label_empty
;
52 * <p>Constructor for AbstractEntityCollectionSection.</p>
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
57 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
58 * @param title a {@link java.lang.String} object.
59 * @param <ENTITY> a ENTITY object.
60 * @param <ELEMENT> a ELEMENT object.
62 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
63 super(formFactory
, conversation
, parentElement
, Section
.CLIENT_INDENT
| style
);
65 this.setText(getTitleString());
68 addExpansionListener(this);
71 private Control
createToolbar() {
72 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
74 Action addAction
= new Action("add", Action
.AS_PUSH_BUTTON
){
76 * @see org.eclipse.jface.action.Action#run()
80 ELEMENT element
= createNewElement();
83 if(! getSection().isExpanded())
84 getSection().setExpanded(true);
85 internalUpdateSection(true);
89 addAction
.setImageDescriptor(new ImageDescriptor() {
92 public ImageData
getImageData() {
93 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
96 addAction
.setToolTipText(getTooltipString());
98 toolBarManager
.add(addAction
);
100 return toolBarManager
.createControl(this);
103 public void showToolbar(){
104 setTextClient(createToolbar());
107 public void removeToolbar(){
114 * @param entity a ENTITY object.
117 public void setEntity(ENTITY entity
) {
118 if(entity
!= null && hasCollectionChanged(entity
)){
119 super.setEntity(entity
);
120 internalUpdateSection(false);
127 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
128 * Override in subclasses if you want to have a different behaviour.
130 protected void setSectionTitle() {
131 if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
132 this.setText(getTitleString() + " +");
134 this.setText(getTitleString());
139 * Removes all content from the container
141 private void destroyDynamicContent(){
142 if(label_empty
!= null){
143 label_empty
.dispose();
150 * Call this method after dynamically changing the client area.
151 * If the options changed is set to true, will also fire a state changed
152 * event to inform the user of unsaved changes.
154 * @param changed a boolean.
156 protected void internalUpdateSection(boolean changed
){
157 destroyDynamicContent();
158 if(isExpanded() || expandSectionWhenContentAvailable())
159 renderContent(isExpanded());
161 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
165 * Whether the entities specific collection changed
170 private boolean hasCollectionChanged(ENTITY newEntity
){
172 // return true on null
173 if(getEntity() == null || newEntity
== null) return true;
175 // if the entities differ the collection has changed
176 if(! getEntity().equals(newEntity
)) return true;
178 Collection
<ELEMENT
> oldCollection
= getCollection(getEntity());
179 Collection
<ELEMENT
> newCollection
= getCollection(newEntity
);
181 // return true on null
182 if(oldCollection
== null || newCollection
== null) return true;
184 // if the collections are object equal, check if the content is equal, too
185 if(oldCollection
.equals(newCollection
)){
187 boolean equal
= CollectionUtils
.isEqualCollection(oldCollection
, newCollection
);
188 // return true when collections are not equal
189 return equal ?
false : true;
195 * Create the elements to be shown in this seciton client area
197 private void renderContent(boolean forceExpansion
)
199 Collection
<ELEMENT
> elements
= getCollection(getEntity());
201 if(elements
== null || elements
.isEmpty()){
202 createEmptyContent();
204 createDynamicContents(elements
);
205 forceExpansion
= true;
208 this.setExpanded(forceExpansion
);
214 * <p>createEmptyContent</p>
216 protected void createEmptyContent(){
217 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
221 * Creates the widgets for the collection
223 * @param elements a {@link java.util.Collection} object.
225 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
228 for(final ELEMENT element
: elements
){
229 SelectionAdapter removeListener
= new SelectionAdapter(){
231 public void widgetSelected(SelectionEvent e
) {
232 removeElement(element
);
233 internalUpdateSection(true);
236 boolean modulo
= i
++%2 == 0;
237 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
238 createElementComposite(element
, removeListener
, StoreUtil
.getColor(colorResource
));
243 * Create the specific widget for the element
245 * @param element a ELEMENT object.
246 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
247 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
249 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
250 AbstractEntityCollectionElement formElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
254 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
258 public void setBackground(Color color
) {
259 if(label_empty
!= null && !label_empty
.isDisposed()){
260 label_empty
.setBackground(color
);
262 super.setBackground(color
);
266 * <p>getTitleString</p>
268 * @return a {@link java.lang.String} object.
270 public String
getTitleString() {
271 return CdmUtils
.Nz(title
);
275 * <p>setTitleString</p>
277 * @param title a {@link java.lang.String} object.
279 public void setTitleString(String title
){
286 public void expansionStateChanging(ExpansionEvent e
) {
287 // logger.warn("Expansion State Changing");
291 public void expansionStateChanged(ExpansionEvent e
) {
293 renderContent(isExpanded());
295 destroyDynamicContent();
299 private boolean expandSectionWhenContentAvailable(){
300 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
304 * Get the specific collection of this entity
306 * @param entity a ENTITY object.
307 * @return a {@link java.util.Collection} object.
309 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
312 * Create a new Element for this collection
314 * @return a ELEMENT object.
316 public abstract ELEMENT
createNewElement();
319 * Add an element to the entities collection
321 * @param element a ELEMENT object.
323 public abstract void addElement(ELEMENT element
);
326 * Remove an element from the entities collection
328 * @param element a ELEMENT object.
330 public abstract void removeElement(ELEMENT element
);
333 * String to display when the collection is empty
335 * @return a {@link java.lang.String} object.
337 public abstract String
getEmptyString();
340 * <p>getTooltipString</p>
342 * @return String to display when hovering the add button
344 protected abstract String
getTooltipString();