Eclipse plugin and editor - java

How to open a file outside of the project programically in a new editor window ? I've been searching all over the internet and nothing seems to work.
I have eclipse 4.4.
I thought something so simple as opening a file will be easy but somehow it keeps me awake at night.
Please help.

I cannot comment as yet, so adding my question/suggestion as an answer.
What do you mean by your question? You mean something like a command to open a target file in some editor that is installed on your computer.
You could try using
Process process = Runtime.getRuntime ().exec ("<path to editor>/some_editor.exe");
If the program allows taking file to open as parameter, you could place that too in commmand above towards the end.
I hope this is what you are looking for.

To open an editor on a file which is not in the current workspace use:
String path = ... path to file
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(path));
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IDE.openInternalEditorOnFileStore(page, fileStore);
Note: some editors may not support files that are not in the workspace.

Related

Is it possible to load an external file/class in a programmatic into the eclipse editor that is located outside the work space?

I'm attempting to load an external class into the Eclipse editor that is not located inside of the local workspace. I should clarify that I am attempting to do this in an Eclipse plugin I am creating.
How this needs to work for me:
I have a double-click event on a view that I have created.
On that double-click, I am getting a database field from SQL that returns an absolute path. (e.g. C:\objects\sourceCode\class.java)
Once the absolute path has been received from the database, I need to open the file inside of an instance of the Eclipse editor.
NOTE: I did attempt to read all the lines of the class I am attempting to open and create a temporary file inside of the Eclipse workspace directory and open that. However, I cannot seem to get this working.
Therefore my question is:
Does anyone know if this is even possible? If it is, can you point me to an article about doing something similar to this or provide the location of an example that may help me in my endeavours?
First note that not all Eclipse editors support external files (the class file editor for example).
If the editor does support external files it does so by supporting IURIEditorInput as the editor input.
FileStoreEditorInput is the standard implementation of IURIEditorInput, use it like this:
File file = .... the file to open
IFileStore fileStore = EFS.getStore(file.getURI());
IEditorInput editorInput = new FileStoreEditorInput(fileStore);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.openEditor(editorInput, "id of editor to open");
As an alternative to the editor input and page.openEditor you can use the IDE.openInternalEditorOnFileStore method which will try and work out the id of the editor to open:
IDE.openInternalEditorOnFileStore(page, fileStore);
IDE is org.eclipse.ui.ide.IDE in the org.eclipse.ui.ide plug-in. EFS is org.eclipse.core.filesystem.EFS in the org.eclipse.core.filesystem plug-in.

How to get the icon of an application on mac? Java

I am trying to get the image of a file on mac, But i cant find any answers
There is some code that works on windows.
String s = "c:/windows/regedit.exe";
File file = new File(s);
sun.awt.shell.ShellFolder sf =
sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(sf.getIcon(true));
There's no clear-cut answer for this. You'll have to do some investigation yourself. If you have an Application in Finder, right-click and choose 'Show package contents'. In the Contents folder, search for the Info.plist file. You'll find an entry containing CFBundleIconFile like this (I took TextEdit as an example):
<key>CFBundleIconFile</key>
<string>Edit.icns</string>
Go inside the Resources folder, and there you will find the .icns file. You can find information on how to do that in this question.

System.getProperty("user.dir") with Eclipse IDE

I've been blasting through the Java Swing tutorials on zetcode.com
I've made it all the way to Basic Swing Components II, the JTextPane component.
In this example, an HTML document is loaded into memory and placed into the textpane.
In order to find the file, zetcode.com uses:
String cd = System.getProperty("user.dir") + "/";
textPane.setPage("File:///" + cd + "test.html");
My IDE of choice is Eclipse Kepler.
I have written the code for this example's class and created the HTML document exactly as zetcode.com has shown on the page. I have placed the HTML file in the same source folder and package as the class which uses it.
But when I run the code, I hear a Windows system error sound and the JFrame pops up without any text inside the textpane.
EDIT 01:
I've named the package "com/zetcode/swingtutorial/basiccomponents/".
I've tried using getClass.getResource("/com/zetcode/swingtutorial/basiccomponents/test.html")
and I figure I must have typed this correctly because I do not get an IOException.
EDIT 02:
Here's another interesting thing:
In zetcode's system, they've used "File:///", which caused Windows to play an error sound.
But when I tried "File://", no error sound plays. D'you think that was just a typo on their part?
Either way, my html doc still isn't displayed on the pane. :S
Do you know what I could be doing wrong?
Many thanks for your help!
Try this:
textPane.setPage(YourClass.class.getResource("test.html"));
If its in package
textPane.setPage(YourClass.class.getResource("/packagename/test.html"));
System.getProperty("user.dir");
Gives you the root context location or the project location folder or you can say the current directory when JVM was started
Accordingly give the path or alternately
Try loading this way
String pathOfHtmlFile = Thread.currentThread().getContextClassLoader()
.getResource("yourHtmlFile").getPath();
textPane.setPage(pathOfHtmlFile);
provided the file is in the classpath.
System.getProperty("user.dir")
Returns the "working folder" from which the application was launched. In Eclipse I believe the working directory is the top level project folder (not in src) or it's in the folder that contains the compiled .class files. Try copying the file to those folders and see when it works.

Changing java security file in windows

I am trying to edit the java.security file in windows. When I add an entry to it and try to save, it says 'Access Denied'. How do I change the permissions to this file.
I have also tried by making notepad to Run as administrator but it didn't work.
Please help.
Try this and say if it works. It worked for me while opening hosts file.
http://www.labnol.org/software/edit-hosts-files-as-administrator/13673/
edit:
The relevant information from the linked page:
Step 1. Open your Windows start menu, search for the notepad application and then right click the notepad icon.
Step 2. Choose “Run as administrator” and then, while inside notepad, browse to folder (java.home\lib\security\java.security).
You can now edit and save that file in the same folder without any issues. To recap, the trick is that instead of directly opening a protected file in the associated application, you run the application first as an administrator and then open the file inside it.
1) Copy original java.security file to desktop.
2) Edit the file there and save it.
3) Copy the file to %JAVA_HOME%/\jre\lib\security
4) Replace the original file with modified one.

Save a file with the option to open it

I am looking for a solution to save a file in a Java (Eclipse RCP) application with the option to open the file right after saving it. Similar is when a browser saves a dowloaded file it allowes to open it. A simple FileDialog has no such option.
I think, you need to open the file yourself after you save it.
Do the following (in windows):
String cmd = "cmd.exe /c start ";
String file = "path\version.txt"; //TODO: your file path here
Runtime.getRuntime().exec(cmd + file);
Similar approach can be applied for linux environment. Search for it yourself.
If you need the option (may be a checkbox) in JFileChooser, you can extend JFileChooser and add your features.

Categories