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 true, 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
){
191 destroyDynamicContent();
192 if(isExpanded() || expandSectionWhenContentAvailable()) {
193 renderContent(isExpanded());
196 firePropertyChangeEvent(this);
201 * Create the elements to be shown in this section client area
203 private void renderContent(boolean forceExpansion
)
205 Collection
<ELEMENT
> elements
= getCollection(getEntity());
207 if(elements
== null || elements
.isEmpty()){
208 createEmptyContent();
210 createDynamicContents(elements
);
211 forceExpansion
= true;
214 this.setExpanded(forceExpansion
);
220 * <p>createEmptyContent</p>
222 protected void createEmptyContent(){
223 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
227 * Creates the widgets for the collection
229 * @param elements a {@link java.util.Collection} object.
231 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
234 for(final ELEMENT element
: elements
){
235 SelectionAdapter removeListener
= new SelectionAdapter(){
237 public void widgetSelected(SelectionEvent e
) {
238 removeElement(element
);
239 internalUpdateSection(true);
242 boolean modulo
= i
++%2 == 0;
243 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
244 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
249 * Create the specific widget for the element
251 * @param element a ELEMENT object.
252 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
253 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
255 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
256 formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
260 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEditorFormSection#setBackground(org.eclipse.swt.graphics.Color)
264 public void setBackground(Color color
) {
265 if(label_empty
!= null && !label_empty
.isDisposed()){
266 label_empty
.setBackground(color
);
268 super.setBackground(color
);
272 * <p>getTitleString</p>
274 * @return a {@link java.lang.String} object.
276 public String
getTitleString() {
277 return CdmUtils
.Nz(title
);
281 * <p>setTitleString</p>
283 * @param title a {@link java.lang.String} object.
285 public void setTitleString(String title
){
293 public void expansionStateChanging(ExpansionEvent e
) {
294 // logger.warn("Expansion State Changing");
299 public void expansionStateChanged(ExpansionEvent e
) {
301 renderContent(isExpanded());
303 destroyDynamicContent();
307 private boolean expandSectionWhenContentAvailable(){
308 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
312 * Remove an element from the entities collection and update the section
314 * @param element a ELEMENT object.
316 public void removeElementAndUpdate(ELEMENT element
) {
317 removeElement(element
);
318 internalUpdateSection(true);
322 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
325 public void update(Observable o
, Object arg
){
326 if(o
instanceof LoginManager
){
331 private void updateToolbar() {
332 if(getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), UPDATE
)){
340 * Get the specific collection of this entity
342 * @param entity a ENTITY object.
343 * @return a {@link java.util.Collection} object.
345 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
348 * Create a new Element for this collection
350 * @return a ELEMENT object.
352 public abstract ELEMENT
createNewElement();
355 * Add an element to the entities collection
357 * @param element a ELEMENT object.
359 public abstract void addElement(ELEMENT element
);
362 * Remove an element from the entities collection
364 * @param element a ELEMENT object.
366 public abstract void removeElement(ELEMENT element
);
369 * String to display when the collection is empty
371 * @return a {@link java.lang.String} object.
373 public abstract String
getEmptyString();
376 * <p>getTooltipString</p>
378 * @return String to display when hovering the add button
380 protected abstract String
getTooltipString();