Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 218884 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/core/databinding/observable/Observables.java (-11 / +39 lines)
Lines 32-38 Link Here
32
import org.eclipse.core.databinding.observable.set.ObservableSet;
32
import org.eclipse.core.databinding.observable.set.ObservableSet;
33
import org.eclipse.core.databinding.observable.value.DecoratingObservableValue;
33
import org.eclipse.core.databinding.observable.value.DecoratingObservableValue;
34
import org.eclipse.core.databinding.observable.value.IObservableValue;
34
import org.eclipse.core.databinding.observable.value.IObservableValue;
35
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
35
import org.eclipse.core.databinding.observable.value.IVetoableValue;
36
import org.eclipse.core.databinding.observable.value.IVetoableValue;
37
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
36
import org.eclipse.core.databinding.observable.value.ValueChangingEvent;
38
import org.eclipse.core.databinding.observable.value.ValueChangingEvent;
37
import org.eclipse.core.internal.databinding.observable.ConstantObservableValue;
39
import org.eclipse.core.internal.databinding.observable.ConstantObservableValue;
38
import org.eclipse.core.internal.databinding.observable.DelayedObservableValue;
40
import org.eclipse.core.internal.databinding.observable.DelayedObservableValue;
Lines 47-54 Link Here
47
import org.eclipse.core.runtime.Assert;
49
import org.eclipse.core.runtime.Assert;
48
50
49
/**
51
/**
50
 * Contains static methods to operate on or return
52
 * Contains static methods to operate on or return {@link IObservable
51
 * {@link IObservable Observables}.
53
 * Observables}.
52
 * 
54
 * 
53
 * @since 1.0
55
 * @since 1.0
54
 */
56
 */
Lines 395-401 Link Here
395
			public synchronized void addStaleListener(IStaleListener listener) {
397
			public synchronized void addStaleListener(IStaleListener listener) {
396
			}
398
			}
397
399
398
			public synchronized void addSetChangeListener(ISetChangeListener listener) {
400
			public synchronized void addSetChangeListener(
401
					ISetChangeListener listener) {
399
			}
402
			}
400
		};
403
		};
401
	}
404
	}
Lines 518-524 Link Here
518
			public synchronized void addStaleListener(IStaleListener listener) {
521
			public synchronized void addStaleListener(IStaleListener listener) {
519
			}
522
			}
520
523
521
			public synchronized void addListChangeListener(IListChangeListener listener) {
524
			public synchronized void addListChangeListener(
525
					IListChangeListener listener) {
522
			}
526
			}
523
		};
527
		};
524
	}
528
	}
Lines 555-561 Link Here
555
	 *         specified key in the given map
559
	 *         specified key in the given map
556
	 * @since 1.2
560
	 * @since 1.2
557
	 */
561
	 */
558
	public static IObservableValue observeMapEntry(IObservableMap map, Object key) {
562
	public static IObservableValue observeMapEntry(IObservableMap map,
563
			Object key) {
559
		return observeMapEntry(map, key, map.getValueType());
564
		return observeMapEntry(map, key, map.getValueType());
560
	}
565
	}
561
566
Lines 573-580 Link Here
573
	 * @param key
578
	 * @param key
574
	 *            the key identifying the map entry to track.
579
	 *            the key identifying the map entry to track.
575
	 * @param valueType
580
	 * @param valueType
576
	 *            the type of the value. May be <code>null</code>, meaning
581
	 *            the type of the value. May be <code>null</code>, meaning the
577
	 *            the value is untyped.
582
	 *            value is untyped.
578
	 * @return an observable value that tracks the value associated with the
583
	 * @return an observable value that tracks the value associated with the
579
	 *         specified key in the given map
584
	 *         specified key in the given map
580
	 * @since 1.1
585
	 * @since 1.1
Lines 594-601 Link Here
594
	 * @param map
599
	 * @param map
595
	 *            the observable map whose entry will be tracked.
600
	 *            the observable map whose entry will be tracked.
596
	 * @param valueType
601
	 * @param valueType
597
	 *            the type of the value. May be <code>null</code>, meaning
602
	 *            the type of the value. May be <code>null</code>, meaning the
598
	 *            the value is untyped.
603
	 *            value is untyped.
599
	 * @return a factory for creating observable values tracking the value of
