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
{
58 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
60 protected Composite container
;
62 private Label label_empty
;
66 private AbstractEntityCollectionElement entityCollectionElement
;
68 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
69 super(formFactory
, parentElement
, ExpandableComposite
.CLIENT_INDENT
| style
);
71 this.setText(getTitleString());
74 addExpansionListener(this);
76 CdmStore
.getLoginManager().addObserver(this);
77 addDisposeListener(new DisposeListener() {
79 public void widgetDisposed(DisposeEvent e
) {
80 CdmStore
.getLoginManager().deleteObserver(AbstractEntityCollectionSection
.this);
85 protected Control
createToolbar() {
86 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
88 Action addAction
= new Action("Add", IAction
.AS_PUSH_BUTTON
){
91 ELEMENT element
= createNewElement();
94 if(! getSection().isExpanded()) {
95 getSection().setExpanded(true);
97 internalUpdateSection(true);
101 addAction
.setImageDescriptor(new ImageDescriptor() {
104 public ImageData
getImageData() {
105 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
108 addAction
.setToolTipText(getTooltipString());
110 Action browseAction
= null;
111 if(allowAddExisting()){
112 browseAction
= new Action("Browse", IAction
.AS_PUSH_BUTTON
){
115 ELEMENT element
= addExisting();
118 if(! getSection().isExpanded()) {
119 getSection().setExpanded(true);
121 internalUpdateSection(true);
125 browseAction
.setImageDescriptor(new ImageDescriptor() {
128 public ImageData
getImageData() {
129 return ImageResources
.getImage(ImageResources
.BROWSE_ICON
).getImageData();
132 browseAction
.setToolTipText("Browse");
135 toolBarManager
.add(addAction
);
136 if(browseAction
!=null){
137 toolBarManager
.add(browseAction
);
140 return toolBarManager
.createControl(this);
144 * using this method is discouraged, use updateToolBar() instead
146 public void showToolbar(){
147 setTextClient(createToolbar());
151 * using this method is discouraged, use updateToolBar() instead
153 public void removeToolbar(){
158 public void setEntity(ENTITY entity
) {
160 super.setEntity(entity
);
161 internalUpdateSection(false);
169 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
170 * Override in subclasses if you want to have a different behaviour.
172 protected void setSectionTitle() {
173 Collection
<ELEMENT
> collection
= getCollection(getEntity());
174 if(collection
!= null && collection
.size() > 0){
175 this.setText(getTitleString() + " +");
177 this.setText(getTitleString());
182 * Removes all content from the container
184 private void destroyDynamicContent(){
185 if(label_empty
!= null){
186 label_empty
.dispose();
193 * Call this method after dynamically changing the client area.
194 * If the options changed is set to <code>true</code>, will also fire a state changed
195 * event to inform the user of unsaved changes.
197 * @param changed a boolean.
199 protected void internalUpdateSection(boolean changed
){
201 destroyDynamicContent();
202 if(isExpanded() || expandSectionWhenContentAvailable()) {
203 renderContent(isExpanded());
206 firePropertyChangeEvent(this);
211 * Create the elements to be shown in this section client area
213 private void renderContent(boolean forceExpansion
)
215 Collection
<ELEMENT
> elements
= getCollection(getEntity());
217 if(elements
== null || elements
.isEmpty()){
218 createEmptyContent();
220 createDynamicContents(elements
);
221 forceExpansion
= true;
224 this.setExpanded(forceExpansion
);
229 protected void createEmptyContent(){
230 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
234 * Creates the widgets for the collection
236 * @param elements a {@link java.util.Collection} object.
238 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
241 for(final ELEMENT element
: elements
){
242 SelectionAdapter removeListener
= new SelectionAdapter(){
244 public void widgetSelected(SelectionEvent e
) {
245 removeElement(element
);
246 internalUpdateSection(true);
249 boolean modulo
= i
++%2 == 0;
250 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
251 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
256 * Create the specific widget for the element
258 * @param element a ELEMENT object.
259 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
260 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
262 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
263 entityCollectionElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
268 public void setBackground(Color color
) {
269 if(label_empty
!= null && !label_empty
.isDisposed()){
270 label_empty
.setBackground(color
);
272 super.setBackground(color
);
276 * <p>getTitleString</p>
278 * @return a {@link java.lang.String} object.
280 public String
getTitleString() {
281 return CdmUtils
.Nz(title
);
285 * <p>setTitleString</p>
287 * @param title a {@link java.lang.String} object.
289 public void setTitleString(String title
){
297 public void expansionStateChanging(ExpansionEvent e
) {
298 // logger.warn("Expansion State Changing");
303 public void expansionStateChanged(ExpansionEvent e
) {
305 renderContent(isExpanded());
307 destroyDynamicContent();
311 private boolean expandSectionWhenContentAvailable(){
312 return PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
316 * Remove an element from the entities collection and update the section
318 * @param element a ELEMENT object.
320 public void removeElementAndUpdate(ELEMENT element
) {
321 removeElement(element
);
322 internalUpdateSection(true);
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
)){
340 public AbstractEntityCollectionElement
getEntityCollectionElement() {
341 return entityCollectionElement
;
346 * Get the specific collection of this entity
348 * @param entity a ENTITY object.
349 * @return a {@link java.util.Collection} object.
351 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
354 * Create a new Element for this collection
356 * @return a ELEMENT object.
358 public abstract ELEMENT
createNewElement();
361 * Add an element to the entities collection
363 * @param element a ELEMENT object.
365 public abstract void addElement(ELEMENT element
);
368 * Add an existing element to the entities collection.
369 * @return the existing element
371 public abstract ELEMENT
addExisting();
374 * If <code>true</code> the section will also display
375 * a browse icon to choose from existing elements.
377 * <b>Note:</b> when returning true you have to make sure
378 * to implement the {@link #addExisting()} method
379 * @return true if existing entities can be added;
382 public abstract boolean allowAddExisting();
385 * Remove an element from the entities collection
387 * @param element a ELEMENT object.
389 public abstract void removeElement(ELEMENT element
);
392 * String to display when the collection is empty
394 * @return a {@link java.lang.String} object.
396 public abstract String
getEmptyString();
399 * <p>getTooltipString</p>
401 * @return String to display when hovering the add button
403 protected abstract String
getTooltipString();