setting root logger setting to WARN
[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
32 * {@link AbstractEntityCollectionSection} by creating an
33 * {@link AbstractEntityCollectionElement} for an ELEMENT which is not directly
34 * added the the model ENTITY.<br>
35 * <br>
36 * This is useful if the ELEMENT is created after the creation of the
37 * AbstractEntityCollectionElement itself and not in this class and also if you
38 * do not want to create empty elements. <br>
39 * <br>
40 * <b>Note:</b> In sub classes: do <b>not</b> override {@link #getCollection(Object)}. Use
41 * {@link #getEntityCollection(Object)} instead
42 *
43 * @author pplitzner
44 * @date 21.01.2014
45 */
46 public abstract class AbstractUnboundEntityCollectionSection<ENTITY, ELEMENT> extends AbstractEntityCollectionSection<ENTITY, ELEMENT> {
47
48 private boolean addUnboundElement = false;
49
50 /**
51 * @param formFactory
52 * @param conversation
53 * @param parentElement
54 * @param title
55 * @param style
56 */
57 public AbstractUnboundEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
58 super(formFactory, conversation, parentElement, title, style);
59 }
60
61 /**
62 * @deprecated this method should not be extended in sub classes of {@link AbstractUnboundEntityCollectionSection}.<br>
63 */
64 @Deprecated
65 @Override
66 public Collection<ELEMENT> getCollection(ENTITY entity) {
67 Collection<ELEMENT> elements = getEntityCollection(entity);
68 if(addUnboundElement){
69 //cloning to avoid saving the dummy element
70 Collection<ELEMENT> clonedElements = new HashSet<ELEMENT>();
71 clonedElements.addAll(elements);
72 clonedElements.add(createNewElement()); //add dummy element which is not bound to entity
73 return clonedElements;
74 }
75 return elements;
76 }
77
78 @Override
79 protected Control createToolbar() {
80 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
81
82 Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
83 /* (non-Javadoc)
84 * @see org.eclipse.jface.action.Action#run()
85 */
86 @Override
87 public void run() {
88 addUnboundElement = true;
89 if(! getSection().isExpanded()) {
90 getSection().setExpanded(true);
91 }
92 internalUpdateSection(false);
93 addUnboundElement = false;
94 }
95 };
96 addAction.setImageDescriptor(new ImageDescriptor() {
97
98 @Override
99 public ImageData getImageData() {
100 return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
101 }
102 });
103 addAction.setToolTipText(getTooltipString());
104
105 toolBarManager.add(addAction);
106
107 return toolBarManager.createControl(this);
108 }
109
110 /**
111 * Get all the elements that are represented by this section.
112 * @param entity the entity which is represented by the parent section
113 * @return a collection of the elements belonging to the entity
114 */
115 protected abstract Collection<ELEMENT> getEntityCollection(ENTITY entity);
116
117 }