Project

General

Profile

Download (6.55 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 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

    
10
package eu.etaxonomy.taxeditor.ui.section;
11

    
12
import java.util.ArrayList;
13
import java.util.Collection;
14

    
15
import org.eclipse.jface.action.ToolBarManager;
16
import org.eclipse.jface.viewers.ISelectionProvider;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.graphics.Color;
19
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.ui.forms.events.ExpansionEvent;
21
import org.eclipse.ui.forms.events.IExpansionListener;
22
import org.eclipse.ui.forms.widgets.ExpandableComposite;
23
import org.eclipse.ui.forms.widgets.TableWrapLayout;
24

    
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
27
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.description.TextData;
30
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
35
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
36
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
37

    
38
/**
39
 * This class visualizes an CDM entity of type ENTITY.
40
 *
41
 * @param <ENTITY> A CDM entity which should be visualized by this section.
42
 *
43
 * @author n.hoffmann
44
 * @created Feb 26, 2010
45
 */
46
public abstract class AbstractCdmDetailSection<ENTITY>
47
			extends AbstractFormSection<ENTITY>
48
			implements IEnableableFormElement, IExpansionListener {
49

    
50
	protected AbstractCdmDetailElement<ENTITY> detailElement;
51

    
52
	public AbstractCdmDetailSection(CdmFormFactory formFactory,
53
			ConversationHolder conversation, ICdmFormElement parentElement,
54
			ISelectionProvider selectionProvider, int style) {
55
	    this(formFactory, null, conversation, parentElement, selectionProvider, style);
56
	}
57

    
58

    
59
	public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
60
			ConversationHolder conversation, ICdmFormElement parentElement,
61
			ISelectionProvider selectionProvider, int style) {
62
		super(formFactory, parentElement, selectionProvider,
63
				ExpandableComposite.CLIENT_INDENT | style);
64

    
65
        setText(getHeading());
66

    
67
        addExpansionListener(this);
68

    
69
        if(clazz==null){
70
            createControls(this, style);
71
        }
72
        else{
73
            createControlsByType(this, clazz, SWT.NULL);
74
        }
75
	}
76

    
77
	protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
78
	    TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
79
	    layout.topMargin = 10;
80
	    layout.numColumns = DEFAULT_NUM_COLUMNS;
81

    
82
	    getLayoutComposite().setLayout(layout);
83
	    if(entityClass==null){
84
	        detailElement = createCdmDetailElement(formElement, style);
85
	    }
86
	    else{
87
	        detailElement = createCdmDetailElementByType(formElement, entityClass, style);
88
	    }
89
	}
90

    
91
	protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
92
	    createControlsByType(formElement, null, style);
93
	}
94

    
95
	protected abstract AbstractCdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
96

    
97
	protected AbstractCdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
98
	    return createCdmDetailElement(parentElement, style);
99
	}
100

    
101
	public abstract String getHeading();
102

    
103
	/** {@inheritDoc} */
104
	@Override
105
	public void dispose() {
106
		if (detailElement instanceof ISelectableElement) {
107
			ISelectableElement selectableElement = (ISelectableElement) detailElement;
108
			if (selectableElement.getSelectionArbitrator() != null) {
109
				formFactory.destroySelectionArbitrator(selectableElement
110
						.getSelectionArbitrator());
111
			}
112
		}
113
		super.dispose();
114
	}
115

    
116
	/** {@inheritDoc} */
117
	@Override
118
	public void setBackground(Color color) {
119
		if (detailElement != null) {
120
			// detailComposite.setBackground(color);
121
		}
122
		super.setBackground(color);
123
	}
124

    
125
	@Override
126
	public void setEntity(ENTITY entity) {
127
		if (detailElement != null) {
128
			detailElement.setEntity(entity);
129
	        Collection<Object> except = new ArrayList<Object>();
130
	        for(ICdmFormElement formElement:detailElement.getElements()){
131
	            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
132
	                except.add(formElement);
133
	            }
134
	        }
135
			detailElement.setEnabled(isEnabled(), except);
136
		}
137
		super.setEntity(entity);
138
		setSectionTitle();
139
		layout();
140

    
141
	}
142

    
143
	protected void setSectionTitle() {
144
		String title = "";
145
		String label = "";
146
		if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
147
			if (getEntity() instanceof DefinedTermBase<?> ){
148
				label = ((DefinedTermBase<?>)getEntity()).getLabel(PreferencesUtil.getGlobalLanguage());
149
				if (label == null){
150
					label = ((DefinedTermBase<?>)getEntity()).getLabel();
151
				}
152

    
153
			} else if (getEntity() instanceof TextData){
154
				label = ((TextData)getEntity()).getLanguageText(PreferencesUtil.getGlobalLanguage()).getText();
155
				if (label == null){
156
					label = ((TextData)getEntity()).getLanguageText(Language.DEFAULT()).getText();
157
				}
158
			} else{
159
				label =((IdentifiableEntity) getEntity()).getTitleCache();
160
			}
161
			title = ": " + label;
162

    
163
			// we have to duplicate ampersands otherwise they are treated as
164
			// mnenomic (see Label.setText() documentation)
165
			// see also #4302
166
			title = title.replace("&", "&&");
167
		}
168
		this.setText(String.format("%s%s", getHeading(), title));
169
		setTextClient(createToolbar());
170
	}
171

    
172
	protected Control createToolbar() {
173
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
174
		return toolBarManager.createControl(this);
175
	}
176

    
177
	public void updateTitle() {
178
		if (!isDisposed()) {
179
			setSectionTitle();
180
			layout();
181
		}
182
	}
183

    
184
	/** {@inheritDoc} */
185
	@Override
186
    public void setIrrelevant(boolean irrelevant) {
187
		if (detailElement != null) {
188

    
189
			detailElement.setIrrelevant(irrelevant);
190
		}
191
	}
192

    
193
	/** {@inheritDoc} */
194
	@Override
195
    public void expansionStateChanging(ExpansionEvent e) {
196
		// logger.warn("Expansion State Changing");
197
	}
198

    
199
	/** {@inheritDoc} */
200
	@Override
201
    public void expansionStateChanged(ExpansionEvent e) {
202
		// logger.warn("Expansion State Changed");
203
	}
204
}
(2-2/9)