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.
9 package eu
.etaxonomy
.taxeditor
.ui
.section
;
11 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
12 import org
.eclipse
.swt
.SWT
;
13 import org
.eclipse
.swt
.events
.SelectionEvent
;
14 import org
.eclipse
.swt
.events
.SelectionListener
;
15 import org
.eclipse
.swt
.graphics
.Color
;
16 import org
.eclipse
.swt
.widgets
.Button
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.swt
.widgets
.Display
;
19 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
24 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
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
.IEnableableFormElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
36 * Visualizes an element of type ENTITY in an {@link AbstractEntityCollectionSection}
37 * and links listener functionalities to it.
39 * @param ENTITY the type of the element which is visualized by this class
42 * @created Nov 16, 2009
44 public abstract class AbstractEntityCollectionElement
<ENTITY
>
45 extends AbstractCdmFormElement
46 implements IEntityElement
<ENTITY
>, SelectionListener
, IEnableableFormElement
{
48 protected ENTITY entity
;
50 private final Composite container
;
53 * Composite "around" the actual content. Is used for control action like e.g. remove button
55 private final Composite box
;
57 private boolean isEnabled
;
59 private Button btnRemove
;
61 private Button btnChooseEntity
;
63 private Color backgroundColor
;
65 private LabelElement warnForReferencedObjects
;
67 public AbstractEntityCollectionElement(CdmFormFactory formFactory
,
68 AbstractFormSection section
, ENTITY entity
, SelectionListener removeListener
,
69 Color backgroundColor
, int style
) {
70 super(formFactory
, (ICdmFormElement
) section
);
75 formFactory
.addPropertyChangeListener(this);
77 box
= formFactory
.createComposite(section
.getLayoutComposite());
78 box
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
81 TableWrapLayout boxLayout
= LayoutConstants
.LAYOUT(4, false);
82 boxLayout
.topMargin
= 4;
83 boxLayout
.bottomMargin
= 4;
84 box
.setLayout(boxLayout
);
86 box
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
88 container
= formFactory
.createComposite(box
);
89 container
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
91 setLayoutComposite(container
);
93 addControl(container
);
94 TableWrapLayout containerLayout
= LayoutConstants
.LAYOUT(2, false);
95 containerLayout
.horizontalSpacing
= 5;
97 container
.setLayout(containerLayout
);
98 container
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
100 if (removeListener
!= null) {
101 btnRemove
= formFactory
.createButton(box
, null, SWT
.PUSH
);
102 addControl(btnRemove
);
103 btnRemove
.setLayoutData(LayoutConstants
.RIGHT());
104 btnRemove
.setImage(ImageResources
105 .getImage(ImageResources
.TRASH_ICON
));
106 btnRemove
.setToolTipText("Remove");
108 btnRemove
.addSelectionListener(removeListener
);
109 btnRemove
.setEnabled(isEnabled
);
115 createControls(this, style
);
119 * Init gets executed before any other setup of the section takes place
121 * Implement this if you want to configure the section
124 // default implementation is empty
127 public abstract void setEntity(ENTITY entity
);
130 public ENTITY
getEntity() {
135 * Sub classes should override to provide the functionality to choose the
136 * entity from existing ones from the data source.<br>
137 * <b>Note:</b> to enable this functionality sub classes have to set
138 * the corresponding flag in the super constructor
139 * @return an existing entity from the data source
141 protected ENTITY
selectFromDialog(){
145 public abstract void createControls(ICdmFormElement element
, int style
);
148 * Mark <code>this</code> element as selected.
151 public void setSelected(boolean selected
) {
153 for (ICdmFormElement element
: getElements()) {
154 if (element
instanceof ISelectable
) {
155 ((ISelectable
) element
).setSelected(selected
);
158 setBackground(selected ? SELECTED
: getPersistentBackground());
162 public void propertyChange(PropertyChangeEvent event
) {
166 Object eventSource
= event
.getSource();
167 if (getElements().contains(eventSource
) || getControls().contains(eventSource
)) {
168 handleEvent(eventSource
);
172 public abstract void handleEvent(Object eventSource
);
175 public void setBackground(Color color
) {
176 if(box
.isDisposed() || container
.isDisposed()){
179 backgroundColor
= color
;
180 super.setBackground(color
);
181 box
.setBackground(color
);
182 container
.setBackground(color
);
188 * React when selection occurs
191 public void widgetSelected(SelectionEvent e
) {
196 public void widgetDefaultSelected(SelectionEvent e
) {
200 public Composite
getLayoutComposite() {
204 public Color
getBackgroundColor() {
205 return backgroundColor
;
208 public Composite
getBox() {
212 public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects
) {
213 this.warnForReferencedObjects
= warnForReferencedObjects
;
216 public void setWarnForReferencingObjects(ICdmFormElement formElement
, int defaultReferencingObjects
){
217 if (entity
instanceof CdmBase
){
218 CdmBase cdmBase
= (CdmBase
)entity
;
219 if (cdmBase
.getId() != 0){
220 long referencingObjectsCount
= CdmStore
.getCommonService().getReferencingObjectsCount(cdmBase
);
222 if (referencingObjectsCount
> defaultReferencingObjects
){
223 setWarnForReferencedObjects(formFactory
.createLabel(formElement
, CdmUtils
.Nz("The "+ cdmBase
.getUserFriendlyTypeName()+" is referenced by more than one object, if you change it, it is changed for all these objects")));
224 warnForReferencedObjects
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
225 warnForReferencedObjects
.setLayout(LayoutConstants
.FILL(2, 3));
226 warnForReferencedObjects
.setForeground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
232 public void setWarnForReferencingObjects(ICdmFormElement formElement
){
233 setWarnForReferencingObjects(formElement
, 1);
236 public void setWarnForReferencingObjectsVisible(boolean isVisible
){
237 if (warnForReferencedObjects
!= null){
238 warnForReferencedObjects
.setVisible(isVisible
);
243 public void setEnabled(boolean enabled
) {
244 this.isEnabled
= enabled
;
245 if (btnRemove
!= null){
246 btnRemove
.setEnabled(enabled
);
248 if (btnChooseEntity
!= null){
249 btnChooseEntity
.setEnabled(enabled
);
252 for (ICdmFormElement element
:this.getElements()){
253 if (element
instanceof IEnableableFormElement
){
254 ((IEnableableFormElement
)element
).setEnabled(enabled
);
260 public boolean isEnabled() {