3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.element
;
13 import java
.util
.HashSet
;
16 import org
.eclipse
.jface
.viewers
.ISelection
;
17 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
18 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
19 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
20 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
21 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
22 import org
.eclipse
.swt
.SWTException
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.events
.SelectionListener
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.Widget
;
28 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
29 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
33 * <p>SelectionArbitrator class.</p>
36 * @created Feb 25, 2010
39 public class SelectionArbitrator
implements SelectionListener
, ISelectionChangedListener
{
40 private IEntityElement entityElement
;
42 private Set
<ISelectionProvider
> selectionProviders
= new HashSet
<ISelectionProvider
>();
45 * <p>Constructor for SelectionArbitrator.</p>
47 public SelectionArbitrator(){}
50 * <p>Constructor for SelectionArbitrator.</p>
52 * @param selectableComposite a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
54 public SelectionArbitrator(IEntityElement selectableComposite
){
55 this.entityElement
= selectableComposite
;
59 public void widgetSelected(SelectionEvent e
) {
60 Widget widget
= e
.widget
;
62 if(widget
instanceof Control
){
63 Control control
= (Control
) widget
;
64 if(isWidgetPartOfFormElement(control
, entityElement
)){
65 if(entityElement
.getEntity() != null){
66 IStructuredSelection selection
= new StructuredSelection(entityElement
.getEntity());
67 setSelection(selection
);
73 private void setSelection(ISelection selection
){
74 for(ISelectionProvider selectionProvider
: selectionProviders
){
75 selectionProvider
.setSelection(selection
);
79 private boolean isWidgetPartOfFormElement(Control widget
, ICdmFormElement element
){
80 // check if widget is the controls
81 if(widget
.equals(element
.getLayoutComposite())){
85 // check if widget is controlled by the given element
86 for(Control control
: element
.getControls()){
87 if(control
.equals(widget
)) return true;
90 // descend into child elements
91 for(ICdmFormElement childElement
: element
.getElements()){
92 isWidgetPartOfFormElement(widget
, childElement
);
99 * <p>addSelectionProvider</p>
101 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
103 public void addSelectionProvider(ISelectionProvider selectionProvider
){
104 selectionProviders
.add(selectionProvider
);
108 * <p>removeSelectionProvider</p>
110 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
112 public void removeSelectionProvider(ISelectionProvider selectionProvider
){
113 selectionProviders
.remove(selectionProvider
);
117 public void widgetDefaultSelected(SelectionEvent e
) {}
120 * <p>Getter for the field <code>entityElement</code>.</p>
122 * @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
124 public IEntityElement
getEntityElement() {
125 return entityElement
;
129 * <p>setEntityComposite</p>
131 * @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
133 public void setEntityComposite(IEntityElement entityElement
) {
134 this.entityElement
= entityElement
;
138 public void selectionChanged(SelectionChangedEvent event
) {
139 // if(!(event.getSource() instanceof CdmDetailsViewer)){
144 entityElement
.setSelected(false);
145 }catch(SWTException e
){
146 StoreUtil
.error(getClass(), "Widgets of element are disposed", e
);
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);