From 63c91528fffc3cfa9ff77ce72db215da2b5f806b Mon Sep 17 00:00:00 2001 From: markiewb Date: Fri, 28 Mar 2014 21:13:48 +0100 Subject: [PATCH] issue #5: Multiple matches: Selected file in dialog will open wrong file --- pom.xml | 7 ++- .../ResourceHyperlinkProvider.java | 54 +++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 07b0e94..36c0a08 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ de.markiewb.netbeans.plugins open-file-at-cursor-plugin - 1.1.0 + 1.1.1 nbm @@ -164,6 +164,11 @@ Features: <img src="https://raw.github.com/markiewb/nb-resource-hyperlink-at-cursor/master/doc/screenshot-1.0.0.png"/> +<h2>Updates in 1.1.1:</h2> +<ul> +<li>[<a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/5">Issue 5</a>]: Fixed: Multiple matches: Selected file in dialog will open wrong file</li> +</ul> + <h2>Updates in 1.1.0:</h2> <ul> <li>[<a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/1">Feature 1</a>]: Support of partial matching (+ options dialog)</li> diff --git a/src/main/java/de/markiewb/netbeans/plugins/resourcehyperlink/ResourceHyperlinkProvider.java b/src/main/java/de/markiewb/netbeans/plugins/resourcehyperlink/ResourceHyperlinkProvider.java index fad50b7..0b43e32 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/resourcehyperlink/ResourceHyperlinkProvider.java +++ b/src/main/java/de/markiewb/netbeans/plugins/resourcehyperlink/ResourceHyperlinkProvider.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.EnumSet; import java.util.HashSet; import java.util.List; @@ -29,6 +30,7 @@ import java.util.prefs.PreferenceChangeEvent; import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; +import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.text.BadLocationException; import javax.swing.text.Document; @@ -276,19 +278,29 @@ private FileObject getSingleMatchOrAskForUserChoice(Collection found List indexedFilePaths = new ArrayList(foundMatches); if (foundMatches.size() >= 2) { - List collector = new ArrayList(); + List collector = new ArrayList(); for (FileObject fileObject : indexedFilePaths) { //convert absolute path to relative regarding the project String path1 = fileObject.getPath().substring(project.getProjectDirectory().getPath().length()); - collector.add(path1); + collector.add(new FileObjectTuple(fileObject, path1)); } - Collections.sort(collector); + Collections.sort(collector, new Comparator() { + @Override + public int compare(FileObjectTuple o1, FileObjectTuple o2) { + return o1.getSecond().compareToIgnoreCase(o2.getSecond()); + } + }); + DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(); + for (FileObjectTuple item : collector) { + defaultComboBoxModel.addElement(item); + } + //TODO replace with floating listbox like "Open implementations" - final JComboBox jList = new JComboBox(collector.toArray(new String[collector.size()])); + final JComboBox jList = new JComboBox(defaultComboBoxModel); if (DialogDisplayer.getDefault().notify(new DialogDescriptor(jList, "Multiple files found. Please choose:")) == NotifyDescriptor.OK_OPTION) { - return indexedFilePaths.get(jList.getSelectedIndex()); + return defaultComboBoxModel.getElementAt(jList.getSelectedIndex()).getFirst(); } } return null; @@ -334,4 +346,36 @@ public String getTooltipText(Document doc, int offset, HyperlinkType type) { return MessageFormat.format("Open {0}{1,choice,0#|1#|1< ({1} matches)}", result.linkTarget, findMatches.size()); } + private static class FileObjectTuple extends Pair { + + public FileObjectTuple(FileObject first, String second) { + super(first, second); + } + + @Override + public String toString() { + return getSecond(); + } + } + + private static class Pair { + + private final First first; + private final Second second; + + private Pair(final First first, final Second second) { + this.first = first; + this.second = second; + } + + public First getFirst() { + return first; + } + + public Second getSecond() { + return second; + } + + } + }