4 package eu
.etaxonomy
.taxeditor
.ui
.element
;
6 import java
.util
.ConcurrentModificationException
;
7 import java
.util
.HashSet
;
11 import org
.eclipse
.core
.runtime
.Assert
;
12 import org
.eclipse
.jface
.util
.IPropertyChangeListener
;
13 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
14 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
15 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
16 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
17 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
18 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.events
.SelectionListener
;
22 import org
.eclipse
.swt
.graphics
.Color
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.TypedListener
;
27 import org
.eclipse
.swt
.widgets
.Widget
;
28 import org
.eclipse
.ui
.IEditorPart
;
29 import org
.eclipse
.ui
.forms
.widgets
.Section
;
30 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
31 import org
.eclipse
.ui
.forms
.widgets
.ToggleHyperlink
;
33 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
34 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
36 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
37 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
41 * Abstract super class for a {@link Section} GUI element that visualizes a CDM
42 * entity, manages a conversation and listens to selections
45 * @param <ENTITY> A CDM entity which should be visualized by this section.
48 * @created Feb 22, 2010
52 //TODO shouldn't ENTITY be bound with super class ICdmBase for example (AbstractFormSection<ENTITY extends ICdmBase>)?
53 public abstract class AbstractFormSection
<ENTITY
> extends Section
implements ISelectionChangedListener
, IEntityElement
<ENTITY
>, IConversationEnabled
{
55 private ISelectionProvider selectionProvider
;
57 private ENTITY entity
;
59 private final Set
<ICdmFormElement
> elements
= new HashSet
<ICdmFormElement
>();
61 protected CdmFormFactory formFactory
;
63 private List
<IPropertyChangeListener
> propertyChangeListeners
;
65 private ICdmFormElement parentElement
;
67 private Color persistentBackgroundColor
;
71 * Constructor for AbstractFormSection.
79 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
81 * @param parentElement
82 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
87 protected AbstractFormSection(CdmFormFactory formFactory
, ICdmFormElement parentElement
, int style
) {
88 super(parentElement
.getLayoutComposite(), style
);
90 this.parentElement
= parentElement
;
92 this.formFactory
= formFactory
;
94 this.setLayoutData(LayoutConstants
.FILL());
96 Composite client
= formFactory
.createComposite(this, SWT
.WRAP
);
97 client
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
99 TableWrapLayout layout
= LayoutConstants
.LAYOUT();
100 layout
.bottomMargin
= 10;
101 layout
.rightMargin
= 5;
103 client
.setLayout(layout
);
105 this.setClient(client
);
111 * Constructor for AbstractFormSection.
115 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
117 * @param conversation
118 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
120 * @param parentElement
121 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
123 * @param selectionProvider
124 * a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
128 protected AbstractFormSection(CdmFormFactory formFactory
, ICdmFormElement parentElement
, ISelectionProvider selectionProvider
, int style
) {
129 this(formFactory
, parentElement
, style
);
130 this.selectionProvider
= selectionProvider
;
135 * Getter for the field <code>propertyChangeListeners</code>.
138 * @return a {@link java.util.Set} object.
141 public List
<IPropertyChangeListener
> getPropertyChangeListeners() {
142 return propertyChangeListeners
;
147 public void setPropertyChangeListeners(
148 List
<IPropertyChangeListener
> propertyChangeListeners
) {
149 this.propertyChangeListeners
= propertyChangeListeners
;
154 * Setter for the field <code>entity</code>.
160 public void setEntity(ENTITY entity
) {
161 this.entity
= entity
;
167 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
171 * Getter for the field <code>entity</code>.
174 * @return a ENTITY object.
177 public ENTITY
getEntity() {
186 * @return a {@link org.eclipse.ui.forms.widgets.ToggleHyperlink} object.
188 public ToggleHyperlink
getToggle() {
197 * @return a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
200 public AbstractFormSection
<ENTITY
> getSection() {
208 * eu.etaxonomy.taxeditor.forms.IPropertyChangeEmitter#firePropertyChangeEvent
213 public void firePropertyChangeEvent(CdmPropertyChangeEvent event
) {
214 Assert
.isNotNull(propertyChangeListeners
, "No property change listeners.");
216 for (Object listener
: propertyChangeListeners
) {
217 ((IPropertyChangeListener
) listener
).propertyChange(event
);
219 } catch (ConcurrentModificationException e
) {
220 MessagingUtils
.warn(getClass(), "ConcurrentModificationException while handling PropertyChangeEvents."
221 + " It seems like this is not critical");
226 * Fires a {@link CdmPropertyChangeEvent} with the given object as source.
229 * the object on which the property changed
231 public void firePropertyChangeEvent(Object object
) {
232 firePropertyChangeEvent(object
, null);
236 * Fires a {@link CdmPropertyChangeEvent} with the given object as source
237 * also containing the originating event
240 * the object on which the property changed
241 * @param originatingEvent
242 * the originating event
244 public void firePropertyChangeEvent(Object object
,
245 PropertyChangeEvent originatingEvent
) {
246 firePropertyChangeEvent(new CdmPropertyChangeEvent(object
,
253 * @see org.eclipse.swt.widgets.Composite#setFocus()
257 public boolean setFocus() {
258 return getClient().setFocus();
265 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
266 * .jface.util.PropertyChangeEvent)
270 public void propertyChange(PropertyChangeEvent event
) {
271 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
278 * org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics
283 public void setBackground(Color color
) {
284 for (ICdmFormElement element
: getElements()) {
285 element
.setBackground(color
);
287 getLayoutComposite().setBackground(color
);
288 super.setBackground(color
);
292 public void setPersistentBackground(Color color
) {
293 persistentBackgroundColor
= color
;
294 setBackground(color
);
298 public Color
getPersistentBackground() {
299 return persistentBackgroundColor
;
309 * a {@link org.eclipse.swt.events.SelectionEvent} object.
311 public void widgetSelected(SelectionEvent e
) {
312 Widget widget
= e
.widget
;
314 if (widget
instanceof Control
) {
315 Control control
= (Control
) widget
;
316 if (checkControlAncestryForWidget(control
)) {
317 if (getEntity() != null) {
318 IStructuredSelection selection
= new StructuredSelection(getEntity());
319 if (selectionProvider
!= null) {
320 selectionProvider
.setSelection(selection
);
327 private boolean checkControlAncestryForWidget(Control control
) {
328 if (control
.equals(this)) {
331 Control parent
= control
.getParent();
332 if (parent
== null) {
335 return checkControlAncestryForWidget(parent
);
342 public void setSelected(boolean selected
) {
344 setBackground(Display
.getCurrent().getSystemColor(
345 SWT
.COLOR_LIST_SELECTION
));
347 setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
353 public void selectionChanged(SelectionChangedEvent event
) {
354 if (event
.getSelection() == CdmFormFactory
.EMPTY_SELECTION
) {
358 IStructuredSelection selection
= (IStructuredSelection
) event
362 Object selectedObject
= selection
.getFirstElement();
364 if (selectedObject
!= null && selectedObject
.equals(getEntity())) {
371 * addSelectionListener
375 * a {@link org.eclipse.swt.events.SelectionListener} object.
377 public void addSelectionListener(SelectionListener listener
) {
378 addListener(SWT
.Selection
, new TypedListener(listener
));
383 * removeSelectionListener
387 * a {@link org.eclipse.swt.events.SelectionListener} object.
389 public void removeSelectionListener(SelectionListener listener
) {
390 removeListener(SWT
.Selection
, listener
);
395 public void addElement(ICdmFormElement element
) {
396 elements
.add(element
);
405 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
408 protected void removeElement(ICdmFormElement element
) {
409 elements
.remove(element
);
418 public void removeElements() {
419 for (ICdmFormElement childElement
: getElements()) {
421 childElement
.removeElements();
423 // unregister selection arbitrator
424 if (childElement
instanceof ISelectableElement
) {
425 ISelectableElement selectableElement
= (ISelectableElement
) childElement
;
426 if (selectableElement
.getSelectionArbitrator() != null) {
427 formFactory
.destroySelectionArbitrator(selectableElement
428 .getSelectionArbitrator());
432 // unregister propertyChangeListener
433 formFactory
.removePropertyChangeListener(childElement
);
435 // dispose of the controls
436 for (Control control
: childElement
.getControls()) {
437 // we added the layoutComposite of the parental element as the
438 // layout composite to this formElement
439 // but we do not want to destroy it.
440 if (control
.equals(childElement
.getLayoutComposite())) {
454 * Getter for the field <code>parentElement</code>.
457 * @return a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
460 public ICdmFormElement
getParentElement() {
461 return parentElement
;
466 * Getter for the field <code>elements</code>.
469 * @return a {@link java.util.Set} object.
472 public Set
<ICdmFormElement
> getElements() {
481 * @return a {@link java.util.Set} object.
484 public Set
<Control
> getControls() {
485 Set
<Control
> controls
= new HashSet
<Control
>();
487 for (Control control
: getChildren()) {
488 controls
.add(control
);
496 public void dispose() {
506 * @return a {@link org.eclipse.swt.widgets.Composite} object.
509 public Composite
getLayoutComposite() {
510 return (Composite
) getClient();
515 public boolean containsFormElement(ICdmFormElement formElement
) {
516 if (formElement
== this) {
519 for (ICdmFormElement element
: getElements()) {
520 boolean contains
= element
.containsFormElement(formElement
);
521 if (contains
== true) {
531 * Getter for the field <code>formFactory</code>.
534 * @return a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
537 public CdmFormFactory
getFormFactory() {
544 * @see eu.etaxonomy.taxeditor.forms.ICdmFormElement#refresh()
552 public void refresh() {
553 // empty default implementation
559 * getConversationHolder
562 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
566 public ConversationHolder
getConversationHolder() {
567 if(getParentElement() instanceof RootElement
|| getParentElement() == null){
569 IEditorPart activeEditor
= AbstractUtility
.getActiveEditor();
570 if(activeEditor
instanceof IConversationEnabled
){
571 ConversationHolder conversation
= ((IConversationEnabled
) AbstractUtility
.getActiveEditor()).getConversationHolder();
574 }else if(getParentElement() instanceof IConversationEnabled
){
575 return ((IConversationEnabled
) getParentElement()).getConversationHolder();
577 MessagingUtils
.messageDialog("Could not get conversation for AbstractFormSection",
578 getClass(), "There is an error in the implementation. There should have been an active editor but it wasn't",
579 new IllegalArgumentException());
587 public void update(CdmDataChangeMap changeEvents
) {