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
.IAction
;
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
.ExpandableComposite
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
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
.ui
.element
.AbstractFormSection
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
37 * This class visualizes an CDM entity of type ENTITY and additionally provides the functionality to add
38 * other elements of type ELEMENT to them.
40 * @param <ENTITY> A CDM entity which should be visualized by this section.
41 * @param <ELEMENT> An element that can be added (multiple times) to this entity.
47 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> extends AbstractFormSection
<ENTITY
> implements IExpansionListener
{
49 protected Composite container
;
51 private Label label_empty
;
56 * <p>Constructor for AbstractEntityCollectionSection.</p>
59 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
61 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
62 * @param title a {@link java.lang.String} object.
63 * @param <ENTITY> a ENTITY object.
64 * @param <ELEMENT> a ELEMENT object.
66 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
67 super(formFactory
, parentElement
, ExpandableComposite
.CLIENT_INDENT
| style
);
69 this.setText(getTitleString());
72 addExpansionListener(this);
75 protected Control
createToolbar() {
76 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
78 Action addAction
= new Action("add", IAction
.AS_PUSH_BUTTON
){
80 * @see org.eclipse.jface.action.Action#run()
84 ELEMENT element
= createNewElement();
87 if(! getSection().isExpanded()) {
88 getSection().setExpanded(true);
90 internalUpdateSection(true);
94 addAction
.setImageDescriptor(new ImageDescriptor() {
97 public ImageData
getImageData() {
98 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
101 addAction
.setToolTipText(getTooltipString());
103 toolBarManager
.add(addAction
);
105 return toolBarManager
.createControl(this);
108 public void showToolbar(){
109 setTextClient(createToolbar());
112 public void removeToolbar(){
119 * @param entity a ENTITY object.
122 public void setEntity(ENTITY entity
) {
124 super.setEntity(entity
);
125 internalUpdateSection(false);
132 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
133 * Override in subclasses if you want to have a different behaviour.
135 protected void setSectionTitle() {
136 if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
137 this.setText(getTitleString() + " +");
139 this.setText(getTitleString());
144 * Removes all content from the container
146 private void destroyDynamicContent(){
147 if(label_empty
!= null){
148 label_empty
.dispose();
155 * Call this method after dynamically changing the client area.
156 * If the options changed is set to true, will also fire a state changed
157 * event to inform the user of unsaved changes.
159 * @param changed a boolean.
161 protected void internalUpdateSection(boolean changed
){
162 destroyDynamicContent();
163 if(isExpanded() || expandSectionWhenContentAvailable()) {
164 renderContent(isExpanded());
167 firePropertyChangeEvent(this);
172 * Create the elements to be shown in this section client area
174 private void renderContent(boolean forceExpansion
)
176 Collection
<ELEMENT
> elements
= getCollection(getEntity());
178 if(elements
== null || elements
.isEmpty()){
179 createEmptyContent();
181 createDynamicContents(elements
);
182 forceExpansion
= true;
185 this.setExpanded(forceExpansion
);
191 * <p>createEmptyContent</p>
193 protected void createEmptyContent(){
194 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
198 * Creates the widgets for the collection
200 * @param elements a {@link java.util.Collection} object.
202 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
205 for(final ELEMENT element
: elements
){
206 SelectionAdapter removeListener
= new SelectionAdapter(){
208 public void widgetSelected(SelectionEvent e
) {
209 removeElement(element
);
210 internalUpdateSection(true);
213 boolean modulo
= i
++%2 == 0;
214 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
215 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
220 * Create the specific widget for the element
222 * @param element a ELEMENT object.
223 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
224 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
226 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
227 formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
231 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
235 public void setBackground(Color color
) {
236 if(label_empty
!= null && !label_empty
.isDisposed()){
237 label_empty
.setBackground(color
);
239 super.setBackground(color
);
243 * <p>getTitleString</p>
245 * @return a {@link java.lang.String} object.
247 public String
getTitleString() {
248 return CdmUtils
.Nz(title
);
252 * <p>setTitleString</p>
254 * @param title a {@link java.lang.String} object.
256 public void setTitleString(String title
){
264 public void expansionStateChanging(ExpansionEvent e
) {
265 // logger.warn("Expansion State Changing");
270 public void expansionStateChanged(ExpansionEvent e
) {
272 renderContent(isExpanded());
274 destroyDynamicContent();
278 private boolean expandSectionWhenContentAvailable(){
279 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
283 * Get the specific collection of this entity
285 * @param entity a ENTITY object.
286 * @return a {@link java.util.Collection} object.
288 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
291 * Create a new Element for this collection
293 * @return a ELEMENT object.
295 public abstract ELEMENT
createNewElement();
298 * Add an element to the entities collection
300 * @param element a ELEMENT object.
302 public abstract void addElement(ELEMENT element
);
305 * Remove an element from the entities collection
307 * @param element a ELEMENT object.
309 public abstract void removeElement(ELEMENT element
);
312 * String to display when the collection is empty
314 * @return a {@link java.lang.String} object.
316 public abstract String
getEmptyString();
319 * <p>getTooltipString</p>
321 * @return String to display when hovering the add button
323 protected abstract String
getTooltipString();