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
.api
.conversation
.ConversationHolder
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
23 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
26 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
27 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
28 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEntityElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
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
46 public abstract class AbstractEntityCollectionElement
<ENTITY
>
47 extends AbstractCdmFormElement
48 implements IEntityElement
<ENTITY
>, SelectionListener
,
49 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 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
);
111 createControls(this, style
);
117 * Init gets executed before any other setup of the section takes place
119 * Implement this if you want to configure the section
122 // default implementation is empty
125 public abstract void setEntity(ENTITY entity
);
128 public ENTITY
getEntity() {
133 * Sub classes should override to provide the functionality to choose the
134 * entity from existing ones from the data source.<br>
135 * <b>Note:</b> to enable this functionality sub classes have to set
136 * the corresponding flag in the super constructor
137 * @return an existing entity from the data source
139 protected ENTITY
selectFromDialog(){
143 public abstract void createControls(ICdmFormElement element
, int style
);
146 * Mark <code>this</code> element as selected.
149 public void setSelected(boolean selected
) {
151 for (ICdmFormElement element
: getElements()) {
152 if (element
instanceof ISelectable
) {
153 ((ISelectable
) element
).setSelected(selected
);
156 setBackground(selected ? SELECTED
: getPersistentBackground());
160 public void propertyChange(PropertyChangeEvent event
) {
164 Object eventSource
= event
.getSource();
165 if (getElements().contains(eventSource
) || getControls().contains(eventSource
)) {
166 handleEvent(eventSource
);
170 public abstract void handleEvent(Object eventSource
);
173 public void setBackground(Color color
) {
174 if(box
.isDisposed() || container
.isDisposed()){
177 backgroundColor
= color
;
178 super.setBackground(color
);
179 box
.setBackground(color
);
180 container
.setBackground(color
);
186 * React when selection occurs
189 public void widgetSelected(SelectionEvent e
) {
194 public void widgetDefaultSelected(SelectionEvent e
) {
198 public Composite
getLayoutComposite() {
202 public Color
getBackgroundColor() {
203 return backgroundColor
;
206 public Composite
getBox() {
211 public ConversationHolder
getConversationHolder() {
212 if (getParentElement() instanceof IConversationEnabled
) {
213 return ((IConversationEnabled
) getParentElement())
214 .getConversationHolder();
216 throw new IllegalArgumentException(
217 "Parent element should be IConversationEnabled");
221 public void update(CdmDataChangeMap changeEvents
) {
224 public void setWarnForReferencedObjects(LabelElement warnForReferencedObjects
) {
225 this.warnForReferencedObjects
= warnForReferencedObjects
;
228 public void setWarnForReferencingObjects(ICdmFormElement formElement
, int defaultReferencingObjects
){
229 if (entity
instanceof CdmBase
){
230 CdmBase cdmBase
= (CdmBase
)entity
;
231 if (cdmBase
.getId() != 0){
232 long referencingObjectsCount
= CdmStore
.getCommonService().getReferencingObjectsCount(cdmBase
);
234 if (referencingObjectsCount
> defaultReferencingObjects
){
235 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")));
236 warnForReferencedObjects
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
237 warnForReferencedObjects
.setLayout(LayoutConstants
.FILL(2, 3));
238 warnForReferencedObjects
.setForeground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
244 public void setWarnForReferencingObjects(ICdmFormElement formElement
){
245 setWarnForReferencingObjects(formElement
, 1);
248 public void setWarnForReferencingObjectsVisible(boolean isVisible
){
249 if (warnForReferencedObjects
!= null){
250 warnForReferencedObjects
.setVisible(isVisible
);