Project

General

Profile

Download (6.13 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 org.eclipse.jface.action.ToolBarManager;
13
import org.eclipse.jface.viewers.ISelectionProvider;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.graphics.Color;
16
import org.eclipse.swt.widgets.Control;
17
import org.eclipse.ui.forms.events.ExpansionEvent;
18
import org.eclipse.ui.forms.events.IExpansionListener;
19
import org.eclipse.ui.forms.widgets.ExpandableComposite;
20
import org.eclipse.ui.forms.widgets.TableWrapLayout;
21

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

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

    
46
	protected ICdmDetailElement<ENTITY> detailElement;
47

    
48
	public AbstractCdmDetailSection(CdmFormFactory formFactory,
49
			ConversationHolder conversation, ICdmFormElement parentElement,
50
			ISelectionProvider selectionProvider, int style) {
51
	    this(formFactory, null, conversation, parentElement, selectionProvider, style);
52
	}
53

    
54

    
55
	public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
56
			ConversationHolder conversation, ICdmFormElement parentElement,
57
			ISelectionProvider selectionProvider, int style) {
58
		super(formFactory, parentElement, selectionProvider,
59
				ExpandableComposite.CLIENT_INDENT | style);
60

    
61
        setText(getHeading());
62

    
63
        addExpansionListener(this);
64

    
65
        if(clazz==null){
66
            createControls(this, style);
67
        }
68
        else{
69
            createControlsByType(this, clazz, SWT.NULL);
70
        }
71
	}
72

    
73
	protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
74
	    TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
75
	    layout.topMargin = 10;
76
	    layout.numColumns = DEFAULT_NUM_COLUMNS;
77

    
78
	    getLayoutComposite().setLayout(layout);
79
	    if(entityClass==null){
80
	        detailElement = createCdmDetailElement(formElement, style);
81
	    }
82
	    else{
83
	        detailElement = createCdmDetailElementByType(formElement, entityClass, style);
84
	    }
85
	}
86

    
87
	protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
88
	    createControlsByType(formElement, null, style);
89
	}
90

    
91
	protected abstract ICdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
92

    
93
	protected ICdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
94
	    return createCdmDetailElement(parentElement, style);
95
	}
96

    
97
	public abstract String getHeading();
98

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

    
112
	/** {@inheritDoc} */
113
	@Override
114
	public void setBackground(Color color) {
115
		if (detailElement != null) {
116
			// detailComposite.setBackground(color);
117
		}
118
		super.setBackground(color);
119
	}
120

    
121
	@Override
122
	public void setEntity(ENTITY entity) {
123
		if (detailElement != null) {
124
			detailElement.setEntity(entity);
125
		}
126
		super.setEntity(entity);
127
		setSectionTitle();
128
		layout();
129
	}
130

    
131
	protected void setSectionTitle() {
132
		String title = "";
133
		String label = "";
134
		if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
135
			if (getEntity() instanceof DefinedTermBase<?> ){
136
				label = ((DefinedTermBase<?>)getEntity()).getLabel(PreferencesUtil.getGlobalLanguage());
137
				if (label == null){
138
					label = ((DefinedTermBase<?>)getEntity()).getLabel();
139
				}
140
				
141
			} else if (getEntity() instanceof TextData){
142
				label = ((TextData)getEntity()).getLanguageText(PreferencesUtil.getGlobalLanguage()).getText();
143
				if (label == null){
144
					label = ((TextData)getEntity()).getLanguageText(Language.DEFAULT()).getText();
145
				}
146
			} else{
147
				label =((IdentifiableEntity) getEntity()).getTitleCache();
148
			}
149
			title = ": " + label;
150
			
151
			// we have to duplicate ampersands otherwise they are treated as
152
			// mnenomic (see Label.setText() documentation)
153
			// see also #4302
154
			title = title.replace("&", "&&");
155
		}
156
		this.setText(String.format("%s%s", getHeading(), title));
157
		setTextClient(createToolbar());
158
	}
159

    
160
	protected Control createToolbar() {
161
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
162
		return toolBarManager.createControl(this);
163
	}
164

    
165
	public void updateTitle() {
166
		if (!isDisposed()) {
167
			setSectionTitle();
168
			layout();
169
		}
170
	}
171

    
172
	/** {@inheritDoc} */
173
	@Override
174
    public void setIrrelevant(boolean irrelevant) {
175
		if (detailElement != null) {
176
			detailElement.setIrrelevant(irrelevant);
177
		}
178
	}
179

    
180
	/** {@inheritDoc} */
181
	@Override
182
    public void expansionStateChanging(ExpansionEvent e) {
183
		// logger.warn("Expansion State Changing");
184
	}
185

    
186
	/** {@inheritDoc} */
187
	@Override
188
    public void expansionStateChanged(ExpansionEvent e) {
189
		// logger.warn("Expansion State Changed");
190
	}
191
}
(2-2/8)