| 1 |
/** |
| 2 |
* Copyright (c) 2005-2007 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 |
|
| 12 |
package org.eclipse.emf.edit.ui.util; |
| 13 |
|
| 14 |
import java.io.IOException; |
| 15 |
import java.io.InputStream; |
| 16 |
import java.lang.reflect.Field; |
| 17 |
import java.lang.reflect.Method; |
| 18 |
import java.util.Arrays; |
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.List; |
| 21 |
import java.util.Map; |
| 22 |
import java.util.Set; |
| 23 |
|
| 24 |
import org.eclipse.core.resources.IFile; |
| 25 |
import org.eclipse.core.resources.IResource; |
| 26 |
import org.eclipse.core.resources.IStorage; |
| 27 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 28 |
import org.eclipse.core.runtime.CoreException; |
| 29 |
import org.eclipse.core.runtime.IPath; |
| 30 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 31 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 32 |
import org.eclipse.core.runtime.Path; |
| 33 |
import org.eclipse.core.runtime.Platform; |
| 34 |
import org.eclipse.core.runtime.content.IContentType; |
| 35 |
import org.eclipse.ui.IEditorDescriptor; |
| 36 |
import org.eclipse.ui.IEditorInput; |
| 37 |
import org.eclipse.ui.IEditorPart; |
| 38 |
import org.eclipse.ui.IEditorRegistry; |
| 39 |
import org.eclipse.ui.IPathEditorInput; |
| 40 |
import org.eclipse.ui.IStorageEditorInput; |
| 41 |
import org.eclipse.ui.IURIEditorInput; |
| 42 |
import org.eclipse.ui.IWorkbench; |
| 43 |
import org.eclipse.ui.IWorkbenchPage; |
| 44 |
import org.eclipse.ui.PartInitException; |
| 45 |
import org.eclipse.ui.PlatformUI; |
| 46 |
import org.eclipse.ui.part.FileEditorInput; |
| 47 |
import org.eclipse.emf.common.EMFPlugin; |
| 48 |
import org.eclipse.emf.common.ui.URIEditorInput; |
| 49 |
import org.eclipse.emf.common.util.EList; |
| 50 |
import org.eclipse.emf.common.util.URI; |
| 51 |
import org.eclipse.emf.common.util.UniqueEList; |
| 52 |
import org.eclipse.emf.ecore.EObject; |
| 53 |
import org.eclipse.emf.ecore.resource.Resource; |
| 54 |
import org.eclipse.emf.ecore.resource.URIConverter; |
| 55 |
import org.eclipse.emf.ecore.resource.URIHandler; |
| 56 |
import org.eclipse.emf.ecore.resource.impl.URIHandlerImpl; |
| 57 |
import org.eclipse.emf.edit.ui.EMFEditUIPlugin; |
| 58 |
import org.osgi.framework.Bundle; |
| 59 |
|
| 60 |
/** |
| 61 |
* @since 2.2.0 |
| 62 |
*/ |
| 63 |
public class EditUIUtil |
| 64 |
{ |
| 65 |
/** |
| 66 |
* Opens the default editor for the resource that contains the specified |
| 67 |
* EObject. This method only works if the resource's URI is a |
| 68 |
* platform resource URI. |
| 69 |
*/ |
| 70 |
public static boolean openEditor(EObject eObject) throws PartInitException |
| 71 |
{ |
| 72 |
if (eObject != null) |
| 73 |
{ |
| 74 |
Resource resource = eObject.eResource(); |
| 75 |
if (resource != null) |
| 76 |
{ |
| 77 |
URI uri = resource.getURI(); |
| 78 |
if (uri != null) |
| 79 |
{ |
| 80 |
IEditorInput editorInput = null; |
| 81 |
if (uri.isPlatformResource()) |
| 82 |
{ |
| 83 |
String path = uri.toPlatformString(true); |
| 84 |
IResource workspaceResource = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(path)); |
| 85 |
if (workspaceResource instanceof IFile) |
| 86 |
{ |
| 87 |
editorInput = EclipseUtil.createFileEditorInput((IFile)workspaceResource); |
| 88 |
} |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
editorInput = new URIEditorInput(uri); |
| 93 |
} |
| 94 |
if (editorInput != null) |
| 95 |
{ |
| 96 |
IWorkbench workbench = PlatformUI.getWorkbench(); |
| 97 |
IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage(); |
| 98 |
IEditorPart editorPart = page.openEditor(editorInput, workbench.getEditorRegistry().getDefaultEditor(uri.lastSegment()).getId()); |
| 99 |
return editorPart != null; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
public static URI getURI(IEditorInput editorInput) |
| 108 |
{ |
| 109 |
return getURI(editorInput, null); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @since 2.11 |
| 114 |
*/ |
| 115 |
public static URI getURI(IEditorInput editorInput, URIConverter uriConverter) |
| 116 |
{ |
| 117 |
URI result = null; |
| 118 |
if (EMFPlugin.IS_ECLIPSE_RUNNING) |
| 119 |
{ |
| 120 |
result = EclipseUtil.getURI(editorInput, uriConverter); |
| 121 |
} |
| 122 |
if (result == null) |
| 123 |
{ |
| 124 |
if (editorInput instanceof URIEditorInput) |
| 125 |
{ |
| 126 |
result = ((URIEditorInput)editorInput).getURI().trimFragment(); |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
result = URI.createURI(editorInput.getName()); |
| 131 |
} |
| 132 |
} |
| 133 |
return result; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Returns the default editor for a given file name. This method is like {@link IEditorRegistry#getDefaultEditor(String)}, |
| 138 |
* but it will not return <code>null</code> unless all applicable content types have no associated editor. |
| 139 |
* |
| 140 |
* @param fileName the file name in the system |
| 141 |
* @return the descriptor of the default editor, or <code>null</code> if not found |
| 142 |
* @since 2.4 |
| 143 |
*/ |
| 144 |
public static IEditorDescriptor getDefaultEditor(String fileName) |
| 145 |
{ |
| 146 |
return fileName != null && fileName.length() != 0 ? |
| 147 |
getDefaultEditor(fileName, Platform.getContentTypeManager().findContentTypesFor(fileName)) : |
| 148 |
null; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Returns the default editor for a given contents and file name. |
| 153 |
* <p> |
| 154 |
* If a file name is not provided, the entire content type registry will be queried. For performance reasons, it is |
| 155 |
* highly recommended to provide a file name if available. |
| 156 |
* |
| 157 |
* @param contents an input stream |
| 158 |
* @param fileName the file name associated to the contents, or <code>null</code> |
| 159 |
* @return the descriptor of the default editor, or <code>null</code> if not found |
| 160 |
* @since 2.4 |
| 161 |
*/ |
| 162 |
public static IEditorDescriptor getDefaultEditor(InputStream contents, String fileName) |
| 163 |
{ |
| 164 |
if (contents != null) |
| 165 |
{ |
| 166 |
try |
| 167 |
{ |
| 168 |
return getDefaultEditor(fileName, Platform.getContentTypeManager().findContentTypesFor(contents, fileName)); |
| 169 |
} |
| 170 |
catch (IOException e) |
| 171 |
{ |
| 172 |
EMFEditUIPlugin.INSTANCE.log(e); |
| 173 |
} |
| 174 |
} |
| 175 |
return getDefaultEditor(fileName); |
| 176 |
} |
| 177 |
|
| 178 |
private static IEditorDescriptor getDefaultEditor(String fileName, IContentType[] contentTypes) |
| 179 |
{ |
| 180 |
IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry(); |
| 181 |
|
| 182 |
if (contentTypes.length == 0) |
| 183 |
{ |
| 184 |
return editorRegistry.getDefaultEditor(fileName, null); |
| 185 |
} |
| 186 |
|
| 187 |
IEditorDescriptor result = null; |
| 188 |
for (int i = 0; result == null && i < contentTypes.length; i++) |
| 189 |
{ |
| 190 |
result = editorRegistry.getDefaultEditor(fileName, contentTypes[i]); |
| 191 |
} |
| 192 |
return result; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Returns the default editor for a given URI. This method actually attempts to open an input stream for the URI and |
| 197 |
* uses its contents, along with the filename (the URI's last segment), to obtain appropriate content types. |
| 198 |
* <p> |
| 199 |
* If a URI converter is specified, it is used to open the stream. Otherwise, the global default |
| 200 |
* {@link URIConverter#INSTANCE instance} is used. |
| 201 |
* |
| 202 |
* @param uri a URI |
| 203 |
* @param uriConverter URI converter from which to obtain an input stream, or <code>null</code> |
| 204 |
* @return the descriptor of the default editor, or <code>null</code> if not found |
| 205 |
* @since 2.4 |
| 206 |
*/ |
| 207 |
public static IEditorDescriptor getDefaultEditor(URI uri, URIConverter uriConverter) |
| 208 |
{ |
| 209 |
String fileName = URI.decode(uri.lastSegment()); |
| 210 |
if (uriConverter == null) |
| 211 |
{ |
| 212 |
uriConverter = URIConverter.INSTANCE; |
| 213 |
} |
| 214 |
InputStream stream = null; |
| 215 |
|
| 216 |
try |
| 217 |
{ |
| 218 |
stream = uriConverter.createInputStream(uri); |
| 219 |
return getDefaultEditor(stream, fileName); |
| 220 |
} |
| 221 |
catch (IOException e) |
| 222 |
{ |
| 223 |
EMFEditUIPlugin.INSTANCE.log(e); |
| 224 |
return getDefaultEditor(fileName); |
| 225 |
} |
| 226 |
finally |
| 227 |
{ |
| 228 |
close(stream); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private static void close(InputStream stream) |
| 233 |
{ |
| 234 |
if (stream != null) |
| 235 |
{ |
| 236 |
try |
| 237 |
{ |
| 238 |
stream.close(); |
| 239 |
} |
| 240 |
catch (IOException e) { EMFEditUIPlugin.INSTANCE.log(e); } |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Returns the editors for a given file name. This method is like {@link IEditorRegistry#getEditors(String)}, |
| 246 |
* but its result will include editors for all applicable content types. |
| 247 |
* |
| 248 |
* @param fileName the file name in the system |
| 249 |
* @param defaultsOnly if <code>true</code>, only the default editor for each content type will be included in the result |
| 250 |
* @return the descriptors of the editors |
| 251 |
* @since 2.4 |
| 252 |
*/ |
| 253 |
public static IEditorDescriptor[] getEditors(String fileName, boolean defaultsOnly) |
| 254 |
{ |
| 255 |
return fileName != null && fileName.length() != 0 ? |
| 256 |
getEditors(fileName, Platform.getContentTypeManager().findContentTypesFor(fileName), defaultsOnly) : |
| 257 |
new IEditorDescriptor[0]; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Returns the editors for a given contents and file name. |
| 262 |
* <p> |
| 263 |
* If a file name is not provided, the entire content type registry will be queried. For performance reasons, it is |
| 264 |
* highly recommended to provide a file name if available. |
| 265 |
* |
| 266 |
* @param contents an input stream |
| 267 |
* @param fileName the file name associated to the contents, or <code>null</code> |
| 268 |
* @param defaultsOnly if <code>true</code>, only the default editor for each content type will be included in the result |
| 269 |
* @return the descriptors of the editors |
| 270 |
* @since 2.4 |
| 271 |
*/ |
| 272 |
public static IEditorDescriptor[] getEditors(InputStream contents, String fileName, boolean defaultsOnly) |
| 273 |
{ |
| 274 |
if (contents != null) |
| 275 |
{ |
| 276 |
try |
| 277 |
{ |
| 278 |
return getEditors(fileName, Platform.getContentTypeManager().findContentTypesFor(contents, fileName), defaultsOnly); |
| 279 |
} |
| 280 |
catch (IOException e) |
| 281 |
{ |
| 282 |
EMFEditUIPlugin.INSTANCE.log(e); |
| 283 |
} |
| 284 |
} |
| 285 |
return getEditors(fileName, defaultsOnly); |
| 286 |
} |
| 287 |
|
| 288 |
private static IEditorDescriptor[] getEditors(String fileName, IContentType[] contentTypes, boolean defaultsOnly) |
| 289 |
{ |
| 290 |
IEditorRegistry editorRegistry = PlatformUI.getWorkbench().getEditorRegistry(); |
| 291 |
|
| 292 |
if (contentTypes.length == 0) |
| 293 |
{ |
| 294 |
return editorRegistry.getEditors(fileName, null); |
| 295 |
} |
| 296 |
|
| 297 |
List<IEditorDescriptor> result = new UniqueEList<IEditorDescriptor>(); |
| 298 |
for (IContentType contentType : contentTypes) |
| 299 |
{ |
| 300 |
if (defaultsOnly) |
| 301 |
{ |
| 302 |
IEditorDescriptor editor = editorRegistry.getDefaultEditor(fileName, contentType); |
| 303 |
if (editor != null) |
| 304 |
{ |
| 305 |
result.add(editor); |
| 306 |
} |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
result.addAll(Arrays.asList(editorRegistry.getEditors(fileName, contentType))); |
| 311 |
} |
| 312 |
} |
| 313 |
return result.toArray(new IEditorDescriptor[result.size()]); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns the editors for a given URI. This method actually attempts to open an input stream for the URI and |
| 318 |
* uses its contents, along with the filename (the URI's last segment), to obtain appropriate content types. |
| 319 |
* <p> |
| 320 |
* If a URI converter is specified, it is used to open the stream. Otherwise, the global default |
| 321 |
* {@link URIConverter#INSTANCE instance} is used. |
| 322 |
* |
| 323 |
* @param uri a URI |
| 324 |
* @param uriConverter a URI converter from which to obtain an input stream, or <code>null</code> |
| 325 |
* @param defaultsOnly if <code>true</code>, only the default editor for each content type will be included in the result |
| 326 |
* @return the descriptors of the editors |
| 327 |
* @since 2.4 |
| 328 |
*/ |
| 329 |
public static IEditorDescriptor[] getEditors(URI uri, URIConverter uriConverter, boolean defaultsOnly) |
| 330 |
{ |
| 331 |
String fileName = URI.decode(uri.lastSegment()); |
| 332 |
if (uriConverter == null) |
| 333 |
{ |
| 334 |
uriConverter = URIConverter.INSTANCE; |
| 335 |
} |
| 336 |
InputStream stream = null; |
| 337 |
|
| 338 |
try |
| 339 |
{ |
| 340 |
stream = uriConverter.createInputStream(uri); |
| 341 |
return getEditors(stream, fileName, defaultsOnly); |
| 342 |
} |
| 343 |
catch (IOException e) |
| 344 |
{ |
| 345 |
EMFEditUIPlugin.INSTANCE.log(e); |
| 346 |
return getEditors(fileName, defaultsOnly); |
| 347 |
} |
| 348 |
finally |
| 349 |
{ |
| 350 |
close(stream); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
private static class StorageURIHandler extends URIHandlerImpl |
| 355 |
{ |
| 356 |
final private URI uri; |
| 357 |
final private IStorage storage; |
| 358 |
|
| 359 |
private StorageURIHandler(URI uri, IStorage storage) |
| 360 |
{ |
| 361 |
this.uri = uri; |
| 362 |
this.storage = storage; |
| 363 |
} |
| 364 |
|
| 365 |
@Override |
| 366 |
public boolean canHandle(URI uri) |
| 367 |
{ |
| 368 |
return uri.equals(this.uri); |
| 369 |
} |
| 370 |
|
| 371 |
@Override |
| 372 |
public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException |
| 373 |
{ |
| 374 |
try |
| 375 |
{ |
| 376 |
return storage.getContents(); |
| 377 |
} |
| 378 |
catch (CoreException exception) |
| 379 |
{ |
| 380 |
throw new Resource.IOWrappedException(exception); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public boolean exists(URI uri, Map<?, ?> options) |
| 386 |
{ |
| 387 |
return true; |
| 388 |
} |
| 389 |
|
| 390 |
@Override |
| 391 |
public Map<String, ?> getAttributes(URI uri, Map<?, ?> options) |
| 392 |
{ |
| 393 |
Map<String, Object> result = new HashMap<String, Object>(); |
| 394 |
Set<String> requestedAttributes = getRequestedAttributes(options); |
| 395 |
if (requestedAttributes == null || requestedAttributes.contains(URIConverter.ATTRIBUTE_READ_ONLY)) |
| 396 |
{ |
| 397 |
result.put(URIConverter.ATTRIBUTE_READ_ONLY, storage.isReadOnly()); |
| 398 |
} |
| 399 |
|
| 400 |
return result; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
private static class EclipseUtil |
| 405 |
{ |
| 406 |
static final Class<?> FILE_CLASS; |
| 407 |
static |
| 408 |
{ |
| 409 |
Class<?> fileClass = null; |
| 410 |
try |
| 411 |
{ |
| 412 |
fileClass = IFile.class; |
| 413 |
} |
| 414 |
catch (Throwable exception) |
| 415 |
{ |
| 416 |
// Ignore any exceptions and assume the class isn't available. |
| 417 |
} |
| 418 |
FILE_CLASS = fileClass; |
| 419 |
} |
| 420 |
|
| 421 |
static final Class<?> FILE_REVISION_CLASS; |
| 422 |
static final Method FILE_REVISION_GET_URI_METHOD; |
| 423 |
static final Method FILE_REVISION_GET_STORAGE; |
| 424 |
static |
| 425 |
{ |
| 426 |
FILE_REVISION_CLASS = loadClass("org.eclipse.team.core", "org.eclipse.team.core.history.IFileRevision"); |
| 427 |
FILE_REVISION_GET_URI_METHOD = getMethod(FILE_REVISION_CLASS, "getURI"); |
| 428 |
FILE_REVISION_GET_STORAGE = getMethod(FILE_REVISION_CLASS, "getStorage", IProgressMonitor.class); |
| 429 |
} |
| 430 |
|
| 431 |
static final Class<?> URI_EDITOR_INPUT_CLASS; |
| 432 |
static |
| 433 |
{ |
| 434 |
Class<?> uriEditorInputClass = null; |
| 435 |
try |
| 436 |
{ |
| 437 |
uriEditorInputClass = IURIEditorInput.class; |
| 438 |
} |
| 439 |
catch (Throwable exception) |
| 440 |
{ |
| 441 |
// The class is not available. |
| 442 |
} |
| 443 |
URI_EDITOR_INPUT_CLASS = uriEditorInputClass; |
| 444 |
} |
| 445 |
|
| 446 |
static final Class<?> STORAGE_CLASS; |
| 447 |
static |
| 448 |
{ |
| 449 |
Class<?> storageClass = null; |
| 450 |
try |
| 451 |
{ |
| 452 |
storageClass = IStorage.class; |
| 453 |
} |
| 454 |
catch (Throwable exception) |
| 455 |
{ |
| 456 |
// Ignore any exceptions and assume the class isn't available. |
| 457 |
} |
| 458 |
STORAGE_CLASS = storageClass; |
| 459 |
} |
| 460 |
|
| 461 |
static final Class<?> STORAGE_EDITOR_INPUT_CLASS; |
| 462 |
static |
| 463 |
{ |
| 464 |
Class<?> storageEditorInputClass = null; |
| 465 |
try |
| 466 |
{ |
| 467 |
storageEditorInputClass = IStorageEditorInput.class; |
| 468 |
} |
| 469 |
catch (Throwable exception) |
| 470 |
{ |
| 471 |
// The class is not available. |
| 472 |
} |
| 473 |
STORAGE_EDITOR_INPUT_CLASS = storageEditorInputClass; |
| 474 |
} |
| 475 |
|
| 476 |
static final Class<?> JAR_ENTRY_RESOURCE_CLASS; |
| 477 |
static final Method JAR_ENTRY_RESOURCE_GET_PACKAGE_FRAGMENT_ROOT; |
| 478 |
static |
| 479 |
{ |
| 480 |
JAR_ENTRY_RESOURCE_CLASS = loadClass("org.eclipse.jdt.core", "org.eclipse.jdt.core.IJarEntryResource"); |
| 481 |
JAR_ENTRY_RESOURCE_GET_PACKAGE_FRAGMENT_ROOT = getMethod(JAR_ENTRY_RESOURCE_CLASS, "getPackageFragmentRoot"); |
| 482 |
} |
| 483 |
|
| 484 |
static final Class<?> NON_JAVA_RESOURCE_CLASS = loadClass("org.eclipse.jdt.core", "org.eclipse.jdt.internal.core.NonJavaResource"); |
| 485 |
static final Field NON_JAVA_RESOURCE_FIELD; |
| 486 |
static |
| 487 |
{ |
| 488 |
Field nonJavaResourceField = null; |
| 489 |
if (NON_JAVA_RESOURCE_CLASS != null) |
| 490 |
{ |
| 491 |
try |
| 492 |
{ |
| 493 |
nonJavaResourceField = NON_JAVA_RESOURCE_CLASS.getDeclaredField("resource"); |
| 494 |
nonJavaResourceField.setAccessible(true); |
| 495 |
} |
| 496 |
catch (Throwable throwable) |
| 497 |
{ |
| 498 |
// Ignore. |
| 499 |
} |
| 500 |
} |
| 501 |
NON_JAVA_RESOURCE_FIELD = nonJavaResourceField; |
| 502 |
} |
| 503 |
|
| 504 |
private static Class<?> loadClass(String bundleID, String className) |
| 505 |
{ |
| 506 |
Bundle bundle = Platform.getBundle(bundleID); |
| 507 |
if (bundle != null && (bundle.getState() & (Bundle.ACTIVE | Bundle.STARTING | Bundle.RESOLVED)) != 0) |
| 508 |
{ |
| 509 |
try |
| 510 |
{ |
| 511 |
return bundle.loadClass(className); |
| 512 |
} |
| 513 |
catch (Throwable exeption) |
| 514 |
{ |
| 515 |
// Ignore any exceptions and assume the class isn't available. |
| 516 |
} |
| 517 |
} |
| 518 |
return null; |
| 519 |
} |
| 520 |
|
| 521 |
private static Method getMethod(Class<?> theClass, String name, Class<?>... parameterTypes) |
| 522 |
{ |
| 523 |
try |
| 524 |
{ |
| 525 |
return theClass == null ? null : theClass.getMethod(name, parameterTypes); |
| 526 |
} |
| 527 |
catch (Throwable exception) |
| 528 |
{ |
| 529 |
return null; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
private static Object invoke(Method method, Object object, Object... arguments) |
| 534 |
{ |
| 535 |
try |
| 536 |
{ |
| 537 |
return method == null ? null : method.invoke(object, arguments); |
| 538 |
} |
| 539 |
catch (Throwable throwable) |
| 540 |
{ |
| 541 |
return null; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
private static Object invoke(String methodName, Object object) |
| 546 |
{ |
| 547 |
if (object != null) |
| 548 |
{ |
| 549 |
Method method = getMethod(object.getClass(), methodName); |
| 550 |
if (method != null) |
| 551 |
{ |
| 552 |
return invoke(method, object); |
| 553 |
} |
| 554 |
} |
| 555 |
return null; |
| 556 |
} |
| 557 |
|
| 558 |
public static URI getURI(IEditorInput editorInput, URIConverter uriConverter) |
| 559 |
{ |
| 560 |
if (FILE_CLASS != null) |
| 561 |
{ |
| 562 |
IFile file = (IFile)editorInput.getAdapter(FILE_CLASS); |
| 563 |
if (file != null) |
| 564 |
{ |
| 565 |
return URI.createPlatformResourceURI(file.getFullPath().toString(), true); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
if (FILE_REVISION_CLASS != null) |
| 570 |
{ |
| 571 |
Object fileRevision = editorInput.getAdapter(FILE_REVISION_CLASS); |
| 572 |
if (fileRevision != null) |
| 573 |
{ |
| 574 |
try |
| 575 |
{ |
| 576 |
URI uri = URI.createURI(((java.net.URI)FILE_REVISION_GET_URI_METHOD.invoke(fileRevision)).toString()); |
| 577 |
IStorage storage = (IStorage)invoke(FILE_REVISION_GET_STORAGE, fileRevision, new NullProgressMonitor()); |
| 578 |
if (storage != null) |
| 579 |
{ |
| 580 |
registerHandler(uri, storage, uriConverter); |
| 581 |
} |
| 582 |
return uri; |
| 583 |
} |
| 584 |
catch (Throwable exception) |
| 585 |
{ |
| 586 |
EMFEditUIPlugin.INSTANCE.log(exception); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
if (STORAGE_CLASS != null) |
| 592 |
{ |
| 593 |
IStorage storage = (IStorage)editorInput.getAdapter(STORAGE_CLASS); |
| 594 |
URI uri = handleStorage(storage, uriConverter); |
| 595 |
if (uri != null) |
| 596 |
{ |
| 597 |
return uri; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
if (editorInput instanceof IPathEditorInput) |
| 602 |
{ |
| 603 |
IPathEditorInput pathEditorInput = (IPathEditorInput)editorInput; |
| 604 |
IPath path = pathEditorInput.getPath(); |
| 605 |
if (path != null) |
| 606 |
{ |
| 607 |
return URI.createFileURI(path.toString()); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
if (STORAGE_EDITOR_INPUT_CLASS != null) |
| 612 |
{ |
| 613 |
if (editorInput instanceof IStorageEditorInput) |
| 614 |
{ |
| 615 |
IStorageEditorInput storageEditorInput = (IStorageEditorInput)editorInput; |
| 616 |
try |
| 617 |
{ |
| 618 |
IStorage storage = storageEditorInput.getStorage(); |
| 619 |
URI uri = handleStorage(storage, uriConverter); |
| 620 |
if (uri != null) |
| 621 |
{ |
| 622 |
return uri; |
| 623 |
} |
| 624 |
} |
| 625 |
catch (CoreException e) |
| 626 |
{ |
| 627 |
// Ignore |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
if (URI_EDITOR_INPUT_CLASS != null) |
| 633 |
{ |
| 634 |
if (editorInput instanceof IURIEditorInput) |
| 635 |
{ |
| 636 |
return URI.createURI(((IURIEditorInput)editorInput).getURI().toString()).trimFragment(); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
URI uri = (URI)editorInput.getAdapter(URI.class); |
| 641 |
if (uri == null) |
| 642 |
{ |
| 643 |
java.net.URI javaNetURI = (java.net.URI)editorInput.getAdapter(java.net.URI.class); |
| 644 |
if (javaNetURI != null) |
| 645 |
{ |
| 646 |
uri = URI.createURI(javaNetURI.toString()); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
return uri; |
| 651 |
} |
| 652 |
|
| 653 |
private static IFile getNonJavaResourceFieldValue(Object object) |
| 654 |
{ |
| 655 |
// This is a very ugly workaround for the fact that org.eclipse.jdt.internal.core.NonJavaResource |
| 656 |
// does not return a meaningful entry name relative to its parent. |
| 657 |
// The entry name will just be the file name and the parent will be the package fragment root |
| 658 |
// which can be arbitrarily many folders up from the entry. |
| 659 |
// There are no methods on the implementation to determine the correct path. |
| 660 |
if (NON_JAVA_RESOURCE_CLASS != null && NON_JAVA_RESOURCE_CLASS.isInstance(object)) |
| 661 |
{ |
| 662 |
try |
| 663 |
{ |
| 664 |
return (IFile)NON_JAVA_RESOURCE_FIELD.get(object); |
| 665 |
} |
| 666 |
catch (Throwable throwable) |
| 667 |
{ |
| 668 |
return null; |
| 669 |
} |
| 670 |
} |
| 671 |
return null; |
| 672 |
} |
| 673 |
|
| 674 |
private static URI handleStorage(IStorage storage, URIConverter uriConverter) |
| 675 |
{ |
| 676 |
if (storage != null) |
| 677 |
{ |
| 678 |
IPath path = storage.getFullPath(); |
| 679 |
if (path != null) |
| 680 |
{ |
| 681 |
if (JAR_ENTRY_RESOURCE_CLASS != null && JAR_ENTRY_RESOURCE_CLASS.isInstance(storage)) |
| 682 |
{ |
| 683 |
IFile file = getNonJavaResourceFieldValue(storage); |
| 684 |
if (file != null) |
| 685 |
{ |
| 686 |
return URI.createPlatformResourceURI(file.getFullPath().toString(), true); |
| 687 |
} |
| 688 |
|
| 689 |
Object packageFragmentRoot = invoke(JAR_ENTRY_RESOURCE_GET_PACKAGE_FRAGMENT_ROOT, storage); |
| 690 |
if (packageFragmentRoot != null) |
| 691 |
{ |
| 692 |
IPath archivePath = (IPath)invoke("getPath", packageFragmentRoot); |
| 693 |
if (archivePath != null) |
| 694 |
{ |
| 695 |
return URI.createURI("archive:" + URI.createFileURI(archivePath.toString()) + "!" + path); |
| 696 |
} |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
URI uri = URI.createURI(path.toString()); |
| 701 |
registerHandler(uri, storage, uriConverter); |
| 702 |
return uri; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
return null; |
| 707 |
} |
| 708 |
|
| 709 |
private static void registerHandler(URI uri, IStorage storage, URIConverter uriConverter) |
| 710 |
{ |
| 711 |
if (uriConverter != null) |
| 712 |
{ |
| 713 |
EList<URIHandler> uriHandlers = uriConverter.getURIHandlers(); |
| 714 |
for (URIHandler uriHandler : uriHandlers) |
| 715 |
{ |
| 716 |
if (uriHandler instanceof StorageURIHandler) |
| 717 |
{ |
| 718 |
return; |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
URI logicalURI = URI.createURI("storage:/").appendSegments(uri.segments()); |
| 723 |
uriConverter.getURIMap().put(uri, logicalURI); |
| 724 |
uriHandlers.add(0, new StorageURIHandler(logicalURI, storage)); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
public static IEditorInput createFileEditorInput(IFile file) |
| 729 |
{ |
| 730 |
return new FileEditorInput(file); |
| 731 |
} |
| 732 |
} |
| 733 |
} |