Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting remote files

Status
Not open for further replies.

Madere

Technical User
Apr 14, 2003
85
GB
Hallo all,

I have a java application that, at some stage, select files from the local filesystem using JFileChooser.showOpenDialog().

Now I want it in a client-server setup. This means opening a GUI for selecting files on the server.
For client-server I use RMI.

I have already tried several scenarios to keep using the JFileChooser, but I cant get the correct setup working.

I am able to get the remote filelist using File.listFiles() bu then it returns a File[] (or as I did an ArrayList, but that is not important). But using the File.listFiles() on the remote side, I do not see any possiblity to keep using the JFileChooser.

In fact i do not want to create the whole file select window myself as JFileChooser is doing.

Does anyone has a solution to select remote files on a client, using RMI, where it looks like that JFileChooser is selecting files locally.

Thanks in advange.
 
sedj,

I was not my intention to offend some1 here. And as I said ealier: I appreciate all help and effort.

But my experience with these forums is that in a lot of cases people reply but do not really know the solution. So their reply is not really helping. I am not saying your or Tims replies are not helping, but I have the feeling.

Besides that I can not know your knowledge and experience about Java etc. So I have to judge that what you write helps me or not.

And besides that I wrote that I still might be doing something wrong or do not understand all of RMI.

If you had to feeling to be offended: sorry about that, it was not my intention.
 
Look we try to help on these forums. We don't have cut-and-paste solutions for every poster with a problem. We use our years of experience to know what is likely to work and what is not. Even if someone out there DID have the exact solution you're after, the spirit of these forums is NOT to give it to you on a plate anyway. The site is called 'Tek-TIPS', not 'Code-u-mart'. There are folks who'll just give you the whole code for no effort on your part, but they're a pain and you learn nothing.

Right. I've taken your problem, along with sedj's and my suggestions, home and tried some things out which give me some confidence that they have some merit. I've took time out to convinced myself that this approach will work.

I've done LOTS of Swing and done remoting using Sockets, RMI, RMI over IIOP and SOAP. I think the solution revolves around wrapping and sub-classing FileSystemView on the Server and Client respectively with RMI to join them together, stub-and-skeleton style. The JFileChooser is a GUI interface - it uses a FileSystemView to know EVERYTHING it needs about the underlying file store. You make a remote version of FileSystemView and JFileChooser will navigate it. Period. Rewriting your own JFileChooser will just add lots of Swing work ON TOP of still having to do the remoting side as well. This is NOT the way to go.

Now, post your code so we can help you in the spirit of these forums and we'll try to get you sorted.

Tim
 
I completely concur with what Tim says - because of the way JFileChooser works - as long as you proxy all required information through the RMI server, then I'm sure it will work.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Well gents,

as I see it now:
when the directory in the "Look in" window does not exist, which is logical because that is a server sided directory, then JFileChooser takes "My Documents"(client sided) as directory. Besides that my FileFilter is ignored.

The getting and listing of remote files works now with File[] getFiles() in the stub.

The problem is now the directory browsing (as I stated before).

As as written before: this whole process is implemented in more files. So it will be very difficult to extract only the lines needed for this JFileChooser. Or I must created a new test project only for this purpose.
 
Have you implemented *every single* method in the view ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
The JFileChooser only takes 'My Documents' as the default because you obviously haven;t overridden the getDefaultDirectory() method of FileSystemView.

You need to override EVERY SINGLE METHOD in your FileSystemView subclass and make them delegate to the remote implementation on the server to get this to work AT ALL.

And you STILL haven't posted any code to let us know if you're understanding our advice. Post the code. Please, please, please post the code. Post the code, please.

Tim
 
Oh, or enable FTP on the server and use its commands to retrieve the information.

Ah, and don't get this people angry, they're scary when they get offended :)

Cheers,
Dian
 
I assumed Madere had already discounted these approaches (sorry if that assumption was wrong).

And to be fair, I think this is an interesting exercise, and I'm interested in the result. I want to help, but the OP is not making it easy.

Tim
 
timw,

Java 1.3:

There is no getDefaultDirectory() in FileSystemView.
There is a getHomeDirectory() which I overrided and giving back the directory on the server. But it looks like that because that directory does not exist localy, JFileChooser falls back to his defaults.

Here the code from a new created test project.

public class Client
{
private MyFileFilter myFilter = new RunFileFilter();


public void doFileChooser()
{
ClientFileChooser fc = new ClientFileChooser();

fc.setFileSystemView(MyFileSystemView.getSuiteFileSystemView());
fc.setFileFilter(myFilter);

System.out.println("Trying to set Current Dir to = " + Bridge.getRunFileBaseDir());
fc.setCurrentDirectory(new File(Bridge.getRunFileBaseDir()));

int returnVal = fc.showOpenDialog(null);

}

public static void main(String[] args)
{
Client client = new Client();
client.doFileChooser();
}
}

public class ClientFileChooser extends JFileChooser
{

/**
*
*/
private static final long serialVersionUID = 1L;

public void setCurrentDirectory(File dir)
{
if(null != dir)
System.out.println("ClientFileChooser.setCurrentDirectory = " + dir.getPath());
else
System.out.println("ClientFileChooser.setCurrentDirectory.");
super.setCurrentDirectory(new File(Bridge.getRunFileBaseDir()));
}

}

