1 |
729887cf
|
n.hoffmann
|
/**
|
2 |
|
|
* Copyright (C) 2007 EDIT
|
3 |
64301fda
|
Katja Luther
|
* European Distributed Institute of Taxonomy
|
4 |
729887cf
|
n.hoffmann
|
* http://www.e-taxonomy.eu
|
5 |
64301fda
|
Katja Luther
|
*
|
6 |
729887cf
|
n.hoffmann
|
* 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 |
78222507
|
n.hoffmann
|
package eu.etaxonomy.taxeditor.ui.element;
|
11 |
729887cf
|
n.hoffmann
|
|
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 |
41e2f693
|
Cherian Mathew
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
28 |
35861667
|
n.hoffmann
|
|
29 |
729887cf
|
n.hoffmann
|
|
30 |
|
|
/**
|
31 |
3be6ef3e
|
n.hoffmann
|
* <p>SelectionArbitrator class.</p>
|
32 |
|
|
*
|
33 |
729887cf
|
n.hoffmann
|
* @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 |
64301fda
|
Katja Luther
|
|
42 |
3be6ef3e
|
n.hoffmann
|
/**
|
43 |
|
|
* <p>Constructor for SelectionArbitrator.</p>
|
44 |
|
|
*/
|
45 |
729887cf
|
n.hoffmann
|
public SelectionArbitrator(){}
|
46 |
64301fda
|
Katja Luther
|
|
47 |
3be6ef3e
|
n.hoffmann
|
/**
|
48 |
|
|
* <p>Constructor for SelectionArbitrator.</p>
|
49 |
|
|
*
|
50 |
78222507
|
n.hoffmann
|
* @param selectableComposite a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
|
51 |
3be6ef3e
|
n.hoffmann
|
*/
|
52 |
729887cf
|
n.hoffmann
|
public SelectionArbitrator(IEntityElement selectableComposite){
|
53 |
|
|
this.entityElement = selectableComposite;
|
54 |
|
|
}
|
55 |
64301fda
|
Katja Luther
|
|
56 |
3be6ef3e
|
n.hoffmann
|
/** {@inheritDoc} */
|
57 |
64301fda
|
Katja Luther
|
@Override
|
58 |
|
|
public void widgetSelected(SelectionEvent e) {
|
59 |
729887cf
|
n.hoffmann
|
Widget widget = e.widget;
|
60 |
64301fda
|
Katja Luther
|
|
61 |
|
|
if(widget instanceof Control && !widget.isDisposed()){
|
62 |
729887cf
|
n.hoffmann
|
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 |
64301fda
|
Katja Luther
|
}
|
70 |
729887cf
|
n.hoffmann
|
}
|
71 |
64301fda
|
Katja Luther
|
|
72 |
729887cf
|
n.hoffmann
|
private void setSelection(ISelection selection){
|
73 |
|
|
for(ISelectionProvider selectionProvider : selectionProviders){
|
74 |
|
|
selectionProvider.setSelection(selection);
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
64301fda
|
Katja Luther
|
|
78 |
729887cf
|
n.hoffmann
|
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 |
64301fda
|
Katja Luther
|
|
84 |
729887cf
|
n.hoffmann
|
// check if widget is controlled by the given element
|
85 |
|
|
for(Control control : element.getControls()){
|
86 |
64301fda
|
Katja Luther
|
if(control.equals(widget)) {
|
87 |
|
|
return true;
|
88 |
|
|
}
|
89 |
729887cf
|
n.hoffmann
|
}
|
90 |
64301fda
|
Katja Luther
|
|
91 |
729887cf
|
n.hoffmann
|
// descend into child elements
|
92 |
|
|
for(ICdmFormElement childElement : element.getElements()){
|
93 |
|
|
isWidgetPartOfFormElement(widget, childElement);
|
94 |
|
|
}
|
95 |
64301fda
|
Katja Luther
|
|
96 |
729887cf
|
n.hoffmann
|
return false;
|
97 |
|
|
}
|
98 |
64301fda
|
Katja Luther
|
|
99 |
3be6ef3e
|
n.hoffmann
|
/**
|
100 |
|
|
* <p>addSelectionProvider</p>
|
101 |
|
|
*
|
102 |
|
|
* @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
|
103 |
|
|
*/
|
104 |
729887cf
|
n.hoffmann
|
public void addSelectionProvider(ISelectionProvider selectionProvider){
|
105 |
|
|
selectionProviders.add(selectionProvider);
|
106 |
|
|
}
|
107 |
64301fda
|
Katja Luther
|
|
108 |
3be6ef3e
|
n.hoffmann
|
/**
|
109 |
|
|
* <p>removeSelectionProvider</p>
|
110 |
|
|
*
|
111 |
|
|
* @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
|
112 |
|
|
*/
|
113 |
729887cf
|
n.hoffmann
|
public void removeSelectionProvider(ISelectionProvider selectionProvider){
|
114 |
|
|
selectionProviders.remove(selectionProvider);
|
115 |
|
|
}
|
116 |
64301fda
|
Katja Luther
|
|
117 |
3be6ef3e
|
n.hoffmann
|
/** {@inheritDoc} */
|
118 |
64301fda
|
Katja Luther
|
@Override
|
119 |
|
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
120 |
729887cf
|
n.hoffmann
|
|
121 |
3be6ef3e
|
n.hoffmann
|
/**
|
122 |
|
|
* <p>Getter for the field <code>entityElement</code>.</p>
|
123 |
|
|
*
|
124 |
78222507
|
n.hoffmann
|
* @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
|
125 |
3be6ef3e
|
n.hoffmann
|
*/
|
126 |
729887cf
|
n.hoffmann
|
public IEntityElement getEntityElement() {
|
127 |
|
|
return entityElement;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
3be6ef3e
|
n.hoffmann
|
/**
|
131 |
|
|
* <p>setEntityComposite</p>
|
132 |
|
|
*
|
133 |
78222507
|
n.hoffmann
|
* @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
|
134 |
3be6ef3e
|
n.hoffmann
|
*/
|
135 |
729887cf
|
n.hoffmann
|
public void setEntityComposite(IEntityElement entityElement) {
|
136 |
|
|
this.entityElement = entityElement;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
3be6ef3e
|
n.hoffmann
|
/** {@inheritDoc} */
|
140 |
64301fda
|
Katja Luther
|
@Override
|
141 |
|
|
public void selectionChanged(SelectionChangedEvent event) {
|
142 |
729887cf
|
n.hoffmann
|
// if(!(event.getSource() instanceof CdmDetailsViewer)){
|
143 |
|
|
// return;
|
144 |
|
|
// }
|
145 |
64301fda
|
Katja Luther
|
|
146 |
729887cf
|
n.hoffmann
|
try{
|
147 |
30c956e4
|
Katja Luther
|
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 |
729887cf
|
n.hoffmann
|
}catch(SWTException e){
|
156 |
41e2f693
|
Cherian Mathew
|
MessagingUtils.error(getClass(), "Widgets of element are disposed", e);
|
157 |
30c956e4
|
Katja Luther
|
|
158 |
729887cf
|
n.hoffmann
|
}
|
159 |
30c956e4
|
Katja Luther
|
|
160 |
729887cf
|
n.hoffmann
|
}
|
161 |
|
|
}
|