diff options
author | Milos Kleint | 2010-12-21 13:40:01 +0000 |
---|---|---|
committer | Milos Kleint | 2010-12-21 13:40:01 +0000 |
commit | 6d224823f7340a1de4160c5127f8f4b46af9c120 (patch) | |
tree | 41cc9ee72a009f1c5430db9d28e43dd6a9e79069 | |
parent | 4d5628f1043d6ddecfc7b5bbfbd34f30dd100aae (diff) | |
download | m2e-core-6d224823f7340a1de4160c5127f8f4b46af9c120.tar.gz m2e-core-6d224823f7340a1de4160c5127f8f4b46af9c120.tar.xz m2e-core-6d224823f7340a1de4160c5127f8f4b46af9c120.zip |
refactor openXmlEditor() method to open also files coming from local repository, additionally split it's functionality for reuse and further refactoring
-rw-r--r-- | org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomHyperlinkDetector.java | 119 |
1 files changed, 93 insertions, 26 deletions
diff --git a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomHyperlinkDetector.java b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomHyperlinkDetector.java index 70ff4c0e..cc0d5dc0 100644 --- a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomHyperlinkDetector.java +++ b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/PomHyperlinkDetector.java @@ -12,6 +12,10 @@ package org.eclipse.m2e.editor.xml; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -48,6 +52,7 @@ import org.eclipse.jface.text.hyperlink.IHyperlink; import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; @@ -61,6 +66,8 @@ import org.eclipse.wst.sse.ui.StructuredTextEditor; import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.actions.OpenPomAction; +import org.eclipse.m2e.core.actions.OpenPomAction.MavenPathStorageEditorInput; +import org.eclipse.m2e.core.core.MavenLogger; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.editor.xml.internal.Messages; import org.eclipse.m2e.editor.xml.internal.NodeOperation; @@ -246,7 +253,7 @@ public class PomHyperlinkDetector implements IHyperlinkDetector { File file = XmlUtils.fileForInputLocation(openLocation); if (file != null) { IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI()); - openXmlEditor(fileStore, openLocation.getLineNumber(), openLocation.getColumnNumber()); + openXmlEditor(fileStore, openLocation.getLineNumber(), openLocation.getColumnNumber(), openLocation.getSource().getModelId()); } } } @@ -369,7 +376,7 @@ public class PomHyperlinkDetector implements IHyperlinkDetector { File file = XmlUtils.fileForInputLocation(location); if (file != null) { IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI()); - openXmlEditor(fileStore, location.getLineNumber(), location.getColumnNumber()); + openXmlEditor(fileStore, location.getLineNumber(), location.getColumnNumber(), location.getSource().getModelId()); } } } @@ -474,9 +481,15 @@ public class PomHyperlinkDetector implements IHyperlinkDetector { if (versionString == null) { return Status.OK_STATUS; } - OpenPomAction.openEditor(gridString, + final IEditorPart page = OpenPomAction.openEditor(gridString, artidString, versionString, monitor); +// TODO: it's preferable to open the xml page, but this code will blink and open overview first and later switch. looks bad +// Display.getDefault().syncExec(new Runnable() { +// public void run() { +// selectEditorPage(page); +// } +// }); return Status.OK_STATUS; } }.schedule(); @@ -542,36 +555,31 @@ public class PomHyperlinkDetector implements IHyperlinkDetector { private void openXmlEditor(final IFileStore fileStore) { - openXmlEditor(fileStore, -1, -1); + openXmlEditor(fileStore, -1, -1, fileStore.getName()); } - private void openXmlEditor(final IFileStore fileStore, int line, int column) { + private void openXmlEditor(final IFileStore fileStore, int line, int column, String name) { + assert fileStore != null; IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if(window != null) { IWorkbenchPage page = window.getActivePage(); if(page != null) { try { - IEditorPart part = IDE.openEditorOnFileStore(page, fileStore); - if(part instanceof FormEditor) { - FormEditor ed = (FormEditor) part; - ed.setActivePage(null); //null means source, always or just in the case of MavenPomEditor? - if(line != -1) { - if(ed.getActiveEditor() instanceof StructuredTextEditor) { - StructuredTextEditor structured = (StructuredTextEditor) ed.getActiveEditor(); - // convert the line and Column numbers to an offset: - IDocument doc = structured.getTextViewer().getDocument(); - if (doc instanceof IStructuredDocument) { - IStructuredDocument document = (IStructuredDocument) doc; - try { - int offset = document.getLineOffset(line - 1); - structured.selectAndReveal(offset + column - 1, 0); - } catch(BadLocationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - } + if(fileStore.getName().equals("pom.xml")) { + IEditorPart part = IDE.openEditorOnFileStore(page, fileStore); + reveal(selectEditorPage(part), line, column); + } else { + //we need special EditorInput for stuff from repository + name = name + ".pom"; //$NON-NLS-1$ + File file = new File(fileStore.toURI()); + try { + IEditorInput input = new MavenPathStorageEditorInput(name, name, file.getAbsolutePath(), + readStream(new FileInputStream(file))); + IEditorPart part = OpenPomAction.openEditor(input, name); + reveal(selectEditorPage(part), line, column); + } catch(IOException e) { + MavenLogger.log("failed opening editor", e); + } } } catch(PartInitException e) { MessageDialog.openInformation( @@ -583,6 +591,65 @@ public class PomHyperlinkDetector implements IHyperlinkDetector { } } } + + private StructuredTextEditor selectEditorPage(IEditorPart part) { + if (part == null) { + return null; + } + if (part instanceof FormEditor) { + FormEditor ed = (FormEditor) part; + ed.setActivePage(null); //null means source, always or just in the case of MavenPomEditor? + if (ed.getActiveEditor() instanceof StructuredTextEditor) { + return (StructuredTextEditor) ed.getActiveEditor(); + } + } + return null; + } + + private void reveal(StructuredTextEditor structured, int line, int column) { + if (structured == null || line < 0 || column < 0) { + return; + } + IDocument doc = structured.getTextViewer().getDocument(); + if (doc instanceof IStructuredDocument) { + IStructuredDocument document = (IStructuredDocument) doc; + try { + int offset = document.getLineOffset(line - 1); + structured.selectAndReveal(offset + column - 1, 0); + } catch(BadLocationException e) { + MavenLogger.log("failed selecting part of editor", e); + } + } + } + + /** + * duplicate of OpenPomAction method + * @param is + * @return + * @throws IOException + */ + private static byte[] readStream(InputStream is) throws IOException { + byte[] b = new byte[is.available()]; + int len = 0; + while(true) { + int n = is.read(b, len, b.length - len); + if(n == -1) { + if(len < b.length) { + byte[] c = new byte[len]; + System.arraycopy(b, 0, c, 0, len); + b = c; + } + return b; + } + len += n; + if(len == b.length) { + byte[] c = new byte[b.length + 1000]; + System.arraycopy(b, 0, c, 0, len); + b = c; + } + } + } + static class ExpressionRegion implements IRegion { |