public class MyFileSystemView extends FileSystemView implements Serializable
{
private static MyFileSystemView mySfsv = null;

/**
*
*/
private static final long serialVersionUID = 1L;

public File createNewFolder(File containingDir) throws IOException
{
// TODO Auto-generated method stub
return null;
}

private MyFileSystemView()
{
System.out.println("Creating SuiteFileSystemView.");
}

public static MyFileSystemView getSuiteFileSystemView()
{
System.out.println("getSuiteFileSystemView.");
if(null == mySfsv)
{
mySfsv = new MyFileSystemView();
}
return mySfsv;
}

public File[] getFiles(File dir, boolean useFileHiding)
{

System.out.println("Getting SuiteFileSystemView File list.");
return Bridge.getFiles(dir);
}

public File getHomeDirectory()
{
System.out.println("SuiteSystemFileView.getHomeDirectory");
System.out.println("HomeDir = " + Bridge.getRunFileBaseDir());
return new File(Bridge.getRunFileBaseDir());
}
}


public class Bridge
{

private final static String myServerService = "TestFileChooser";
private static IServer remoteObj = null;

private static IServer getRemoteObj()
{
if(null == remoteObj)
{
String myHost = "";
int myPort = 0;
myHost = "10.200.114.6";
myPort = 1341;

String url = "rmi://" + myHost + ":" + myPort + "/" + myServerService;

System.out.println("URL = [" + url + "]");

try
{
remoteObj = (IServer)Naming.lookup(url);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NotBoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return remoteObj;
}


public static String getRunFileBaseDir()
{
String baseDir = "";
try
{
baseDir = getRemoteObj().getRunFileBaseDir();
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Returning RunFileBaseDir = " + baseDir);
return baseDir;
}

public static File[] getFiles(File aPath)
{
File[] list = null;
try
{
list = getRemoteObj().getFiles("");
}
catch (RemoteException e)
{
e.printStackTrace();
}
return list;
}

}

public class Server extends UnicastRemoteObject implements IServer, Serializable
{

private String myHost = "10.200.114.6";
private int myPort = 1341;
private final static String myServerService = "TestFileChooser";

private final static String myRunFileBaseDir = "C:\\eGate\\data\\SuiteServer\\Test\\run\\TestParseCreate";

private static final long serialVersionUID = 1L;

/**
*
* @throws RemoteException
*/
public Server() throws RemoteException
{
super();
}

public void startServer()
{
System.out.println("Entered StartServer.");
String url = "rmi://" + myHost + ":" + myPort + "/" + myServerService;
System.out.println("URL = [" + url + "]");
try
{
LocateRegistry.createRegistry(myPort);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
System.out.println("Exption by Creation of RMI Registry.");
}
try
{
LocateRegistry.getRegistry(myPort);
}
catch (RemoteException e)
{
System.out.println("Exption by Getting of RMI Registry.");
}

try
{
Naming.bind(url, this);

System.out.println("Bind done.");

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (AlreadyBoundException e) {
e.printStackTrace();
}
System.out.println("Exit StartServer.");
}

public String getPathSeparator() throws RemoteException
{
return File.separator;
}

public static void main(String[] args)
{
try
{
Server server = new Server();
server.startServer();
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public String getRunFileBaseDir() throws RemoteException
{
return myRunFileBaseDir;
}


public File[] getFiles(String aPath) throws RemoteException
{
System.out.println("Getting Remote File List.");
File file = new File(myRunFileBaseDir);
return file.listFiles();
}

}
 
Diancecht,

I dont wanna use FTP or mapping a network drive.

The client will run on windows, the server on UNIX (or might be any other OS).
I dont wanna install/active a FTP server on the UNIX only for this purpose.


Timw,

I am using a Bridge class because I wanna be able to select files from the client or from the server. This depends on selecting the host at startup (not implemented in this test).
 
Not in Java 1.3


Are you sure this is that way to go? Did you consider other sollutions? Browsing files in a server is something that has been already implemented in a lot of ways.

tim, for sure is a good exercise, but I still think is reinventing the wheel.

Cheers,
Dian
 
So you're not overriding all the methods in FileSystemView, then, are you. When browsing the local file store, use the system default implementation of FileSystemView with JFileChooser. When browsing the remote file store, use your stubbed implementation which overrides ALL methods and delagates across the wire for each one.

Tim
 
You may also need to extend java.io.File, and re-implement some of the methods in that (ie the exists() method etc).

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Guys,

No FTP client/server setup.

I debugged JFileChooser.setCurrentDirectory().

He checks if the new directory (server directory) exists locally. Which is not the case of course. So he sets it to "My Documents".

I believe I have to write completely my onw JFileChooser with building up the GUI.
 
Does it create a new File, or ask the FileSystemView if the directory exists, or perform an exists() call on one of the file objects you returned from your view ? If either the latter two, then extending java.io.File will fix the error.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Here the code of the default JFileChooser.setCurrentDirectory()

public void setCurrentDirectory(File dir) {
File oldValue = currentDirectory;

if (dir != null && !dir.exists()) {
dir = currentDirectory;
}
if (dir == null) {
dir = getFileSystemView().getDefaultDirectory();
}
if (currentDirectory != null) {
/* Verify the toString of object */
if (this.currentDirectory.equals(dir)) {
return;
}
}

File prev = null;
while (!isTraversable(dir) && prev != dir) {
prev = dir;
dir = getFileSystemView().getParentDirectory(dir);
}
currentDirectory = dir;

firePropertyChange(DIRECTORY_CHANGED_PROPERTY, oldValue, currentDirectory);
}


When dir does not exist, currentDirectory is taken which is "My Documents".

When I leave dir == null, the server directory is taken (using FileSystemView.getDefaultDirectory()), but after the while loop, currentDirectory = null. That causes other problems.
 
Btw, what are you planning to do after exploring the server, retrieve the files?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top