Project

General

Profile

Download (7.38 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
import java.util.EnumSet;
15

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

    
26
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
27
import eu.etaxonomy.cdm.common.CdmUtils;
28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.description.TextData;
31
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
32
import eu.etaxonomy.cdm.model.permission.CRUD;
33
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
34
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36
import eu.etaxonomy.taxeditor.store.StoreUtil;
37
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
38
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
39
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
40
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
41
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
42

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

    
55
	protected AbstractCdmDetailElement<ENTITY> detailElement;
56

    
57
	public AbstractCdmDetailSection(CdmFormFactory formFactory,
58
			ConversationHolder conversation, ICdmFormElement parentElement,
59
			ISelectionProvider selectionProvider, int style) {
60
	    this(formFactory, null, conversation, parentElement, selectionProvider, style);
61
	}
62

    
63

    
64
	public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
65
			ConversationHolder conversation, ICdmFormElement parentElement,
66
			ISelectionProvider selectionProvider, int style) {
67
		super(formFactory, parentElement, selectionProvider,
68
				ExpandableComposite.CLIENT_INDENT | style);
69

    
70
        setText(getHeading());
71

    
72
        addExpansionListener(this);
73

    
74
        if(clazz==null){
75
            createControls(this, style);
76
        }
77
        else{
78
            createControlsByType(this, clazz, SWT.NULL);
79
        }
80
	}
81

    
82
	@Override
83
	public void setText(String title) {
84
	    super.setText(StoreUtil.cleanTitleString(title));
85
	}
86

    
87
	protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
88
	    TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
89
	    layout.topMargin = 10;
90
	    layout.numColumns = DEFAULT_NUM_COLUMNS;
91

    
92
	    getLayoutComposite().setLayout(layout);
93
	    if(entityClass==null){
94
	        detailElement = createCdmDetailElement(formElement, style);
95
	    }
96
	    else{
97
	        detailElement = createCdmDetailElementByType(formElement, entityClass, style);
98
	    }
99
	}
100

    
101
	protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
102
	    createControlsByType(formElement, null, style);
103
	}
104

    
105
	protected abstract AbstractCdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
106

    
107
	protected AbstractCdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
108
	    return createCdmDetailElement(parentElement, style);
109
	}
110

    
111
	public abstract String getHeading();
112

    
113
	/** {@inheritDoc} */
114
	@Override
115
	public void dispose() {
116
		if (detailElement instanceof ISelectableElement) {
117
			ISelectableElement selectableElement = (ISelectableElement) detailElement;
118
			if (selectableElement.getSelectionArbitrator() != null) {
119
				formFactory.destroySelectionArbitrator(selectableElement
120
						.getSelectionArbitrator());
121
			}
122
		}
123
		super.dispose();
124
	}
125

    
126
	/** {@inheritDoc} */
127
	@Override
128
	public void setBackground(Color color) {
129
		if (detailElement != null) {
130
			// detailComposite.setBackground(color);
131
		}
132
		super.setBackground(color);
133
	}
134

    
135
	@Override
136
	public void setEntity(ENTITY entity) {
137
	    EnumSet<CRUD> requiredCrud;
138
	    if(entity == null || StoreUtil.getCdmEntity(entity).getId() == 0) {
139
            // new entity, not yet saved
140
            requiredCrud = EnumSet.of(CRUD.CREATE);
141
        } else {
142
            requiredCrud = EnumSet.of(CRUD.UPDATE);
143
        }
144
		if (detailElement != null) {
145
			detailElement.setEntity(entity);
146
			formFactory.adapt(detailElement);
147
	        Collection<Object> except = new ArrayList<>();
148
	        for(ICdmFormElement formElement:detailElement.getElements()){
149
	            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
150
	                except.add(formElement);
151
	            }
152
	        }
153
	        if (entity != null){
154
	            boolean hasPermission = CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(entity), requiredCrud);
155
	            detailElement.setEnabled(isEnabled() && hasPermission, except);
156
	        }else{
157
	            detailElement.setEnabled(isEnabled() && true, except);
158
	        }
159

    
160
		}
161
		super.setEntity(entity);
162
		setSectionTitle();
163
		layout();
164

    
165
	}
166

    
167
	protected void setSectionTitle() {
168
		String title = "";
169
		String label = "";
170
		if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
171
			if (getEntity() instanceof DefinedTermBase<?> ){
172
				label = ((DefinedTermBase<?>)getEntity()).getLabel(PreferencesUtil.getGlobalLanguage());
173
				if (label == null){
174
					label = ((DefinedTermBase<?>)getEntity()).getLabel();
175
				}
176
			} else if (getEntity() instanceof TextData){
177
				label = ((TextData)getEntity()).getLanguageText(PreferencesUtil.getGlobalLanguage()).getText();
178
				if (label == null){
179
					label = ((TextData)getEntity()).getLanguageText(Language.DEFAULT()).getText();
180
				}
181
			}
182
			if(CdmUtils.isBlank(label)){
183
			    label = ((IdentifiableEntity) getEntity()).getTitleCache();
184
			}
185
			if(CdmUtils.isBlank(label)){
186
			    label = ((IdentifiableEntity) getEntity()).generateTitle();
187
			}
188
			title = ": " + label;
189
		}
190
		this.setText(String.format("%s%s", getHeading(), title));
191
		setTextClient(createToolbar());
192
	}
193

    
194
	protected Control createToolbar() {
195
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
196
		return toolBarManager.createControl(this);
197
	}
198

    
199
	public void updateTitle() {
200
		if (!isDisposed()) {
201
			setSectionTitle();
202
			layout();
203
		}
204
	}
205

    
206
	/** {@inheritDoc} */
207
	@Override
208
    public void setIrrelevant(boolean irrelevant) {
209
		if (detailElement != null) {
210

    
211
			detailElement.setIrrelevant(irrelevant);
212
		}
213
	}
214

    
215
	/** {@inheritDoc} */
216
	@Override
217
    public void expansionStateChanging(ExpansionEvent e) {
218
		// logger.warn("Expansion State Changing");
219
	}
220

    
221
	/** {@inheritDoc} */
222
	@Override
223
    public void expansionStateChanged(ExpansionEvent e) {
224
		// logger.warn("Expansion State Changed");
225
	}
226
}
(2-2/9)