refactoring of package names for consistency
[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
30
31 /**
32 * <p>SelectionArbitrator class.</p>
33 *
34 * @author n.hoffmann
35 * @created Feb 25, 2010
36 * @version 1.0
37 */
38 public class SelectionArbitrator implements SelectionListener, ISelectionChangedListener{
39 private IEntityElement entityElement;
40
41 private Set<ISelectionProvider> selectionProviders = new HashSet<ISelectionProvider>();
42
43 /**
44 * <p>Constructor for SelectionArbitrator.</p>
45 */
46 public SelectionArbitrator(){}
47
48 /**
49 * <p>Constructor for SelectionArbitrator.</p>
50 *
51 * @param selectableComposite a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
52 */
53 public SelectionArbitrator(IEntityElement selectableComposite){
54 this.entityElement = selectableComposite;
55 }
56
57 /** {@inheritDoc} */
58 public void widgetSelected(SelectionEvent e) {
59 Widget widget = e.widget;
60
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);
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)) return true;
87 }
88
89 // descend into child elements
90 for(ICdmFormElement childElement : element.getElements()){
91 isWidgetPartOfFormElement(widget, childElement);
92 }
93
94 return false;
95 }
96
97 /**
98 * <p>addSelectionProvider</p>
99 *
100 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
101 */
102 public void addSelectionProvider(ISelectionProvider selectionProvider){
103 selectionProviders.add(selectionProvider);
104 }
105
106 /**
107 * <p>removeSelectionProvider</p>
108 *
109 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
110 */
111 public void removeSelectionProvider(ISelectionProvider selectionProvider){
112 selectionProviders.remove(selectionProvider);
113 }
114
115 /** {@inheritDoc} */
116 public void widgetDefaultSelected(SelectionEvent e) {}
117
118 /**
119 * <p>Getter for the field <code>entityElement</code>.</p>
120 *
121 * @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
122 */
123 public IEntityElement getEntityElement() {
124 return entityElement;
125 }
126
127 /**
128 * <p>setEntityComposite</p>
129 *
130 * @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
131 */
132 public void setEntityComposite(IEntityElement entityElement) {
133 this.entityElement = entityElement;
134 }
135
136 /** {@inheritDoc} */
137 public void selectionChanged(SelectionChangedEvent event) {
138 // if(!(event.getSource() instanceof CdmDetailsViewer)){
139 // return;
140 // }
141
142 try{
143 entityElement.setSelected(false);
144 }catch(SWTException e){
145 StoreUtil.error(getClass(), "Widgets of element are disposed", e);
146 }
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);
152 }
153 }
154 }
155 }