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