merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / dna / AbstractUnboundEntityCollectionSection.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.ui.section.occurrence.dna;
11
12 import java.util.Collection;
13 import java.util.HashSet;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.ToolBarManager;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.ImageData;
21 import org.eclipse.swt.widgets.Control;
22
23 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24 import eu.etaxonomy.taxeditor.model.ImageResources;
25 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
27 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionElement;
28 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
29
30 /**
31 * This class extends the functionality of {@link AbstractEntityCollectionSection}
32 * by creating an {@link AbstractEntityCollectionElement} for an
33 * ELEMENT which is not directly added the the model ENTITY.<br>
34 * <br>
35 * This is useful if the ELEMENT is created after the creation of the AbstractEntityCollectionElement itself
36 * and not in this class.
37 *
38 * @author pplitzner
39 * @date 21.01.2014
40 */
41 public abstract class AbstractUnboundEntityCollectionSection<ENTITY, ELEMENT> extends AbstractEntityCollectionSection<ENTITY, ELEMENT> {
42
43 private boolean addUnboundElement = false;
44
45 /**
46 * @param formFactory
47 * @param conversation
48 * @param parentElement
49 * @param title
50 * @param style
51 */
52 public AbstractUnboundEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
53 super(formFactory, conversation, parentElement, title, style);
54 }
55
56 /**
57 * @deprecated this method should not be extended in sub classes of {@link AbstractUnboundEntityCollectionSection}.<br>
58 */
59 @Deprecated
60 @Override
61 public Collection<ELEMENT> getCollection(ENTITY entity) {
62 Collection<ELEMENT> elements = getEntityCollection(entity);
63 if(addUnboundElement){
64 //cloning to avoid saving the dummy element
65 Collection<ELEMENT> clonedElements = new HashSet<ELEMENT>();
66 clonedElements.addAll(elements);
67 clonedElements.add(createNewElement()); //add dummy element which is not bound to entity
68 return clonedElements;
69 }
70 return elements;
71 }
72
73 @Override
74 protected Control createToolbar() {
75 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
76
77 Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
78 /* (non-Javadoc)
79 * @see org.eclipse.jface.action.Action#run()
80 */
81 @Override
82 public void run() {
83 addUnboundElement = true;
84 if(! getSection().isExpanded()) {
85 getSection().setExpanded(true);
86 }
87 internalUpdateSection(false);
88 addUnboundElement = false;
89 }
90 };
91 addAction.setImageDescriptor(new ImageDescriptor() {
92
93 @Override
94 public ImageData getImageData() {
95 return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
96 }
97 });
98 addAction.setToolTipText(getTooltipString());
99
100 toolBarManager.add(addAction);
101
102 return toolBarManager.createControl(this);
103 }
104
105 /**
106 * Get all the elements that are represented by this section.
107 * @param entity the entity which is represented by the parent section
108 * @return a collection of the elements belonging to the entity
109 */
110 protected abstract Collection<ELEMENT> getEntityCollection(ENTITY entity);
111
112 }