3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.section
;
13 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.events
.SelectionEvent
;
16 import org
.eclipse
.swt
.events
.SelectionListener
;
17 import org
.eclipse
.swt
.graphics
.Color
;
18 import org
.eclipse
.swt
.widgets
.Button
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Event
;
21 import org
.eclipse
.swt
.widgets
.Layout
;
22 import org
.eclipse
.swt
.widgets
.Listener
;
23 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
27 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
28 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
38 * Visualizes an element of type ENTITY in an {@link AbstractEntityCollectionSection}
39 * and links listener functionalities to it.
41 * @param ENTITY the type of the element which is visualized by this class
44 * @created Nov 16, 2009
47 public abstract class AbstractEntityCollectionElement
<ENTITY
> extends
48 AbstractCdmFormElement
implements IEntityElement
<ENTITY
>,
49 SelectionListener
, IConversationEnabled
{
51 protected ENTITY entity
;
53 private final Composite container
;
56 * Composite "around" the actual content. Is used for control action like e.g. remove button
58 private final Composite box
;
60 private Button btnRemove
;
61 protected Button btnChooseEntity
;
63 private Color backgroundColor
;
65 public AbstractEntityCollectionElement(CdmFormFactory formFactory
,
66 AbstractFormSection section
, ENTITY entity
,
67 SelectionListener removeListener
, Color backgroundColor
, int style
) {
68 this(formFactory
, section
, entity
, removeListener
, false, backgroundColor
, style
);
70 public AbstractEntityCollectionElement(CdmFormFactory formFactory
,
71 AbstractFormSection section
, ENTITY entity
, SelectionListener removeListener
,
72 boolean isChoosableEntity
, Color backgroundColor
, int style
) {
73 super(formFactory
, (ICdmFormElement
) section
);
77 formFactory
.addPropertyChangeListener(this);
79 box
= formFactory
.createComposite(section
.getLayoutComposite());
80 box
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
83 TableWrapLayout boxLayout
= LayoutConstants
.LAYOUT(4, false);
84 boxLayout
.topMargin
= 4;
85 boxLayout
.bottomMargin
= 4;
86 box
.setLayout(boxLayout
);
88 box
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
90 container
= formFactory
.createComposite(box
);
91 container
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
93 setLayoutComposite(container
);
95 addControl(container
);
96 Layout containerLayout
= LayoutConstants
.LAYOUT(2, false);
98 container
.setLayout(containerLayout
);
99 container
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
101 if(isChoosableEntity
){
102 btnChooseEntity
= formFactory
.createButton(box
, null, SWT
.PUSH
);
103 addControl(btnChooseEntity
);
105 btnChooseEntity
.setLayoutData(LayoutConstants
.RIGHT());
106 btnChooseEntity
.setImage(ImageResources
.getImage(ImageResources
.BROWSE_ICON
));
107 btnChooseEntity
.setToolTipText("Browse");
108 btnChooseEntity
.addListener(SWT
.Selection
, new Listener() {
111 public void handleEvent(Event event
) {
112 ENTITY entity
= selectFromDialog();
114 if(getParentElement() instanceof AbstractEntityCollectionSection
){
115 ((AbstractEntityCollectionSection
)getParentElement()).removeElement(getEntity());
117 ((AbstractEntityCollectionSection
)getParentElement()).addElement(entity
);
118 ((AbstractEntityCollectionSection
)getParentElement()).firePropertyChangeEvent(getParentElement());
126 if (removeListener
!= null) {
127 btnRemove
= formFactory
.createButton(box
, null, SWT
.PUSH
);
128 addControl(btnRemove
);
129 btnRemove
.setLayoutData(LayoutConstants
.RIGHT());
130 btnRemove
.setImage(ImageResources
131 .getImage(ImageResources
.TRASH_ICON
));
132 btnRemove
.setToolTipText("Remove");
134 btnRemove
.addSelectionListener(removeListener
);
137 createControls(this, style
);
143 * Init gets executed before any other setup of the section takes place
145 * Implement this if you want to configure the section
148 // default implementation is empty
151 public abstract void setEntity(ENTITY entity
);
154 public ENTITY
getEntity() {
159 * Sub classes should override to provide the functionality to choose the
160 * entity from existing ones from the data source.<br>
161 * <b>Note:</b> to enable this functionality sub classes have to set
162 * the corresponding flag in the super constructor
163 * @return an existing entity from the data source
165 protected ENTITY
selectFromDialog(){
169 public abstract void createControls(ICdmFormElement element
, int style
);
172 * Mark <code>this</code> element as selected.
175 public void setSelected(boolean selected
) {
177 for (ICdmFormElement element
: getElements()) {
178 if (element
instanceof ISelectable
) {
179 ((ISelectable
) element
).setSelected(selected
);
182 setBackground(selected ? SELECTED
: getPersistentBackground());
187 public void propertyChange(PropertyChangeEvent event
) {
191 Object eventSource
= event
.getSource();
192 if (getElements().contains(eventSource
) || getControls().contains(eventSource
)) {
193 handleEvent(eventSource
);
197 public abstract void handleEvent(Object eventSource
);
201 public void setBackground(Color color
) {
202 backgroundColor
= color
;
203 super.setBackground(color
);
204 box
.setBackground(color
);
205 container
.setBackground(color
);
211 * React when selection occurs
214 public void widgetSelected(SelectionEvent e
) {
220 public void widgetDefaultSelected(SelectionEvent e
) {
225 public Composite
getLayoutComposite() {
229 public Color
getBackgroundColor() {
230 return backgroundColor
;
233 public Composite
getBox() {
238 public ConversationHolder
getConversationHolder() {
239 if (getParentElement() instanceof IConversationEnabled
) {
240 return ((IConversationEnabled
) getParentElement())
241 .getConversationHolder();
243 throw new IllegalArgumentException(
244 "Parent element should be IConversationEnabled");
249 public void update(CdmDataChangeMap changeEvents
) {