604
	 * @return a factory for creating observable values tracking the value of
600
	 *         the observable map entry identified by a particular key object.
605
	 *         the observable map entry identified by a particular key object.
601
	 * @since 1.1
606
	 * @since 1.1
Lines 618-625 Link Here
618
	 * @param master
623
	 * @param master
619
	 *            the observable value that identifies which map entry to track.
624
	 *            the observable value that identifies which map entry to track.
620
	 * @param valueType
625
	 * @param valueType
621
	 *            the type of the value. May be <code>null</code>, meaning
626
	 *            the type of the value. May be <code>null</code>, meaning the
622
	 *            the value is untyped.
627
	 *            value is untyped.
623
	 * @return an observable value tracking the current value of the specified
628
	 * @return an observable value tracking the current value of the specified
624
	 *         key in the given map an observable value that tracks the current
629
	 *         key in the given map an observable value that tracks the current
625
	 *         value of the named property for the current value of the master
630
	 *         value of the named property for the current value of the master
Lines 631-634 Link Here
631
		return MasterDetailObservables.detailValue(master,
636
		return MasterDetailObservables.detailValue(master,
632
				mapEntryValueFactory(map, valueType), valueType);
637
				mapEntryValueFactory(map, valueType), valueType);
633
	}
638
	}
639
640
	/**
641
	 * Copies the current value of the source observable to the destination
642
	 * observable, and upon value change events fired by the source observable,
643
	 * updates the destination observable accordingly, until the source
644
	 * observable is disposed. This method assumes that both observables are on
645
	 * the same realm.
646
	 * 
647
	 * @param source
648
	 *            the source observable
649
	 * @param destination
650
	 *            the destination observable
651
	 * @since 1.2
652
	 */
653
	public static void pipe(IObservableValue source,
654
			final IObservableValue destination) {
655
		destination.setValue(source.getValue());
656
		source.addValueChangeListener(new IValueChangeListener() {
657
			public void handleValueChange(ValueChangeEvent event) {
658
				destination.setValue(event.diff.getNewValue());
659
			}
660
		});
661
	}
634
}
662
}
(-)META-INF/MANIFEST.MF (-1 / +3 lines)
Lines 19-24 Link Here
19
 org.eclipse.ui.browser;ui.workbench=split;mandatory:="ui.workbench",
19
 org.eclipse.ui.browser;ui.workbench=split;mandatory:="ui.workbench",
20
 org.eclipse.ui.commands,
20
 org.eclipse.ui.commands,
21
 org.eclipse.ui.contexts,
21
 org.eclipse.ui.contexts,
22
 org.eclipse.ui.databinding,
22
 org.eclipse.ui.dialogs;ui.workbench=split;mandatory:="ui.workbench",
23
 org.eclipse.ui.dialogs;ui.workbench=split;mandatory:="ui.workbench",
23
 org.eclipse.ui.dnd,
24
 org.eclipse.ui.dnd,
24
 org.eclipse.ui.fieldassist,
25
 org.eclipse.ui.fieldassist,
Lines 96-102 Link Here
96
 org.eclipse.swt;bundle-version="[3.3.0,4.0.0)",
97
 org.eclipse.swt;bundle-version="[3.3.0,4.0.0)",
97
 org.eclipse.core.expressions;bundle-version="[3.2.0,4.0.0)",
98
 org.eclipse.core.expressions;bundle-version="[3.2.0,4.0.0)",
98
 org.eclipse.jface.databinding;bundle-version="[1.1.0,2.0.0)",
99
 org.eclipse.jface.databinding;bundle-version="[1.1.0,2.0.0)",
99
 org.eclipse.core.databinding;bundle-version="[1.0.0,2.0.0)"
100
 org.eclipse.core.databinding.property;bundle-version="[1.2.0,2.0.0)",
101
 org.eclipse.core.databinding.observable;bundle-version="[1.2.0,2.0.0)"
100
Import-Package: com.ibm.icu.text,
102
Import-Package: com.ibm.icu.text,
101
 com.ibm.icu.util,
103
 com.ibm.icu.util,
102
 javax.xml.parsers,
104
 javax.xml.parsers,
