Project

General

Profile

Download (4.38 KB) Statistics
| Branch: | Tag: | Revision:
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
	public void widgetSelected(SelectionEvent e) {
58
		Widget widget = e.widget;
59
		
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);
66
				}
67
			}
68
		}	
69
	}
70
	
71
	private void setSelection(ISelection selection){
72
		for(ISelectionProvider selectionProvider : selectionProviders){
73
			selectionProvider.setSelection(selection);
74
		}
75
	}
76
	
77
	private boolean isWidgetPartOfFormElement(Control widget, ICdmFormElement element){
78
		// check if widget is the controls
79
		if(widget.equals(element.getLayoutComposite())){
80
			return true;
81
		}
82
		
83
		// check if widget is controlled by the given element
84
		for(Control control : element.getControls()){
85
			if(control.equals(widget)) return true;
86
		}
87
		
88
		// descend into child elements
89
		for(ICdmFormElement childElement : element.getElements()){
90
			isWidgetPartOfFormElement(widget, childElement);
91
		}
92
		
93
		return false;
94
	}
95
	
96
	/**
97
	 * <p>addSelectionProvider</p>
98
	 *
99
	 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
100
	 */
101
	public void addSelectionProvider(ISelectionProvider selectionProvider){
102
		selectionProviders.add(selectionProvider);
103
	}
104
	
105
	/**
106
	 * <p>removeSelectionProvider</p>
107
	 *
108
	 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
109
	 */
110
	public void removeSelectionProvider(ISelectionProvider selectionProvider){
111
		selectionProviders.remove(selectionProvider);
112
	}
113
	
114
	/** {@inheritDoc} */
115
	public void widgetDefaultSelected(SelectionEvent e) {}
116

    
117
	/**
118
	 * <p>Getter for the field <code>entityElement</code>.</p>
119
	 *
120
	 * @return a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
121
	 */
122
	public IEntityElement getEntityElement() {
123
		return entityElement;
124
	}
125

    
126
	/**
127
	 * <p>setEntityComposite</p>
128
	 *
129
	 * @param entityElement a {@link eu.etaxonomy.taxeditor.ui.element.IEntityElement} object.
130
	 */
131
	public void setEntityComposite(IEntityElement entityElement) {
132
		this.entityElement = entityElement;
133
	}
134

    
135
	/** {@inheritDoc} */
136
	public void selectionChanged(SelectionChangedEvent event) {
137
//		if(!(event.getSource() instanceof CdmDetailsViewer)){
138
//			return;
139
//		}
140
		
141
		try{
142
		entityElement.setSelected(false);
143
		}catch(SWTException e){
144
			MessagingUtils.error(getClass(), "Widgets of element are disposed", e);
145
		}
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);
151
			}
152
		}
153
	}
154
}
(38-38/44)