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
;
57 public void widgetSelected(SelectionEvent e
) {
58 Widget widget
= e
.widget
;
60 if(widget
instanceof Control
){
61 Control control
= (Control
) widget
;
62 if(isWidgetPartOfFormElement(control
, entityElement
)){
63 if(entityElement
.getEntity() != null){
64 IStructuredSelection selection
= new StructuredSelection(entityElement
.getEntity());
65 setSelection(selection
);
71 private void setSelection(ISelection selection
){
72 for(ISelectionProvider selectionProvider
: selectionProviders
){
73 selectionProvider
.setSelection(selection
);
77 private boolean isWidgetPartOfFormElement(Control widget
, ICdmFormElement element
){
78 // check if widget is the controls
79 if(widget
.equals(element
.getLayoutComposite())){
83 // check if widget is controlled by the given element
84 for(Control control
: element
.getControls()){
85 if(control
.equals(widget
)) return true;
88 // descend into child elements
89 for(ICdmFormElement childElement
: element
.getElements()){
90 isWidgetPartOfFormElement(widget
, childElement
);
97 * <p>addSelectionProvider</p>
99 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
101 public void addSelectionProvider(ISelectionProvider selectionProvider
){
102 selectionProviders
.add(selectionProvider
);
106 * <p>removeSelectionProvider</p>
108 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
110 public void removeSelectionProvider(ISelectionProvider selectionProvider
){
111 selectionProviders
.remove(selectionProvider
);
115 public void widgetDefaultSelected(SelectionEvent e
) {}
118 * <p>Getter for the field <code>entityElement</code>.</p>
120 * @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
122 public IEntityElement
getEntityElement() {
123 return entityElement
;
127 * <p>setEntityComposite</p>
129 * @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
131 public void setEntityComposite(IEntityElement entityElement
) {
132 this.entityElement
= entityElement
;
136 public void selectionChanged(SelectionChangedEvent event
) {
137 // if(!(event.getSource() instanceof CdmDetailsViewer)){
142 entityElement
.setSelected(false);
143 }catch(SWTException e
){
144 MessagingUtils
.error(getClass(), "Widgets of element are disposed", e
);
146 ISelection selection
= event
.getSelection();
147 if(selection
!= null && selection
instanceof StructuredSelection
){
148 Object selectedElement
= ((StructuredSelection
) selection
).getFirstElement();
149 if(entityElement
.getEntity() != null && entityElement
.getEntity().equals(selectedElement
)){
150 entityElement
.setSelected(true);