(-)Eclipse (+101 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 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 - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.ui.databinding;
14
15
import org.eclipse.core.databinding.observable.value.IObservableValue;
16
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.core.runtime.IAdapterManager;
18
import org.eclipse.core.runtime.Platform;
19
import org.eclipse.ui.ISelectionService;
20
import org.eclipse.ui.services.IServiceLocator;
21
22
/**
23
 * Factory methods for creating observables for Workbench objects
24
 * 
25
 * @since 3.5
26
 */
27
public class WorkbenchObservables {
28
	/**
29
	 * Returns an observable with values of the given target type. If the
30
	 * wrapped observable's value is of the target type, or can be adapted to
31
	 * the target type, this is taken as the value of the returned observable,
32
	 * otherwise <code>null</code>.
33
	 * 
34
	 * @param master
35
	 *            the observable whose value should be adapted
36
	 * @param adapter
37
	 *            the target type
38
	 * @return an observable with values of the given type, or <code>null</code>
39
	 *         if the current value of the given observable does not adapt to
40
	 *         the target type
41
	 */
42
	public static IObservableValue observeDetailAdaptedValue(
43
			IObservableValue master, Class adapter) {
44
		return observeDetailAdaptedValue(master, adapter, Platform
45
				.getAdapterManager());
46
	}
47
48
	/**
49
	 * Returns an observable with values of the given target type. If the
50
	 * wrapped observable's value is of the target type, or can be adapted to
51
	 * the target type, this is taken as the value of the returned observable,
52
	 * otherwise <code>null</code>.
53
	 * 
54
	 * @param master
55
	 *            the observable whose value should be adapted
56
	 * @param adapter
57
	 *            the target type
58
	 * @param adapterManager
59
	 *            the adapter manager used to adapt the master value
60
	 * @return an observable with values of the given type, or <code>null</code>
61
	 *         if the current value of the given observable does not adapt to
62
	 *         the target type
63
	 */
64
	public static IObservableValue observeDetailAdaptedValue(
65
			IObservableValue master, Class adapter,
66
			IAdapterManager adapterManager) {
67
		return WorkbenchProperties.adaptedValue(adapter, adapterManager)
68
				.observeDetail(master);
69
	}
70
71
	/**
72
	 * Returns an observable value that tracks the post selection of a selection
73
	 * service obtained through the given service locator, and adapts the first
74
	 * element of that selection to the given target type.
75
	 * <p>
76
	 * This method can be used by view or editor implementers to tie into the
77
	 * selection service, for example as follows:
78
	 * 
79
	 * <pre>
80
	 * IObservableValue selection = WorkbenchObservables
81
	 * 		.observeAdaptedSingleSelection(getSite(), IResource.class);
82
	 * </pre>
83
	 * 
84
	 * </p>
85
	 * 
86
	 * @param locator
87
	 *            a service locator with an available {@link ISelectionService}
88
	 * @param targetType
89
	 *            the target type
90
	 * @return an observable value whose value type is the given target type
91
	 */
92
	public static IObservableValue observeAdaptedSingleSelection(
93
			IServiceLocator locator, Class targetType) {
94
		ISelectionService selectionService = (ISelectionService) locator
95
				.getService(ISelectionService.class);
96
		Assert.isNotNull(selectionService);
97
		return WorkbenchProperties.singleSelection(null, true).value(
98
				WorkbenchProperties.adaptedValue(targetType)).observe(
99
				selectionService);
100
	}
101
}
(-)Eclipse (+293 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 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 - initial API and implementation
11
 *******************************************************************************/
12
package org.eclipse.ui.databinding;
13
14
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.List;
17
18
import org.eclipse.core.databinding.observable.list.ListDiff;
19
import org.eclipse.core.databinding.property.INativePropertyListener;
20
import org.eclipse.core.databinding.property.IProperty;
21
import org.eclipse.core.databinding.property.ISimplePropertyListener;
22
import org.eclipse.core.databinding.property.NativePropertyListener;
23
import org.eclipse.core.databinding.property.list.IListProperty;
24
import org.eclipse.core.databinding.property.list.SimpleListProperty;
25
import org.eclipse.core.databinding.property.value.IValueProperty;
26
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
27
import org.eclipse.core.runtime.IAdapterManager;
28
import org.eclipse.core.runtime.Platform;
29
import org.eclipse.jface.viewers.ISelection;
30
import org.eclipse.jface.viewers.IStructuredSelection;
31
import org.eclipse.ui.ISelectionListener;
32
import org.eclipse.ui.ISelectionService;
33
import org.eclipse.ui.IWorkbenchPart;
34
35
/**
36
 * Factory methods for creating properties for the Workbench.
37
 * 
38
 * <p>
39
 * Examples:
40
 * 
41
 * <pre>
42
 * WorkbenchProperties.singleSelection().observe(
43
 * 		getSite().getService(ISelectionService.class))
44
 * </pre>
45
 * 
46
 * </p>
47
 * 
48
 * @since 3.5
49
 */
50
public class WorkbenchProperties {
51
	/**
52
	 * Returns a value property which observes the source object as the adapted
53
	 * type, using the platform adapter manager. If the source is of the target
54
	 * type, or can be adapted to the target type, this is used as the value of
55
	 * property, otherwise <code>null</code>.
56
	 * 
57
	 * @param adapter
58
	 *            the adapter class
59
	 * @return a value property which observes the source object as the adapted
60
	 *         type.
61
	 */
62
	public static IValueProperty adaptedValue(Class adapter) {
63
		return adaptedValue(adapter, Platform.getAdapterManager());
64
	}
65
66
	/**
67
	 * Returns a value property which observes the source object as the adapted
68
	 * type. If the source object is of the target type, or can be adapted to
69
	 * the target type, this is used as the value of property, otherwise
70
	 * <code>null</code>.
71
	 * 
72
	 * @param adapter
73
	 *            the adapter class
74
	 * @param adapterManager
75
	 *            the adapter manager used to adapt source objects
76
	 * @return a value property which observes the source object as the adapted
77
	 *         type.
78
	 */
79
	public static IValueProperty adaptedValue(final Class adapter,
80
			final IAdapterManager adapterManager) {
81
		return new AdaptedValueProperty(adapter, adapterManager);
82
	}
83
84
	/**
85
	 * Returns a property for observing the first element of a structured
86
	 * selection as exposed by {@link ISelectionService}.
87
	 * 
88
	 * @return an observable value
89
	 */
90
	public static IValueProperty singleSelection() {
91
		return singleSelection(null, false);
92
	}
93
94
	/**
95
	 * Returns a property for observing the first element of a structured
96
	 * selection as exposed by {@link ISelectionService}.
97
	 * 
98
	 * @param partId
99
	 *            the part id, or <code>null</code> if the selection can be from
100
	 *            any part
101
	 * @param postSelection
102
	 *            <code>true</code> if the selection should be delayed for
103
	 *            keyboard-triggered selections
104
	 * 
105
	 * @return an observable value
106
	 */
107
	public static IValueProperty singleSelection(String partId,
108
			boolean postSelection) {
109
		return new SingleSelectionProperty(partId, postSelection);
110
	}
111
112
	/**
113
	 * Returns a property for observing the elements of a structured selection
114
	 * as exposed by {@link ISelectionService}.
115
	 * 
116
	 * @return an observable value
117
	 */
118
	public static IListProperty multipleSelection() {
119
		return multipleSelection(null, false);
120
	}
121
122
	/**
123
	 * Returns a property for observing the elements of a structured selection
124
	 * as exposed by {@link ISelectionService}.
125
	 * 
126
	 * @param partId
127
	 *            the part id, or <code>null</code> if the selection can be from
128
	 *            any part
129
	 * @param postSelection
130
	 *            <code>true</code> if the selection should be delayed for
131
	 *            keyboard-triggered selections
132
	 * 
133
	 * @return an observable value
134
	 */
135
	public static IListProperty multipleSelection(String partId,
136
			boolean postSelection) {
137
		return new MultiSelectionProperty(partId, postSelection);
138
	}
139
140
	static final class AdaptedValueProperty extends SimpleValueProperty {
141
		private final Class adapter;
142
		private final IAdapterManager adapterManager;
143
144
		private AdaptedValueProperty(Class adapter,
145
				IAdapterManager adapterManager) {
146
			this.adapter = adapter;
147
			this.adapterManager = adapterManager;
148
		}
149
150
		public Object getValueType() {
151
			return adapter;
152
		}
153
154
		protected Object doGetValue(Object source) {
155
			if (adapter.isInstance(source))
156
				return source;
157
			return adapterManager.getAdapter(source, adapter);
158
		}
159
160
		protected void doSetValue(Object source, Object value) {
161
			throw new UnsupportedOperationException();
162
		}
163
164
		public INativePropertyListener adaptListener(
165
				ISimplePropertyListener listener) {
166
			return null;
167
		}
168
	}
169
170
	static class SingleSelectionProperty extends SimpleValueProperty {
171
		private final String partId;
172
		private final boolean post;
173
174
		SingleSelectionProperty(String partId, boolean post) {
175
			this.partId = partId;
176
			this.post = post;
177
		}
178
179
		public INativePropertyListener adaptListener(
180
				ISimplePropertyListener listener) {
181
			return new SelectionServiceListener(this, listener, partId, post);
182
		}
183
184
		protected Object doGetValue(Object source) {
185
			ISelection selection;
186
			if (partId != null) {
187
				selection = ((ISelectionService) source).getSelection(partId);
188
			} else {
189
				selection = ((ISelectionService) source).getSelection();
190
			}
191
			if (selection instanceof IStructuredSelection) {
192
				return ((IStructuredSelection) selection).getFirstElement();
193
			}
194
			return null;
195
		}
196
197
		protected void doSetValue(Object source, Object value) {
198
			throw new UnsupportedOperationException();
199
		}
200
201
		public Object getValueType() {
202
			return Object.class;
203
		}
204
	}
205
206
	static class MultiSelectionProperty extends SimpleListProperty {
207
		private final String partId;
208
		private final boolean post;
209
210
		MultiSelectionProperty(String partId, boolean post) {
211
			this.partId = partId;
212
			this.post = post;
213
		}
214
215
		public INativePropertyListener adaptListener(
216
				ISimplePropertyListener listener) {
217
			return new SelectionServiceListener(this, listener, partId, post);
218
		}
219
220
		public Object getElementType() {
221
			return Object.class;
222
		}
223
224
		protected List doGetList(Object source) {
225
			ISelection selection;
226
			if (partId != null) {
227
				selection = ((ISelectionService) source).getSelection(partId);
228
			} else {
229
				selection = ((ISelectionService) source).getSelection();
230
			}
231
			if (selection instanceof IStructuredSelection) {
232
				return new ArrayList(((IStructuredSelection) selection)
233
						.toList());
234
			}
235
			return Collections.EMPTY_LIST;
236
		}
237
238
		protected void doSetList(Object source, List list, ListDiff diff) {
239
			throw new UnsupportedOperationException();
240
		}
241
	}
242
243
	static class SelectionServiceListener extends NativePropertyListener
244
			implements ISelectionListener {
245
		private final String partId;
246
		private final boolean post;
247
248
		public SelectionServiceListener(IProperty property,
249
				ISimplePropertyListener wrapped, String partID, boolean post) {
250
			super(property, wrapped);
251
			this.partId = partID;
252
			this.post = post;
253
		}
254
255
		protected void doAddTo(Object source) {
256
			ISelectionService selectionService = (ISelectionService) source;
257
			if (post) {
258
				if (partId != null) {
259
					selectionService.addPostSelectionListener(partId, this);
260
				} else {
261
					selectionService.addPostSelectionListener(this);
262
				}
263
			} else {
264
				if (partId != null) {
265
					selectionService.addSelectionListener(partId, this);
266
				} else {
267
					selectionService.addSelectionListener(this);
268
				}
269
			}
270
		}
271
272
		protected void doRemoveFrom(Object source) {
273
			ISelectionService selectionService = (ISelectionService) source;
274
			if (post) {
275
				if (partId != null) {
276
					selectionService.removePostSelectionListener(partId, this);
277
				} else {
278
					selectionService.removePostSelectionListener(this);
279
				}
280
			} else {
281
				if (partId != null) {
282
					selectionService.removeSelectionListener(partId, this);
283
				} else {
284
					selectionService.removeSelectionListener(this);
285
				}
286
			}
287
		}
288
289
		public void selectionChanged(IWorkbenchPart part, ISelection selection) {
290
			fireChange(null, null);
291
		}
292
	}
293
}

Return to bug 218884