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 org
.eclipse
.jface
.util
.PropertyChangeEvent
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.SelectionEvent
;
15 import org
.eclipse
.swt
.events
.SelectionListener
;
16 import org
.eclipse
.swt
.graphics
.Color
;
17 import org
.eclipse
.swt
.widgets
.Button
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
21 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
23 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
24 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
25 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
26 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
27 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
28 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
34 * Visualizes an element of type ENTITY in an {@link AbstractEntityCollectionSection}
35 * and links listener functionalities to it.
37 * @param ENTITY the type of the element which is visualized by this class
40 * @created Nov 16, 2009
42 public abstract class AbstractEntityCollectionElement
<ENTITY
> extends
43 AbstractCdmFormElement
implements IEntityElement
<ENTITY
>,
44 SelectionListener
, IConversationEnabled
{
46 protected ENTITY entity
;
48 private final Composite container
;
51 * Composite "around" the actual content. Is used for control action like e.g. remove button
53 private final Composite box
;
55 private Button btnRemove
;
56 protected Button btnChooseEntity
;
58 private Color backgroundColor
;
60 public AbstractEntityCollectionElement(CdmFormFactory formFactory
,
61 AbstractFormSection section
, ENTITY entity
, SelectionListener removeListener
,
62 Color backgroundColor
, int style
) {
63 super(formFactory
, (ICdmFormElement
) section
);
68 formFactory
.addPropertyChangeListener(this);
70 box
= formFactory
.createComposite(section
.getLayoutComposite());
71 box
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
74 TableWrapLayout boxLayout
= LayoutConstants
.LAYOUT(4, false);
75 boxLayout
.topMargin
= 4;
76 boxLayout
.bottomMargin
= 4;
77 box
.setLayout(boxLayout
);
79 box
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
81 container
= formFactory
.createComposite(box
);
82 container
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
84 setLayoutComposite(container
);
86 addControl(container
);
87 TableWrapLayout containerLayout
= LayoutConstants
.LAYOUT(2, false);
88 containerLayout
.horizontalSpacing
= 5;
90 container
.setLayout(containerLayout
);
91 container
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
93 if (removeListener
!= null) {
94 btnRemove
= formFactory
.createButton(box
, null, SWT
.PUSH
);
95 addControl(btnRemove
);
96 btnRemove
.setLayoutData(LayoutConstants
.RIGHT());
97 btnRemove
.setImage(ImageResources
98 .getImage(ImageResources
.TRASH_ICON
));
99 btnRemove
.setToolTipText("Remove");
101 btnRemove
.addSelectionListener(removeListener
);
104 createControls(this, style
);
110 * Init gets executed before any other setup of the section takes place
112 * Implement this if you want to configure the section
115 // default implementation is empty
118 public abstract void setEntity(ENTITY entity
);
121 public ENTITY
getEntity() {
126 * Sub classes should override to provide the functionality to choose the
127 * entity from existing ones from the data source.<br>
128 * <b>Note:</b> to enable this functionality sub classes have to set
129 * the corresponding flag in the super constructor
130 * @return an existing entity from the data source
132 protected ENTITY
selectFromDialog(){
136 public abstract void createControls(ICdmFormElement element
, int style
);
139 * Mark <code>this</code> element as selected.
142 public void setSelected(boolean selected
) {
144 for (ICdmFormElement element
: getElements()) {
145 if (element
instanceof ISelectable
) {
146 ((ISelectable
) element
).setSelected(selected
);
149 setBackground(selected ? SELECTED
: getPersistentBackground());
154 public void propertyChange(PropertyChangeEvent event
) {
158 Object eventSource
= event
.getSource();
159 if (getElements().contains(eventSource
) || getControls().contains(eventSource
)) {
160 handleEvent(eventSource
);
164 public abstract void handleEvent(Object eventSource
);
168 public void setBackground(Color color
) {
169 if(box
.isDisposed() || container
.isDisposed()){
172 backgroundColor
= color
;
173 super.setBackground(color
);
174 box
.setBackground(color
);
175 container
.setBackground(color
);
181 * React when selection occurs
184 public void widgetSelected(SelectionEvent e
) {
190 public void widgetDefaultSelected(SelectionEvent e
) {
195 public Composite
getLayoutComposite() {
199 public Color
getBackgroundColor() {
200 return backgroundColor
;
203 public Composite
getBox() {
208 public ConversationHolder
getConversationHolder() {
209 if (getParentElement() instanceof IConversationEnabled
) {
210 return ((IConversationEnabled
) getParentElement())
211 .getConversationHolder();
213 throw new IllegalArgumentException(
214 "Parent element should be IConversationEnabled");
219 public void update(CdmDataChangeMap changeEvents
) {