| 1 |
/** |
| 2 |
* Copyright (c) 2008 Matthew Hall and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Matthew Hall - initial API and implementation (bug 194734) |
| 10 |
* Matthew Hall - bug 195222, 247997, 261843, 264307 |
| 11 |
* Hasan Ceylan - patch in bug 262160 |
| 12 |
* Tom Schindl <[email protected]> - port to EMF in 262160 |
| 13 |
*/ |
| 14 |
package org.eclipse.emf.databinding.edit; |
| 15 |
|
| 16 |
import org.eclipse.core.databinding.property.list.IListProperty; |
| 17 |
import org.eclipse.core.databinding.property.map.IMapProperty; |
| 18 |
import org.eclipse.core.databinding.property.set.ISetProperty; |
| 19 |
import org.eclipse.core.databinding.property.value.IValueProperty; |
| 20 |
import org.eclipse.emf.databinding.FeaturePath; |
| 21 |
import org.eclipse.emf.databinding.edit.internal.EMFEditListProperty; |
| 22 |
import org.eclipse.emf.databinding.edit.internal.EMFEditListPropertyDecorator; |
| 23 |
import org.eclipse.emf.databinding.edit.internal.EMFEditMapProperty; |
| 24 |
import org.eclipse.emf.databinding.edit.internal.EMFEditMapPropertyDecorator; |
| 25 |
import org.eclipse.emf.databinding.edit.internal.EMFEditMultiListProperty; |
| 26 |
import org.eclipse.emf.databinding.edit.internal.EMFEditResourceContentProperty; |
| 27 |
import org.eclipse.emf.databinding.edit.internal.EMFEditSetProperty; |
| 28 |
import org.eclipse.emf.databinding.edit.internal.EMFEditSetPropertyDecorator; |
| 29 |
import org.eclipse.emf.databinding.edit.internal.EMFEditValueProperty; |
| 30 |
import org.eclipse.emf.databinding.edit.internal.EMFEditValuePropertyDecorator; |
| 31 |
import org.eclipse.emf.ecore.EObject; |
| 32 |
import org.eclipse.emf.ecore.EStructuralFeature; |
| 33 |
import org.eclipse.emf.edit.domain.EditingDomain; |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* <p><b>PROVISIONAL:</b> This API is subject to arbitrary change, including renaming or removal.</p> |
| 38 |
* |
| 39 |
* A factory to create property bound attributes for {@link EObject} which use an {@link EditingDomain} to make changes to the {@link EObject} |
| 40 |
* |
| 41 |
* @since 2.5 |
| 42 |
*/ |
| 43 |
public class EMFEditProperties |
| 44 |
{ |
| 45 |
/** |
| 46 |
* Turn on debug logging |
| 47 |
*/ |
| 48 |
public static final boolean DEBUG = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns a value property for the given {@link EStructuralFeature} |
| 52 |
* |
| 53 |
* @param editingDomain the editing domain |
| 54 |
* @param feature |
| 55 |
* the feature instance the property is created for |
| 56 |
* @return a value property for the given {@link EStructuralFeature} |
| 57 |
*/ |
| 58 |
public static IEMFEditValueProperty value(EditingDomain editingDomain, EStructuralFeature feature) |
| 59 |
{ |
| 60 |
return value(editingDomain, FeaturePath.fromList(feature)); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns a value property for the given nested {@link EStructuralFeature} |
| 65 |
* feature like the <code>name</code> of a <code>person</code> |
| 66 |
* |
| 67 |
* @param editingDomain the editing domain |
| 68 |
* @param featurePath |
| 69 |
* path to the feature |
| 70 |
* @return a value property for the given {@link FeaturePath} |
| 71 |
*/ |
| 72 |
public static IEMFEditValueProperty value(EditingDomain editingDomain, FeaturePath featurePath) |
| 73 |
{ |
| 74 |
IValueProperty property; |
| 75 |
property = new EMFEditValueProperty(editingDomain, featurePath.getFeaturePath()[0]); |
| 76 |
|
| 77 |
IEMFEditValueProperty featureProperty = new EMFEditValuePropertyDecorator(editingDomain, property, featurePath.getFeaturePath()[0]); |
| 78 |
|
| 79 |
for (int i = 1; i < featurePath.getFeaturePath().length; i++) |
| 80 |
{ |
| 81 |
featureProperty = featureProperty.value(featurePath.getFeaturePath()[i]); |
| 82 |
} |
| 83 |
|
| 84 |
return featureProperty; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns multiple value properties for the given |
| 89 |
* {@link EStructuralFeature}s |
| 90 |
* |
| 91 |
* @param editingDomain |
| 92 |
* the editing domain |
| 93 |
* @param features |
| 94 |
* the feature instances the properties are created for |
| 95 |
* @return an array of properties for the given {@link EStructuralFeature}s |
| 96 |
*/ |
| 97 |
public static IEMFEditValueProperty[] values(EditingDomain editingDomain, EStructuralFeature... features) |
| 98 |
{ |
| 99 |
IEMFEditValueProperty[] properties = new IEMFEditValueProperty [features.length]; |
| 100 |
for (int i = 0; i < properties.length; i++) |
| 101 |
properties[i] = value(editingDomain, features[i]); |
| 102 |
return properties; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns multiple value property for the given nested |
| 107 |
* {@link EStructuralFeature} features like the <code>name</code> of a |
| 108 |
* <code>person</code> |
| 109 |
* |
| 110 |
* @param editingDomain |
| 111 |
* the editing domain |
| 112 |
* @param featurePaths |
| 113 |
* path to the feature |
| 114 |
* @return an array of properties for the given {@link FeaturePath}s |
| 115 |
*/ |
| 116 |
public static IEMFEditValueProperty[] values(EditingDomain editingDomain, FeaturePath... featurePaths) |
| 117 |
{ |
| 118 |
IEMFEditValueProperty[] properties = new IEMFEditValueProperty [featurePaths.length]; |
| 119 |
for (int i = 0; i < properties.length; i++) |
| 120 |
properties[i] = value(editingDomain, featurePaths[i]); |
| 121 |
return properties; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns a set property for the given {@link EStructuralFeature} |
| 126 |
* @param editingDomain |
| 127 |
* the editing domain |
| 128 |
* @param feature |
| 129 |
* the feature instance the property is created for |
| 130 |
* @return a list property for the given {@link EStructuralFeature} |
| 131 |
*/ |
| 132 |
public static IEMFEditSetProperty set(EditingDomain editingDomain, EStructuralFeature feature) |
| 133 |
{ |
| 134 |
ISetProperty property; |
| 135 |
property = new EMFEditSetProperty(editingDomain, feature); |
| 136 |
return new EMFEditSetPropertyDecorator(editingDomain, property, feature); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns a set property for the given {@link FeaturePath} |
| 141 |
* @param editingDomain |
| 142 |
* the editing domain |
| 143 |
* @param featurePath the feature path |
| 144 |
* @return a list property for the given {@link FeaturePath} |
| 145 |
*/ |
| 146 |
public static IEMFEditSetProperty set(EditingDomain editingDomain, FeaturePath featurePath) |
| 147 |
{ |
| 148 |
int len = featurePath.getFeaturePath().length; |
| 149 |
if (len > 1) |
| 150 |
{ |
| 151 |
IValueProperty property; |
| 152 |
property = new EMFEditValueProperty(editingDomain, featurePath.getFeaturePath()[0]); |
| 153 |
|
| 154 |
IEMFEditValueProperty featureProperty = new EMFEditValuePropertyDecorator(editingDomain, property, featurePath.getFeaturePath()[0]); |
| 155 |
|
| 156 |
for (int i = 1; i < featurePath.getFeaturePath().length - 1; i++) |
| 157 |
{ |
| 158 |
featureProperty = featureProperty.value(featurePath.getFeaturePath()[i]); |
| 159 |
} |
| 160 |
|
| 161 |
return featureProperty.set(set(editingDomain, featurePath.getFeaturePath()[len - 1])); |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
return set(editingDomain, featurePath.getFeaturePath()[len - 1]); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns a list property for the given {@link EStructuralFeature} |
| 171 |
* |
| 172 |
* @param editingDomain |
| 173 |
* the editing domain |
| 174 |
* @param feature |
| 175 |
* the feature instance the property is created for |
| 176 |
* @return a list property for the given {@link EStructuralFeature} |
| 177 |
*/ |
| 178 |
public static IEMFEditListProperty list(EditingDomain editingDomain, EStructuralFeature feature) |
| 179 |
{ |
| 180 |
IListProperty property; |
| 181 |
property = new EMFEditListProperty(editingDomain, feature); |
| 182 |
return new EMFEditListPropertyDecorator(editingDomain, property, feature); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns a list property for the given {@link FeaturePath} |
| 187 |
* @return a list property for the given {@link FeaturePath} |
| 188 |
*/ |
| 189 |
public static IEMFEditListProperty list(EditingDomain editingDomain, FeaturePath featurePath) |
| 190 |
{ |
| 191 |
int len = featurePath.getFeaturePath().length; |
| 192 |
if (len > 1) |
| 193 |
{ |
| 194 |
IValueProperty property; |
| 195 |
property = new EMFEditValueProperty(editingDomain, featurePath.getFeaturePath()[0]); |
| 196 |
|
| 197 |
IEMFEditValueProperty featureProperty = new EMFEditValuePropertyDecorator(editingDomain, property, featurePath.getFeaturePath()[0]); |
| 198 |
|
| 199 |
for (int i = 1; i < featurePath.getFeaturePath().length - 1; i++) |
| 200 |
{ |
| 201 |
featureProperty = featureProperty.value(featurePath.getFeaturePath()[i]); |
| 202 |
} |
| 203 |
|
| 204 |
return featureProperty.list(list(editingDomain, featurePath.getFeaturePath()[len - 1])); |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
return list(editingDomain, featurePath.getFeaturePath()[len - 1]); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Combine multiple multi-value features into one observable list property |
| 214 |
* @param editingDomain the editing domain |
| 215 |
* @param features the features to add to the list |
| 216 |
* @return the list property |
| 217 |
*/ |
| 218 |
public static IEMFEditListProperty multiList(EditingDomain editingDomain, EStructuralFeature... features) |
| 219 |
{ |
| 220 |
IEMFEditListProperty[] multi = new IEMFEditListProperty [features.length]; |
| 221 |
int i = 0; |
| 222 |
for (EStructuralFeature feature : features) |
| 223 |
{ |
| 224 |
multi[i++] = list(editingDomain, feature); |
| 225 |
} |
| 226 |
|
| 227 |
return multiList(editingDomain, multi); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Combine multiple features below a common path into one observable list property |
| 232 |
* @param editingDomain the editing domain |
| 233 |
* @param rootPath the root path |
| 234 |
* @param features the features |
| 235 |
* @return the list property |
| 236 |
*/ |
| 237 |
public static IEMFEditListProperty multiList(EditingDomain editingDomain, FeaturePath rootPath, EStructuralFeature... features) |
| 238 |
{ |
| 239 |
IEMFEditListProperty[] multi = new IEMFEditListProperty [features.length]; |
| 240 |
int i = 0; |
| 241 |
int l = rootPath.getFeaturePath().length; |
| 242 |
for (EStructuralFeature f : features) |
| 243 |
{ |
| 244 |
EStructuralFeature[] p = new EStructuralFeature [l + 1]; |
| 245 |
System.arraycopy(rootPath.getFeaturePath(), 0, p, 0, l); |
| 246 |
p[l] = f; |
| 247 |
multi[i++] = list(editingDomain, FeaturePath.fromList(p)); |
| 248 |
} |
| 249 |
return multiList(editingDomain, multi); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Combine the features identified by the the path into one observable list property |
| 254 |
* @param editingDomain the editing domain |
| 255 |
* @param featurePaths the feature paths |
| 256 |
* @return the list property |
| 257 |
*/ |
| 258 |
public static IEMFEditListProperty multiList(EditingDomain editingDomain, FeaturePath... featurePaths) |
| 259 |
{ |
| 260 |
IEMFEditListProperty[] multi = new IEMFEditListProperty [featurePaths.length]; |
| 261 |
int i = 0; |
| 262 |
|
| 263 |
for (FeaturePath path : featurePaths) |
| 264 |
{ |
| 265 |
multi[i++] = list(editingDomain, path); |
| 266 |
} |
| 267 |
|
| 268 |
return multiList(editingDomain, multi); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Combine the given list properties into one observable list property |
| 273 |
* @param editingDomain the editing domain |
| 274 |
* @param properties the properties |
| 275 |
* @return the list property |
| 276 |
*/ |
| 277 |
public static IEMFEditListProperty multiList(EditingDomain editingDomain, IEMFEditListProperty... properties) |
| 278 |
{ |
| 279 |
return new EMFEditMultiListProperty(editingDomain, properties); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Returns a map property for the given {@link EStructuralFeature}. Objects lacking the named property are treated the same as if the |
| 284 |
* property always contains an empty map. |
| 285 |
* |
| 286 |
* @param editingDomain |
| 287 |
* the editing domain |
| 288 |
* @param feature |
| 289 |
* the feature the property is created for |
| 290 |
* @return a map property for the given {@link EStructuralFeature} |
| 291 |
*/ |
| 292 |
public static IEMFEditMapProperty map(EditingDomain editingDomain, EStructuralFeature feature) |
| 293 |
{ |
| 294 |
IMapProperty property; |
| 295 |
property = new EMFEditMapProperty(editingDomain, feature); |
| 296 |
return new EMFEditMapPropertyDecorator(editingDomain, property, feature); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Returns a property to observe a resource-content |
| 301 |
* |
| 302 |
* @param editingDomain the editing domain |
| 303 |
* @return the property |
| 304 |
* @since 2.6 |
| 305 |
*/ |
| 306 |
public static IEMFEditListProperty resource(EditingDomain editingDomain) |
| 307 |
{ |
| 308 |
IListProperty property; |
| 309 |
property = new EMFEditResourceContentProperty(editingDomain); |
| 310 |
return new EMFEditListPropertyDecorator(editingDomain, property, null); |
| 311 |
} |
| 312 |
} |