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
.taxeditor
.model
.ImageResources
;
23 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
24 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
25 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractEntityCollectionElement
;
26 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractEntityCollectionSection
;
29 * This class extends the functionality of
30 * {@link AbstractEntityCollectionSection} by creating an
31 * {@link AbstractEntityCollectionElement} for an ELEMENT which is not directly
32 * added the the model ENTITY.<br>
34 * This is useful if the ELEMENT is created after the creation of the
35 * AbstractEntityCollectionElement itself and not in this class and also if you
36 * do not want to create empty elements. <br>
38 * <b>Note:</b> In sub classes: do <b>not</b> override {@link #getCollection(Object)}. Use
39 * {@link #getEntityCollection(Object)} instead
44 public abstract class AbstractUnboundEntityCollectionSection
<ENTITY
, ELEMENT
>
45 extends AbstractEntityCollectionSection
<ENTITY
, ELEMENT
> {
47 protected boolean addUnboundElement
= false;
49 public AbstractUnboundEntityCollectionSection(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String title
, int style
) {
50 super(formFactory
, parentElement
, title
, style
);
54 * This method can not be extended in sub classes.
55 * Use {@link #getEntityCollection(Object)} instead.
58 public final Collection
<ELEMENT
> getCollection(ENTITY entity
) {
59 Collection
<ELEMENT
> elements
= getEntityCollection(entity
);
60 if(addUnboundElement
){
61 //cloning to avoid saving the dummy element
62 Collection
<ELEMENT
> clonedElements
= new ArrayList
<>();
63 clonedElements
.addAll(elements
);
64 clonedElements
.add(createNewElement()); //add dummy element which is not bound to entity
65 return clonedElements
;
71 protected Control
createToolbar() {
72 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
74 Action addAction
= new Action("add", IAction
.AS_PUSH_BUTTON
){
78 addUnboundElement
= true;
79 if(! getSection().isExpanded()) {
80 getSection().setExpanded(true);
82 internalUpdateSection(false);
83 addUnboundElement
= false;
86 addAction
.setImageDescriptor(new ImageDescriptor() {
89 public ImageData
getImageData() {
90 return ImageResources
.getImage(ImageResources
.ADD_ICON
).getImageData();
93 addAction
.setToolTipText(getTooltipString());
95 toolBarManager
.add(addAction
);
97 return toolBarManager
.createControl(this);
101 * Get all the elements that are represented by this section.
102 * @param entity the entity which is represented by the parent section
103 * @return a collection of the elements belonging to the entity
105 protected abstract Collection
<ELEMENT
> getEntityCollection(ENTITY entity
);