2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.ui
.section
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.Collections
;
14 import java
.util
.Comparator
;
15 import java
.util
.EnumSet
;
16 import java
.util
.List
;
17 import java
.util
.Observable
;
18 import java
.util
.Observer
;
20 import org
.eclipse
.jface
.action
.Action
;
21 import org
.eclipse
.jface
.action
.IAction
;
22 import org
.eclipse
.jface
.action
.ToolBarManager
;
23 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.events
.DisposeEvent
;
26 import org
.eclipse
.swt
.events
.DisposeListener
;
27 import org
.eclipse
.swt
.events
.SelectionAdapter
;
28 import org
.eclipse
.swt
.events
.SelectionEvent
;
29 import org
.eclipse
.swt
.events
.SelectionListener
;
30 import org
.eclipse
.swt
.graphics
.Color
;
31 import org
.eclipse
.swt
.graphics
.ImageData
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Control
;
34 import org
.eclipse
.swt
.widgets
.Label
;
35 import org
.eclipse
.ui
.forms
.events
.ExpansionEvent
;
36 import org
.eclipse
.ui
.forms
.events
.IExpansionListener
;
37 import org
.eclipse
.ui
.forms
.widgets
.ExpandableComposite
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
40 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
41 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 import eu
.etaxonomy
.cdm
.model
.permission
.CRUD
;
43 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
44 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
45 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
46 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
47 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
48 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
49 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
50 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
51 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
52 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
53 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
56 * This class visualizes an CDM entity of type ENTITY and additionally provides
57 * the functionality to add other elements of type ELEMENT to them.
59 * @param <ENTITY> A CDM entity which should be visualized by this section.
60 * @param <ELEMENT> An element that can be added (multiple times) to this entity.
64 public abstract class AbstractEntityCollectionSection
<ENTITY
, ELEMENT
>
65 extends AbstractFormSection
<ENTITY
>
66 implements IExpansionListener
, Observer
{
68 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
70 protected Composite container
;
72 private Label label_empty
;
76 private AbstractEntityCollectionElement
<ENTITY
> entityCollectionElement
;
78 public AbstractEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
79 super(formFactory
, parentElement
, ExpandableComposite
.CLIENT_INDENT
| style
);
81 this.setText(getTitleString());
84 addExpansionListener(this);
86 CdmStore
.getLoginManager().addObserver(this);
87 addDisposeListener(new DisposeListener() {
89 public void widgetDisposed(DisposeEvent e
) {
90 CdmStore
.getLoginManager().deleteObserver(AbstractEntityCollectionSection
.this);
95 protected Control
createToolbar() {
96 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
98 Action addAction
= new Action("Add", IAction
.AS_PUSH_BUTTON
){
101 ELEMENT element
= createNewElement();
104 if(! getSection().isExpanded()) {
105 getSection().setExpanded(true);
107 internalUpdateSection(true);
111 addAction
.setImageDescriptor(new ImageDescriptor() {
114 public ImageData
getImageData() {
115 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
118 addAction
.setToolTipText(getTooltipString());
120 Action browseAction
= null;
121 if(allowAddExisting()){
122 browseAction
= new Action("Browse", IAction
.AS_PUSH_BUTTON
){
125 ELEMENT element
= addExisting();
128 if(! getSection().isExpanded()) {
129 getSection().setExpanded(true);
131 internalUpdateSection(true);
135 browseAction
.setImageDescriptor(new ImageDescriptor() {
138 public ImageData
getImageData() {
139 return ImageResources
.getImage(ImageResources
.BROWSE_ICON
).getImageData();
142 browseAction
.setToolTipText("Browse");
145 toolBarManager
.add(addAction
);
146 if(browseAction
!=null){
147 toolBarManager
.add(browseAction
);
150 addAction(toolBarManager
);
152 return toolBarManager
.createControl(this);
155 protected void addAction(ToolBarManager toolBarManager
) {
156 // default implementation empty
160 * using this method is discouraged, use updateToolBar() instead
162 public void showToolbar(){
163 setTextClient(createToolbar());
167 * using this method is discouraged, use updateToolBar() instead
169 public void removeToolbar(){
174 public void setEntity(ENTITY entity
) {
176 super.setEntity(entity
);
177 internalUpdateSection(false);
185 * Sets the title for the section. Adds a "+" sign if the collection is not empty for this section.
186 * Override in subclasses if you want to have a different behaviour.
188 protected void setSectionTitle() {
189 Collection
<ELEMENT
> collection
= getCollection(getEntity());
190 if(collection
!= null && collection
.size() > 0){
191 this.setText(getTitleString() + " +");
193 this.setText(getTitleString());
198 * Removes all content from the container
200 private void destroyDynamicContent(){
201 if(label_empty
!= null){
202 label_empty
.dispose();
209 * Call this method after dynamically changing the client area.
210 * If the options changed is set to <code>true</code>, will also fire a state changed
211 * event to inform the user of unsaved changes.
213 * @param changed a boolean.
215 protected void internalUpdateSection(boolean changed
){
217 destroyDynamicContent();
218 if(isExpanded() || expandSectionWhenContentAvailable()) {
219 renderContent(isExpanded());
222 firePropertyChangeEvent(this);
227 * Create the elements to be shown in this section client area
229 private void renderContent(boolean forceExpansion
){
230 Collection
<ELEMENT
> collection
= getCollection(getEntity());
232 if(collection
== null || collection
.isEmpty()){
233 createEmptyContent();
235 List
<ELEMENT
> elements
= new ArrayList
<>(collection
);
236 Collections
.sort(elements
, getComparator());
237 createDynamicContents(elements
);
238 forceExpansion
= true;
241 this.setExpanded(forceExpansion
);
246 protected void createEmptyContent(){
247 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
251 * Creates the widgets for the collection
253 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
256 for(final ELEMENT element
: elements
){
257 SelectionAdapter removeListener
= new SelectionAdapter(){
259 public void widgetSelected(SelectionEvent e
) {
260 removeElement(element
);
261 internalUpdateSection(true);
264 boolean modulo
= i
++%2 == 0;
265 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
266 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
271 * Create the specific widget for the element
273 * @param element a ELEMENT object.
274 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
275 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
277 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
278 entityCollectionElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
282 public void setBackground(Color color
) {
283 if(label_empty
!= null && !label_empty
.isDisposed()){
284 label_empty
.setBackground(color
);
286 super.setBackground(color
);
292 public String
getTitleString() {
293 return CdmUtils
.Nz(title
);
299 public void setTitleString(String title
){
306 public void expansionStateChanging(ExpansionEvent e
) {
307 // logger.warn("Expansion State Changing");
311 public void expansionStateChanged(ExpansionEvent e
) {
313 renderContent(isExpanded());
315 destroyDynamicContent();
319 private boolean expandSectionWhenContentAvailable(){
320 return PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
, true);
324 * Remove an element from the entities collection and update the section
326 * @param element a ELEMENT object.
328 public void removeElementAndUpdate(ELEMENT element
) {
329 removeElement(element
);
330 internalUpdateSection(true);
334 public void update(Observable o
, Object arg
){
335 if(o
instanceof LoginManager
){
340 protected void updateToolbar() {
341 if( !(getEntity() instanceof CdmBase
) || (getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), UPDATE
)) ){
348 public AbstractEntityCollectionElement
<ENTITY
> getEntityCollectionElement() {
349 return entityCollectionElement
;
353 * Returns the {@link Comparator} specific for the ELEMENTs
354 * which is used to sort the elements
355 * @return the comparator for ELEMENT
357 public abstract Comparator
<ELEMENT
> getComparator();
360 * Get the specific collection of this entity
362 * @param entity a ENTITY object.
363 * @return a {@link java.util.Collection} object.
365 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
369 * Create a new Element for this collection
371 * @return a ELEMENT object.
373 public abstract ELEMENT
createNewElement();
376 * Add an element to the entities collection
378 * @param element a ELEMENT object.
380 public abstract void addElement(ELEMENT element
);
383 * Add an existing element to the entities collection.
384 * @return the existing element
386 public abstract ELEMENT
addExisting();
389 * If <code>true</code> the section will also display
390 * a browse icon to choose from existing elements.
392 * <b>Note:</b> when returning true you have to make sure
393 * to implement the {@link #addExisting()} method
394 * @return true if existing entities can be added;
397 public abstract boolean allowAddExisting();
400 * Remove an element from the entities collection
402 * @param element a ELEMENT object.
404 public abstract void removeElement(ELEMENT element
);
407 * String to display when the collection is empty
409 * @return a {@link java.lang.String} object.
411 public abstract String
getEmptyString();
414 * <p>getTooltipString</p>
416 * @return String to display when hovering the add button
418 protected abstract String
getTooltipString();