4 package eu
.etaxonomy
.taxeditor
.ui
.section
;
6 import java
.util
.Collection
;
7 import java
.util
.EnumSet
;
8 import java
.util
.Observable
;
9 import java
.util
.Observer
;
11 import org
.eclipse
.jface
.action
.Action
;
12 import org
.eclipse
.jface
.action
.IAction
;
13 import org
.eclipse
.jface
.action
.ToolBarManager
;
14 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.events
.DisposeEvent
;
17 import org
.eclipse
.swt
.events
.DisposeListener
;
18 import org
.eclipse
.swt
.events
.SelectionAdapter
;
19 import org
.eclipse
.swt
.events
.SelectionEvent
;
20 import org
.eclipse
.swt
.events
.SelectionListener
;
21 import org
.eclipse
.swt
.graphics
.Color
;
22 import org
.eclipse
.swt
.graphics
.ImageData
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Label
;
26 import org
.eclipse
.ui
.forms
.events
.ExpansionEvent
;
27 import org
.eclipse
.ui
.forms
.events
.IExpansionListener
;
28 import org
.eclipse
.ui
.forms
.widgets
.ExpandableComposite
;
30 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
31 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
32 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
33 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
34 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
35 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
36 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
37 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
38 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
39 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
40 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
42 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
46 * This class visualizes an CDM entity of type ENTITY and additionally provides the functionality to add
47 * other elements of type ELEMENT to them.
49 * @param <ENTITY> A CDM entity which should be visualized by this section.
50 * @param <ELEMENT> An element that can be added (multiple times) to this entity.
56 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> extends AbstractFormSection
<ENTITY
> implements IExpansionListener
, Observer
{
61 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
63 protected Composite container
;
65 private Label label_empty
;
70 * <p>Constructor for AbstractEntityCollectionSection.</p>
73 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
75 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
76 * @param title a {@link java.lang.String} object.
77 * @param <ENTITY> a ENTITY object.
78 * @param <ELEMENT> a ELEMENT object.
80 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
81 super(formFactory
, parentElement
, ExpandableComposite
.CLIENT_INDENT
| style
);
83 this.setText(getTitleString());
86 addExpansionListener(this);
88 CdmStore
.getLoginManager().addObserver(this);
89 addDisposeListener(new DisposeListener() {
91 public void widgetDisposed(DisposeEvent e
) {
92 CdmStore
.getLoginManager().deleteObserver(AbstractEntityCollectionSection
.this);
97 protected Control
createToolbar() {
98 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
100 Action addAction
= new Action("add", IAction
.AS_PUSH_BUTTON
){
102 * @see org.eclipse.jface.action.Action#run()
106 ELEMENT element
= createNewElement();
109 if(! getSection().isExpanded()) {
110 getSection().setExpanded(true);
112 internalUpdateSection(true);
116 addAction
.setImageDescriptor(new ImageDescriptor() {
119 public ImageData
getImageData() {
120 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
123 addAction
.setToolTipText(getTooltipString());
125 toolBarManager
.add(addAction
);
127 return toolBarManager
.createControl(this);
131 * using this method is discouraged, use updateToolBar() instead
133 public void showToolbar(){
134 setTextClient(createToolbar());
138 * using this method is discouraged, use updateToolBar() instead
140 public void removeToolbar(){
147 * @param entity a ENTITY object.
150 public void setEntity(ENTITY entity
) {
152 super.setEntity(entity
);
153 internalUpdateSection(false);
161 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
162 * Override in subclasses if you want to have a different behaviour.
164 protected void setSectionTitle() {
165 if(getCollection(getEntity()) != null && getCollection(getEntity()).size() > 0){
166 this.setText(getTitleString() + " +");
168 this.setText(getTitleString());
173 * Removes all content from the container
175 private void destroyDynamicContent(){
176 if(label_empty
!= null){
177 label_empty
.dispose();
184 * Call this method after dynamically changing the client area.
185 * If the options changed is set to <code>true</code>, will also fire a state changed
186 * event to inform the user of unsaved changes.
188 * @param changed a boolean.
190 protected void internalUpdateSection(boolean changed
){
192 destroyDynamicContent();
193 if(isExpanded() || expandSectionWhenContentAvailable()) {
194 renderContent(isExpanded());
197 firePropertyChangeEvent(this);
202 * Create the elements to be shown in this section client area
204 private void renderContent(boolean forceExpansion
)
206 Collection
<ELEMENT
> elements
= getCollection(getEntity());
208 if(elements
== null || elements
.isEmpty()){
209 createEmptyContent();
211 createDynamicContents(elements
);
212 forceExpansion
= true;
215 this.setExpanded(forceExpansion
);
221 * <p>createEmptyContent</p>
223 protected void createEmptyContent(){
224 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
228 * Creates the widgets for the collection
230 * @param elements a {@link java.util.Collection} object.
232 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
235 for(final ELEMENT element
: elements
){
236 SelectionAdapter removeListener
= new SelectionAdapter(){
238 public void widgetSelected(SelectionEvent e
) {
239 removeElement(element
);
240 internalUpdateSection(true);
243 boolean modulo
= i
++%2 == 0;
244 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
245 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
250 * Create the specific widget for the element
252 * @param element a ELEMENT object.
253 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
254 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
256 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
257 formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
261 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
265 public void setBackground(Color color
) {
266 if(label_empty
!= null && !label_empty
.isDisposed()){
267 label_empty
.setBackground(color
);
269 super.setBackground(color
);
273 * <p>getTitleString</p>
275 * @return a {@link java.lang.String} object.
277 public String
getTitleString() {
278 return CdmUtils
.Nz(title
);
282 * <p>setTitleString</p>
284 * @param title a {@link java.lang.String} object.
286 public void setTitleString(String title
){
294 public void expansionStateChanging(ExpansionEvent e
) {
295 // logger.warn("Expansion State Changing");
300 public void expansionStateChanged(ExpansionEvent e
) {
302 renderContent(isExpanded());
304 destroyDynamicContent();
308 private boolean expandSectionWhenContentAvailable(){
309 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
313 * Remove an element from the entities collection and update the section
315 * @param element a ELEMENT object.
317 public void removeElementAndUpdate(ELEMENT element
) {
318 removeElement(element
);
319 internalUpdateSection(true);
323 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
326 public void update(Observable o
, Object arg
){
327 if(o
instanceof LoginManager
){
332 private void updateToolbar() {
333 if(getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), UPDATE
)){
341 * Get the specific collection of this entity
343 * @param entity a ENTITY object.
344 * @return a {@link java.util.Collection} object.
346 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
349 * Create a new Element for this collection
351 * @return a ELEMENT object.
353 public abstract ELEMENT
createNewElement();
356 * Add an element to the entities collection
358 * @param element a ELEMENT object.
360 public abstract void addElement(ELEMENT element
);
363 * Remove an element from the entities collection
365 * @param element a ELEMENT object.
367 public abstract void removeElement(ELEMENT element
);
370 * String to display when the collection is empty
372 * @return a {@link java.lang.String} object.
374 public abstract String
getEmptyString();
377 * <p>getTooltipString</p>
379 * @return String to display when hovering the add button
381 protected abstract String
getTooltipString();