2 * Copyright (C) 2014 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
.occurrence
.dna
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
14 import org
.eclipse
.jface
.action
.Action
;
15 import org
.eclipse
.jface
.action
.IAction
;
16 import org
.eclipse
.jface
.action
.ToolBarManager
;
17 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.graphics
.ImageData
;
20 import org
.eclipse
.swt
.widgets
.Control
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
23 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
24 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
25 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
26 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractEntityCollectionElement
;
27 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractEntityCollectionSection
;
30 * This class extends the functionality of
31 * {@link AbstractEntityCollectionSection} by creating an
32 * {@link AbstractEntityCollectionElement} for an ELEMENT which is not directly
33 * added the the model ENTITY.<br>
35 * This is useful if the ELEMENT is created after the creation of the
36 * AbstractEntityCollectionElement itself and not in this class and also if you
37 * do not want to create empty elements. <br>
39 * <b>Note:</b> In sub classes: do <b>not</b> override {@link #getCollection(Object)}. Use
40 * {@link #getEntityCollection(Object)} instead
45 public abstract class AbstractUnboundEntityCollectionSection
<ENTITY
, ELEMENT
>
46 extends AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> {
48 private boolean addUnboundElement
= false;
50 public AbstractUnboundEntityCollectionSection(CdmFormFactory formFactory
, ConversationHolder conversation
, ICdmFormElement parentElement
, String title
, int style
) {
51 super(formFactory
, conversation
, parentElement
, title
, style
);
55 * This method can not be extended in sub classes.
56 * Use {@link #getEntityCollection(Object)} instead.
59 public final Collection
<ELEMENT
> getCollection(ENTITY entity
) {
60 Collection
<ELEMENT
> elements
= getEntityCollection(entity
);
61 if(addUnboundElement
){
62 //cloning to avoid saving the dummy element
63 Collection
<ELEMENT
> clonedElements
= new ArrayList
<>();
64 clonedElements
.addAll(elements
);
65 clonedElements
.add(createNewElement()); //add dummy element which is not bound to entity
66 return clonedElements
;
72 protected Control
createToolbar() {
73 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
75 Action addAction
= new Action("add", IAction
.AS_PUSH_BUTTON
){
79 addUnboundElement
= true;
80 if(! getSection().isExpanded()) {
81 getSection().setExpanded(true);
83 internalUpdateSection(false);
84 addUnboundElement
= false;
87 addAction
.setImageDescriptor(new ImageDescriptor() {
90 public ImageData
getImageData() {
91 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
94 addAction
.setToolTipText(getTooltipString());
96 toolBarManager
.add(addAction
);
98 return toolBarManager
.createControl(this);
102 * Get all the elements that are represented by this section.
103 * @param entity the entity which is represented by the parent section
104 * @return a collection of the elements belonging to the entity
106 protected abstract Collection
<ELEMENT
> getEntityCollection(ENTITY entity
);