4 package eu
.etaxonomy
.taxeditor
.ui
.section
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collection
;
8 import java
.util
.Collections
;
9 import java
.util
.Comparator
;
10 import java
.util
.EnumSet
;
11 import java
.util
.List
;
12 import java
.util
.Observable
;
13 import java
.util
.Observer
;
15 import org
.eclipse
.jface
.action
.Action
;
16 import org
.eclipse
.jface
.action
.IAction
;
17 import org
.eclipse
.jface
.action
.ToolBarManager
;
18 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.DisposeEvent
;
21 import org
.eclipse
.swt
.events
.DisposeListener
;
22 import org
.eclipse
.swt
.events
.SelectionAdapter
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.events
.SelectionListener
;
25 import org
.eclipse
.swt
.graphics
.Color
;
26 import org
.eclipse
.swt
.graphics
.ImageData
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Control
;
29 import org
.eclipse
.swt
.widgets
.Label
;
30 import org
.eclipse
.ui
.forms
.events
.ExpansionEvent
;
31 import org
.eclipse
.ui
.forms
.events
.IExpansionListener
;
32 import org
.eclipse
.ui
.forms
.widgets
.ExpandableComposite
;
34 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
35 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
36 import eu
.etaxonomy
.cdm
.model
.permission
.CRUD
;
37 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
38 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
39 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
40 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
42 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
44 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
45 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
46 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
47 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
50 * This class visualizes an CDM entity of type ENTITY and additionally provides the functionality to add
51 * other elements of type ELEMENT to them.
53 * @param <ENTITY> A CDM entity which should be visualized by this section.
54 * @param <ELEMENT> An element that can be added (multiple times) to this entity.
60 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> extends AbstractFormSection
<ENTITY
> implements IExpansionListener
, Observer
{
62 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
64 protected Composite container
;
66 private Label label_empty
;
70 private AbstractEntityCollectionElement
<ENTITY
> entityCollectionElement
;
72 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
73 super(formFactory
, parentElement
, ExpandableComposite
.CLIENT_INDENT
| style
);
75 this.setText(getTitleString());
78 addExpansionListener(this);
80 CdmStore
.getLoginManager().addObserver(this);
81 addDisposeListener(new DisposeListener() {
83 public void widgetDisposed(DisposeEvent e
) {
84 CdmStore
.getLoginManager().deleteObserver(AbstractEntityCollectionSection
.this);
89 protected Control
createToolbar() {
90 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
92 Action addAction
= new Action("Add", IAction
.AS_PUSH_BUTTON
){
95 ELEMENT element
= createNewElement();
98 if(! getSection().isExpanded()) {
99 getSection().setExpanded(true);
101 internalUpdateSection(true);
105 addAction
.setImageDescriptor(new ImageDescriptor() {
108 public ImageData
getImageData() {
109 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
112 addAction
.setToolTipText(getTooltipString());
114 Action browseAction
= null;
115 if(allowAddExisting()){
116 browseAction
= new Action("Browse", IAction
.AS_PUSH_BUTTON
){
119 ELEMENT element
= addExisting();
122 if(! getSection().isExpanded()) {
123 getSection().setExpanded(true);
125 internalUpdateSection(true);
129 browseAction
.setImageDescriptor(new ImageDescriptor() {
132 public ImageData
getImageData() {
133 return ImageResources
.getImage(ImageResources
.BROWSE_ICON
).getImageData();
136 browseAction
.setToolTipText("Browse");
139 toolBarManager
.add(addAction
);
140 if(browseAction
!=null){
141 toolBarManager
.add(browseAction
);
144 addAction(toolBarManager
);
146 return toolBarManager
.createControl(this);
149 protected void addAction(ToolBarManager toolBarManager
) {
150 // default implementation empty
154 * using this method is discouraged, use updateToolBar() instead
156 public void showToolbar(){
157 setTextClient(createToolbar());
161 * using this method is discouraged, use updateToolBar() instead
163 public void removeToolbar(){
168 public void setEntity(ENTITY entity
) {
170 super.setEntity(entity
);
171 internalUpdateSection(false);
179 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
180 * Override in subclasses if you want to have a different behaviour.
182 protected void setSectionTitle() {
183 Collection
<ELEMENT
> collection
= getCollection(getEntity());
184 if(collection
!= null && collection
.size() > 0){
185 this.setText(getTitleString() + " +");
187 this.setText(getTitleString());
192 * Removes all content from the container
194 private void destroyDynamicContent(){
195 if(label_empty
!= null){
196 label_empty
.dispose();
203 * Call this method after dynamically changing the client area.
204 * If the options changed is set to <code>true</code>, will also fire a state changed
205 * event to inform the user of unsaved changes.
207 * @param changed a boolean.
209 protected void internalUpdateSection(boolean changed
){
211 destroyDynamicContent();
212 if(isExpanded() || expandSectionWhenContentAvailable()) {
213 renderContent(isExpanded());
216 firePropertyChangeEvent(this);
221 * Create the elements to be shown in this section client area
223 private void renderContent(boolean forceExpansion
)
225 Collection
<ELEMENT
> collection
= getCollection(getEntity());
227 if(collection
== null || collection
.isEmpty()){
228 createEmptyContent();
230 List
<ELEMENT
> elements
= new ArrayList
<>(collection
);
231 Collections
.sort(elements
, getComparator());
232 createDynamicContents(elements
);
233 forceExpansion
= true;
236 this.setExpanded(forceExpansion
);
241 protected void createEmptyContent(){
242 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
246 * Creates the widgets for the collection
248 * @param elements a {@link java.util.Collection} object.
250 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
253 for(final ELEMENT element
: elements
){
254 SelectionAdapter removeListener
= new SelectionAdapter(){
256 public void widgetSelected(SelectionEvent e
) {
257 removeElement(element
);
258 internalUpdateSection(true);
261 boolean modulo
= i
++%2 == 0;
262 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
263 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
268 * Create the specific widget for the element
270 * @param element a ELEMENT object.
271 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
272 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
274 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
275 entityCollectionElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
280 public void setBackground(Color color
) {
281 if(label_empty
!= null && !label_empty
.isDisposed()){
282 label_empty
.setBackground(color
);
284 super.setBackground(color
);
288 * <p>getTitleString</p>
290 * @return a {@link java.lang.String} object.
292 public String
getTitleString() {
293 return CdmUtils
.Nz(title
);
297 * <p>setTitleString</p>
299 * @param title a {@link java.lang.String} object.
301 public void setTitleString(String title
){
309 public void expansionStateChanging(ExpansionEvent e
) {
310 // logger.warn("Expansion State Changing");
315 public void expansionStateChanged(ExpansionEvent e
) {
317 renderContent(isExpanded());
319 destroyDynamicContent();
323 private boolean expandSectionWhenContentAvailable(){
324 return PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
, true);
328 * Remove an element from the entities collection and update the section
330 * @param element a ELEMENT object.
332 public void removeElementAndUpdate(ELEMENT element
) {
333 removeElement(element
);
334 internalUpdateSection(true);
338 public void update(Observable o
, Object arg
){
339 if(o
instanceof LoginManager
){
344 protected void updateToolbar() {
345 if(getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), UPDATE
)){
352 public AbstractEntityCollectionElement
<ENTITY
> getEntityCollectionElement() {
353 return entityCollectionElement
;
357 * Returns the {@link Comparator} specific for the ELEMENTs
358 * which is used to sort the elements
359 * @return the comparator for ELEMENT
361 public abstract Comparator
<ELEMENT
> getComparator();
364 * Get the specific collection of this entity
366 * @param entity a ENTITY object.
367 * @return a {@link java.util.Collection} object.
369 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
373 * Create a new Element for this collection
375 * @return a ELEMENT object.
377 public abstract ELEMENT
createNewElement();
380 * Add an element to the entities collection
382 * @param element a ELEMENT object.
384 public abstract void addElement(ELEMENT element
);
387 * Add an existing element to the entities collection.
388 * @return the existing element
390 public abstract ELEMENT
addExisting();
393 * If <code>true</code> the section will also display
394 * a browse icon to choose from existing elements.
396 * <b>Note:</b> when returning true you have to make sure
397 * to implement the {@link #addExisting()} method
398 * @return true if existing entities can be added;
401 public abstract boolean allowAddExisting();
404 * Remove an element from the entities collection
406 * @param element a ELEMENT object.
408 public abstract void removeElement(ELEMENT element
);
411 * String to display when the collection is empty
413 * @return a {@link java.lang.String} object.
415 public abstract String
getEmptyString();
418 * <p>getTooltipString</p>
420 * @return String to display when hovering the add button
422 protected abstract String
getTooltipString();