Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / dna / AbstractUnboundEntityCollectionSection.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.ui.section.occurrence.dna;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13
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;
21
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;
28
29 /**
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>
34 * <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>
38 * <br>
39 * <b>Note:</b> In sub classes: do <b>not</b> override {@link #getCollection(Object)}. Use
40 * {@link #getEntityCollection(Object)} instead
41 *
42 * @author pplitzner
43 * @date 21.01.2014
44 */
45 public abstract class AbstractUnboundEntityCollectionSection<ENTITY, ELEMENT>
46 extends AbstractEntityCollectionSection<ENTITY, ELEMENT> {
47
48 private boolean addUnboundElement = false;
49
50 public AbstractUnboundEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
51 super(formFactory, conversation, parentElement, title, style);
52 }
53
54 /**
55 * This method can not be extended in sub classes.
56 * Use {@link #getEntityCollection(Object)} instead.
57 */
58 @Override
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;
67 }
68 return elements;
69 }
70
71 @Override
72 protected Control createToolbar() {
73 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
74
75 Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
76
77 @Override
78 public void run() {
79 addUnboundElement = true;
80 if(! getSection().isExpanded()) {
81 getSection().setExpanded(true);
82 }
83 internalUpdateSection(false);
84 addUnboundElement = false;
85 }
86 };
87 addAction.setImageDescriptor(new ImageDescriptor() {
88
89 @Override
90 public ImageData getImageData() {
91 return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
92 }
93 });
94 addAction.setToolTipText(getTooltipString());
95
96 toolBarManager.add(addAction);
97
98 return toolBarManager.createControl(this);
99 }
100
101 /**
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
105 */
106 protected abstract Collection<ELEMENT> getEntityCollection(ENTITY entity);
107
108 }