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
{
56 * The default number of columns in detail sections
58 public static final int DEFAULT_NUM_COLUMNS
= 2;
60 private ISelectionProvider selectionProvider
;
62 private ENTITY entity
;
64 private final Set
<ICdmFormElement
> elements
= new HashSet
<ICdmFormElement
>();
66 protected CdmFormFactory formFactory
;
68 private List
<IPropertyChangeListener
> propertyChangeListeners
;
70 private ICdmFormElement parentElement
;
72 private Color persistentBackgroundColor
;
76 * Constructor for AbstractFormSection.
84 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
86 * @param parentElement
87 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
92 protected AbstractFormSection(CdmFormFactory formFactory
, ICdmFormElement parentElement
, int style
) {
93 super(parentElement
.getLayoutComposite(), style
);
95 this.parentElement
= parentElement
;
97 this.formFactory
= formFactory
;
99 this.setLayoutData(LayoutConstants
.FILL());
101 Composite client
= formFactory
.createComposite(this, SWT
.WRAP
);
102 client
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
104 TableWrapLayout layout
= LayoutConstants
.LAYOUT();
105 layout
.bottomMargin
= 10;
106 layout
.rightMargin
= 5;
108 client
.setLayout(layout
);
110 this.setClient(client
);
116 * Constructor for AbstractFormSection.
120 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
122 * @param conversation
123 * a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
125 * @param parentElement
126 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
128 * @param selectionProvider
129 * a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
133 protected AbstractFormSection(CdmFormFactory formFactory
, ICdmFormElement parentElement
, ISelectionProvider selectionProvider
, int style
) {
134 this(formFactory
, parentElement
, style
);
135 this.selectionProvider
= selectionProvider
;
140 * Getter for the field <code>propertyChangeListeners</code>.
143 * @return a {@link java.util.Set} object.
146 public List
<IPropertyChangeListener
> getPropertyChangeListeners() {
147 return propertyChangeListeners
;
152 public void setPropertyChangeListeners(
153 List
<IPropertyChangeListener
> propertyChangeListeners
) {
154 this.propertyChangeListeners
= propertyChangeListeners
;
159 * Setter for the field <code>entity</code>.
165 public void setEntity(ENTITY entity
) {
166 this.entity
= entity
;
172 * @see eu.etaxonomy.taxeditor.forms.IEntityElement#getEntity()
176 * Getter for the field <code>entity</code>.
179 * @return a ENTITY object.
182 public ENTITY
getEntity() {
191 * @return a {@link org.eclipse.ui.forms.widgets.ToggleHyperlink} object.
193 public ToggleHyperlink
getToggle() {
202 * @return a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection}
205 public AbstractFormSection
<ENTITY
> getSection() {
213 * eu.etaxonomy.taxeditor.forms.IPropertyChangeEmitter#firePropertyChangeEvent
218 public void firePropertyChangeEvent(CdmPropertyChangeEvent event
) {
219 Assert
.isNotNull(propertyChangeListeners
, "No property change listeners.");
221 for (Object listener
: propertyChangeListeners
) {
222 ((IPropertyChangeListener
) listener
).propertyChange(event
);
224 } catch (ConcurrentModificationException e
) {
225 MessagingUtils
.warn(getClass(), "ConcurrentModificationException while handling PropertyChangeEvents."
226 + " It seems like this is not critical");
231 * Fires a {@link CdmPropertyChangeEvent} with the given object as source.
234 * the object on which the property changed
236 public void firePropertyChangeEvent(Object object
) {
237 firePropertyChangeEvent(object
, null);
241 * Fires a {@link CdmPropertyChangeEvent} with the given object as source
242 * also containing the originating event
245 * the object on which the property changed
246 * @param originatingEvent
247 * the originating event
249 public void firePropertyChangeEvent(Object object
,
250 PropertyChangeEvent originatingEvent
) {
251 firePropertyChangeEvent(new CdmPropertyChangeEvent(object
,
258 * @see org.eclipse.swt.widgets.Composite#setFocus()
262 public boolean setFocus() {
263 return getClient().setFocus();
270 * org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse
271 * .jface.util.PropertyChangeEvent)
275 public void propertyChange(PropertyChangeEvent event
) {
276 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
283 * org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics
288 public void setBackground(Color color
) {
289 for (ICdmFormElement element
: getElements()) {
290 element
.setBackground(color
);
292 getLayoutComposite().setBackground(color
);
293 super.setBackground(color
);
297 public void setPersistentBackground(Color color
) {
298 persistentBackgroundColor
= color
;
299 setBackground(color
);
303 public Color
getPersistentBackground() {
304 return persistentBackgroundColor
;
314 * a {@link org.eclipse.swt.events.SelectionEvent} object.
316 public void widgetSelected(SelectionEvent e
) {
317 Widget widget
= e
.widget
;
319 if (widget
instanceof Control
) {
320 Control control
= (Control
) widget
;
321 if (checkControlAncestryForWidget(control
)) {
322 if (getEntity() != null) {
323 IStructuredSelection selection
= new StructuredSelection(getEntity());
324 if (selectionProvider
!= null) {
325 selectionProvider
.setSelection(selection
);
332 private boolean checkControlAncestryForWidget(Control control
) {
333 if (control
.equals(this)) {
336 Control parent
= control
.getParent();
337 if (parent
== null) {
340 return checkControlAncestryForWidget(parent
);
347 public void setSelected(boolean selected
) {
349 setBackground(Display
.getCurrent().getSystemColor(
350 SWT
.COLOR_LIST_SELECTION
));
352 setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
358 public void selectionChanged(SelectionChangedEvent event
) {
359 if (event
.getSelection() == CdmFormFactory
.EMPTY_SELECTION
) {
363 IStructuredSelection selection
= (IStructuredSelection
) event
367 Object selectedObject
= selection
.getFirstElement();
369 if (selectedObject
!= null && selectedObject
.equals(getEntity())) {
376 * addSelectionListener
380 * a {@link org.eclipse.swt.events.SelectionListener} object.
382 public void addSelectionListener(SelectionListener listener
) {
383 addListener(SWT
.Selection
, new TypedListener(listener
));
388 * removeSelectionListener
392 * a {@link org.eclipse.swt.events.SelectionListener} object.
394 public void removeSelectionListener(SelectionListener listener
) {
395 removeListener(SWT
.Selection
, listener
);
400 public void addElement(ICdmFormElement element
) {
401 elements
.add(element
);
410 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
413 protected void removeElement(ICdmFormElement element
) {
414 elements
.remove(element
);
423 public void removeElements() {
424 for (ICdmFormElement childElement
: getElements()) {
426 childElement
.removeElements();
428 // unregister selection arbitrator
429 if (childElement
instanceof ISelectableElement
) {
430 ISelectableElement selectableElement
= (ISelectableElement
) childElement
;
431 if (selectableElement
.getSelectionArbitrator() != null) {
432 formFactory
.destroySelectionArbitrator(selectableElement
433 .getSelectionArbitrator());
437 // unregister propertyChangeListener
438 formFactory
.removePropertyChangeListener(childElement
);
440 // dispose of the controls
441 for (Control control
: childElement
.getControls()) {
442 // we added the layoutComposite of the parental element as the
443 // layout composite to this formElement
444 // but we do not want to destroy it.
445 if (control
.equals(childElement
.getLayoutComposite())) {
459 * Getter for the field <code>parentElement</code>.
462 * @return a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
465 public ICdmFormElement
getParentElement() {
466 return parentElement
;
471 * Getter for the field <code>elements</code>.
474 * @return a {@link java.util.Set} object.
477 public Set
<ICdmFormElement
> getElements() {
486 * @return a {@link java.util.Set} object.
489 public Set
<Control
> getControls() {
490 Set
<Control
> controls
= new HashSet
<Control
>();
492 for (Control control
: getChildren()) {
493 controls
.add(control
);
501 public void dispose() {
511 * @return a {@link org.eclipse.swt.widgets.Composite} object.
514 public Composite
getLayoutComposite() {
515 return (Composite
) getClient();
520 public boolean containsFormElement(ICdmFormElement formElement
) {
521 if (formElement
== this) {
524 for (ICdmFormElement element
: getElements()) {
525 boolean contains
= element
.containsFormElement(formElement
);
526 if (contains
== true) {
536 * Getter for the field <code>formFactory</code>.
539 * @return a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
542 public CdmFormFactory
getFormFactory() {
549 * @see eu.etaxonomy.taxeditor.forms.ICdmFormElement#refresh()
557 public void refresh() {
558 // empty default implementation
564 * getConversationHolder
567 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
571 public ConversationHolder
getConversationHolder() {
572 if(getParentElement() instanceof RootElement
|| getParentElement() == null){
574 IEditorPart activeEditor
= AbstractUtility
.getActiveEditor();
575 if(activeEditor
instanceof IConversationEnabled
){
576 ConversationHolder conversation
= ((IConversationEnabled
) AbstractUtility
.getActiveEditor()).getConversationHolder();
579 }else if(getParentElement() instanceof IConversationEnabled
){
580 return ((IConversationEnabled
) getParentElement()).getConversationHolder();
582 MessagingUtils
.messageDialog("Could not get conversation for AbstractFormSection",
583 getClass(), "There is an error in the implementation. There should have been an active editor but it wasn't",
584 new IllegalArgumentException());
592 public void update(CdmDataChangeMap changeEvents
) {