merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / SelectionArbitrator.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.element;
12
13 import java.util.HashSet;
14 import java.util.Set;
15
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;
27
28 import eu.etaxonomy.taxeditor.store.StoreUtil;
29 import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
30
31
32 /**
33 * <p>SelectionArbitrator class.</p>
34 *
35 * @author n.hoffmann
36 * @created Feb 25, 2010
37 * @version 1.0
38 */
39 public class SelectionArbitrator implements SelectionListener, ISelectionChangedListener{
40 private IEntityElement entityElement;
41
42 private Set<ISelectionProvider> selectionProviders = new HashSet<ISelectionProvider>();
43
44 /**
45 * <p>Constructor for SelectionArbitrator.</p>
46 */
47 public SelectionArbitrator(){}
48
49 /**
50 * <p>Constructor for SelectionArbitrator.</p>
51 *
52 * @param selectableComposite a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
53 */
54 public SelectionArbitrator(IEntityElement selectableComposite){
55 this.entityElement = selectableComposite;
56 }
57
58 /** {@inheritDoc} */
59 public void widgetSelected(SelectionEvent e) {
60 Widget widget = e.widget;
61
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);
68 }
69 }
70 }
71 }
72
73 private void setSelection(ISelection selection){
74 for(ISelectionProvider selectionProvider : selectionProviders){
75 selectionProvider.setSelection(selection);
76 }
77 }
78
79 private boolean isWidgetPartOfFormElement(Control widget, ICdmFormElement element){
80 // check if widget is the controls
81 if(widget.equals(element.getLayoutComposite())){
82 return true;
83 }
84
85 // check if widget is controlled by the given element
86 for(Control control : element.getControls()){
87 if(control.equals(widget)) return true;
88 }
89
90 // descend into child elements
91 for(ICdmFormElement childElement : element.getElements()){
92 isWidgetPartOfFormElement(widget, childElement);
93 }
94
95 return false;
96 }
97
98 /**
99 * <p>addSelectionProvider</p>
100 *
101 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
102 */
103 public void addSelectionProvider(ISelectionProvider selectionProvider){
104 selectionProviders.add(selectionProvider);
105 }
106
107 /**
108 * <p>removeSelectionProvider</p>
109 *
110 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
111 */
112 public void removeSelectionProvider(ISelectionProvider selectionProvider){
113 selectionProviders.remove(selectionProvider);
114 }
115
116 /** {@inheritDoc} */
117 public void widgetDefaultSelected(SelectionEvent e) {}
118
119 /**
120 * <p>Getter for the field <code>entityElement</code>.</p>
121 *
122 * @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
123 */
124 public IEntityElement getEntityElement() {
125 return entityElement;
126 }
127
128 /**
129 * <p>setEntityComposite</p>
130 *
131 * @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
132 */
133 public void setEntityComposite(IEntityElement entityElement) {
134 this.entityElement = entityElement;
135 }
136
137 /** {@inheritDoc} */
138 public void selectionChanged(SelectionChangedEvent event) {
139 // if(!(event.getSource() instanceof CdmDetailsViewer)){
140 // return;
141 // }
142
143 try{
144 entityElement.setSelected(false);
145 }catch(SWTException e){
146 StoreUtil.error(getClass(), "Widgets of element are disposed", e);
147 }
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);
153 }
154 }
155 }
156 }