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