-// $Id$
-/**
- * Copyright (C) 2013 EDIT
- * European Distributed Institute of Taxonomy
- * http://www.e-taxonomy.eu
- *
- * The contents of this file are subject to the Mozilla Public License Version 1.1
- * See LICENSE.TXT at the top of this package for the full license terms.
- */
-package eu.etaxonomy.taxeditor.ui.campanula.basicFields;
-
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.jface.wizard.WizardDialog;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Shell;
-import org.springframework.security.core.GrantedAuthority;
-
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
-import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
-import eu.etaxonomy.cdm.common.CdmUtils;
-import eu.etaxonomy.cdm.model.common.Group;
-import eu.etaxonomy.cdm.model.common.ICdmBase;
-import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
-import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
-import eu.etaxonomy.taxeditor.model.ImageResources;
-import eu.etaxonomy.taxeditor.preference.Resources;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
-import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
-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.IEnableableFormElement;
-import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
-import eu.etaxonomy.taxeditor.ui.element.ILabeledElement;
-import eu.etaxonomy.taxeditor.ui.element.ISelectableElement;
-import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
-import eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator;
-import eu.etaxonomy.taxeditor.ui.selection.EditFromSelectionWizard;
-import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
-
-/**
- * @author pplitzner
- * @date 13.08.2013
- *
- */
-public class EntitySelectionFieldController<T extends ICdmBase> extends AbstractCdmFormElement implements SelectionListener, IEnableableFormElement, ISelectableElement, IEntityElement<T>, ILabeledElement, IConversationEnabled {
-
- /**
- * Bitmask for configuring functionality of selection element
- */
- public static final int NOTHING = 0; // 000
- public static final int EDITABLE = 1 << 0; // 001
- public static final int DELETABLE = 1 << 1; // 010
- public static final int SELECTABLE = 1 << 2; // 100
- public static final int ALL = EDITABLE | DELETABLE | SELECTABLE; // 111
-
- protected T entity;
-
- //TODO also control "label" with this class?
-// protected Label label;
- protected Label text;
- protected Button button_selection;
-
- private SelectionArbitrator selectionArbitrator;
-
- protected Button button_edit;
-
-// private final String labelString;
-
- private Composite selectableComposite;
-
- private Button button_remove;
-
- private final boolean isEditable;
-
- private final boolean isDeletable;
-
- private final ConversationHolder conversation;
- private Class<T> clazz;
-
- /**
- * <p>
- * Constructor for AbstractSelectionElement.
- * </p>
- *
- * @param formFactory
- * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
- * object.
- * @param conversation
- * TODO
- * @param parentElement
- * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
- * object.
- * @param labelString
- * a {@link java.lang.String} object.
- * @param entity
- * a T object.
- * @param isEditable
- * a boolean.
- * @param isSelectable
- * a boolean.
- * @param isDeletable
- * a boolean.
- * @param style
- * a int.
- * @param <T>
- * a T object.
- */
- public EntitySelectionFieldController(EntitySelectionField entitySelectionField, CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, T entity, int mode) {
- super(formFactory, parentElement);
-
- setPropertyChangeListeners(formFactory.getPropertyChangeListeners());
- formFactory.addPropertyChangeListener(this);
-
- this.isEditable = (mode & EDITABLE) == EDITABLE;
- this.isDeletable = (mode & DELETABLE) == DELETABLE;
- boolean isSelectable = (mode & SELECTABLE) == SELECTABLE;
-
-// this.labelString = labelString;
-
- this.conversation = conversation;
-
- if (isSelectable && formFactory.getSelectionProvider() != null) {
- selectionArbitrator = formFactory.createSelectionArbitrator(this);
- }
-
- createControls(entitySelectionField);
-
- setEntity(entity);
- }
-
- public EntitySelectionFieldController(EntitySelectionField entitySelectionField, CdmFormFactory formFactory, ConversationHolder conversation, ICdmFormElement parentElement, Class<T> clazz, T entity, int mode) {
- this(entitySelectionField, formFactory, conversation, parentElement, entity, mode);
- this.clazz = clazz;
- }
-
- private void createControls(EntitySelectionField entitySelectionField) {
-
-// label = formFactory.createLabel(getLayoutComposite(), labelString, SWT.NULL);
-//
-// addControl(label);
-
- selectableComposite = entitySelectionField;
-
- int columns = 2;
- if (isEditable) {
- columns += 1;
- }
- if (isDeletable) {
- columns += 1;
- }
-
- selectableComposite.setLayout(LayoutConstants.LAYOUT(columns, false));
- selectableComposite.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
-
- addControl(selectableComposite);
-
- text = entitySelectionField.getText();
- addControl(text);
-
- text.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
- text.setBackground(AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
-
- button_selection = entitySelectionField.getButton_selection();
- button_selection.setImage(ImageResources.getImage(ImageResources.BROWSE_ICON));
- button_selection.setToolTipText("Browse existing");
-
- addControl(button_selection);
- button_selection.addSelectionListener(this);
-
- button_edit = entitySelectionField.getButton_edit();
- if (isEditable) {
- addControl(button_edit);
- //TODO: this worked with the extracted (but now reverted) interfaces done for campanula
-// button_edit.addSelectionListener(new EditListener(this));
- }
- else{
- button_edit.setVisible(false);
- }
-
- button_remove = entitySelectionField.getButton_remove();
- if (isDeletable) {
- addControl(button_remove);
- button_remove.addSelectionListener(new DeleteListener(this));
- }
- else{
- button_remove.setVisible(false);
- }
- }
-
- @Override
- public void widgetSelected(SelectionEvent e) {
- T selection = SelectionDialogFactory.getSelectionFromDialog(clazz, getShell(), getConversationHolder(), getEntity());
- setSelectionInternal(selection);
- }
-
- /**
- * Return the selected object
- *
- * @return a T object.
- */
- public T getSelection() {
- return entity;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
- */
- /** {@inheritDoc} */
- @Override
- public void setEnabled(boolean enabled) {
- button_selection.setEnabled(enabled);
- if (isEditable) {
- button_edit.setEnabled(enabled && entity != null);
- }
- }
-
- /**
- * <p>
- * setSelectionInternal
- * </p>
- *
- * @param selection
- * a T object.
- */
- protected void setSelectionInternal(T selection) {
- if (selection != null && !selection.equals(this.entity)) {
- setEntity(selection);
- firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
- }
- }
-
- /**
- * <p>
- * Setter for the field <code>entity</code>.
- * </p>
- *
- * @param selection
- * a T object.
- */
- public void setEntity(T selection) {
- this.entity = selection;
- updateElement();
- }
-
- /**
- * Updates this elements view
- */
- protected void updateElement() {
- String title = CdmUtils.Nz(getTitle());
- // we have to duplicate ampersands otherwise they are treated as
- // mnenomic (see Label.setText() documentation)
- title = title.replace("&", "&&");
- text.setText(title); // title can be null
- if (isEditable) {
- button_edit.setEnabled(entity != null);
- }
- }
-
- public void updateFromWizard() {
- updateElement();
- firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
- }
-
- /**
- * <p>
- * getTitle
- * </p>
- *
- * @return a {@link java.lang.String} object.
- */
- protected String getTitle() {
- if (entity != null) {
- if (entity instanceof IIdentifiableEntity) {
- return ((IIdentifiableEntity) entity).getTitleCache();
- } else if (entity instanceof Group) {
- return ((Group) entity).getName();
- } else if (entity instanceof GrantedAuthority) {
- return ((GrantedAuthority) entity).getAuthority();
- }
- }
- return "";
- }
-
- /** {@inheritDoc} */
- @Override
- public void setSelected(boolean selected) {
- setBackground(selected ? SELECTED : getPersistentBackground());
- }
-
- /*
- * (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;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.ISelectableElement#
- * getSelectionArbitrator()
- */
- /**
- * <p>
- * Getter for the field <code>selectionArbitrator</code>.
- * </p>
- *
- * @return a {@link eu.etaxonomy.taxeditor.ui.element.SelectionArbitrator}
- * object.
- */
- @Override
- public SelectionArbitrator getSelectionArbitrator() {
- return selectionArbitrator;
- }
-
- public Shell getShell() {
- return getLayoutComposite().getShell();
- }
-
- /** {@inheritDoc} */
- @Override
- public void setIrrelevant(boolean irrelevant) {
- String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_TEXT_DISABLED_BACKGROUND;
-
- Color color = AbstractUtility.getColor(colorId);
- text.setBackground(color);
- }
-
- private class DeleteListener extends SelectionAdapter {
-
- private final EntitySelectionFieldController<T> selectionElement;
-
- public DeleteListener(EntitySelectionFieldController<T> selectionElement) {
- this.selectionElement = selectionElement;
- }
-
- @Override
- public void widgetSelected(SelectionEvent e) {
- setEntity(null);
- firePropertyChangeEvent(new CdmPropertyChangeEvent(selectionElement, null));
- }
- }
-
- private class EditListener extends SelectionAdapter {
-
- private final EntitySelectionElement<T> selectionElement;
-
- public EditListener(EntitySelectionElement<T> selectionElement) {
- this.selectionElement = selectionElement;
- }
-
- /** {@inheritDoc} */
- @Override
- public void widgetSelected(SelectionEvent e) {
- WizardDialog dialog = new WizardDialog(selectionElement.getShell(), new EditFromSelectionWizard(selectionElement));
- if (dialog.open() == IStatus.OK) {
- selectionElement.updateFromWizard();
- }
- }
- }
-
- // not used
- /** {@inheritDoc} */
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
- }
-
- /**
- * <p>
- * getConversationHolder
- * </p>
- *
- * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
- * object.
- */
- @Override
- public ConversationHolder getConversationHolder() {
- return conversation;
- }
-
- /** {@inheritDoc} */
- @Override
- public void setBackground(Color color) {
-// label.setBackground(color);
- }
-
- /** {@inheritDoc} */
- @Override
- public void setLabel(String labelString) {
-// if (label != null) {
-// label.setText(labelString);
-// }
- }
-
- /**
- * <p>
- * Getter for the field <code>label</code>.
- * </p>
- *
- * @return a {@link java.lang.String} object.
- */
- @Override
- public String getLabel() {
-// if (label != null) {
-// return label.getText();
-// }
- return null;
- }
-
- /** {@inheritDoc} */
- @Override
- public void update(CdmDataChangeMap changeEvents) {
- }
-}