views:

28

answers:

1

Hello,

I want to display results of a file search. I want to enable a context menu for a file selection, which will be the system's context menu.

For example, if the user right-clicks a file in Windows - I want to display the popup-menu with the options:

  • Open
  • Open with...
  • Edit
  • Send to...
  • Copy
  • Cut
  • etc...

And, if possible - this menu will be generated automatically, depending on the operating system.

If that is not possible or too complex - I'd like to at least enable a "Locate on disk" option which will open a Windows Explorer (or its equivalent in other system) in the file's folder and select the file.

The application is written in Java (JDK 7) using SWT.

Thanks.

+1  A: 

Take a look at the example of how to use a popup menu:

Snippet131

Once you are in the handleEvent() method you can perform any logic you need in order to add menu items to your context menu.

In order to get platform specific behavior you can use System.getProperty() with a combination of the "os.name", "os.arch", and "os.version" strings in order to determine which platform you are running. Then just use if statements to conditionally add menu items to your menu.

rancidfishbreath
If I understand correctly, that is not a general enough solution. I don't know if it can be done, but I'd like to get the system's context menu, instead of creating it myself. It seems as if applications that have that feature use the system's actual menu somehow. Maybe they've just done a good work creating the menu themselves. I don't know.If that's not possible - how can I at least create a menu option "Locate on disk" which opens the system's file explorer GUI and selects the file?
shwartz
There is no such thing as the "system's context menu". The context menu is application specific. Even if you think of the context menu that pops up when you right click on your Windows Desktop it is still application - it just so happens the application is Windows Explorer. And you are right applications just create the menu themselves and the SWT menu is a native menu so it will look like those other applications. If you just want to open a file dialog then use FileDialog. An example can be found at: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/FileDialogExample.htm
rancidfishbreath
Since this context menu is available from both FileDialog dialogs and Windows Explorer (and probably other locations), I thought it might be some system object which is available. It's not a FileDialog I'm displaying, but search results - and I wanted to enable that menu. I'll settle on just implementing a "Open file location" for Windows systems (I found here how to do it).Thanks!
shwartz