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
;
32 * <p>SelectionArbitrator class.</p>
35 * @created Feb 25, 2010
38 public class SelectionArbitrator
implements SelectionListener
, ISelectionChangedListener
{
39 private IEntityElement entityElement
;
41 private Set
<ISelectionProvider
> selectionProviders
= new HashSet
<ISelectionProvider
>();
44 * <p>Constructor for SelectionArbitrator.</p>
46 public SelectionArbitrator(){}
49 * <p>Constructor for SelectionArbitrator.</p>
51 * @param selectableComposite a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
53 public SelectionArbitrator(IEntityElement selectableComposite
){
54 this.entityElement
= selectableComposite
;
58 public void widgetSelected(SelectionEvent e
) {
59 Widget widget
= e
.widget
;
61 if(widget
instanceof Control
){
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
)) return true;
89 // descend into child elements
90 for(ICdmFormElement childElement
: element
.getElements()){
91 isWidgetPartOfFormElement(widget
, childElement
);
98 * <p>addSelectionProvider</p>
100 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
102 public void addSelectionProvider(ISelectionProvider selectionProvider
){
103 selectionProviders
.add(selectionProvider
);
107 * <p>removeSelectionProvider</p>
109 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
111 public void removeSelectionProvider(ISelectionProvider selectionProvider
){
112 selectionProviders
.remove(selectionProvider
);
116 public void widgetDefaultSelected(SelectionEvent e
) {}
119 * <p>Getter for the field <code>entityElement</code>.</p>
121 * @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
123 public IEntityElement
getEntityElement() {
124 return entityElement
;
128 * <p>setEntityComposite</p>
130 * @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
132 public void setEntityComposite(IEntityElement entityElement
) {
133 this.entityElement
= entityElement
;
137 public void selectionChanged(SelectionChangedEvent event
) {
138 // if(!(event.getSource() instanceof CdmDetailsViewer)){
143 entityElement
.setSelected(false);
144 }catch(SWTException e
){
145 StoreUtil
.error(getClass(), "Widgets of element are disposed", e
);
147 ISelection selection
= event
.getSelection();
148 if(selection
!= null && selection
instanceof StructuredSelection
){
149 Object selectedElement
= ((StructuredSelection
) selection
).getFirstElement();
150 if(entityElement
.getEntity() != null && entityElement
.getEntity().equals(selectedElement
)){
151 entityElement
.setSelected(true);