diff options
author | Neil Guzman | 2013-09-04 15:38:19 +0000 |
---|---|---|
committer | Alexander Kurtakov | 2013-10-02 10:00:04 +0000 |
commit | 10d599fc5181dac1f71550d3993d17dfba1dd68a (patch) | |
tree | dfc04035e95f308a5a60277bd819729a06b2ef3f | |
parent | 218eebe82873cf3747454b8df3ae6a103e3b8bed (diff) | |
download | org.eclipse.linuxtools-10d599fc5181dac1f71550d3993d17dfba1dd68a.tar.gz org.eclipse.linuxtools-10d599fc5181dac1f71550d3993d17dfba1dd68a.tar.xz org.eclipse.linuxtools-10d599fc5181dac1f71550d3993d17dfba1dd68a.zip |
RPM: modify behaviour of hyperlinks B#416446
The options available when Ctrl + hovering on any kind
of valid hyperlink within a specfile editor are modified based
on whether or not the hyperlink is a valid URL or file name.
Valid URLs will be given the option to OPEN IN A BROWSER. Valid
URLs that are linking to a file will be given the option to DOWNLOAD
the file. If the file does not exist and is a patch, it will offer to
CREATE it. If the file exists, then it will offer to OPEN the file.
Non URLs will either OPEN the file if it exists, or CREATE the file
if it is a patch.
Also, "Unkown Hyperlink" showing up when hovering over a macro and valid
URL will now show a simple text saying what it will do.
Bug: 416446
Change-Id: I51021ca1163dded22a74874e9133ff3c1b4f1b60
Signed-off-by: Neil Guzman <nguzman@redhat.com>
Reviewed-on: https://git.eclipse.org/r/16119
Tested-by: Hudson CI
Reviewed-by: Alexander Kurtakov <akurtako@redhat.com>
IP-Clean: Alexander Kurtakov <akurtako@redhat.com>
Tested-by: Alexander Kurtakov <akurtako@redhat.com>
9 files changed, 163 insertions, 67 deletions
diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/hyperlink/SourcesFileHyperlinkDetectorTest.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/hyperlink/SourcesFileHyperlinkDetectorTest.java index c5af06c3e8..cf8a8c0ba9 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/hyperlink/SourcesFileHyperlinkDetectorTest.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor.tests/src/org/eclipse/linuxtools/rpm/ui/editor/tests/hyperlink/SourcesFileHyperlinkDetectorTest.java @@ -4,7 +4,7 @@ * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html - * + * * Contributors: * Red Hat - initial API and implementation *******************************************************************************/ @@ -31,7 +31,10 @@ public class SourcesFileHyperlinkDetectorTest extends FileTestCase { @Before public void init() throws CoreException { super.setUp(); - String testText = "Source0: test.zip\nPatch0: test.patch\n"; + String testText = "Source0: test.zip\n" + + "Patch0: test.patch\n" + + "Source1: www.example.com/test.zip\n" + + "Source2: http://www.example.com/test.zip\n"; newFile(testText); } @@ -45,20 +48,52 @@ public class SourcesFileHyperlinkDetectorTest extends FileTestCase { editor.doRevertToSaved(); SourcesFileHyperlinkDetector elementDetector = new SourcesFileHyperlinkDetector(); elementDetector.setEditor(editor); - // test source element + // test source0 element IRegion region = new Region(10, 0); IHyperlink[] returned = elementDetector.detectHyperlinks( editor.getSpecfileSourceViewer(), region, false); - assertEquals(2, returned.length); + // because test.zip does not exist, and is not a valid url + // it should not have hyperlinks + assertNull(returned); + } + + @Test + public void testDetectHyperlinksInvalidURL() throws PartInitException { + IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench() + .getActiveWorkbenchWindow().getActivePage(), testFile, + "org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor"); - // test empty - region = new Region(4, 0); - returned = elementDetector.detectHyperlinks( + editor = (SpecfileEditor) openEditor; + editor.doRevertToSaved(); + SourcesFileHyperlinkDetector elementDetector = new SourcesFileHyperlinkDetector(); + elementDetector.setEditor(editor); + // test source1 element + IRegion region = new Region(47, 0); + IHyperlink[] returned = elementDetector.detectHyperlinks( editor.getSpecfileSourceViewer(), region, false); + // because the protocol is missing, it should not show any hyperlinks assertNull(returned); } @Test + public void testDetectHyperlinksValidURL() throws PartInitException { + IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench() + .getActiveWorkbenchWindow().getActivePage(), testFile, + "org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor"); + + editor = (SpecfileEditor) openEditor; + editor.doRevertToSaved(); + SourcesFileHyperlinkDetector elementDetector = new SourcesFileHyperlinkDetector(); + elementDetector.setEditor(editor); + // test source2 element + IRegion region = new Region(82, 0); + IHyperlink[] returned = elementDetector.detectHyperlinks( + editor.getSpecfileSourceViewer(), region, false); + // 1 = Download from URL (Open in browser should not show up because URLHyperlinkWithMacroDetector detects that) + assertEquals(1, returned.length); + } + + @Test public void testDetectNoPatchInProject() throws PartInitException { IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage(), testFile, diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/Messages.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/Messages.java index dd0838ff96..3e0789f309 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/Messages.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/Messages.java @@ -4,7 +4,7 @@ * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html - * + * * Contributors: * Red Hat - initial API and implementation *******************************************************************************/ @@ -23,6 +23,7 @@ public final class Messages extends NLS { public static String SourcesFileHyperlink_0; public static String SourcesFileHyperlink_1; public static String SourcesFileHyperlink_2; + public static String SourcesFileHyperlink_3; static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileCreateHyperlink.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileCreateHyperlink.java index 2ccf5718e4..47a2d709b3 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileCreateHyperlink.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileCreateHyperlink.java @@ -23,6 +23,7 @@ import org.eclipse.core.runtime.Path; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.hyperlink.IHyperlink; import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog; +import org.eclipse.osgi.util.NLS; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; @@ -72,7 +73,7 @@ public class SourcesFileCreateHyperlink implements IHyperlink { */ @Override public String getHyperlinkText() { - return Messages.SourcesFileHyperlink_2 + ' ' + fileName; + return NLS.bind(Messages.SourcesFileHyperlink_2, fileName); } /** diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileDownloadHyperlink.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileDownloadHyperlink.java index 83b962358e..bf7cd3a64a 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileDownloadHyperlink.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileDownloadHyperlink.java @@ -62,7 +62,7 @@ public class SourcesFileDownloadHyperlink implements IHyperlink { */ @Override public String getHyperlinkText() { - return Messages.SourcesFileHyperlink_1 + ' ' + fileName; + return NLS.bind(Messages.SourcesFileHyperlink_1, fileName); } /** diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlink.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlink.java index 2b6ce9e03f..b627500678 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlink.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlink.java @@ -17,6 +17,7 @@ import org.eclipse.core.resources.IResource; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.hyperlink.IHyperlink; import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog; +import org.eclipse.osgi.util.NLS; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; @@ -62,7 +63,7 @@ public class SourcesFileHyperlink implements IHyperlink { */ @Override public String getHyperlinkText() { - return Messages.SourcesFileHyperlink_0 + ' ' + fileName; + return NLS.bind(Messages.SourcesFileHyperlink_0, fileName); } /** @@ -98,5 +99,4 @@ public class SourcesFileHyperlink implements IHyperlink { SpecfileLog.logError(e); } } - }
\ No newline at end of file diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlinkDetector.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlinkDetector.java index 152da2c84f..56fffccca6 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlinkDetector.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SourcesFileHyperlinkDetector.java @@ -7,15 +7,13 @@ * * Contributors: * Red Hat - initial API and implementation - * Neil Guzman - create patches hyperlink (B#413508) *******************************************************************************/ package org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink; -import org.eclipse.core.resources.IContainer; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.Path; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; @@ -25,6 +23,7 @@ import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; import org.eclipse.jface.text.hyperlink.IHyperlink; import org.eclipse.linuxtools.internal.rpm.ui.editor.UiUtils; import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.linuxtools.rpm.ui.editor.utils.RPMUtils; import org.eclipse.ui.part.FileEditorInput; /** @@ -36,6 +35,7 @@ public class SourcesFileHyperlinkDetector extends AbstractHyperlinkDetector { private SpecfileEditor editor; private static final String PATCH_IDENTIFIER = "Patch"; //$NON-NLS-1$ private static final String SOURCE_IDENTIFIER = "Source"; //$NON-NLS-1$ + private static final String URL_IDENTIFIER = "URL"; //$NON-NLS-1$ /** * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, @@ -44,7 +44,6 @@ public class SourcesFileHyperlinkDetector extends AbstractHyperlinkDetector { @Override public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { - boolean patchExists = false; if (region == null || textViewer == null) { return null; } @@ -71,67 +70,47 @@ public class SourcesFileHyperlinkDetector extends AbstractHyperlinkDetector { } catch (BadLocationException ex) { return null; } + List<IHyperlink> tempHList = new ArrayList<IHyperlink>(); + // !! it feels like there is duplicate code, fix that !! if (editor.getEditorInput() instanceof FileEditorInput) { IFile original = ((FileEditorInput) editor.getEditorInput()) .getFile(); if (line.startsWith(SOURCE_IDENTIFIER) - || line.startsWith(PATCH_IDENTIFIER)) { + || line.startsWith(PATCH_IDENTIFIER) + || line.startsWith(URL_IDENTIFIER)) { int delimiterIndex = line.indexOf(':') + 1; - String fileName = line.substring(delimiterIndex).trim(); - patchExists = patchExists(original, fileName); + String identifierValue = line.substring(delimiterIndex).trim(); + boolean validURL = RPMUtils.isValidUrl(identifierValue); + // if valid URL, get its file name; else make file name the original identifier value + String fileName = validURL ? RPMUtils.getURLFilename(identifierValue) : identifierValue; + String resolvedFileName = UiUtils.resolveDefines(editor.getSpecfile(), fileName); + boolean fileExists = RPMUtils.fileExistsInSources(original, resolvedFileName); if (region.getOffset() > lineInfo.getOffset() - + line.indexOf(fileName)) { + + line.indexOf(identifierValue)) { IRegion fileNameRegion = new Region(lineInfo.getOffset() - + line.indexOf(fileName), fileName.length()); - if (line.startsWith(PATCH_IDENTIFIER) && !patchExists) { - return new IHyperlink[] { - new SourcesFileCreateHyperlink(original, UiUtils - .resolveDefines(editor.getSpecfile(), - fileName), fileNameRegion) }; + + line.indexOf(identifierValue), identifierValue.length()); + if (fileExists) { + // add "Open" file option + tempHList.add(new SourcesFileHyperlink(original, resolvedFileName, fileNameRegion)); } else { - return new IHyperlink[] { - new SourcesFileHyperlink(original, UiUtils - .resolveDefines(editor.getSpecfile(), - fileName), fileNameRegion), - new SourcesFileDownloadHyperlink(original, UiUtils - .resolveDefines(editor.getSpecfile(), - fileName), fileNameRegion) }; + if (line.startsWith(PATCH_IDENTIFIER) && !identifierValue.endsWith("/")) { //$NON-NLS-1$ + // add "Create" patch option using filename + tempHList.add(new SourcesFileCreateHyperlink(original, resolvedFileName, fileNameRegion)); + } + } + // if valid URL and has a valid file + if (validURL && !identifierValue.endsWith("/")) { //$NON-NLS-1$ + // add "Download" option + tempHList.add(new SourcesFileDownloadHyperlink(original, UiUtils.resolveDefines(editor.getSpecfile(), identifierValue), fileNameRegion)); } } } } - return null; + return tempHList.isEmpty() ? null : tempHList.toArray(new IHyperlink[tempHList.size()]); } public void setEditor(SpecfileEditor editor) { this.editor = editor; } - - /** - * Helper method to check if the patch file exists within the - * current project. - * - * @return True if the file exists - */ - private boolean patchExists(IFile original, String patchName) { - boolean rc = true; - IContainer container = original.getParent(); - IResource resourceToOpen = container.findMember(patchName); - IFile file = null; - - if (resourceToOpen == null) { - IResource sourcesFolder = container.getProject().findMember( - "SOURCES"); //$NON-NLS-1$ - file = container.getFile(new Path(patchName)); - if (sourcesFolder != null) { - file = ((IFolder) sourcesFolder).getFile(new Path(patchName)); - } - if (!file.exists()) { - rc = false; - } - } - - return rc; - } } diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SpecfileElementHyperlink.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SpecfileElementHyperlink.java index 7471fa5441..fe920f6f10 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SpecfileElementHyperlink.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/SpecfileElementHyperlink.java @@ -19,6 +19,7 @@ import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.hyperlink.IHyperlink; import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog; import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileElement; +import org.eclipse.osgi.util.NLS; import org.eclipse.ui.IEditorDescriptor; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; @@ -47,7 +48,7 @@ public class SpecfileElementHyperlink implements IHyperlink { @Override public String getHyperlinkText() { - return null; + return NLS.bind(Messages.SourcesFileHyperlink_3, source.getName()); } @Override diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/messages.properties b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/messages.properties index c15aab737d..a82d475aab 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/messages.properties +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/hyperlink/messages.properties @@ -14,6 +14,7 @@ SourcesFileDownloadHyperlink_2=Remote resource doesn't exist SourcesFileDownloadHyperlink_3=Resource not found SourcesFileDownloadHyperlink_4=Downloading: {0} SourcesFileDownloadHyperlink_5=Create file -SourcesFileHyperlink_0=Open -SourcesFileHyperlink_1=Download -SourcesFileHyperlink_2=Create
\ No newline at end of file +SourcesFileHyperlink_0=Open {0} +SourcesFileHyperlink_1=Download {0} +SourcesFileHyperlink_2=Create {0} +SourcesFileHyperlink_3=Go to {0} diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/utils/RPMUtils.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/utils/RPMUtils.java index 8585f08fcd..e0b7c3efa9 100644 --- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/utils/RPMUtils.java +++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/utils/RPMUtils.java @@ -10,7 +10,16 @@ *******************************************************************************/ package org.eclipse.linuxtools.rpm.ui.editor.utils; +import java.net.MalformedURLException; +import java.net.URL; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.Path; import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; @@ -41,4 +50,73 @@ public class RPMUtils { } }); } + + /** + * Check if the line passed in is a valid URL. + * + * @param line The line to check if is a valid URL. + * @return True if valid URL, false otherwise. + */ + public static boolean isValidUrl(String line) { + try { + new URL(line); + return true; + } catch (MalformedURLException e) { + return false; + } + } + + /** + * Get the file from the URL if any. + * + * @param url The URL to get the file from. + * @return Return the filename. + */ + public static String getURLFilename(String url) { + String rc = ""; //$NON-NLS-1$ + + try { + // URL#getPath will ignore any queries after the filename + String fileName = new URL(url).getPath(); + int lastSegment = fileName.lastIndexOf("/") + 1; //$NON-NLS-1$ + rc = fileName.substring(lastSegment).trim(); + } catch (IndexOutOfBoundsException e) { + SpecfileLog.logError(e); + } catch (MalformedURLException e) { + SpecfileLog.logError(e); + } + + return rc; + } + + /** + * Check if the file exists within the current project. + * It will first check the root of the project and then the sources. If the + * file cannot be found in either, return false. + * An empty file name would immediately return false. + * + * @return True if the file exists. + */ + public static boolean fileExistsInSources(IFile original, String fileName) { + if (fileName.trim().isEmpty()) { + return false; + } + IContainer container = original.getParent(); + IResource resourceToOpen = container.findMember(fileName); + IFile file = null; + + if (resourceToOpen == null) { + IResource sourcesFolder = container.getProject().findMember( + "SOURCES"); //$NON-NLS-1$ + file = container.getFile(new Path(fileName)); + if (sourcesFolder != null) { + file = ((IFolder) sourcesFolder).getFile(new Path(fileName)); + } + if (!file.exists()) { + return false; + } + } + + return true; + } } |