Skip to content

Commit

Permalink
issue #5: Multiple matches: Selected file in dialog will open wrong file
Browse files Browse the repository at this point in the history
  • Loading branch information
markiewb committed Mar 28, 2014
1 parent 6294f36 commit 63c9152
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.markiewb.netbeans.plugins</groupId>
<artifactId>open-file-at-cursor-plugin</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<packaging>nbm</packaging>

<properties>
Expand Down Expand Up @@ -164,6 +164,11 @@ Features:

&lt;img src="https://raw.github.com/markiewb/nb-resource-hyperlink-at-cursor/master/doc/screenshot-1.0.0.png"/&gt;

&lt;h2&gt;Updates in 1.1.1:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/5"&gt;Issue 5&lt;/a&gt;]: Fixed: Multiple matches: Selected file in dialog will open wrong file&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Updates in 1.1.0:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/1"&gt;Feature 1&lt;/a&gt;]: Support of partial matching (+ options dialog)&lt;/li&gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
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;
import java.util.Set;
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;
Expand Down Expand Up @@ -276,19 +278,29 @@ private FileObject getSingleMatchOrAskForUserChoice(Collection<FileObject> found
List<FileObject> indexedFilePaths = new ArrayList<FileObject>(foundMatches);

if (foundMatches.size() >= 2) {
List<String> collector = new ArrayList<String>();
List<FileObjectTuple> collector = new ArrayList<FileObjectTuple>();

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<FileObjectTuple>() {
@Override
public int compare(FileObjectTuple o1, FileObjectTuple o2) {
return o1.getSecond().compareToIgnoreCase(o2.getSecond());
}
});

DefaultComboBoxModel<FileObjectTuple> defaultComboBoxModel = new DefaultComboBoxModel<FileObjectTuple>();
for (FileObjectTuple item : collector) {
defaultComboBoxModel.addElement(item);
}

//TODO replace with floating listbox like "Open implementations"
final JComboBox<String> jList = new JComboBox<String>(collector.toArray(new String[collector.size()]));
final JComboBox<FileObjectTuple> jList = new JComboBox<FileObjectTuple>(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;
Expand Down Expand Up @@ -334,4 +346,36 @@ public String getTooltipText(Document doc, int offset, HyperlinkType type) {
return MessageFormat.format("<html>Open <b>{0}</b>{1,choice,0#|1#|1< ({1} matches)}", result.linkTarget, findMatches.size());
}

private static class FileObjectTuple extends Pair<FileObject, String> {

public FileObjectTuple(FileObject first, String second) {
super(first, second);
}

@Override
public String toString() {
return getSecond();
}
}

private static class Pair<First, Second> {

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;
}

}

}

0 comments on commit 63c9152

Please sign in to comment.