blob: 378ee898673be828a2dd39d74273871f161125ce [file] [log] [blame]
lmandel3c550af2005-06-16 05:46:02 +00001/*******************************************************************************
vbaciuc3b34ee2008-05-14 12:29:48 +00002 * Copyright (c) 2001, 2008 IBM Corporation and others.
lmandel3c550af2005-06-16 05:46:02 +00003 * 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
amywuecebb042007-04-10 20:07:35 +00007 *
lmandel3c550af2005-06-16 05:46:02 +00008 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.wst.xml.ui.internal.validation.core.errorinfo;
12
13import java.net.URL;
14
15import org.eclipse.core.resources.IFile;
16import org.eclipse.core.resources.ResourcesPlugin;
17import org.eclipse.core.runtime.IPath;
18import org.eclipse.core.runtime.Path;
vbaciuc3b34ee2008-05-14 12:29:48 +000019import org.eclipse.core.runtime.content.IContentType;
lmandel3c550af2005-06-16 05:46:02 +000020import org.eclipse.jface.text.BadLocationException;
21import org.eclipse.jface.text.IDocument;
22import org.eclipse.swt.widgets.Display;
23import org.eclipse.ui.IEditorDescriptor;
24import org.eclipse.ui.IEditorPart;
vbaciuc3b34ee2008-05-14 12:29:48 +000025import org.eclipse.ui.IEditorRegistry;
lmandel3c550af2005-06-16 05:46:02 +000026import org.eclipse.ui.IWorkbench;
vbaciuc3b34ee2008-05-14 12:29:48 +000027import org.eclipse.ui.IWorkbenchPage;
lmandel3c550af2005-06-16 05:46:02 +000028import org.eclipse.ui.IWorkbenchWindow;
vbaciuc3b34ee2008-05-14 12:29:48 +000029import org.eclipse.ui.PlatformUI;
lmandel3c550af2005-06-16 05:46:02 +000030import org.eclipse.ui.editors.text.TextEditor;
31import org.eclipse.ui.part.FileEditorInput;
32import org.eclipse.ui.texteditor.IDocumentProvider;
33import org.eclipse.wst.xml.core.internal.validation.core.logging.LoggerFactory;
lmandel3c550af2005-06-16 05:46:02 +000034
35
david_williamsef7420b2006-11-23 04:30:09 +000036public 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 }
vbaciuc3b34ee2008-05-14 12:29:48 +000051 final IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
52 if ((iFile != null) && iFile.exists()) {
david_williamsef7420b2006-11-23 04:30:09 +000053 // Open the editor for this file.
vbaciuc3b34ee2008-05-14 12:29:48 +000054 final IWorkbench workbench = PlatformUI.getWorkbench();
david_williamsef7420b2006-11-23 04:30:09 +000055 final IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
lmandel3c550af2005-06-16 05:46:02 +000056
david_williamsef7420b2006-11-23 04:30:09 +000057 Display.getDefault().asyncExec(new Runnable() {
58 public void run() {
59 try {
vbaciuc3b34ee2008-05-14 12:29:48 +000060 IContentType contentType = iFile.getContentDescription().getContentType();
61 IEditorRegistry editorRegistry = workbench.getEditorRegistry();
62 String fileName = iFile.getName();
63 IEditorDescriptor descriptor = editorRegistry.getDefaultEditor(fileName, contentType);
david_williamsef7420b2006-11-23 04:30:09 +000064 String editorId;
65 if (descriptor != null) {
66 editorId = descriptor.getId();
67 }
68 else {
vbaciuc3b34ee2008-05-14 12:29:48 +000069 descriptor = editorRegistry.getDefaultEditor(fileName + ".txt"); //$NON-NLS-1$
70 editorId = descriptor.getId();
david_williamsef7420b2006-11-23 04:30:09 +000071 }
vbaciuc3b34ee2008-05-14 12:29:48 +000072
73 if (editorId != null)
74 {
75 FileEditorInput editorInput = new FileEditorInput(iFile);
76 IWorkbenchPage activePage = workbenchWindow.getActivePage();
77 activePage.openEditor(editorInput, editorId);
78 }
david_williamsef7420b2006-11-23 04:30:09 +000079 }
vbaciuc3b34ee2008-05-14 12:29:48 +000080 catch (Exception ex) {
david_williamsef7420b2006-11-23 04:30:09 +000081 LoggerFactory.getLoggerInstance().logError("Exception encountered when attempting to open file: " + iFile + "\n\n", ex); //$NON-NLS-1$ //$NON-NLS-2$
david_williamsef7420b2006-11-23 04:30:09 +000082 }
83 }
84 });
lmandel3c550af2005-06-16 05:46:02 +000085
david_williamsef7420b2006-11-23 04:30:09 +000086 Runnable runnable = new Runnable() {
87 public void run() {
vbaciuc3b34ee2008-05-14 12:29:48 +000088 IEditorPart editorPart = workbenchWindow.getActivePage().getActiveEditor();
david_williamsef7420b2006-11-23 04:30:09 +000089 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 }
amywuecebb042007-04-10 20:07:35 +0000118}