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
.persistence
.hibernate
.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 return toolBarManager
.createControl(this);
148 * using this method is discouraged, use updateToolBar() instead
150 public void showToolbar(){
151 setTextClient(createToolbar());
155 * using this method is discouraged, use updateToolBar() instead
157 public void removeToolbar(){
162 public void setEntity(ENTITY entity
) {
164 super.setEntity(entity
);
165 internalUpdateSection(false);
173 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
174 * Override in subclasses if you want to have a different behaviour.
176 protected void setSectionTitle() {
177 Collection
<ELEMENT
> collection
= getCollection(getEntity());
178 if(collection
!= null && collection
.size() > 0){
179 this.setText(getTitleString() + " +");
181 this.setText(getTitleString());
186 * Removes all content from the container
188 private void destroyDynamicContent(){
189 if(label_empty
!= null){
190 label_empty
.dispose();
197 * Call this method after dynamically changing the client area.
198 * If the options changed is set to <code>true</code>, will also fire a state changed
199 * event to inform the user of unsaved changes.
201 * @param changed a boolean.
203 protected void internalUpdateSection(boolean changed
){
205 destroyDynamicContent();
206 if(isExpanded() || expandSectionWhenContentAvailable()) {
207 renderContent(isExpanded());
210 firePropertyChangeEvent(this);
215 * Create the elements to be shown in this section client area
217 private void renderContent(boolean forceExpansion
)
219 Collection
<ELEMENT
> collection
= getCollection(getEntity());
221 if(collection
== null || collection
.isEmpty()){
222 createEmptyContent();
224 List
<ELEMENT
> elements
= new ArrayList
<>(collection
);
225 Collections
.sort(elements
, getComparator());
226 createDynamicContents(elements
);
227 forceExpansion
= true;
230 this.setExpanded(forceExpansion
);
235 protected void createEmptyContent(){
236 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
240 * Creates the widgets for the collection
242 * @param elements a {@link java.util.Collection} object.
244 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
247 for(final ELEMENT element
: elements
){
248 SelectionAdapter removeListener
= new SelectionAdapter(){
250 public void widgetSelected(SelectionEvent e
) {
251 removeElement(element
);
252 internalUpdateSection(true);
255 boolean modulo
= i
++%2 == 0;
256 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
257 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
262 * Create the specific widget for the element
264 * @param element a ELEMENT object.
265 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
266 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
268 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
269 entityCollectionElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
274 public void setBackground(Color color
) {
275 if(label_empty
!= null && !label_empty
.isDisposed()){
276 label_empty
.setBackground(color
);
278 super.setBackground(color
);
282 * <p>getTitleString</p>
284 * @return a {@link java.lang.String} object.
286 public String
getTitleString() {
287 return CdmUtils
.Nz(title
);
291 * <p>setTitleString</p>
293 * @param title a {@link java.lang.String} object.
295 public void setTitleString(String title
){
303 public void expansionStateChanging(ExpansionEvent e
) {
304 // logger.warn("Expansion State Changing");
309 public void expansionStateChanged(ExpansionEvent e
) {
311 renderContent(isExpanded());
313 destroyDynamicContent();
317 private boolean expandSectionWhenContentAvailable(){
318 return PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
);
322 * Remove an element from the entities collection and update the section
324 * @param element a ELEMENT object.
326 public void removeElementAndUpdate(ELEMENT element
) {
327 removeElement(element
);
328 internalUpdateSection(true);
332 public void update(Observable o
, Object arg
){
333 if(o
instanceof LoginManager
){
338 private void updateToolbar() {
339 if(getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), UPDATE
)){
346 public AbstractEntityCollectionElement
<ENTITY
> getEntityCollectionElement() {
347 return entityCollectionElement
;
351 * Returns the {@link Comparator} specific for the ELEMENTs
352 * which is used to sort the elements
353 * @return the comparator for ELEMENT
355 public abstract Comparator
<ELEMENT
> getComparator();
358 * Get the specific collection of this entity
360 * @param entity a ENTITY object.
361 * @return a {@link java.util.Collection} object.
363 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
367 * Create a new Element for this collection
369 * @return a ELEMENT object.
371 public abstract ELEMENT
createNewElement();
374 * Add an element to the entities collection
376 * @param element a ELEMENT object.
378 public abstract void addElement(ELEMENT element
);
381 * Add an existing element to the entities collection.
382 * @return the existing element
384 public abstract ELEMENT
addExisting();
387 * If <code>true</code> the section will also display
388 * a browse icon to choose from existing elements.
390 * <b>Note:</b> when returning true you have to make sure
391 * to implement the {@link #addExisting()} method
392 * @return true if existing entities can be added;
395 public abstract boolean allowAddExisting();
398 * Remove an element from the entities collection
400 * @param element a ELEMENT object.
402 public abstract void removeElement(ELEMENT element
);
405 * String to display when the collection is empty
407 * @return a {@link java.lang.String} object.
409 public abstract String
getEmptyString();
412 * <p>getTooltipString</p>
414 * @return String to display when hovering the add button
416 protected abstract String
getTooltipString();