Project

General

Profile

Download (7.38 KB) Statistics
| Branch: | Tag: | Revision:
1 729887cf n.hoffmann
/**
2 0c52f39c n.hoffmann
 * Copyright (C) 2007 EDIT
3 ac12d86b Patric Plitzner
 * European Distributed Institute of Taxonomy
4 0c52f39c n.hoffmann
 * http://www.e-taxonomy.eu
5 ac12d86b Patric Plitzner
 *
6 0c52f39c n.hoffmann
 * 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 729887cf n.hoffmann
10 f211dd28 n.hoffmann
package eu.etaxonomy.taxeditor.ui.section;
11 729887cf n.hoffmann
12 9c1f8392 Patrick Plitzner
import java.util.ArrayList;
13
import java.util.Collection;
14 df947da5 Katja Luther
import java.util.EnumSet;
15 9c1f8392 Patrick Plitzner
16 6747dad6 n.hoffmann
import org.eclipse.jface.action.ToolBarManager;
17 729887cf n.hoffmann
import org.eclipse.jface.viewers.ISelectionProvider;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.graphics.Color;
20 6747dad6 n.hoffmann
import org.eclipse.swt.widgets.Control;
21 2d9a13f7 n.hoffmann
import org.eclipse.ui.forms.events.ExpansionEvent;
22
import org.eclipse.ui.forms.events.IExpansionListener;
23 ac12d86b Patric Plitzner
import org.eclipse.ui.forms.widgets.ExpandableComposite;
24 729887cf n.hoffmann
import org.eclipse.ui.forms.widgets.TableWrapLayout;
25
26 2d9a13f7 n.hoffmann
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
27 2aa1d458 Patrick Plitzner
import eu.etaxonomy.cdm.common.CdmUtils;
28 729887cf n.hoffmann
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29 0ab85ea9 Katja Luther
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.description.TextData;
31 d9a8f632 Patric Plitzner
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
32 df947da5 Katja Luther
import eu.etaxonomy.cdm.model.permission.CRUD;
33
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
34 0ab85ea9 Katja Luther
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
35 df947da5 Katja Luther
import eu.etaxonomy.taxeditor.store.CdmStore;
36 d98abaef Patrick Plitzner
import eu.etaxonomy.taxeditor.store.StoreUtil;
37 78222507 n.hoffmann
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
38
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
39 dacb59c9 Patric Plitzner
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
40 78222507 n.hoffmann
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
41
import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
42 729887cf n.hoffmann
43
/**
44 ac12d86b Patric Plitzner
 * 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 729887cf n.hoffmann
 * @author n.hoffmann
49
 * @created Feb 26, 2010
50
 */
51 9c1f8392 Patrick Plitzner
public abstract class AbstractCdmDetailSection<ENTITY>
52
			extends AbstractFormSection<ENTITY>
