Merge branch 'hotfix/5.45.1'
[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.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;
27
28 /**
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>
33 * <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>
37 * <br>
38 * <b>Note:</b> In sub classes: do <b>not</b> override {@link #getCollection(Object)}. Use
39 * {@link #getEntityCollection(Object)} instead
40 *
41 * @author pplitzner
42 * @date 21.01.2014
43 */
44 public abstract class AbstractUnboundEntityCollectionSection<ENTITY, ELEMENT>
45 extends AbstractEntityCollectionSection<ENTITY, ELEMENT> {
46
47 protected boolean addUnboundElement = false;
48
49 public AbstractUnboundEntityCollectionSection(CdmFormFactory formFactory, ICdmFormElement parentElement, String title, int style) {
50 super(formFactory, parentElement, title, style);
51 }
52
53 /**
54 * This method can not be extended in sub classes.
55 * Use {@link #getEntityCollection(Object)} instead.
56 */
57 @Override
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;
66 }
67 return elements;
68 }
69
70 @Override
71 protected Control createToolbar() {
72 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
73
74 Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
75
76 @Override
77 public void run() {
78 addUnboundElement = true;
79 if(! getSection().isExpanded()) {
80 getSection().setExpanded(true);
81 }
82 internalUpdateSection(false);
83 addUnboundElement = false;
84 }
85 };
86 addAction.setImageDescriptor(new ImageDescriptor() {
87
88 @Override
89 public ImageData getImageData() {
90 return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
91 }
92 });
93 addAction.setToolTipText(getTooltipString());
94
95 toolBarManager.add(addAction);
96
97 return toolBarManager.createControl(this);
98 }
99
100 /**
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
104 */
105 protected abstract Collection<ELEMENT> getEntityCollection(ENTITY entity);
106
107 }