4 package eu
.etaxonomy
.taxeditor
.ui
.section
;
6 import java
.util
.Collection
;
8 import org
.eclipse
.jface
.action
.Action
;
9 import org
.eclipse
.jface
.action
.ToolBarManager
;
10 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
11 import org
.eclipse
.swt
.SWT
;
12 import org
.eclipse
.swt
.events
.SelectionAdapter
;
13 import org
.eclipse
.swt
.events
.SelectionEvent
;
14 import org
.eclipse
.swt
.events
.SelectionListener
;
15 import org
.eclipse
.swt
.graphics
.Color
;
16 import org
.eclipse
.swt
.graphics
.ImageData
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.swt
.widgets
.Control
;
19 import org
.eclipse
.swt
.widgets
.Label
;
20 import org
.eclipse
.ui
.forms
.events
.ExpansionEvent
;
21 import org
.eclipse
.ui
.forms
.events
.IExpansionListener
;
22 import org
.eclipse
.ui
.forms
.widgets
.Section
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
25 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
26 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
27 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
28 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
29 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
30 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
36 * <p>Abstract AbstractEntityCollectionSection class.</p>
41 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> extends AbstractFormSection
<ENTITY
> implements IExpansionListener
{
43 protected Composite container
;
45 private Label label_empty
;
50 * <p>Constructor for AbstractEntityCollectionSection.</p>
53 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
55 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
56 * @param title a {@link java.lang.String} object.
57 * @param <ENTITY> a ENTITY object.
58 * @param <ELEMENT> a ELEMENT object.
60 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
61 super(formFactory
, parentElement
, Section
.CLIENT_INDENT
| style
);
63 this.setText(getTitleString());
66 addExpansionListener(this);
69 protected Control
createToolbar() {
70 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
72 Action addAction
= new Action("add", Action
.AS_PUSH_BUTTON
){
74 * @see org.eclipse.jface.action.Action#run()
78 ELEMENT element
= createNewElement();
81 if(! getSection().isExpanded())
82 getSection().setExpanded(true);
83 internalUpdateSection(true);
87 addAction
.setImageDescriptor(new ImageDescriptor() {
90 public ImageData
getImageData() {
91 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
94 addAction
.setToolTipText(getTooltipString());
96 toolBarManager
.add(addAction
);
98 return toolBarManager
.createControl(this);
101 public void showToolbar(){
102 setTextClient(createToolbar());
105 public void removeToolbar(){
112 * @param entity a ENTITY object.
115 public void setEntity(ENTITY entity
) {
117 super.setEntity(entity
);
118 internalUpdateSection(false);
125 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
126 * Override in subclasses if you want to have a different behaviour.
128 protected void setSectionTitle() {
129 if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
130 this.setText(getTitleString() + " +");
132 this.setText(getTitleString());
137 * Removes all content from the container
139 private void destroyDynamicContent(){
140 if(label_empty
!= null){
141 label_empty
.dispose();
148 * Call this method after dynamically changing the client area.
149 * If the options changed is set to true, will also fire a state changed
150 * event to inform the user of unsaved changes.
152 * @param changed a boolean.
154 protected void internalUpdateSection(boolean changed
){
155 destroyDynamicContent();
156 if(isExpanded() || expandSectionWhenContentAvailable())
157 renderContent(isExpanded());
159 firePropertyChangeEvent(this);
163 * Create the elements to be shown in this seciton client area
165 private void renderContent(boolean forceExpansion
)
167 Collection
<ELEMENT
> elements
= getCollection(getEntity());
169 if(elements
== null || elements
.isEmpty()){
170 createEmptyContent();
172 createDynamicContents(elements
);
173 forceExpansion
= true;
176 this.setExpanded(forceExpansion
);
182 * <p>createEmptyContent</p>
184 protected void createEmptyContent(){
185 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
189 * Creates the widgets for the collection
191 * @param elements a {@link java.util.Collection} object.
193 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
196 for(final ELEMENT element
: elements
){
197 SelectionAdapter removeListener
= new SelectionAdapter(){
199 public void widgetSelected(SelectionEvent e
) {
200 removeElement(element
);
201 internalUpdateSection(true);
204 boolean modulo
= i
++%2 == 0;
205 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
206 createElementComposite(element
, removeListener
, StoreUtil
.getColor(colorResource
));
211 * Create the specific widget for the element
213 * @param element a ELEMENT object.
214 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
215 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
217 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
218 formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
222 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
226 public void setBackground(Color color
) {
227 if(label_empty
!= null && !label_empty
.isDisposed()){
228 label_empty
.setBackground(color
);
230 super.setBackground(color
);
234 * <p>getTitleString</p>
236 * @return a {@link java.lang.String} object.
238 public String
getTitleString() {
239 return CdmUtils
.Nz(title
);
243 * <p>setTitleString</p>
245 * @param title a {@link java.lang.String} object.
247 public void setTitleString(String title
){
254 public void expansionStateChanging(ExpansionEvent e
) {
255 // logger.warn("Expansion State Changing");
259 public void expansionStateChanged(ExpansionEvent e
) {
261 renderContent(isExpanded());
263 destroyDynamicContent();
267 private boolean expandSectionWhenContentAvailable(){
268 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
272 * Get the specific collection of this entity
274 * @param entity a ENTITY object.
275 * @return a {@link java.util.Collection} object.
277 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
280 * Create a new Element for this collection
282 * @return a ELEMENT object.
284 public abstract ELEMENT
createNewElement();
287 * Add an element to the entities collection
289 * @param element a ELEMENT object.
291 public abstract void addElement(ELEMENT element
);
294 * Remove an element from the entities collection
296 * @param element a ELEMENT object.
298 public abstract void removeElement(ELEMENT element
);
301 * String to display when the collection is empty
303 * @return a {@link java.lang.String} object.
305 public abstract String
getEmptyString();
308 * <p>getTooltipString</p>
310 * @return String to display when hovering the add button
312 protected abstract String
getTooltipString();