1 11 package org.eclipse.ui.ide; 12 13 import java.net.URI ; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.osgi.util.NLS; 20 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Shell; 23 24 import org.eclipse.core.filesystem.EFS; 25 import org.eclipse.core.filesystem.IFileStore; 26 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IAdaptable; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.MultiStatus; 31 import org.eclipse.core.runtime.Platform; 32 import org.eclipse.core.runtime.QualifiedName; 33 import org.eclipse.core.runtime.SafeRunner; 34 import org.eclipse.core.runtime.content.IContentDescription; 35 import org.eclipse.core.runtime.content.IContentType; 36 import org.eclipse.core.runtime.content.IContentTypeMatcher; 37 38 import org.eclipse.core.resources.IFile; 39 import org.eclipse.core.resources.IMarker; 40 import org.eclipse.core.resources.IResource; 41 import org.eclipse.core.resources.IResourceDelta; 42 import org.eclipse.core.resources.IWorkspaceRoot; 43 import org.eclipse.core.resources.ResourcesPlugin; 44 import org.eclipse.core.resources.mapping.IModelProviderDescriptor; 45 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 46 import org.eclipse.core.resources.mapping.ModelProvider; 47 import org.eclipse.core.resources.mapping.ModelStatus; 48 import org.eclipse.core.resources.mapping.ResourceChangeValidator; 49 import org.eclipse.core.resources.mapping.ResourceMapping; 50 import org.eclipse.core.resources.mapping.ResourceMappingContext; 51 import org.eclipse.core.resources.mapping.ResourceTraversal; 52 53 import org.eclipse.jface.dialogs.ErrorDialog; 54 import org.eclipse.jface.dialogs.IDialogConstants; 55 import org.eclipse.jface.util.SafeRunnable; 56 import org.eclipse.jface.viewers.IStructuredSelection; 57 58 import org.eclipse.ui.IEditorDescriptor; 59 import org.eclipse.ui.IEditorInput; 60 import org.eclipse.ui.IEditorPart; 61 import org.eclipse.ui.IEditorRegistry; 62 import org.eclipse.ui.IMarkerHelpRegistry; 63 import org.eclipse.ui.ISaveableFilter; 64 import org.eclipse.ui.IWorkbenchPage; 65 import org.eclipse.ui.IWorkbenchPart; 66 import org.eclipse.ui.IWorkbenchWindow; 67 import org.eclipse.ui.PartInitException; 68 import org.eclipse.ui.PlatformUI; 69 import org.eclipse.ui.Saveable; 70 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 71 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 72 import org.eclipse.ui.internal.ide.registry.MarkerHelpRegistry; 73 import org.eclipse.ui.internal.ide.registry.MarkerHelpRegistryReader; 74 import org.eclipse.ui.internal.misc.UIStats; 75 import org.eclipse.ui.part.FileEditorInput; 76 77 84 public final class IDE { 85 119 public static final QualifiedName EDITOR_KEY = new QualifiedName( 120 "org.eclipse.ui.internal.registry.ResourceEditorRegistry", "EditorProperty"); 122 126 public static final String EDITOR_ID_ATTR = "org.eclipse.ui.editorID"; 128 131 public static final String RESOURCE_PERSPECTIVE_ID = "org.eclipse.ui.resourcePerspective"; 133 137 private static MarkerHelpRegistry markerHelpRegistry = null; 138 139 149 public interface SharedImages { 150 153 public final static String IMG_OBJ_PROJECT = "IMG_OBJ_PROJECT"; 155 158 public final static String IMG_OBJ_PROJECT_CLOSED = "IMG_OBJ_PROJECT_CLOSED"; 160 163 public final static String IMG_OPEN_MARKER = "IMG_OPEN_MARKER"; 165 168 public final static String IMG_OBJS_TASK_TSK = "IMG_OBJS_TASK_TSK"; 170 173 public final static String IMG_OBJS_BKMRK_TSK = "IMG_OBJS_BKMRK_TSK"; } 175 176 182 public interface Preferences { 183 184 199 public static final String PROJECT_OPEN_NEW_PERSPECTIVE = "PROJECT_OPEN_NEW_PERSPECTIVE"; 201 212 public static final String SHOW_WORKSPACE_SELECTION_DIALOG = "SHOW_WORKSPACE_SELECTION_DIALOG"; 214 222 public static final String MAX_RECENT_WORKSPACES = "MAX_RECENT_WORKSPACES"; 224 231 public static final String RECENT_WORKSPACES = "RECENT_WORKSPACES"; 233 241 public static final String RECENT_WORKSPACES_PROTOCOL = "RECENT_WORKSPACES_PROTOCOL"; 243 } 244 245 251 private static class SaveFilter implements ISaveableFilter { 252 private final IResource[] roots; 253 254 258 public SaveFilter(IResource[] roots) { 259 this.roots = roots; 260 } 261 262 265 public boolean select(Saveable saveable, 266 IWorkbenchPart[] containingParts) { 267 if (isDescendantOfRoots(saveable)) { 268 return true; 269 } 270 for (int i = 0; i < containingParts.length; i++) { 272 IWorkbenchPart workbenchPart = containingParts[i]; 273 if (workbenchPart instanceof IEditorPart) { 274 IEditorPart editorPart = (IEditorPart) workbenchPart; 275 if (isEditingDescendantOf(editorPart)) { 276 return true; 277 } 278 } 279 } 280 return false; 281 } 282 283 290 private boolean isDescendantOfRoots(Saveable saveable) { 291 ResourceMapping mapping = ResourceUtil.getResourceMapping(saveable); 293 if (mapping != null) { 294 try { 295 ResourceTraversal[] traversals = mapping.getTraversals( 296 ResourceMappingContext.LOCAL_CONTEXT, null); 297 for (int i = 0; i < traversals.length; i++) { 298 ResourceTraversal traversal = traversals[i]; 299 IResource[] resources = traversal.getResources(); 300 for (int j = 0; j < resources.length; j++) { 301 IResource resource = resources[j]; 302 if (isDescendantOfRoots(resource)) { 303 return true; 304 } 305 } 306 } 307 } catch (CoreException e) { 308 IDEWorkbenchPlugin 309 .log( 310 NLS 311 .bind( 312 "An internal error occurred while determining the resources for {0}", saveable.getName()), e); } 314 } else { 315 IFile file = ResourceUtil.getFile(saveable); 317 if (file != null) { 318 return isDescendantOfRoots(file); 319 } 320 } 321 return false; 322 } 323 324 333 private boolean isDescendantOfRoots(IResource resource) { 334 for (int l = 0; l < roots.length; l++) { 335 IResource root = roots[l]; 336 if (root.getFullPath().isPrefixOf(resource.getFullPath())) { 337 return true; 338 } 339 } 340 return false; 341 } 342 343 352 private boolean isEditingDescendantOf(IEditorPart part) { 353 IFile file = ResourceUtil.getFile(part.getEditorInput()); 354 if (file != null) { 355 return isDescendantOfRoots(file); 356 } 357 return false; 358 } 359 360 } 361 362 365 private IDE() { 366 } 368 369 374 public static IMarkerHelpRegistry getMarkerHelpRegistry() { 375 if (markerHelpRegistry == null) { 376 markerHelpRegistry = new MarkerHelpRegistry(); 377 new MarkerHelpRegistryReader().addHelp(markerHelpRegistry); 378 } 379 return markerHelpRegistry; 380 } 381 382 394 public static void gotoMarker(IEditorPart editor, IMarker marker) { 395 IGotoMarker gotoMarker = null; 396 if (editor instanceof IGotoMarker) { 397 gotoMarker = (IGotoMarker) editor; 398 } else { 399 gotoMarker = (IGotoMarker) editor.getAdapter(IGotoMarker.class); 400 } 401 if (gotoMarker != null) { 402 gotoMarker.gotoMarker(marker); 403 } 404 } 405 406 425 public static IEditorPart openEditor(IWorkbenchPage page, 426 IEditorInput input, String editorId) throws PartInitException { 427 if (page == null) { 429 throw new IllegalArgumentException (); 430 } 431 432 return page.openEditor(input, editorId); 434 } 435 436 464 public static IEditorPart openEditor(IWorkbenchPage page, URI uri, 465 String editorId, boolean activate) throws PartInitException { 466 if (page == null) { 468 throw new IllegalArgumentException (); 469 } 470 471 IFileStore fileStore; 472 try { 473 fileStore = EFS.getStore(uri); 474 } catch (CoreException e) { 475 throw new PartInitException( 476 IDEWorkbenchMessages.IDE_coreExceptionFileStore, e); 477 } 478 479 IEditorInput input = getEditorInput(fileStore); 480 481 return page.openEditor(input, editorId, activate); 483 } 484 485 495 private static IEditorInput getEditorInput(IFileStore fileStore) { 496 IFile workspaceFile = getWorkspaceFile(fileStore); 497 if (workspaceFile != null) 498 return new FileEditorInput(workspaceFile); 499 return new FileStoreEditorInput(fileStore); 500 } 501 502 511 private static IFile getWorkspaceFile(IFileStore fileStore) { 512 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 513 IFile[] files = root.findFilesForLocationURI(fileStore.toURI()); 514 files = filterNonExistentFiles(files); 515 if (files == null || files.length == 0) 516 return null; 517 518 return files[0]; 520 } 521 522 530 private static IFile[] filterNonExistentFiles(IFile[] files) { 531 if (files == null) 532 return null; 533 534 int length = files.length; 535 ArrayList existentFiles = new ArrayList (length); 536 for (int i = 0; i < length; i++) { 537 if (files[i].exists()) 538 existentFiles.add(files[i]); 539 } 540 return (IFile[]) existentFiles.toArray(new IFile[existentFiles.size()]); 541 } 542 543 566 public static IEditorPart openEditor(IWorkbenchPage page, 567 IEditorInput input, String editorId, boolean activate) 568 throws PartInitException { 569 if (page == null) { 571 throw new IllegalArgumentException (); 572 } 573 574 return page.openEditor(input, editorId, activate); 576 } 577 578 601 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, 602 boolean activate) throws PartInitException { 603 return openEditor(page, input, activate, true); 604 } 605 606 633 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, 634 boolean activate, boolean determineContentType) 635 throws PartInitException { 636 if (page == null) { 638 throw new IllegalArgumentException (); 639 } 640 641 IEditorDescriptor editorDesc = getEditorDescriptor(input, 643 determineContentType); 644 return page.openEditor(new FileEditorInput(input), editorDesc.getId(), 645 activate); 646 } 647 648 667 public static IEditorPart openEditor(IWorkbenchPage page, IFile input) 668 throws PartInitException { 669 if (page == null) { 671 throw new IllegalArgumentException (); 672 } 673 674 IEditorDescriptor editorDesc = getEditorDescriptor(input); 676 return page.openEditor(new FileEditorInput(input), editorDesc.getId()); 677 } 678 679 698 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, 699 String editorId) throws PartInitException { 700 if (page == null) { 702 throw new IllegalArgumentException (); 703 } 704 705 return page.openEditor(new FileEditorInput(input), editorId); 707 } 708 709 732 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, 733 String editorId, boolean activate) throws PartInitException { 734 if (page == null) { 736 throw new IllegalArgumentException (); 737 } 738 739 return page.openEditor(new FileEditorInput(input), editorId, activate); 741 } 742 743 772 public static IEditorDescriptor getEditorDescriptor(IFile file) 773 throws PartInitException { 774 return getEditorDescriptor(file, true); 775 } 776 777 810 public static IEditorDescriptor getEditorDescriptor(IFile file, 811 boolean determineContentType) throws PartInitException { 812 813 if (file == null) { 814 throw new IllegalArgumentException (); 815 } 816 817 return getEditorDescriptor(file.getName(), PlatformUI.getWorkbench() 818 .getEditorRegistry(), getDefaultEditor(file, 819 determineContentType)); 820 } 821 822 851 public static IEditorDescriptor getEditorDescriptor(String name) 852 throws PartInitException { 853 return getEditorDescriptor(name, true); 854 } 855 856 889 public static IEditorDescriptor getEditorDescriptor(String name, 890 boolean inferContentType) throws PartInitException { 891 892 if (name == null) { 893 throw new IllegalArgumentException (); 894 } 895 896 IContentType contentType = inferContentType ? Platform 897 .getContentTypeManager().findContentTypeFor(name) : null; 898 IEditorRegistry editorReg = PlatformUI.getWorkbench() 899 .getEditorRegistry(); 900 901 return getEditorDescriptor(name, editorReg, editorReg.getDefaultEditor( 902 name, contentType)); 903 } 904 905 921 private static IEditorDescriptor getEditorDescriptor(String name, 922 IEditorRegistry editorReg, IEditorDescriptor defaultDescriptor) 923 throws PartInitException { 924 925 if (defaultDescriptor != null) { 926 return defaultDescriptor; 927 } 928 929 IEditorDescriptor editorDesc = defaultDescriptor; 930 931 if (editorReg.isSystemInPlaceEditorAvailable(name)) { 933 editorDesc = editorReg 934 .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); 935 } 936 937 if (editorDesc == null 939 && editorReg.isSystemExternalEditorAvailable(name)) { 940 editorDesc = editorReg 941 .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); 942 } 943 944 if (editorDesc == null) { 946 editorDesc = editorReg 947 .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID); 948 } 949 950 if (editorDesc == null) { 952 throw new PartInitException( 953 IDEWorkbenchMessages.IDE_noFileEditorFound); 954 } 955 956 return editorDesc; 957 } 958 959 983 public static IEditorPart openEditor(IWorkbenchPage page, IMarker marker) 984 throws PartInitException { 985 return openEditor(page, marker, true); 986 } 987 988 1013 public static IEditorPart openEditor(IWorkbenchPage page, IMarker marker, 1014 boolean activate) throws PartInitException { 1015 if (page == null || marker == null) { 1017 throw new IllegalArgumentException (); 1018 } 1019 1020 if (!(marker.getResource() instanceof IFile)) { 1022 IDEWorkbenchPlugin 1023 .log("Open editor on marker failed; marker resource not an IFile"); return null; 1025 } 1026 IFile file = (IFile) marker.getResource(); 1027 1028 IEditorRegistry editorReg = PlatformUI.getWorkbench() 1030 .getEditorRegistry(); 1031 IEditorDescriptor editorDesc = null; 1032 try { 1033 String editorID = (String ) marker.getAttribute(EDITOR_ID_ATTR); 1034 if (editorID != null) { 1035 editorDesc = editorReg.findEditor(editorID); 1036 } 1037 } catch (CoreException e) { 1038 } 1040 1041 IEditorPart editor = null; 1043 if (editorDesc == null) { 1044 editor = openEditor(page, file, activate); 1045 } else { 1046 editor = page.openEditor(new FileEditorInput(file), editorDesc 1047 .getId(), activate); 1048 } 1049 1050 if (editor != null) { 1052 gotoMarker(editor, marker); 1053 } 1054 1055 return editor; 1056 } 1057 1058 1080 public static IEditorPart openEditorOnFileStore(IWorkbenchPage page, IFileStore fileStore) throws PartInitException { 1081 if (page == null) { 1083 throw new IllegalArgumentException (); 1084 } 1085 1086 IEditorInput input = getEditorInput(fileStore); 1087 String editorId = getEditorId(fileStore); 1088 1089 return page.openEditor(input, editorId); 1091 } 1092 1093 1103 private static String getEditorId(IFileStore fileStore) { 1104 IEditorDescriptor descriptor; 1105 try { 1106 descriptor = IDE.getEditorDescriptor(fileStore.getName()); 1107 } catch (PartInitException e) { 1108 return null; 1109 } 1110 if (descriptor != null) 1111 return descriptor.getId(); 1112 return null; 1113 } 1114 1115 1132 public static boolean saveAllEditors(final IResource[] resourceRoots, 1133 final boolean confirm) { 1134 1135 if (resourceRoots.length == 0) { 1136 return true; 1137 } 1138 1139 final boolean[] result = new boolean[] { true }; 1140 SafeRunner.run(new SafeRunnable(IDEWorkbenchMessages.ErrorOnSaveAll) { 1141 public void run() { 1142 IWorkbenchWindow w = PlatformUI.getWorkbench() 1143 .getActiveWorkbenchWindow(); 1144 if (w == null) { 1145 IWorkbenchWindow[] windows = PlatformUI.getWorkbench() 1146 .getWorkbenchWindows(); 1147 if (windows.length > 0) 1148 w = windows[0]; 1149 } 1150 if (w != null) { 1151 result[0] = PlatformUI.getWorkbench().saveAll(w, w, 1152 new SaveFilter(resourceRoots), confirm); 1153 } 1154 } 1155 }); 1156 return result[0]; 1157 } 1158 1159 1169 public static void setDefaultEditor(IFile file, String editorID) { 1170 try { 1171 file.setPersistentProperty(EDITOR_KEY, editorID); 1172 } catch (CoreException e) { 1173 } 1175 } 1176 1177 1194 public static IEditorDescriptor getDefaultEditor(IFile file) { 1195 return getDefaultEditor(file, true); 1196 } 1197 1198 1219 public static IEditorDescriptor getDefaultEditor(IFile file, 1220 boolean determineContentType) { 1221 IEditorRegistry editorReg = PlatformUI.getWorkbench() 1223 .getEditorRegistry(); 1224 try { 1225 String editorID = file.getPersistentProperty(EDITOR_KEY); 1226 if (editorID != null) { 1227 IEditorDescriptor desc = editorReg.findEditor(editorID); 1228 if (desc != null) { 1229 return desc; 1230 } 1231 } 1232 } catch (CoreException e) { 1233 } 1235 1236 IContentType contentType = null; 1237 if (determineContentType) { 1238 contentType = getContentType(file); 1239 } 1240 return editorReg.getDefaultEditor(file.getName(), contentType); 1242 } 1243 1244 1253 public static List computeSelectedResources( 1254 IStructuredSelection originalSelection) { 1255 List resources = null; 1256 for (Iterator e = originalSelection.iterator(); e.hasNext();) { 1257 Object next = e.next(); 1258 Object resource = null; 1259 if (next instanceof IResource) { 1260 resource = next; 1261 } else if (next instanceof IAdaptable) { 1262 resource = ((IAdaptable) next).getAdapter(IResource.class); 1263 } 1264 if (resource != null) { 1265 if (resources == null) { 1266 resources = new ArrayList (originalSelection.size()); 1269 } 1270 resources.add(resource); 1271 } 1272 } 1273 if (resources == null) { 1274 return Collections.EMPTY_LIST; 1275 } 1276 return resources; 1277 1278 } 1279 1280 1289 public static IContentType getContentType(IFile file) { 1290 try { 1291 UIStats.start(UIStats.CONTENT_TYPE_LOOKUP, file.getName()); 1292 IContentDescription contentDescription = file 1293 .getContentDescription(); 1294 if (contentDescription == null) { 1295 return null; 1296 } 1297 return contentDescription.getContentType(); 1298 } catch (CoreException e) { 1299 return null; 1300 } finally { 1301 UIStats.end(UIStats.CONTENT_TYPE_LOOKUP, file, file.getName()); 1302 } 1303 } 1304 1305 1314 public static IContentType guessContentType(IFile file) { 1315 String fileName = file.getName(); 1316 try { 1317 UIStats.start(UIStats.CONTENT_TYPE_LOOKUP, fileName); 1318 IContentTypeMatcher matcher = file.getProject() 1319 .getContentTypeMatcher(); 1320 return matcher.findContentTypeFor(fileName); 1321 } catch (CoreException e) { 1322 return null; 1323 } finally { 1324 UIStats.end(UIStats.CONTENT_TYPE_LOOKUP, file, fileName); 1325 } 1326 } 1327 1328 1352 public static boolean promptToConfirm(final Shell shell, 1353 final String title, String message, IResourceDelta delta, 1354 String [] ignoreModelProviderIds, boolean syncExec) { 1355 IStatus status = ResourceChangeValidator.getValidator().validateChange( 1356 delta, null); 1357 if (status.isOK()) { 1358 return true; 1359 } 1360 final IStatus displayStatus; 1361 if (status.isMultiStatus()) { 1362 List result = new ArrayList (); 1363 IStatus[] children = status.getChildren(); 1364 for (int i = 0; i < children.length; i++) { 1365 IStatus child = children[i]; 1366 if (!isIgnoredStatus(child, ignoreModelProviderIds)) { 1367 result.add(child); 1368 } 1369 } 1370 if (result.isEmpty()) { 1371 return true; 1372 } 1373 if (result.size() == 1) { 1374 displayStatus = (IStatus) result.get(0); 1375 } else { 1376 displayStatus = new MultiStatus(status.getPlugin(), status 1377 .getCode(), (IStatus[]) result 1378 .toArray(new IStatus[result.size()]), status 1379 .getMessage(), status.getException()); 1380 } 1381 } else { 1382 if (isIgnoredStatus(status, ignoreModelProviderIds)) { 1383 return true; 1384 } 1385 displayStatus = status; 1386 } 1387 1388 if (message == null) { 1389 message = IDEWorkbenchMessages.IDE_sideEffectWarning; 1390 } 1391 final String dialogMessage = NLS.bind( 1392 IDEWorkbenchMessages.IDE_areYouSure, message); 1393 1394 final boolean[] result = new boolean[] { false }; 1395 Runnable runnable = new Runnable () { 1396 public void run() { 1397 ErrorDialog dialog = new ErrorDialog(shell, title, 1398 dialogMessage, displayStatus, IStatus.ERROR 1399 | IStatus.WARNING | IStatus.INFO) { 1400 protected void createButtonsForButtonBar(Composite parent) { 1401 createButton(parent, IDialogConstants.YES_ID, 1402 IDialogConstants.YES_LABEL, false); 1403 createButton(parent, IDialogConstants.NO_ID, 1404 IDialogConstants.NO_LABEL, true); 1405 createDetailsButton(parent); 1406 } 1407 1408 1413 protected void buttonPressed(int id) { 1414 if (id == IDialogConstants.YES_ID) { 1415 super.buttonPressed(IDialogConstants.OK_ID); 1416 } else if (id == IDialogConstants.NO_ID) { 1417 super.buttonPressed(IDialogConstants.CANCEL_ID); 1418 } 1419 super.buttonPressed(id); 1420 } 1421 }; 1422 int code = dialog.open(); 1423 result[0] = code == 0; 1424 } 1425 }; 1426 if (syncExec) { 1427 shell.getDisplay().syncExec(runnable); 1428 } else { 1429 runnable.run(); 1430 } 1431 return result[0]; 1432 } 1433 1434 private static boolean isIgnoredStatus(IStatus status, 1435 String [] ignoreModelProviderIds) { 1436 if (ignoreModelProviderIds == null) { 1437 return false; 1438 } 1439 if (status instanceof ModelStatus) { 1440 ModelStatus ms = (ModelStatus) status; 1441 for (int i = 0; i < ignoreModelProviderIds.length; i++) { 1442 String id = ignoreModelProviderIds[i]; 1443 if (ms.getModelProviderId().equals(id)) { 1444 return true; 1445 } 1446 IModelProviderDescriptor desc = ModelProvider 1447 .getModelProviderDescriptor(id); 1448 String [] extended = desc.getExtendedModels(); 1449 if (isIgnoredStatus(status, extended)) { 1450 return true; 1451 } 1452 } 1453 } 1454 return false; 1455 } 1456} 1457 | Popular Tags |