| 1 |
/** |
| 2 |
* Copyright (c) 2002-2010 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 v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.edit.provider.resource; |
| 12 |
|
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Collection; |
| 16 |
import java.util.Collections; |
| 17 |
import java.util.Date; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.eclipse.emf.common.command.Command; |
| 21 |
import org.eclipse.emf.common.command.UnexecutableCommand; |
| 22 |
import org.eclipse.emf.common.notify.AdapterFactory; |
| 23 |
import org.eclipse.emf.common.notify.Notification; |
| 24 |
import org.eclipse.emf.common.util.ResourceLocator; |
| 25 |
import org.eclipse.emf.ecore.EObject; |
| 26 |
import org.eclipse.emf.ecore.EcorePackage; |
| 27 |
import org.eclipse.emf.ecore.resource.Resource; |
| 28 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
| 29 |
import org.eclipse.emf.edit.EMFEditPlugin; |
| 30 |
import org.eclipse.emf.edit.command.AddCommand; |
| 31 |
import org.eclipse.emf.edit.command.CommandParameter; |
| 32 |
import org.eclipse.emf.edit.command.MoveCommand; |
| 33 |
import org.eclipse.emf.edit.command.RemoveCommand; |
| 34 |
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
| 35 |
import org.eclipse.emf.edit.domain.EditingDomain; |
| 36 |
import org.eclipse.emf.edit.provider.ComposedImage; |
| 37 |
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider; |
| 38 |
import org.eclipse.emf.edit.provider.IItemLabelProvider; |
| 39 |
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor; |
| 40 |
import org.eclipse.emf.edit.provider.IItemPropertySource; |
| 41 |
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider; |
| 42 |
import org.eclipse.emf.edit.provider.ITreeItemContentProvider; |
| 43 |
import org.eclipse.emf.edit.provider.ItemProviderAdapter; |
| 44 |
import org.eclipse.emf.edit.provider.ViewerNotification; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* This is the item provider adapter for a {@link org.eclipse.emf.ecore.resource.Resource} object. |
| 49 |
*/ |
| 50 |
public class ResourceItemProvider |
| 51 |
extends ItemProviderAdapter |
| 52 |
implements |
| 53 |
IEditingDomainItemProvider, |
| 54 |
IStructuredItemContentProvider, |
| 55 |
ITreeItemContentProvider, |
| 56 |
IItemLabelProvider, |
| 57 |
IItemPropertySource |
| 58 |
{ |
| 59 |
/** |
| 60 |
* This constructs an instance from a factory and a notifier. |
| 61 |
*/ |
| 62 |
public ResourceItemProvider(AdapterFactory adapterFactory) |
| 63 |
{ |
| 64 |
super(adapterFactory); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) |
| 69 |
{ |
| 70 |
if (itemPropertyDescriptors == null) |
| 71 |
{ |
| 72 |
super.getPropertyDescriptors(object); |
| 73 |
itemPropertyDescriptors.add |
| 74 |
(new IItemPropertyDescriptor() |
| 75 |
{ |
| 76 |
public void setPropertyValue(Object object, Object value) |
| 77 |
{ |
| 78 |
throw new UnsupportedOperationException(); |
| 79 |
} |
| 80 |
|
| 81 |
public void resetPropertyValue(Object object) |
| 82 |
{ |
| 83 |
throw new UnsupportedOperationException(); |
| 84 |
} |
| 85 |
|
| 86 |
public boolean isSortChoices(Object object) |
| 87 |
{ |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
public boolean isPropertySet(Object object) |
| 92 |
{ |
| 93 |
return ((Resource)object).getURI() != null; |
| 94 |
} |
| 95 |
|
| 96 |
public boolean isMultiLine(Object object) |
| 97 |
{ |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
public boolean isMany(Object object) |
| 102 |
{ |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
public boolean isCompatibleWith(Object object, Object anotherObject, IItemPropertyDescriptor anotherPropertyDescriptor) |
| 107 |
{ |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
public Object getPropertyValue(Object object) |
| 112 |
{ |
| 113 |
return ((Resource)object).getURI(); |
| 114 |
} |
| 115 |
|
| 116 |
public IItemLabelProvider getLabelProvider(Object object) |
| 117 |
{ |
| 118 |
return |
| 119 |
new IItemLabelProvider() |
| 120 |
{ |
| 121 |
public String getText(Object object) |
| 122 |
{ |
| 123 |
return object.toString(); |
| 124 |
} |
| 125 |
|
| 126 |
public Object getImage(Object object) |
| 127 |
{ |
| 128 |
return null; |
| 129 |
} |
| 130 |
}; |
| 131 |
} |
| 132 |
|
| 133 |
public String getId(Object object) |
| 134 |
{ |
| 135 |
return "uri"; |
| 136 |
} |
| 137 |
|
| 138 |
public Object getHelpContextIds(Object object) |
| 139 |
{ |
| 140 |
return null; |
| 141 |
} |
| 142 |
|
| 143 |
public String[] getFilterFlags(Object object) |
| 144 |
{ |
| 145 |
return null; |
| 146 |
} |
| 147 |
|
| 148 |
public Object getFeature(Object object) |
| 149 |
{ |
| 150 |
return Resource.RESOURCE__URI; |
| 151 |
} |
| 152 |
|
| 153 |
public String getDisplayName(Object object) |
| 154 |
{ |
| 155 |
// TODO |
| 156 |
return "URI"; |
| 157 |
} |
| 158 |
|
| 159 |
public String getDescription(Object object) |
| 160 |
{ |
| 161 |
// TODO |
| 162 |
return "The number of resources"; |
| 163 |
} |
| 164 |
|
| 165 |
public Collection<?> getChoiceOfValues(Object object) |
| 166 |
{ |
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
public String getCategory(Object object) |
| 171 |
{ |
| 172 |
return null; |
| 173 |
} |
| 174 |
|
| 175 |
public boolean canSetProperty(Object object) |
| 176 |
{ |
| 177 |
return false; |
| 178 |
} |
| 179 |
}); |
| 180 |
itemPropertyDescriptors.add |
| 181 |
(new IItemPropertyDescriptor() |
| 182 |
{ |
| 183 |
public void setPropertyValue(Object object, Object value) |
| 184 |
{ |
| 185 |
throw new UnsupportedOperationException(); |
| 186 |
} |
| 187 |
|
| 188 |
public void resetPropertyValue(Object object) |
| 189 |
{ |
| 190 |
throw new UnsupportedOperationException(); |
| 191 |
} |
| 192 |
|
| 193 |
public boolean isSortChoices(Object object) |
| 194 |
{ |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
public boolean isPropertySet(Object object) |
| 199 |
{ |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
public boolean isMultiLine(Object object) |
| 204 |
{ |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
public boolean isMany(Object object) |
| 209 |
{ |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
public boolean isCompatibleWith(Object object, Object anotherObject, IItemPropertyDescriptor anotherPropertyDescriptor) |
| 214 |
{ |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
public Object getPropertyValue(Object object) |
| 219 |
{ |
| 220 |
return ((Resource)object).getTimeStamp(); |
| 221 |
} |
| 222 |
|
| 223 |
public IItemLabelProvider getLabelProvider(Object object) |
| 224 |
{ |
| 225 |
return |
| 226 |
new IItemLabelProvider() |
| 227 |
{ |
| 228 |
public String getText(Object object) |
| 229 |
{ |
| 230 |
return EcoreUtil.convertToString(EcorePackage.Literals.EDATE, new Date((Long)object)); |
| 231 |
} |
| 232 |
|
| 233 |
public Object getImage(Object object) |
| 234 |
{ |
| 235 |
return null; |
| 236 |
} |
| 237 |
}; |
| 238 |
} |
| 239 |
|
| 240 |
public String getId(Object object) |
| 241 |
{ |
| 242 |
return "timeStamp"; |
| 243 |
} |
| 244 |
|
| 245 |
public Object getHelpContextIds(Object object) |
| 246 |
{ |
| 247 |
return null; |
| 248 |
} |
| 249 |
|
| 250 |
public String[] getFilterFlags(Object object) |
| 251 |
{ |
| 252 |
return null; |
| 253 |
} |
| 254 |
|
| 255 |
public Object getFeature(Object object) |
| 256 |
{ |
| 257 |
return Resource.RESOURCE__TIME_STAMP; |
| 258 |
} |
| 259 |
|
| 260 |
public String getDisplayName(Object object) |
| 261 |
{ |
| 262 |
// TODO |
| 263 |
return "Time Stamp"; |
| 264 |
} |
| 265 |
|
| 266 |
public String getDescription(Object object) |
| 267 |
{ |
| 268 |
// TODO |
| 269 |
return "The version time stamp"; |
| 270 |
} |
| 271 |
|
| 272 |
public Collection<?> getChoiceOfValues(Object object) |
| 273 |
{ |
| 274 |
return null; |
| 275 |
} |
| 276 |
|
| 277 |
public String getCategory(Object object) |
| 278 |
{ |
| 279 |
return null; |
| 280 |
} |
| 281 |
|
| 282 |
public boolean canSetProperty(Object object) |
| 283 |
{ |
| 284 |
return false; |
| 285 |
} |
| 286 |
}); |
| 287 |
} |
| 288 |
return itemPropertyDescriptors; |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public boolean hasChildren(Object object) |
| 293 |
{ |
| 294 |
return !((Resource)object).isLoaded() || super.hasChildren(object); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public Collection<?> getChildren(Object object) |
| 299 |
{ |
| 300 |
synchronized (object) |
| 301 |
{ |
| 302 |
// Don't include controlled children here, they'll show up under their container. |
| 303 |
// |
| 304 |
Resource.Internal resource = (Resource.Internal)object; |
| 305 |
if (resource.isLoading()) |
| 306 |
{ |
| 307 |
// TODO |
| 308 |
return Collections.singleton("Loading..."); |
| 309 |
} |
| 310 |
else |
| 311 |
{ |
| 312 |
List<EObject> contents = resource.getContents(); |
| 313 |
Collection<Object> result = new ArrayList<Object>(contents.size()); |
| 314 |
for (Object o : contents) |
| 315 |
{ |
| 316 |
if (!AdapterFactoryEditingDomain.isControlled(o)) |
| 317 |
{ |
| 318 |
result.add(o); |
| 319 |
} |
| 320 |
} |
| 321 |
return result; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* This returns the parent of the Resource. |
| 328 |
*/ |
| 329 |
@Override |
| 330 |
public Object getParent(Object object) |
| 331 |
{ |
| 332 |
return ((Resource)object).getResourceSet(); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* This returns Resource.gif. |
| 337 |
*/ |
| 338 |
@Override |
| 339 |
public Object getImage(Object object) |
| 340 |
{ |
| 341 |
Resource resource = (Resource)object; |
| 342 |
// Object image = URI.createURI(getResourceLocator().getImage("full/obj16/Resource").toString() + "#" + resource.getURI().fileExtension()); |
| 343 |
Object image = getResourceLocator().getImage("full/obj16/Resource"); |
| 344 |
|
| 345 |
// Overlay if the resource is the target for any controlled objects. |
| 346 |
// |
| 347 |
for (Object o : resource.getContents()) |
| 348 |
{ |
| 349 |
if (AdapterFactoryEditingDomain.isControlled(o)) |
| 350 |
{ |
| 351 |
List<Object> images = new ArrayList<Object>(2); |
| 352 |
images.add(image); |
| 353 |
images.add(getImage("full/ovr16/ControlledObjectTarget")); |
| 354 |
image = new ComposedImage(images); |
| 355 |
break; |
| 356 |
} |
| 357 |
} |
| 358 |
return image; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* This returns the label text for the adapted class. |
| 363 |
*/ |
| 364 |
@Override |
| 365 |
public String getText(Object object) |
| 366 |
{ |
| 367 |
Resource resource = (Resource)object; |
| 368 |
return resource.getURI() == null ? "" : resource.getURI().toString(); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* This handles notification by calling {@link #fireNotifyChanged(Notification) fireNotifyChanged}. |
| 373 |
*/ |
| 374 |
@Override |
| 375 |
public void notifyChanged(Notification notification) |
| 376 |
{ |
| 377 |
switch (notification.getFeatureID(Resource.class)) |
| 378 |
{ |
| 379 |
case Resource.RESOURCE__URI: |
| 380 |
// case Resource.RESOURCE__IS_MODIFIED: |
| 381 |
// case Resource.RESOURCE__IS_LOADED: |
| 382 |
// case Resource.RESOURCE__IS_TRACKING_MODIFICATION: |
| 383 |
// case Resource.RESOURCE__RESOURCE_SET: |
| 384 |
{ |
| 385 |
fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true)); |
| 386 |
return; |
| 387 |
} |
| 388 |
// When an object is controlled, the only change will be in the new resource's contents, so it must |
| 389 |
// refresh the whole viewer to hit the object's label. |
| 390 |
// |
| 391 |
case Resource.RESOURCE__CONTENTS: |
| 392 |
{ |
| 393 |
fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false)); |
| 394 |
return; |
| 395 |
} |
| 396 |
} |
| 397 |
super.notifyChanged(notification); |
| 398 |
} |
| 399 |
|
| 400 |
@Override |
| 401 |
public Collection<?> getNewChildDescriptors(Object object, EditingDomain editingDomain, Object sibling) |
| 402 |
{ |
| 403 |
return Collections.emptyList(); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children |
| 408 |
* that can be created under this object. |
| 409 |
*/ |
| 410 |
@Override |
| 411 |
protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) |
| 412 |
{ |
| 413 |
super.collectNewChildDescriptors(newChildDescriptors, object); |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Return the resource locator for this item provider's resources. |
| 418 |
*/ |
| 419 |
@Override |
| 420 |
public ResourceLocator getResourceLocator() |
| 421 |
{ |
| 422 |
return EMFEditPlugin.INSTANCE; |
| 423 |
} |
| 424 |
|
| 425 |
@Override |
| 426 |
protected Command factorRemoveCommand(EditingDomain domain, CommandParameter commandParameter) |
| 427 |
{ |
| 428 |
Resource resource = (Resource)commandParameter.getOwner(); |
| 429 |
if (!resource.getContents().containsAll(commandParameter.getCollection())) |
| 430 |
{ |
| 431 |
return UnexecutableCommand.INSTANCE; |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
return new RemoveCommand(domain, resource.getContents(), commandParameter.getCollection()); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
@Override |
| 440 |
protected Command factorMoveCommand(EditingDomain domain, CommandParameter commandParameter) |
| 441 |
{ |
| 442 |
Resource resource = (Resource)commandParameter.getOwner(); |
| 443 |
if (!resource.getContents().contains(commandParameter.getValue())) |
| 444 |
{ |
| 445 |
return UnexecutableCommand.INSTANCE; |
| 446 |
} |
| 447 |
else |
| 448 |
{ |
| 449 |
return new MoveCommand(domain, resource.getContents(), commandParameter.getValue(), commandParameter.getIndex()); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
@Override |
| 454 |
protected Command factorAddCommand(EditingDomain domain, CommandParameter commandParameter) |
| 455 |
{ |
| 456 |
Resource resource = (Resource)commandParameter.getOwner(); |
| 457 |
return new AddCommand(domain, resource.getContents(), commandParameter.getCollection()); |
| 458 |
} |
| 459 |
} |