2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.ui
.section
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.EnumSet
;
15 import java
.util
.Observable
;
16 import java
.util
.Observer
;
18 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.DisposeEvent
;
21 import org
.eclipse
.swt
.events
.DisposeListener
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Display
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
27 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
31 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
32 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
35 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
36 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
42 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
48 * @created Feb 26, 2010
50 public abstract class AbstractCdmDetailElement
<T
> extends AbstractCdmFormElement
implements ICdmDetailElement
<T
>, Observer
{
54 private boolean irrelevant
;
56 private boolean enabled
;
58 private LabelElement warnForReferencedObjects
;
60 private EnumSet
<CRUD
> requiredCrud
= null;
62 public AbstractCdmDetailElement(CdmFormFactory formFactory
,
63 ICdmFormElement formElement
) {
64 super(formFactory
, formElement
);
65 // register as property change listener
66 formFactory
.addPropertyChangeListener(this);
67 CdmStore
.getLoginManager().addObserver(this);
69 getLayoutComposite().getParent().addDisposeListener(new DisposeListener() {
72 public void widgetDisposed(DisposeEvent e
) {
73 CdmStore
.getLoginManager().deleteObserver(AbstractCdmDetailElement
.this);
79 * Sets the entity and updates controls
85 public void setEntity(T entity
) {
87 // the id is always 0 if the entity was not yet saved, so it is new in this case
88 if(getEntity() == null || StoreUtil
.getCdmEntity(getEntity()).getId() == 0) {
89 // new entity, not yet saved
90 requiredCrud
= EnumSet
.of(CRUD
.CREATE
);
92 requiredCrud
= EnumSet
.of(CRUD
.UPDATE
);
98 public T
getEntity() {
103 * Updates all widgets to display the latest data
105 protected void updateContent() {
107 createControls(this, entity
, SWT
.WRAP
);
108 reflowParentScrolledForm(true);
110 updateControlStates();
114 public void setSelected(boolean selected
) {
115 Composite section
= getLayoutComposite().getParent();
116 section
.setBackground(selected ? SELECTED
: getPersistentBackground());
120 * Sets all field to the given enablement state except the objects in
123 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#setEnabled(boolean)
127 * a {@link java.util.Collection} object.
129 public void setEnabled(boolean enabled
, Collection
<Object
> except
) {
130 this.enabled
= enabled
;
131 for (ICdmFormElement element
: getElements()) {
132 if (element
instanceof IEnableableFormElement
) {
133 IEnableableFormElement enableableElement
= (IEnableableFormElement
) element
;
134 if (except
!= null && except
.contains(enableableElement
)) {
135 // enableableElement.setEnabled(enabled);
137 enableableElement
.setEnabled(enabled
);
144 public boolean isEnabled() {
150 * Setter for the field <code>irrelevant</code>.
156 * a {@link java.util.Collection} object.
158 public void setIrrelevant(boolean irrelevant
, Collection
<Object
> except
) {
159 // logger.warn("Setting " + this.getClass().getSimpleName() +
160 // " to irrelevant state: " + irrelevant);
161 for (ICdmFormElement element
: getElements()) {
162 if (element
instanceof IEnableableFormElement
) {
163 IEnableableFormElement relevantElement
= (IEnableableFormElement
) element
;
164 if (except
!= null && except
.contains(relevantElement
)) {
166 // logger.warn("not setting elements irrelevance: " +
167 // relevantElement.getClass().getSimpleName() + " in :" +
168 // this.getClass().getSimpleName());
170 relevantElement
.setIrrelevant(irrelevant
);
178 public void setEnabled(boolean enabled
) {
179 setEnabled(enabled
, null);
184 public void setIrrelevant(boolean irrelevant
) {
185 this.irrelevant
= irrelevant
;
186 setIrrelevant(irrelevant
, null);
190 * Create all content elements
193 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
200 protected abstract void createControls(ICdmFormElement formElement
,
201 T entity
, int style
);
205 public void propertyChange(PropertyChangeEvent event
) {
210 handleTitleCacheRelevantChange(event
);
212 Object eventSource
= event
.getSource();
214 if (eventSource
instanceof ICdmFormElement
) {
215 ICdmFormElement eventElement
= (ICdmFormElement
) eventSource
;
216 ICdmFormElement eventElementContainer
= eventElement
219 if (eventElementContainer
== this) {
220 if (event
instanceof CdmPropertyChangeEvent
) {
221 if (((CdmPropertyChangeEvent
) event
).hasException()) {
222 handleException((CdmPropertyChangeEvent
) event
);
226 // call specific handler implementation
227 handleEvent(eventSource
);
229 firePropertyChangeEvent(this, event
);
230 // update possible parents
231 updateParentSection();
236 protected void handleTitleCacheRelevantChange(PropertyChangeEvent event
) {
237 boolean isRelevant
= getEntity() instanceof TaxonName
238 || getEntity() instanceof TaxonBase
;
239 isRelevant
&= this instanceof AbstractIdentifiableEntityDetailElement
;
242 ((AbstractIdentifiableEntityDetailElement
) this)
243 .updateToggleableCacheField();
244 updateParentSection();
248 private void updateParentSection() {
249 if (getParentElement() instanceof AbstractCdmDetailSection
) {
250 ((AbstractCdmDetailSection
) getParentElement()).updateTitle();
252 reflowParentScrolledForm(true);
256 * Gets called when the source of the event is a child widget of this
260 * a {@link java.lang.Object} object.
262 public abstract void handleEvent(Object eventSource
);
264 public void handleException(CdmPropertyChangeEvent event
) {
265 // override this in subclasses if you want to deal with the error
266 MessagingUtils
.error(event
.getSource().getClass(), event
.getException());
269 public boolean isIrrelevant() {
274 public ConversationHolder
getConversationHolder() {
275 if (getParentElement() instanceof IConversationEnabled
) {
276 return ((IConversationEnabled
) getParentElement())
277 .getConversationHolder();
279 throw new IllegalArgumentException(
280 "Parent element should be IConversationEnabled");
285 public void update(CdmDataChangeMap changeEvents
) {
289 public void update(Observable o
, Object arg
){
290 if(o
instanceof LoginManager
){
291 updateControlStates();
296 protected void updateControlStates(){
297 Collection
<Object
> except
= new ArrayList
<Object
>();
298 for(ICdmFormElement formElement
:getElements()){
299 if(formElement
instanceof IEnableableFormElement
&& !((IEnableableFormElement
) formElement
).isEnabled()){
300 except
.add(formElement
);
303 setEnabled(getEntity() != null && CdmStore
.currentAuthentiationHasPermission(StoreUtil
.getCdmEntity(getEntity()), requiredCrud
), except
);
306 public LabelElement
getWarnForReferencedObjects() {
307 return warnForReferencedObjects
;
310 public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects
) {
311 this.warnForReferencedObjects
= warnForReferencedObjects
;
314 public void setWarnForReferencingObjects(ICdmFormElement formElement
){
315 if (getEntity() instanceof CdmBase
){
316 CdmBase cdmBase
= (CdmBase
) getEntity();
317 if (cdmBase
.getId() != 0){
318 Integer referencingObjectsCount
= CdmStore
.getCommonService().getReferencingObjectsCount(cdmBase
);
320 if (referencingObjectsCount
> 1){
321 setWarnForReferencedObjects(formFactory
.createLabel(formElement
, CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects")));
322 getWarnForReferencedObjects().setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
323 getWarnForReferencedObjects().setLayout(LayoutConstants
.FILL(2, 3));
324 getWarnForReferencedObjects().setForeground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
331 public void setWarnForReferencingObjectsVisible(boolean isVisible
){
332 if (getWarnForReferencedObjects() != null){
333 getWarnForReferencedObjects().setVisible(isVisible
);