78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 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 * Matthew Hall - bug 206839
|
|
11 * Matthew Hall - bug 124684
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module org.eclipse.jface.databinding.viewers.ViewersObservables;
|
85
|
15 import org.eclipse.jface.databinding.viewers.IViewerObservableSet;
|
|
16 import org.eclipse.jface.databinding.viewers.IViewerObservableList;
|
|
17 import org.eclipse.jface.databinding.viewers.IViewerObservableValue;
|
78
|
18
|
|
19 import java.lang.all;
|
|
20
|
|
21 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
22 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
23 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
24 import org.eclipse.jface.databinding.swt.SWTObservables;
|
|
25 import org.eclipse.jface.internal.databinding.viewers.CheckableCheckedElementsObservableSet;
|
|
26 import org.eclipse.jface.internal.databinding.viewers.CheckboxViewerCheckedElementsObservableSet;
|
|
27 import org.eclipse.jface.internal.databinding.viewers.SelectionProviderMultipleSelectionObservableList;
|
|
28 import org.eclipse.jface.internal.databinding.viewers.SelectionProviderSingleSelectionObservableValue;
|
|
29 import org.eclipse.jface.internal.databinding.viewers.ViewerInputObservableValue;
|
|
30 import org.eclipse.jface.internal.databinding.viewers.ViewerMultipleSelectionObservableList;
|
|
31 import org.eclipse.jface.internal.databinding.viewers.ViewerSingleSelectionObservableValue;
|
|
32 import org.eclipse.jface.viewers.CheckboxTableViewer;
|
|
33 import org.eclipse.jface.viewers.CheckboxTreeViewer;
|
|
34 import org.eclipse.jface.viewers.ICheckable;
|
|
35 import org.eclipse.jface.viewers.ISelectionProvider;
|
|
36 import org.eclipse.jface.viewers.IStructuredSelection;
|
|
37 import org.eclipse.jface.viewers.Viewer;
|
|
38 import org.eclipse.swt.widgets.Display;
|
|
39
|
|
40 /**
|
|
41 * Factory methods for creating observables for JFace viewers
|
|
42 *
|
|
43 * @since 1.1
|
|
44 */
|
|
45 public class ViewersObservables {
|
|
46
|
|
47 /**
|
|
48 * Returns an observable value that tracks the current selection of the
|
|
49 * given selection provider. If the selection provider provides selections
|
|
50 * of type {@link IStructuredSelection}, the observable value will be the
|
|
51 * first element of the structured selection as returned by
|
|
52 * {@link IStructuredSelection#getFirstElement()}.
|
|
53 *
|
|
54 * @param selectionProvider
|
|
55 * @return the observable value tracking the (single) selection of the given
|
|
56 * selection provider
|
|
57 */
|
|
58 public static IObservableValue observeSingleSelection(
|
|
59 ISelectionProvider selectionProvider) {
|
|
60 if (null !is cast(Viewer)selectionProvider) {
|
|
61 return observeSingleSelection(cast(Viewer) selectionProvider);
|
|
62 }
|
|
63 return new SelectionProviderSingleSelectionObservableValue(
|
|
64 SWTObservables.getRealm(Display.getDefault()),
|
|
65 selectionProvider);
|
|
66 }
|
|
67
|
|
68 /**
|
|
69 * Returns an observable list that tracks the current selection of the
|
|
70 * given selection provider. Assumes that the selection provider provides
|
|
71 * selections of type {@link IStructuredSelection}. Note that the
|
|
72 * observable list will not honor the full contract of
|
|
73 * <code>java.util.List</code> in that it may delete or reorder elements
|
|
74 * based on what the selection provider returns from
|
|
75 * {@link ISelectionProvider#getSelection()} after having called
|
|
76 * {@link ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)}
|
|
77 * based on the requested change to the observable list. The affected
|
|
78 * methods are <code>add</code>, <code>addAll</code>, and
|
|
79 * <code>set</code>.
|
|
80 *
|
|
81 * @param selectionProvider
|
|
82 * @return the observable value tracking the (multi) selection of the given
|
|
83 * selection provider
|
|
84 *
|
|
85 * @since 1.2
|
|
86 */
|
|
87 public static IObservableList observeMultiSelection(
|
|
88 ISelectionProvider selectionProvider) {
|
|
89 if (null !is cast(Viewer)selectionProvider) {
|
|
90 return observeMultiSelection(cast(Viewer) selectionProvider);
|
|
91 }
|
|
92 return new SelectionProviderMultipleSelectionObservableList(
|
|
93 SWTObservables.getRealm(Display.getDefault()),
|
|
94 selectionProvider, Object.classinfo);
|
|
95 }
|
|
96
|
|
97 /**
|
|
98 * Returns an observable value that tracks the current selection of the
|
|
99 * given viewer. If the viewer provides selections of type
|
|
100 * {@link IStructuredSelection}, the observable value will be the first
|
|
101 * element of the structured selection as returned by
|
|
102 * {@link IStructuredSelection#getFirstElement()}.
|
|
103 *
|
|
104 * @param viewer
|
|
105 * the viewer
|
|
106 * @return the observable value tracking the (single) selection of the given
|
|
107 * viewer
|
|
108 * @since 1.2
|
|
109 */
|
|
110 public static IViewerObservableValue observeSingleSelection(Viewer viewer) {
|
|
111 return new ViewerSingleSelectionObservableValue(
|
|
112 SWTObservables.getRealm(Display.getDefault()),
|
|
113 viewer);
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * Returns an observable list that tracks the current selection of the
|
|
118 * given viewer. Assumes that the viewer provides
|
|
119 * selections of type {@link IStructuredSelection}. Note that the
|
|
120 * observable list will not honor the full contract of
|
|
121 * <code>java.util.List</code> in that it may delete or reorder elements
|
|
122 * based on what the viewer returns from
|
|
123 * {@link ISelectionProvider#getSelection()} after having called
|
|
124 * {@link ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)}
|
|
125 * based on the requested change to the observable list. The affected
|
|
126 * methods are <code>add</code>, <code>addAll</code>, and
|
|
127 * <code>set</code>.
|
|
128 *
|
|
129 * @param viewer
|
|
130 * @return the observable value tracking the (multi) selection of the given
|
|
131 * selection provider
|
|
132 *
|
|
133 * @since 1.2
|
|
134 */
|
|
135 public static IViewerObservableList observeMultiSelection(
|
|
136 Viewer viewer) {
|
|
137 return new ViewerMultipleSelectionObservableList(
|
|
138 SWTObservables.getRealm(Display.getDefault()),
|
|
139 viewer, Object.classinfo);
|
|
140 }
|
|
141
|
|
142 /**
|
|
143 * Returns an observable value that tracks the input of the given viewer.
|
|
144 * <p>
|
|
145 * The returned observer is blind to changes in the viewer's input unless
|
|
146 * its {@link IObservableValue#setValue(Object)} method is called directly.
|
|
147 *
|
|
148 * @param viewer
|
|
149 * the viewer to observe
|
|
150 * @return an observable value tracking the input of the given viewer
|
|
151 * @since 1.2
|
|
152 */
|
|
153 public static IObservableValue observeInput(Viewer viewer) {
|
|
154 return new ViewerInputObservableValue(SWTObservables.getRealm(viewer
|
|
155 .getControl().getDisplay()), viewer);
|
|
156 }
|
|
157
|
|
158 /**
|
|
159 * Returns an observable set that tracks the checked elements of the given
|
|
160 * <code>ICheckable</code>.
|
|
161 *
|
|
162 * @param checkable
|
|
163 * {@link ICheckable} containing the checked elements to track
|
|
164 * @param elementType
|
|
165 * element type of the returned set
|
|
166 * @return an observable set tracking the checked elements of the given
|
|
167 * checkable.
|
|
168 * @since 1.2
|
|
169 */
|
|
170 public static IObservableSet observeCheckedElements(ICheckable checkable,
|
|
171 Object elementType) {
|
|
172 if (null !is cast(CheckboxTableViewer)checkable) {
|
|
173 return observeCheckedElements(cast(CheckboxTableViewer) checkable,
|
|
174 elementType);
|
|
175 }
|
|
176 if (null !is cast(CheckboxTreeViewer)checkable) {
|
|
177 return observeCheckedElements(cast(CheckboxTreeViewer) checkable,
|
|
178 elementType);
|
|
179 }
|
|
180 return new CheckableCheckedElementsObservableSet(SWTObservables
|
|
181 .getRealm(Display.getDefault()), checkable, elementType);
|
|
182 }
|
|
183
|
|
184 /**
|
|
185 * Returns an observable set that tracks the checked elements of the given
|
|
186 * viewer. Assumes that the viewer implements {@link ICheckable}.
|
|
187 *
|
|
188 * @param viewer
|
|
189 * {@link CheckboxTableViewer} containing the checked elements to
|
|
190 * track.
|
|
191 * @param elementType
|
|
192 * element type of the returned set
|
|
193 * @return an observable set that tracks the checked elements of the given
|
|
194 * viewer.
|
|
195 * @since 1.2
|
|
196 */
|
|
197 public static IViewerObservableSet observeCheckedElements(
|
|
198 CheckboxTableViewer viewer, Object elementType) {
|
|
199 return new CheckboxViewerCheckedElementsObservableSet(SWTObservables
|
|
200 .getRealm(viewer.getControl().getDisplay()), viewer,
|
|
201 elementType);
|
|
202 }
|
|
203
|
|
204 /**
|
|
205 * Returns an observable set that tracks the checked elements of the given
|
|
206 * viewer. Assumes that the viewer implements {@link ICheckable}.
|
|
207 *
|
|
208 * @param viewer
|
|
209 * {@link CheckboxTreeViewer} containing the checked elements to
|
|
210 * track.
|
|
211 * @param elementType
|
|
212 * element type of the returned set
|
|
213 * @return an observable set that tracks the checked elements of the given
|
|
214 * viewer.
|
|
215 * @since 1.2
|
|
216 */
|
|
217 public static IViewerObservableSet observeCheckedElements(
|
|
218 CheckboxTreeViewer viewer, Object elementType) {
|
|
219 return new CheckboxViewerCheckedElementsObservableSet(SWTObservables
|
|
220 .getRealm(viewer.getControl().getDisplay()), viewer,
|
|
221 elementType);
|
|
222 }
|
|
223 }
|