Project

General

Profile

Download (4.15 KB) Statistics
| Branch: | Tag: | Revision:
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> extends AbstractEntityCollectionSection<ENTITY, ELEMENT> {
46

    
47
    private boolean addUnboundElement = false;
48

    
49
    /**
50
     * @param formFactory
51
     * @param conversation
52
     * @param parentElement
53
     * @param title
54
     * @param style
55
     */
56
    public AbstractUnboundEntityCollectionSection(CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, String title, int style) {
57
        super(formFactory, conversation, parentElement, title, style);
58
    }
59

    
60
    /**
61
     * @deprecated this method should not be extended in sub classes of {@link AbstractUnboundEntityCollectionSection}.<br>
62
     */
63
    @Deprecated
64
    @Override
65
    public Collection<ELEMENT> getCollection(ENTITY entity) {
66
        Collection<ELEMENT> elements = getEntityCollection(entity);
67
        if(addUnboundElement){
68
            //cloning to avoid saving the dummy element
69
            Collection<ELEMENT> clonedElements = new ArrayList<>();
70
            clonedElements.addAll(elements);
71
            clonedElements.add(createNewElement()); //add dummy element which is not bound to entity
72
            return clonedElements;
73
        }
74
        return elements;
75
    }
76

    
77
    @Override
78
    protected Control createToolbar() {
79
        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
80

    
81
        Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
82
            /* (non-Javadoc)
83
             * @see org.eclipse.jface.action.Action#run()
84
             */
85
            @Override
86
            public void run() {
87
                addUnboundElement = true;
88
                if(! getSection().isExpanded()) {
89
                    getSection().setExpanded(true);
90
                }
91
                internalUpdateSection(false);
92
                addUnboundElement = false;
93
            }
94
        };
95
        addAction.setImageDescriptor(new ImageDescriptor() {
96

    
97
            @Override
98
            public ImageData getImageData() {
99
                return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
100
            }
101
        });
102
        addAction.setToolTipText(getTooltipString());
103

    
104
        toolBarManager.add(addAction);
105

    
106
        return toolBarManager.createControl(this);
107
    }
108

    
109
    /**
110
     * Get all the elements that are represented by this section.
111
     * @param entity the entity which is represented by the parent section
112
     * @return a collection of the elements belonging to the entity
113
     */
114
    protected abstract Collection<ELEMENT> getEntityCollection(ENTITY entity);
115

    
116
}
(2-2/32)