53 e3a4a3ff Andreas Müller
			implements IEnableableFormElement, IExpansionListener {
54 0c52f39c n.hoffmann
55 9c1f8392 Patrick Plitzner
	protected AbstractCdmDetailElement<ENTITY> detailElement;
56 0c52f39c n.hoffmann
57 729887cf n.hoffmann
	public AbstractCdmDetailSection(CdmFormFactory formFactory,
58 0c52f39c n.hoffmann
			ConversationHolder conversation, ICdmFormElement parentElement,
59
			ISelectionProvider selectionProvider, int style) {
60 139902da Patric Plitzner
	    this(formFactory, null, conversation, parentElement, selectionProvider, style);
61 729887cf n.hoffmann
	}
62 ac12d86b Patric Plitzner
63 b9a0d300 l.morris
	public AbstractCdmDetailSection(CdmFormFactory formFactory, Class<ENTITY> clazz,
64
			ConversationHolder conversation, ICdmFormElement parentElement,
65
			ISelectionProvider selectionProvider, int style) {
66
		super(formFactory, parentElement, selectionProvider,
67 ac12d86b Patric Plitzner
				ExpandableComposite.CLIENT_INDENT | style);
68
69 139902da Patric Plitzner
        setText(getHeading());
70 b9a0d300 l.morris
71 139902da Patric Plitzner
        addExpansionListener(this);
72 b9a0d300 l.morris
73 139902da Patric Plitzner
        if(clazz==null){
74
            createControls(this, style);
75
        }
76
        else{
77
            createControlsByType(this, clazz, SWT.NULL);
78
        }
79 b9a0d300 l.morris
	}
80
81 d98abaef Patrick Plitzner
	@Override
82
	public void setText(String title) {
83
	    super.setText(StoreUtil.cleanTitleString(title));
84
	}
85
86 139902da Patric Plitzner
	protected void createControlsByType(AbstractCdmDetailSection<ENTITY> formElement, Class<ENTITY> entityClass, int style) {
87
	    TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
88
	    layout.topMargin = 10;
89 98fbf545 Patrick Plitzner
	    layout.numColumns = DEFAULT_NUM_COLUMNS;
90 139902da Patric Plitzner
91
	    getLayoutComposite().setLayout(layout);
92
	    if(entityClass==null){
93
	        detailElement = createCdmDetailElement(formElement, style);
94
	    }
95
	    else{
96
	        detailElement = createCdmDetailElementByType(formElement, entityClass, style);
97
	    }
98 b9a0d300 l.morris
	}
99
100 fd6c7492 Patric Plitzner
	protected void createControls(AbstractCdmDetailSection<ENTITY> formElement, int style) {
101 139902da Patric Plitzner
	    createControlsByType(formElement, null, style);
102 fd6c7492 Patric Plitzner
	}
103
104 9c1f8392 Patrick Plitzner
	protected abstract AbstractCdmDetailElement<ENTITY> createCdmDetailElement(AbstractCdmDetailSection<ENTITY> parentElement, int style);
105 0c52f39c n.hoffmann
106 9c1f8392 Patrick Plitzner
	protected AbstractCdmDetailElement<ENTITY> createCdmDetailElementByType(AbstractCdmDetailSection<ENTITY> parentElement, Class<ENTITY> entityClass, int style){
107 139902da Patric Plitzner
	    return createCdmDetailElement(parentElement, style);
108
	}
109 729887cf n.hoffmann
110
	public abstract String getHeading();
111 0c52f39c n.hoffmann
112 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
113 729887cf n.hoffmann
	@Override
114
	public void dispose() {
115 0c52f39c n.hoffmann
		if (detailElement instanceof ISelectableElement) {
116 6a6ea365 n.hoffmann
			ISelectableElement selectableElement = (ISelectableElement) detailElement;
117 0c52f39c n.hoffmann
			if (selectableElement.getSelectionArbitrator() != null) {
118
				formFactory.destroySelectionArbitrator(selectableElement
119
						.getSelectionArbitrator());
120 6a6ea365 n.hoffmann
			}
121 729887cf n.hoffmann
		}
122
		super.dispose();
123
	}
124 0c52f39c n.hoffmann
125 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
126 729887cf n.hoffmann
	@Override
127
	public void setBackground(Color color) {
128 0c52f39c n.hoffmann
		if (detailElement != null) {
129 20a37084 Katja Luther
		    detailElement.setBackground(color);
130 729887cf n.hoffmann
		}
131
		super.setBackground(color);
132
	}
133 0c52f39c n.hoffmann
134 87f9934d n.hoffmann
	@Override
135 74b9f670 n.hoffmann
	public void setEntity(ENTITY entity) {
136 df947da5 Katja Luther
	    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 0c52f39c n.hoffmann
		if (detailElement != null) {
144 729887cf n.hoffmann
			detailElement.setEntity(entity);
145 90f25990 Katja Luther
			formFactory.adapt(detailElement);
146 0f9d8bd0 Andreas Müller
	        Collection<Object> except = new ArrayList<>();
147 4eb4e01e Patrick Plitzner
	        for(ICdmFormElement formElement:detailElement.getElements()){
148
	            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
149
	                except.add(formElement);
150
	            }
151
	        }
152 90f25990 Katja Luther
	        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 df947da5 Katja Luther
159 729887cf n.hoffmann
		}
160
		super.setEntity(entity);
161
		setSectionTitle();
162
		layout();
163 a452be91 Katja Luther
164 729887cf n.hoffmann
	}
165 0c52f39c n.hoffmann
166
	protected void setSectionTitle() {
167 729887cf n.hoffmann
		String title = "";
168 0ab85ea9 Katja Luther
		String label = "";
169 d9a8f632 Patric Plitzner
		if (getEntity() != null && (getEntity() instanceof IdentifiableEntity) && !(getEntity() instanceof SpecimenOrObservationBase)) {
170 0ab85ea9 Katja Luther
			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 2aa1d458 Patrick Plitzner
			}
181
			if(CdmUtils.isBlank(label)){
182 c9269c8d Patrick Plitzner
			    label = ((IdentifiableEntity) getEntity()).getTitleCache();
183
			}
184
			if(CdmUtils.isBlank(label)){
185
			    label = ((IdentifiableEntity) getEntity()).generateTitle();
186 0ab85ea9 Katja Luther
			}
187
			title = ": " + label;
188 729887cf n.hoffmann
		}
189 89f83939 n.hoffmann
		this.setText(String.format("%s%s", getHeading(), title));
190 6747dad6 n.hoffmann
		setTextClient(createToolbar());
191
	}
192
193
	protected Control createToolbar() {
194
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
195
		return toolBarManager.createControl(this);
196 729887cf n.hoffmann
	}
197 0c52f39c n.hoffmann
198
	public void updateTitle() {
199
		if (!isDisposed()) {
200 30140758 n.hoffmann
			setSectionTitle();
201
			layout();
202
		}
203 729887cf n.hoffmann
	}
204 0c52f39c n.hoffmann
205 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
206 ac12d86b Patric Plitzner
	@Override
207
    public void setIrrelevant(boolean irrelevant) {
208 0c52f39c n.hoffmann
		if (detailElement != null) {
209 a452be91 Katja Luther
210 729887cf n.hoffmann
			detailElement.setIrrelevant(irrelevant);
211
		}
212
	}
213 0c52f39c n.hoffmann
214 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
215 ac12d86b Patric Plitzner
	@Override
216
    public void expansionStateChanging(ExpansionEvent e) {
217 0c52f39c n.hoffmann
		// logger.warn("Expansion State Changing");
218 2d9a13f7 n.hoffmann
	}
219 0c52f39c n.hoffmann
220 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
221 ac12d86b Patric Plitzner
	@Override
222
    public void expansionStateChanged(ExpansionEvent e) {
223 0c52f39c n.hoffmann
		// logger.warn("Expansion State Changed");
224 2d9a13f7 n.hoffmann
	}
225 729887cf n.hoffmann
}