Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Prompt user for an application to open a file with?
Prompt user for an application to open a file with? [message #837661] Fri, 06 April 2012 00:17 Go to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 6
Registered: December 2011
Junior Member
I want to open arbitrary files from Eclipse. Currently I'm doing it like that:

 if (((File) selectedElement).isFile()) {
        try {
    	        Desktop.getDesktop().open((File) selectedElement);
    	} catch (IOException e) {
    		//TODO prompt for the appropriate application to open this file.
    		e.printStackTrace();
    	}
    }


Unfortunately, this only works, if the OS has a default application associated with the file type. That's why, if there isn't any default application defined, I want to ask the user which application I should use. But I have no idea how to 1) find a list of available application and 2) open the file with that application. Any hints how to implement that in a platform-independent way?
Re: Prompt user for an application to open a file with? [message #838200 is a reply to message #837661] Fri, 06 April 2012 18:21 Go to previous messageGo to next message
Hamed Mohammadi is currently offline Hamed MohammadiFriend
Messages: 50
Registered: May 2010
Location: Shiraz - Iran
Member

I use instead
org.eclipse.swt.program.Program.launch(filename);


but I have same problem as you.

[Updated on: Fri, 06 April 2012 18:22]

Report message to a moderator

Re: Prompt user for an application to open a file with? [message #840499 is a reply to message #837661] Tue, 10 April 2012 06:49 Go to previous message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 2012-04-06 02:17, Missing name Mising name wrote:
> I want to open arbitrary files from Eclipse. Currently I'm doing it like
> that:
>
> if (((File) selectedElement).isFile()) {
> try {
> Desktop.getDesktop().open((File) selectedElement);
> } catch (IOException e) {
> //TODO prompt for the appropriate application to open this file.
> e.printStackTrace();
> }
> }
>
> Unfortunately, this only works, if the OS has a default application
> associated with the file type. That's why, if there isn't any default
> application defined, I want to ask the user which application I should
> use. But I have no idea how to 1) find a list of available application
> and 2) open the file with that application. Any hints how to implement
> that in a platform-independent way?

As described by Hamed Mohammadi, Program.launch could be used. On
Windows, the built-in functionality will also ask the user for a program
selection if no associated application is found. Further, this function
returns false on failure, so you could still open your own dialog, if
all fails.

If this approach is not suitable for you, you could use something like

Program program = Program.findProgram("/your_file_extension/");
if (program != null) {
if (!program.execute(fileName)) {
/your error handling/
}
}

This works, if a program can be started with a provided file name (or
other string-like argument like an URL) successfully.

HTH & Greetings from Bremen,

Daniel Krügler
Previous Topic:My buttons are not shown in JFrame Fullscreen
Next Topic:Error when closing program cause of running job
Goto Forum:
  


Current Time: Fri Apr 26 17:44:49 GMT 2024

Powered by FUDForum. Page generated in 0.03284 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top