Skip to content

[NETBEANS-3193] Support remote files in Open Recent File menu #1543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.netbeans.modules.openfile.RecentFiles.HistoryItem;
import org.openide.awt.*;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.URLMapper;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.actions.Presenter;
Expand Down Expand Up @@ -224,8 +226,15 @@ public void actionPerformed(ActionEvent evt) {
msg = OFMSG_NO_RECENT_FILE;
}
}
if( null == msg )
msg = openFile(path);

try {
URL url = new URL(path);
if( null == msg ) msg = openFile(url);
} catch (MalformedURLException ex) {
msg = ""; //NOI18N
}


if (msg != null) {
StatusDisplayer.getDefault().setStatusText(msg);
Toolkit.getDefaultToolkit().beep();
Expand All @@ -235,19 +244,15 @@ public void actionPerformed(ActionEvent evt) {

/**
* Open a file.
* @param path the path to the file or {@code null}.
* @param url the path to the file.
* @return error message or {@code null} on success.
*/
private String openFile(String path) {
if(path == null || path.length() == 0) {
return OFMSG_PATH_IS_NOT_DEFINED;
}
File f = new File(path);
if (!f.exists()) {
private String openFile(URL url) {
FileObject fo = URLMapper.findFileObject(url);
if (fo == null || !fo.isValid()) {
return OFMSG_FILE_NOT_EXISTS;
}
File nf = FileUtil.normalizeFile(f);
return OpenFile.open(FileUtil.toFileObject(nf), -1);
return OpenFile.open(fo, -1);
}

/** Menu that checks its enabled state just before is populated */
Expand Down
29 changes: 18 additions & 11 deletions ide/utilities/src/org/netbeans/modules/openfile/RecentFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -42,7 +44,7 @@
import javax.swing.ImageIcon;
import org.netbeans.modules.openfile.RecentFiles.HistoryItem;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.modules.OnStop;
Expand Down Expand Up @@ -162,8 +164,7 @@ static List<HistoryItem> load() {
String value = _prefs.get(curKey, null);
if (value != null) {
try {
int id = new Integer(
curKey.substring(PROP_URL_PREFIX.length())).intValue();
int id = Integer.parseInt(curKey.substring(PROP_URL_PREFIX.length()));
HistoryItem hItem = new HistoryItem(id, value,
_prefs.getByteArray(PROP_ICON_PREFIX + id, null));
int ind = result.indexOf(hItem);
Expand Down Expand Up @@ -313,7 +314,7 @@ private static Icon findIconForPath(String path) {
}

private static String obtainPath(TopComponent tc) {
Object file = tc.getClientProperty( RECENT_FILE_KEY );
Object file = tc.getClientProperty( RECENT_FILE_KEY ); //TODO: possible dead code. RECENT_FILE_KEY not found in entire codebase
if( file instanceof File )
return ((File)file).getPath();
if( tc instanceof CloneableTopComponent ) {
Expand All @@ -338,14 +339,14 @@ private static HistoryItem findHistoryItem(String path) {
}

static String convertFile2Path(FileObject fo) {
File f = FileUtil.toFile(fo);
return f == null ? null : f.getPath();
return fo.toURL().toExternalForm();
}

static FileObject convertPath2File(String path) {
File f = new File(path);
f = FileUtil.normalizeFile(f);
return f == null ? null : FileUtil.toFileObject(f);
try {
return URLMapper.findFileObject(new URL(path));
} catch (MalformedURLException ex) {}
return null;
}

/** Checks recent files history and removes non-valid entries */
Expand Down Expand Up @@ -411,8 +412,14 @@ static void pruneHistory() {
Iterator<HistoryItem> it = history.iterator();
while (it.hasNext()) {
HistoryItem historyItem = it.next();
File f = new File(historyItem.getPath());
if (!f.exists()) {
boolean prune = false;
try {
FileObject fo = URLMapper.findFileObject(new URL(historyItem.getPath()));
if (fo == null || !fo.isValid()) prune = true;
} catch (MalformedURLException ex) {
prune = true;
}
if (prune) {
it.remove();
}
}
Expand Down