Mercurial > projects > dwt2
comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/viewers/SelectionProviderSingleSelectionObservableValue.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Brad Reynolds - bug 137877 | |
11 * Brad Reynolds - bug 164653 | |
12 * Brad Reynolds - bug 147515 | |
13 * Ashley Cambrell - bug 198906 | |
14 *******************************************************************************/ | |
15 | |
16 module org.eclipse.jface.internal.databinding.viewers.SelectionProviderSingleSelectionObservableValue; | |
17 | |
18 import java.lang.all; | |
19 | |
20 import org.eclipse.core.databinding.observable.Diffs; | |
21 import org.eclipse.core.databinding.observable.Realm; | |
22 import org.eclipse.core.databinding.observable.value.AbstractObservableValue; | |
23 import org.eclipse.jface.util.Util; | |
24 import org.eclipse.jface.viewers.ISelection; | |
25 import org.eclipse.jface.viewers.ISelectionChangedListener; | |
26 import org.eclipse.jface.viewers.ISelectionProvider; | |
27 import org.eclipse.jface.viewers.IStructuredSelection; | |
28 import org.eclipse.jface.viewers.SelectionChangedEvent; | |
29 import org.eclipse.jface.viewers.StructuredSelection; | |
30 | |
31 /** | |
32 * Observes single selection of an <code>ISelectionProvider</code>. | |
33 * | |
34 * @since 1.1 | |
35 */ | |
36 public class SelectionProviderSingleSelectionObservableValue : | |
37 AbstractObservableValue { | |
38 | |
39 private final ISelectionProvider selectionProvider; | |
40 | |
41 private bool updating = false; | |
42 | |
43 private Object currentSelection; | |
44 | |
45 private ISelectionChangedListener selectionChangedListener; | |
46 | |
47 /** | |
48 * Constructs a new instance associated with the provided | |
49 * <code>selectionProvider</code>. In order to initialize itself properly | |
50 * the constructor invokes {@link #doGetValue()}. This could be dangerous | |
51 * for subclasses, see {@link #doGetValue()} for an explanation. | |
52 * | |
53 * @param realm | |
54 * | |
55 * @param selectionProvider | |
56 * @see #doGetValue() | |
57 */ | |
58 public this(Realm realm, | |
59 ISelectionProvider selectionProvider) { | |
60 super(realm); | |
61 if (selectionProvider is null) { | |
62 throw new IllegalArgumentException( | |
63 "The 'selectionProvider' parameter is null."); //$NON-NLS-1$ | |
64 } | |
65 | |
66 this.selectionProvider = selectionProvider; | |
67 this.currentSelection = doGetValue(); | |
68 | |
69 selectionChangedListener = new class() ISelectionChangedListener { | |
70 public void selectionChanged(SelectionChangedEvent event) { | |
71 if (!updating) { | |
72 Object oldSelection = currentSelection; | |
73 currentSelection = doGetValue(); | |
74 fireValueChange(Diffs.createValueDiff(oldSelection, | |
75 currentSelection)); | |
76 } | |
77 } | |
78 }; | |
79 selectionProvider.addSelectionChangedListener(selectionChangedListener); | |
80 } | |
81 | |
82 /** | |
83 * Sets the selection to the provided <code>value</code>. Value change | |
84 * events are fired after selection is set in the selection provider. | |
85 * | |
86 * @param value | |
87 * object to set as selected, <code>null</code> if wanting to | |
88 * remove selection | |
89 */ | |
90 public void doSetValue(Object value) { | |
91 try { | |
92 updating = true; | |
93 | |
94 Object oldSelection = currentSelection; | |
95 selectionProvider | |
96 .setSelection(value is null ? StructuredSelection.EMPTY | |
97 : new StructuredSelection(value)); | |
98 currentSelection = doGetValue(); | |
99 if (!Util.equals(oldSelection, currentSelection)) { | |
100 fireValueChange(Diffs.createValueDiff(oldSelection, | |
101 currentSelection)); | |
102 } | |
103 } finally { | |
104 updating = false; | |
105 } | |
106 } | |
107 | |
108 /** | |
109 * Retrieves the current selection. | |
110 * <p> | |
111 * If a subclass overrides this method it must not depend upon the subclass | |
112 * to have been fully initialized before this method is invoked. | |
113 * <code>doGetValue()</code> is invoked by the | |
114 * {@link #SelectionProviderSingleSelectionObservableValue(Realm, ISelectionProvider) constructor} | |
115 * which means the subclass's constructor will not have fully executed | |
116 * before this method is invoked. | |
117 * </p> | |
118 * | |
119 * @return selection will be an instance of | |
120 * <code>IStructuredSelection</code> if a selection exists, | |
121 * <code>null</code> if no selection | |
122 * @see #SelectionProviderSingleSelectionObservableValue(Realm, | |
123 * ISelectionProvider) | |
124 */ | |
125 protected Object doGetValue() { | |
126 ISelection selection = selectionProvider.getSelection(); | |
127 if (null !is cast(IStructuredSelection)selection) { | |
128 IStructuredSelection sel = cast(IStructuredSelection) selection; | |
129 return sel.getFirstElement(); | |
130 } | |
131 | |
132 return null; | |
133 } | |
134 | |
135 public Object getValueType() { | |
136 return null; | |
137 } | |
138 | |
139 /* | |
140 * (non-Javadoc) | |
141 * | |
142 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose() | |
143 */ | |
144 public synchronized void dispose() { | |
145 selectionProvider | |
146 .removeSelectionChangedListener(selectionChangedListener); | |
147 super.dispose(); | |
148 } | |
149 } |