|
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 |
} |