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
.cdm
.model
.common
.IVersionableEntity
;
28 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
29 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
30 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
31 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
32 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
33 import eu
.etaxonomy
.taxeditor
.ui
.forms
.AbstractFormSection
;
34 import eu
.etaxonomy
.taxeditor
.ui
.forms
.CdmFormFactory
;
35 import eu
.etaxonomy
.taxeditor
.ui
.forms
.CdmPropertyChangeEvent
;
36 import eu
.etaxonomy
.taxeditor
.ui
.forms
.ICdmFormElement
;
39 * <p>Abstract AbstractEntityCollectionSection class.</p>
44 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> extends AbstractFormSection
<ENTITY
> implements IExpansionListener
{
46 protected Composite container
;
48 private Label label_empty
;
53 * <p>Constructor for AbstractEntityCollectionSection.</p>
56 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
58 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
59 * @param title a {@link java.lang.String} object.
60 * @param <ENTITY> a ENTITY object.
61 * @param <ELEMENT> a ELEMENT object.
63 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
64 super(formFactory
, conversation
, parentElement
, Section
.CLIENT_INDENT
| style
);
66 this.setText(getTitleString());
67 setTextClient(createToolbar());
69 addExpansionListener(this);
72 private Control
createToolbar() {
73 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
75 Action addAction
= new Action("add", Action
.AS_PUSH_BUTTON
){
77 * @see org.eclipse.jface.action.Action#run()
81 ELEMENT element
= createNewElement();
84 if(! getSection().isExpanded())
85 getSection().setExpanded(true);
86 internalUpdateSection(true);
90 addAction
.setImageDescriptor(new ImageDescriptor() {
93 public ImageData
getImageData() {
94 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
97 addAction
.setToolTipText(getTooltipString());
99 toolBarManager
.add(addAction
);
101 return toolBarManager
.createControl(this);
107 * @param entity a ENTITY object.
109 public void setEntity(ENTITY entity
) {
110 if(entity
!= null && hasCollectionChanged(entity
)){
111 super.setEntity(entity
);
112 internalUpdateSection(false);
119 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
120 * Override in subclasses if you want to have a different behaviour.
122 protected void setSectionTitle() {
123 if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
124 this.setText(getTitleString() + " +");
126 this.setText(getTitleString());
131 * Removes all content from the container
133 private void destroyDynamicContent(){
134 if(label_empty
!= null){
135 label_empty
.dispose();
142 * Call this method after dynamically changing the client area.
143 * If the options changed is set to true, will also fire a state changed
144 * event to inform the user of unsaved changes.
146 * @param changed a boolean.
148 protected void internalUpdateSection(boolean changed
){
149 destroyDynamicContent();
150 if(isExpanded() || expandSectionWhenContentAvailable())
151 renderContent(isExpanded());
153 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
157 * Whether the entities specific collection changed
162 private boolean hasCollectionChanged(ENTITY newEntity
){
164 // return true on null
165 if(getEntity() == null || newEntity
== null) return true;
167 // if the entities differ the collection has changed
168 if(! getEntity().equals(newEntity
)) return true;
170 Collection
<ELEMENT
> oldCollection
= getCollection(getEntity());
171 Collection
<ELEMENT
> newCollection
= getCollection(newEntity
);
173 // return true on null
174 if(oldCollection
== null || newCollection
== null) return true;
176 // if the collections are object equal, check if the content is equal, too
177 if(oldCollection
.equals(newCollection
)){
179 boolean equal
= CollectionUtils
.isEqualCollection(oldCollection
, newCollection
);
180 // return true when collections are not equal
181 return equal ?
false : true;
187 * Create the elements to be shown in this seciton client area
189 private void renderContent(boolean forceExpansion
)
191 Collection
<ELEMENT
> elements
= getCollection(getEntity());
193 if(elements
== null || elements
.size() == 0){
194 createEmptyContent();
196 createDynamicContents(elements
);
197 forceExpansion
= true;
200 this.setExpanded(forceExpansion
);
206 * <p>createEmptyContent</p>
208 protected void createEmptyContent(){
209 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
213 * Creates the widgets for the collection
215 * @param elements a {@link java.util.Collection} object.
217 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
220 for(final ELEMENT element
: elements
){
221 SelectionAdapter removeListener
= new SelectionAdapter(){
223 public void widgetSelected(SelectionEvent e
) {
224 removeElement(element
);
225 internalUpdateSection(true);
228 boolean modulo
= i
++%2 == 0;
229 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
230 createElementComposite(element
, removeListener
, StoreUtil
.getColor(colorResource
));
235 * Create the specific widget for the element
237 * @param element a ELEMENT object.
238 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
239 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
241 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
242 AbstractEntityCollectionElement formElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
246 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
250 public void setBackground(Color color
) {
251 if(label_empty
!= null && !label_empty
.isDisposed()){
252 label_empty
.setBackground(color
);
254 super.setBackground(color
);
258 * <p>getTitleString</p>
260 * @return a {@link java.lang.String} object.
262 public String
getTitleString() {
263 return CdmUtils
.Nz(title
);
267 * <p>setTitleString</p>
269 * @param title a {@link java.lang.String} object.
271 public void setTitleString(String title
){
276 public void expansionStateChanging(ExpansionEvent e
) {
277 // logger.warn("Expansion State Changing");
281 public void expansionStateChanged(ExpansionEvent e
) {
283 renderContent(isExpanded());
285 destroyDynamicContent();
289 private boolean expandSectionWhenContentAvailable(){
290 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
294 * Get the specific collection of this entity
296 * @param entity a ENTITY object.
297 * @return a {@link java.util.Collection} object.
299 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
302 * Create a new Element for this collection
304 * @return a ELEMENT object.
306 public abstract ELEMENT
createNewElement();
309 * Add an element to the entities collection
311 * @param element a ELEMENT object.
313 public abstract void addElement(ELEMENT element
);
316 * Remove an element from the entities collection
318 * @param element a ELEMENT object.
320 public abstract void removeElement(ELEMENT element
);
323 * String to display when the collection is empty
325 * @return a {@link java.lang.String} object.
327 public abstract String
getEmptyString();
330 * <p>getTooltipString</p>
332 * @return String to display when hovering the add button
334 protected abstract String
getTooltipString();