-// $Id$
/**
* Copyright (C) 2007 EDIT
* European Distributed Institute of Taxonomy
package eu.etaxonomy.taxeditor.ui.section;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.EnumSet;
+import java.util.Observable;
+import java.util.Observer;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.store.LoginManager;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
-import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
/**
- * <p>
- * Abstract AbstractCdmDetailElement class.
- * </p>
*
* @author n.hoffmann
* @created Feb 26, 2010
* @version 1.0
*/
-public abstract class AbstractCdmDetailElement<T> extends AbstractCdmFormElement implements IEntityElement<T>, IEnableableFormElement, IConversationEnabled {
+public abstract class AbstractCdmDetailElement<T> extends AbstractCdmFormElement implements ICdmDetailElement<T>, Observer {
private T entity;
private boolean irrelevant;
- /**
- * <p>
- * Constructor for AbstractCdmDetailElement.
- * </p>
- *
- * @param formFactory
- * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
- * object.
- * @param formElement
- * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
- * object.
- * @param <T>
- * a T object.
- */
+ private boolean enabled;
+
+ private EnumSet<CRUD> requiredCrud = null;
+
public AbstractCdmDetailElement(CdmFormFactory formFactory,
ICdmFormElement formElement) {
super(formFactory, formElement);
// register as property change listener
formFactory.addPropertyChangeListener(this);
+ CdmStore.getLoginManager().addObserver(this);
+
+ getLayoutComposite().getParent().addDisposeListener(new DisposeListener() {
+
+ @Override
+ public void widgetDisposed(DisposeEvent e) {
+ CdmStore.getLoginManager().deleteObserver(AbstractCdmDetailElement.this);
+ }
+ });
}
/**
* @param entity
* a T object.
*/
- public void setEntity(T entity) {
- this.entity = entity;
- updateContent();
+ @Override
+ public void setEntity(T entity) {
+ this.entity = entity;
+ // the id is always 0 if the entity was not yet saved, so it is new in this case
+ if(getEntity() == null || StoreUtil.getCdmEntity(getEntity()).getId() == 0) {
+ // new entity, not yet saved
+ requiredCrud = EnumSet.of(CRUD.CREATE);
+ } else {
+ requiredCrud = EnumSet.of(CRUD.UPDATE);
+ }
+ updateContent();
}
- /*
- * (non-Javadoc)
- *
- * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
- */
- /**
- * <p>
- * Getter for the field <code>entity</code>.
- * </p>
- *
- * @return a T object.
- */
@Override
public T getEntity() {
return entity;
protected void updateContent() {
removeElements();
createControls(this, entity, SWT.WRAP);
- getLayoutComposite().layout();
+ reflowParentScrolledForm(true);
+
+ updateControlStates();
}
- /*
- * (non-Javadoc)
- *
- * @see
- * eu.etaxonomy.taxeditor.forms.ISelectableComposite#setSelected(boolean)
- */
- /** {@inheritDoc} */
- @Override
+ @Override
public void setSelected(boolean selected) {
Composite section = getLayoutComposite().getParent();
section.setBackground(selected ? SELECTED : getPersistentBackground());
* a {@link java.util.Collection} object.
*/
public void setEnabled(boolean enabled, Collection<Object> except) {
+ this.enabled = enabled;
for (ICdmFormElement element : getElements()) {
if (element instanceof IEnableableFormElement) {
IEnableableFormElement enableableElement = (IEnableableFormElement) element;
}
}
+ @Override
+ public boolean isEnabled() {
+ return enabled;
+ }
+
/**
* <p>
* Setter for the field <code>irrelevant</code>.
if (getParentElement() instanceof AbstractCdmDetailSection) {
((AbstractCdmDetailSection) getParentElement()).updateTitle();
}
+ reflowParentScrolledForm(true);
}
/**
*/
public abstract void handleEvent(Object eventSource);
- /**
- * <p>
- * handleException
- * </p>
- *
- * @param event
- * a
- * {@link eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent}
- * object.
- */
public void handleException(CdmPropertyChangeEvent event) {
// override this in subclasses if you want to deal with the error
- AbstractUtility.error(event.getSource().getClass(), event.getException());
+ MessagingUtils.error(event.getSource().getClass(), event.getException());
}
- /**
- * <p>
- * isIrrelevant
- * </p>
- *
- * @return a boolean.
- */
public boolean isIrrelevant() {
return irrelevant;
}
- /**
- * <p>
- * getConversationHolder
- * </p>
- *
- * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
- * object.
- */
@Override
public ConversationHolder getConversationHolder() {
if (getParentElement() instanceof IConversationEnabled) {
@Override
public void update(CdmDataChangeMap changeEvents) {
}
+
+ @Override
+ public void update(Observable o, Object arg){
+ if(o instanceof LoginManager){
+ updateControlStates();
+ }
+ }
+
+
+ protected void updateControlStates(){
+ Collection<Object> except = new ArrayList<Object>();
+ for(ICdmFormElement formElement:getElements()){
+ if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
+ except.add(formElement);
+ }
+ }
+ setEnabled(getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud), except);
+ }
}