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

    
69
        setText(getHeading());
70

    
71
        addExpansionListener(this);
72

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

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

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

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

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

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

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

    
110
	public abstract String getHeading();
111

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

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

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

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

    
164
	}
165

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

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

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

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

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

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

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