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

(-)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 (+70 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 - bugs 206839, 124684, 239302, 245647, 194734, 195222,
11
 *                    264286
12
 *******************************************************************************/
13
14
package org.eclipse.ui.databinding;
15
16
import org.eclipse.core.databinding.observable.Diffs;
17
import org.eclipse.core.databinding.observable.value.DecoratingObservableValue;
18
import org.eclipse.core.databinding.observable.value.IObservableValue;
19
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
20
import org.eclipse.core.runtime.IAdapterManager;
21
import org.eclipse.core.runtime.Platform;
22
23
/**
24
 * Factory methods for creating observables for JFace viewers
25
 * 
26
 * @since 3.5
27
 */
28
public class WorkbenchObservables {
29
30
	/**
31
	 * Returns an observable with values of the given target type. If the
32
	 * wrapped observable's value is of the target type, or can be adapted to
33
	 * the target type, this is taken as the value of the returned observable,
34
	 * otherwise <code>null</code>.
35
	 * 
36
	 * @param observable
37
	 *            the observable whose value should be adapted
38
	 * @param targetType
39
	 *            the target type
40
	 * @return an observable with values of the given type, or <code>null</code>
41
	 *         if the current value of the given observable does not adapt to
42
	 *         the target type
43
	 * 
44
	 */
45
	public static IObservableValue observeAdaptedValue(IObservableValue observable,
46
			final Class targetType) {
47
		final IAdapterManager adapterManager = Platform.getAdapterManager();
48
		return new DecoratingObservableValue(observable, false) {
49
			public Object getValue() {
50
				return adapted(super.getValue());
51
			}
52
53
			protected void handleValueChange(ValueChangeEvent event) {
54
				fireValueChange(Diffs.createValueDiff(adapted(event.diff.getOldValue()),
55
						adapted(event.diff.getNewValue())));
56
			}
57
58
			private Object adapted(Object object) {
59
				if (targetType.isInstance(object)) {
60
					return object;
61
				}
62
				return adapterManager.getAdapter(object, targetType);
63
			}
64
65
			public Object getValueType() {
66
				return targetType;
67
			}
68
		};
69
	}
70
}
(-)Eclipse (+273 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
 *******************************************************************************/
11
package org.eclipse.ui.databinding;
12
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.List;
16
17
import org.eclipse.core.databinding.observable.list.ListDiff;
18
import org.eclipse.core.databinding.property.INativePropertyListener;
19
import org.eclipse.core.databinding.property.ISimplePropertyListener;
20
import org.eclipse.core.databinding.property.SimplePropertyEvent;
21
import org.eclipse.core.databinding.property.list.IListProperty;
22
import org.eclipse.core.databinding.property.list.SimpleListProperty;
23
import org.eclipse.core.databinding.property.value.IValueProperty;
24
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
25
import org.eclipse.jface.viewers.ISelection;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.ui.ISelectionListener;
28
import org.eclipse.ui.ISelectionService;
29
import org.eclipse.ui.IWorkbenchPart;
30
31
/**
32
 * Helper class for providing observables for the Workbench.
33
 * 
34
 * <p>
35
 * Examples:
36
 * 
37
 * <pre>
38
 * WorkbenchProperties.singleSelection().observe(
39
 * 		getSite().getService(ISelectionService.class))
40
 * </pre>
41
 * 
42
 * </p>
43
 * 
44
 * @since 3.5
45
 */
46
public class WorkbenchProperties {
47
48
	/**
49
	 * Returns a property for observing the first element of a structured
50
	 * selection as exposed by {@link ISelectionService}.
51
	 * 
52
	 * @return an observable value
53
	 */
54
	public static IValueProperty singleSelection() {
55
		return singleSelection(null, false);
56
	}
57
58
	/**
59
	 * Returns a property for observing the first element of a structured
60
	 * selection as exposed by {@link ISelectionService}.
61
	 * 
62
	 * @param partId
63
	 *            the part id, or <code>null</code> if the selection can be from
64
	 *            any part
65
	 * @param postSelection
66
	 *            <code>true</code> if the selection should be delayed for
67
	 *            keyboard-triggered selections
68
	 * 
69
	 * @return an observable value
70
	 */
71
	public static IValueProperty singleSelection(String partId, boolean postSelection) {
72
		return new SingleSelectionProperty(partId, postSelection);
73
	}
74
75
	/**
76
	 * Returns a property for observing the elements of a structured selection
77
	 * as exposed by {@link ISelectionService}.
78
	 * 
79
	 * @return an observable value
80
	 */
81
	public static IListProperty multipleSelection() {
82
		return multipleSelection(null, false);
83
	}
84
85
	/**
86
	 * Returns a property for observing the elements of a structured selection
87
	 * as exposed by {@link ISelectionService}.
88
	 * 
89
	 * @param partId
90
	 *            the part id, or <code>null</code> if the selection can be from
91
	 *            any part
92
	 * @param postSelection
93
	 *            <code>true</code> if the selection should be delayed for
94
	 *            keyboard-triggered selections
95
	 * 
96
	 * @return an observable value
97
	 */
98
	public static IListProperty multipleSelection(String partId, boolean postSelection) {
99
		return new MultiSelectionProperty(partId, postSelection);
100
	}
101
102
	static class SingleSelectionProperty extends SimpleValueProperty {
103
104
		private final String partId;
105
		private final boolean post;
106
107
		SingleSelectionProperty(String partId, boolean post) {
108
			this.partId = partId;
109
			this.post = post;
110
		}
111
112
		class SingleSelectionListener implements INativePropertyListener,
113
				ISelectionListener {
114
			private final ISimplePropertyListener wrapped;
115
116
			public SingleSelectionListener(ISimplePropertyListener wrapped) {
117
				this.wrapped = wrapped;
118
			}
119
120
			public void addTo(Object source) {
121
				if (post) {
122
					if (partId != null) {
123
						((ISelectionService) source).addPostSelectionListener(partId,
124
								this);
125
					} else {
126
						((ISelectionService) source).addPostSelectionListener(this);
127
					}
128
				} else {
129
					if (partId != null) {
130
						((ISelectionService) source).addSelectionListener(partId, this);
131
					} else {
132
						((ISelectionService) source).addSelectionListener(this);
133
					}
134
				}
135
			}
136
137
			public void removeFrom(Object source) {
138
				if (post) {
139
					if (partId != null) {
140
						((ISelectionService) source).removePostSelectionListener(partId,
141
								this);
142
					} else {
143
						((ISelectionService) source).removePostSelectionListener(this);
144
					}
145
				} else {
146
					if (partId != null) {
147
						((ISelectionService) source)
148
								.removeSelectionListener(partId, this);
149
					} else {
150
						((ISelectionService) source).removeSelectionListener(this);
151
					}
152
				}
153
			}
154
155
			public void selectionChanged(IWorkbenchPart part, ISelection selection) {
156
				wrapped.handleEvent(new SimplePropertyEvent(SimplePropertyEvent.CHANGE,
157
						null, SingleSelectionProperty.this, null));
158
			}
159
		}
160
161
		public INativePropertyListener adaptListener(ISimplePropertyListener listener) {
162
			return null;
163
		}
164
165
		protected Object doGetValue(Object source) {
166
			ISelection selection;
167
			if (partId != null) {
168
				selection = ((ISelectionService) source).getSelection(partId);
169
			} else {
170
				selection = ((ISelectionService) source).getSelection();
171
			}
172
			if (selection instanceof IStructuredSelection) {
173
				return ((IStructuredSelection) selection).getFirstElement();
174
			}
175
			return null;
176
		}
177
178
		protected void doSetValue(Object source, Object value) {
179
			throw new UnsupportedOperationException();
180
		}
181
182
		public Object getValueType() {
183
			return Object.class;
184
		}
185
186
	}
187
188
	static class MultiSelectionProperty extends SimpleListProperty {
189
190
		private final String partId;
191
		private final boolean post;
192
193
		MultiSelectionProperty(String partId, boolean post) {
194
			this.partId = partId;
195
			this.post = post;
196
		}
197
198
		class SingleSelectionListener implements INativePropertyListener,
199
				ISelectionListener {
200
			private final ISimplePropertyListener wrapped;
201
202
			public SingleSelectionListener(ISimplePropertyListener wrapped) {
203
				this.wrapped = wrapped;
204
			}
205
206
			public void addTo(Object source) {
207
				if (post) {
208
					if (partId != null) {
209
						((ISelectionService) source).addPostSelectionListener(partId,
210
								this);
211
					} else {
212
						((ISelectionService) source).addPostSelectionListener(this);
213
					}
214
				} else {
215
					if (partId != null) {
216
						((ISelectionService) source).addSelectionListener(partId, this);
217
					} else {
218
						((ISelectionService) source).addSelectionListener(this);
219
					}
220
				}
221
			}
222
223
			public void removeFrom(Object source) {
224
				if (post) {
225
					if (partId != null) {
226
						((ISelectionService) source).removePostSelectionListener(partId,
227
								this);
228
					} else {
229
						((ISelectionService) source).removePostSelectionListener(this);
230
					}
231
				} else {
232
					if (partId != null) {
233
						((ISelectionService) source)
234
								.removeSelectionListener(partId, this);
235
					} else {
236
						((ISelectionService) source).removeSelectionListener(this);
237
					}
238
				}
239
			}
240
241
			public void selectionChanged(IWorkbenchPart part, ISelection selection) {
242
				wrapped.handleEvent(new SimplePropertyEvent(SimplePropertyEvent.CHANGE,
243
						null, MultiSelectionProperty.this, null));
244
			}
245
		}
246
247
		public INativePropertyListener adaptListener(ISimplePropertyListener listener) {
248
			return null;
249
		}
250
251
		public Object getElementType() {
252
			return Object.class;
253
		}
254
255
		protected List doGetList(Object source) {
256
			ISelection selection;
257
			if (partId != null) {
258
				selection = ((ISelectionService) source).getSelection(partId);
259
			} else {
260
				selection = ((ISelectionService) source).getSelection();
261
			}
262
			if (selection instanceof IStructuredSelection) {
263
				return new ArrayList(((IStructuredSelection) selection).toList());
264
			}
265
			return Collections.EMPTY_LIST;
266
		}
267
268
		protected void doSetList(Object source, List list, ListDiff diff) {
269
			throw new UnsupportedOperationException();
270
		}
271
272
	}
273
}

Return to bug 218884