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
.element
;
12 import java
.util
.HashSet
;
15 import org
.eclipse
.jface
.viewers
.ISelection
;
16 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
17 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
18 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
19 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
20 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
21 import org
.eclipse
.swt
.SWTException
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.events
.SelectionListener
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Widget
;
27 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
31 * <p>SelectionArbitrator class.</p>
34 * @created Feb 25, 2010
37 public class SelectionArbitrator
implements SelectionListener
, ISelectionChangedListener
{
38 private IEntityElement entityElement
;
40 private Set
<ISelectionProvider
> selectionProviders
= new HashSet
<ISelectionProvider
>();
43 * <p>Constructor for SelectionArbitrator.</p>
45 public SelectionArbitrator(){}
48 * <p>Constructor for SelectionArbitrator.</p>
50 * @param selectableComposite a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
52 public SelectionArbitrator(IEntityElement selectableComposite
){
53 this.entityElement
= selectableComposite
;
58 public void widgetSelected(SelectionEvent e
) {
59 Widget widget
= e
.widget
;
61 if(widget
instanceof Control
&& !widget
.isDisposed()){
62 Control control
= (Control
) widget
;
63 if(isWidgetPartOfFormElement(control
, entityElement
)){
64 if(entityElement
.getEntity() != null){
65 IStructuredSelection selection
= new StructuredSelection(entityElement
.getEntity());
66 setSelection(selection
);
72 private void setSelection(ISelection selection
){
73 for(ISelectionProvider selectionProvider
: selectionProviders
){
74 selectionProvider
.setSelection(selection
);
78 private boolean isWidgetPartOfFormElement(Control widget
, ICdmFormElement element
){
79 // check if widget is the controls
80 if(widget
.equals(element
.getLayoutComposite())){
84 // check if widget is controlled by the given element
85 for(Control control
: element
.getControls()){
86 if(control
.equals(widget
)) {
91 // descend into child elements
92 for(ICdmFormElement childElement
: element
.getElements()){
93 isWidgetPartOfFormElement(widget
, childElement
);
100 * <p>addSelectionProvider</p>
102 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
104 public void addSelectionProvider(ISelectionProvider selectionProvider
){
105 selectionProviders
.add(selectionProvider
);
109 * <p>removeSelectionProvider</p>
111 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
113 public void removeSelectionProvider(ISelectionProvider selectionProvider
){
114 selectionProviders
.remove(selectionProvider
);
119 public void widgetDefaultSelected(SelectionEvent e
) {}
122 * <p>Getter for the field <code>entityElement</code>.</p>
124 * @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
126 public IEntityElement
getEntityElement() {
127 return entityElement
;
131 * <p>setEntityComposite</p>
133 * @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
135 public void setEntityComposite(IEntityElement entityElement
) {
136 this.entityElement
= entityElement
;
141 public void selectionChanged(SelectionChangedEvent event
) {
142 // if(!(event.getSource() instanceof CdmDetailsViewer)){
147 entityElement
.setSelected(false);
148 ISelection selection
= event
.getSelection();
149 if(selection
!= null && selection
instanceof StructuredSelection
){
150 Object selectedElement
= ((StructuredSelection
) selection
).getFirstElement();
151 if(entityElement
.getEntity() != null && entityElement
.getEntity().equals(selectedElement
)){
152 entityElement
.setSelected(true);
155 }catch(SWTException e
){
156 MessagingUtils
.error(getClass(), "Widgets of element are disposed", e
);