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
)
231 Collection
<ELEMENT
> collection
= getCollection(getEntity());
233 if(collection
== null || collection
.isEmpty()){
234 createEmptyContent();
236 List
<ELEMENT
> elements
= new ArrayList
<>(collection
);
237 Collections
.sort(elements
, getComparator());
238 createDynamicContents(elements
);
239 forceExpansion
= true;
242 this.setExpanded(forceExpansion
);
247 protected void createEmptyContent(){
248 label_empty
= formFactory
.createLabel(getLayoutComposite(), getEmptyString());
252 * Creates the widgets for the collection
254 * @param elements a {@link java.util.Collection} object.
256 protected void createDynamicContents(Collection
<ELEMENT
> elements
)
259 for(final ELEMENT element
: elements
){
260 SelectionAdapter removeListener
= new SelectionAdapter(){
262 public void widgetSelected(SelectionEvent e
) {
263 removeElement(element
);
264 internalUpdateSection(true);
267 boolean modulo
= i
++%2 == 0;
268 String colorResource
= modulo ? Resources
.COLOR_LIST_EVEN
: Resources
.COLOR_LIST_ODD
;
269 createElementComposite(element
, removeListener
, AbstractUtility
.getColor(colorResource
));
274 * Create the specific widget for the element
276 * @param element a ELEMENT object.
277 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
278 * @param backgroundColor a {@link org.eclipse.swt.graphics.Color} object.
280 protected void createElementComposite(ELEMENT element
, SelectionListener removeListener
, Color backgroundColor
){
281 entityCollectionElement
= formFactory
.createEntityCollectionElement(this, element
, removeListener
, backgroundColor
, SWT
.NULL
);
286 public void setBackground(Color color
) {
287 if(label_empty
!= null && !label_empty
.isDisposed()){
288 label_empty
.setBackground(color
);
290 super.setBackground(color
);
294 * <p>getTitleString</p>
296 * @return a {@link java.lang.String} object.
298 public String
getTitleString() {
299 return CdmUtils
.Nz(title
);
303 * <p>setTitleString</p>
305 * @param title a {@link java.lang.String} object.
307 public void setTitleString(String title
){
315 public void expansionStateChanging(ExpansionEvent e
) {
316 // logger.warn("Expansion State Changing");
321 public void expansionStateChanged(ExpansionEvent e
) {
323 renderContent(isExpanded());
325 destroyDynamicContent();
329 private boolean expandSectionWhenContentAvailable(){
330 return PreferencesUtil
.getBooleanValue(IPreferenceKeys
.SHOULD_EXPAND_SECTION_WHEN_DATA_AVAILABLE
, true);
334 * Remove an element from the entities collection and update the section
336 * @param element a ELEMENT object.
338 public void removeElementAndUpdate(ELEMENT element
) {
339 removeElement(element
);
340 internalUpdateSection(true);
344 public void update(Observable o
, Object arg
){
345 if(o
instanceof LoginManager
){
350 protected void updateToolbar() {
351 if( !(getEntity() instanceof CdmBase
) || (getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), UPDATE
)) ){
358 public AbstractEntityCollectionElement
<ENTITY
> getEntityCollectionElement() {
359 return entityCollectionElement
;
363 * Returns the {@link Comparator} specific for the ELEMENTs
364 * which is used to sort the elements
365 * @return the comparator for ELEMENT
367 public abstract Comparator
<ELEMENT
> getComparator();
370 * Get the specific collection of this entity
372 * @param entity a ENTITY object.
373 * @return a {@link java.util.Collection} object.
375 public abstract Collection
<ELEMENT
> getCollection(ENTITY entity
);
379 * Create a new Element for this collection
381 * @return a ELEMENT object.
383 public abstract ELEMENT
createNewElement();
386 * Add an element to the entities collection
388 * @param element a ELEMENT object.
390 public abstract void addElement(ELEMENT element
);
393 * Add an existing element to the entities collection.
394 * @return the existing element
396 public abstract ELEMENT
addExisting();
399 * If <code>true</code> the section will also display
400 * a browse icon to choose from existing elements.
402 * <b>Note:</b> when returning true you have to make sure
403 * to implement the {@link #addExisting()} method
404 * @return true if existing entities can be added;
407 public abstract boolean allowAddExisting();
410 * Remove an element from the entities collection
412 * @param element a ELEMENT object.
414 public abstract void removeElement(ELEMENT element
);
417 * String to display when the collection is empty
419 * @return a {@link java.lang.String} object.
421 public abstract String
getEmptyString();
424 * <p>getTooltipString</p>
426 * @return String to display when hovering the add button
428 protected abstract String
getTooltipString();