lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2008 IBM Corporation and others. |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 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 |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.wst.xml.ui.internal.validation.core.errorinfo; |
| 12 | |
| 13 | import java.net.URL; |
| 14 | |
| 15 | import org.eclipse.core.resources.IFile; |
| 16 | import org.eclipse.core.resources.ResourcesPlugin; |
| 17 | import org.eclipse.core.runtime.IPath; |
| 18 | import org.eclipse.core.runtime.Path; |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 19 | import org.eclipse.core.runtime.content.IContentType; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 20 | import org.eclipse.jface.text.BadLocationException; |
| 21 | import org.eclipse.jface.text.IDocument; |
| 22 | import org.eclipse.swt.widgets.Display; |
| 23 | import org.eclipse.ui.IEditorDescriptor; |
| 24 | import org.eclipse.ui.IEditorPart; |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 25 | import org.eclipse.ui.IEditorRegistry; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 26 | import org.eclipse.ui.IWorkbench; |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 27 | import org.eclipse.ui.IWorkbenchPage; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 28 | import org.eclipse.ui.IWorkbenchWindow; |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 29 | import org.eclipse.ui.PlatformUI; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 30 | import org.eclipse.ui.editors.text.TextEditor; |
| 31 | import org.eclipse.ui.part.FileEditorInput; |
| 32 | import org.eclipse.ui.texteditor.IDocumentProvider; |
| 33 | import org.eclipse.wst.xml.core.internal.validation.core.logging.LoggerFactory; |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 34 | |
| 35 | |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 36 | public class ReferencedFileErrorUtility { |
| 37 | public static void openEditorAndGotoError(String uristring, final int line, final int column) { |
| 38 | if (uristring != null) { |
| 39 | try { |
| 40 | URL uri = new URL(uristring); |
| 41 | if (uri != null) { |
| 42 | if ("file".equals(uri.getProtocol())) //$NON-NLS-1$ |
| 43 | { |
| 44 | String pathString = uri.getPath(); |
| 45 | IPath path = new Path(pathString); |
| 46 | String device = path.getDevice(); |
| 47 | if ((device != null) && device.startsWith("/")) //$NON-NLS-1$ |
| 48 | { |
| 49 | path = path.setDevice(device.substring(1)); |
| 50 | } |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 51 | final IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); |
| 52 | if ((iFile != null) && iFile.exists()) { |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 53 | // Open the editor for this file. |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 54 | final IWorkbench workbench = PlatformUI.getWorkbench(); |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 55 | final IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 56 | |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 57 | Display.getDefault().asyncExec(new Runnable() { |
| 58 | public void run() { |
| 59 | try { |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 60 | IContentType contentType = iFile.getContentDescription().getContentType(); |
| 61 | IEditorRegistry editorRegistry = workbench.getEditorRegistry(); |
| 62 | String fileName = iFile.getName(); |
| 63 | IEditorDescriptor descriptor = editorRegistry.getDefaultEditor(fileName, contentType); |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 64 | String editorId; |
| 65 | if (descriptor != null) { |
| 66 | editorId = descriptor.getId(); |
| 67 | } |
| 68 | else { |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 69 | descriptor = editorRegistry.getDefaultEditor(fileName + ".txt"); //$NON-NLS-1$ |
| 70 | editorId = descriptor.getId(); |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 71 | } |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 72 | |
| 73 | if (editorId != null) |
| 74 | { |
| 75 | FileEditorInput editorInput = new FileEditorInput(iFile); |
| 76 | IWorkbenchPage activePage = workbenchWindow.getActivePage(); |
| 77 | activePage.openEditor(editorInput, editorId); |
| 78 | } |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 79 | } |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 80 | catch (Exception ex) { |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 81 | LoggerFactory.getLoggerInstance().logError("Exception encountered when attempting to open file: " + iFile + "\n\n", ex); //$NON-NLS-1$ //$NON-NLS-2$ |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | }); |
lmandel | 3c550af | 2005-06-16 05:46:02 +0000 | [diff] [blame] | 85 | |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 86 | Runnable runnable = new Runnable() { |
| 87 | public void run() { |
vbaciu | c3b34ee | 2008-05-14 12:29:48 +0000 | [diff] [blame] | 88 | IEditorPart editorPart = workbenchWindow.getActivePage().getActiveEditor(); |
david_williams | ef7420b | 2006-11-23 04:30:09 +0000 | [diff] [blame] | 89 | gotoError(editorPart, line, column); |
| 90 | } |
| 91 | }; |
| 92 | Display.getCurrent().asyncExec(runnable); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | catch (Exception e) { |
| 98 | // Do nothing. |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void gotoError(IEditorPart editorPart, int line, int column) { |
| 104 | if (editorPart != null) { |
| 105 | TextEditor textEditor = (TextEditor) editorPart.getAdapter(TextEditor.class); |
| 106 | if (textEditor != null) { |
| 107 | try { |
| 108 | IDocumentProvider dp = textEditor.getDocumentProvider(); |
| 109 | IDocument document = (dp != null) ? dp.getDocument(textEditor.getEditorInput()) : null; |
| 110 | textEditor.selectAndReveal(document.getLineOffset(line - 1) + column - 1, 0); |
| 111 | } |
| 112 | catch (BadLocationException x) { |
| 113 | // marker refers to invalid text position -> do nothing |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 118 | } |