Eclipse RCP - Launch editor from file menu - eclipse

What would be the best approach to launch an editor from the file menu(File>New). Should I encase the editor inside a view and launch the view from a command?

If you want an editor without a file, the best approach would be to go through the normal wizard creation stuff, but instead of creating a real file, use a virtual input instead. These might both be relevant to your interests: http://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_on_a_file_in_the_workspace%3F and http://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_programmatically%3F and http://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_on_something_that_is_not_a_file%3F. I did see something ages ago which was about creating totally virtual inputs. If I see it, I'll edit the post. Otherwise, it should be as simple as creating a subclass of org.eclipse.ui.IEditorInput and then not pointing it at any resource, but using it to create the editor.

Related

How to write an Eclipse component (editor, launcher, ...) that can execute my custom DSL files?

Let's say I have defined a small little textual DSL and I have an 'interpreter' that will parse and execute commands of a file in that DSL.
What is the best way to start such an interpreter in Eclipse?
For instance, I want to double click on a file and, based on the fact that it has the extension .mydsl, start the interpreter. Another option could be using a launcher with "Run As ...".
So should I look at implementing a ...
custom launcher (extending org.eclipse.debug.core.launchConfigurationTypes, http://www.vogella.com/tutorials/EclipseLauncherFramework/article.html), or
editor, that is not actually editing the file, but will be called when the file is double clicked (extending EditorPart, https://wiki.eclipse.org/FAQ_How_do_I_create_my_own_editor%3F)
something else?
Thanks for any hints!
For that I would recommend to implement a custom launcher to get for free:
in main toolbar Run button with history support
ability to share launch configuration via version control (in Common tab choose Shared file)
Launch Group support
etc.
In Project Explorer, Package Explorer, Navigator and similar views a double-click opens a file. In order not to confuse the user you should not change this behavior. But in addition to the custom launcher you could consider to implement an editor with a run button similar to PDE's Product Configuration Editor or a view similar to the Ant view that shows your DSL scripts that can be run on double-click.

How to change open with options in navigator depending upon name of the resource?

I am working on eclipse RCP application which implements CommonNavigator view to display navigator. I have few LinkedResources in navigator that link to files on the file system with custom extension. These custom extension files are opened in custom editor as well as in TextEditor.
One of the file named default.ext will be common to all the projects and I want to keep it read only. Is it possible to open file in custom editor only? For ex. Default.ext should be opened in only custom editor, however Test.ext should be opened in custom editor as well as text editor.
This way I could handle save action in my editor depending upon file name and keep the file read only.
Is there any other way to keep files read only?
Short answer: not possible in the way you describe.
Long answer: if somebody really wants to modify a file then there's no way or need to stop this. What you can do is either (1) hide the file from user or (2) set Read-only flag to discourage users from modifying the file.

How to add views to Show In menu for particular file types

I use an older plugin called Veloeclipse for editing Velocity templates in Eclipse. There's been no development on this since 2009, which isn't a problem because it's mainly just for syntax highlighting and format validation. The really annoying thing about it, however, is that when I try to do Show In to view the current Velocity template within my Package Explorer or Project Explorer, the only available option is Properties. That's not really useful. I really need to be able to get to the file in one of the regular explorer views.
So I have sort of two questions:
Is there a way to configure this without having to monkey with any code? A configuration file or something? I've grepped through my Eclipse installation and haven't seen anything, but I'm hoping that there's something I'm missing.
So assuming that the answer to my first question is no, how do I go about modifying the plugin code so that it will show more than the Properties view in the Show In menu? Most of what I found on the plugin development wiki comes from the other direction: how to make your view or perspective appear in the Show In menu.
Any help with this would be hugely appreciated!
Try to check the plugin source code. it might do something different than other editors. What I mean is that the show in menu item that you have there is not the usual extension point but a hard coded context menu option.

How to programmatically reload a text editor in Eclipse?

In Eclipse, if I change a file programmatically, and it is open in a text editor, it doesn't always reload, not even when refreshing the resource programmatically. How can I forcibly reload the text editor from code so that it show the changed file contents?
In your project explorer or navigator, you can right-click on the file that's currently open and select refresh. This has always worked for me, even when editing files with several programs. Make sure to click the file itself, not parent objects like packages or folders or projects.
Edit
Refreshing programmatically? I would look into an Eclipse scripting tool:
http://eclipse-shell.sourceforge.net/
I guess there was another one called Monkey, but it doesn't appear to be maintained.
I don't know of any possibility to programmatically reload the file.
Some editors (e.g. GMF editors) look for changes in the underlying files, and refresh themselves, but this is not required at all.
I don't think that a forced reload is an option implemented globally, as in some cases there could be some merging steps involved that can be quite erroneous.
My ideas to solve this:
Have a specific editor that refreshes its content when the used resource changes (this can be timeconsuming);
Or close the editors of the file and reopen them (this is ugly in the eye of the user).
Since the Luna release of eclipse there's no need to reload files with F5/manual Refresh.
Really nice, especially as there was a bug with the F5 key binding.

Bypassing Lock on Generated Code in NetBeans 6.5?

In NetBeans the GUI Builder generates code that is off limits to you from the editor.
I know you can switch to Design Mode and specify custom code and then enter edit in indirectly that way, but it's a pain to not be able to edit text that's 2 lines from your cursor.
Can I allow editing within those regions... I promise I'll be careful :)
Thanks.
Note: I don't want to lose code generation
If you open it in an external editor, there are two possibilities:
there is no guarantee that the changes you make will work (and not break anything) in the Netbeans UI Editor
The next time you edit the file in Netbeans it will be regenerated.
Either way, probably not what you want. The best thing to do would probably be to make the whole design up in the gui editor and when you are happy with it, delete the comments and continue on.
[sarcasm]
With the drag and drop gui editor, what could you possibly want to change by hand anyway?
[/sarcasm] ;)
If you open the .java files in Notepad or WordPad, you'll find pairs of comments that say
// GEN-BEGIN: <name of section>
...
// GEN-END: <name of section>
Delete those comments, and two things will happen.
You'll be able to edit anywhere in the file in NetBeans.
NetBeans will not be able to regenerate the file from the GUI editor.
So it can be done, but don't do it unless you really mean it.
EDIT: "Note: I don't want to lose code generation." Ok, let's see here.
You want to change what is generated.
You don't want to lose code generation.
You don't want to use the facilities NetBeans provides for adding custom code.
It sounds like you're wishing yourself trouble. :D
I had a same problem. I extended JPanel with my own panel class and I was not able to pack it with the rest of UI. First I searched the web and I found your question. I was not able to find elegant solution to this problem.
Therefore, I decided to create another java class and copy NetBeans created code into it. It worked like a charm.
I agree, it would be nice if we can modify existing code like in .NET.
I copy the initcomponents() method, create my own method newInitComponents() paste the generated code, and override what needs to be overridden before i distribute the code. That way when a change is made in the editor, you can still copy paste the code into your new method. You keep the functionality without costing you the ability to override where necessary. Just call newInitComponents() in your constructor rather than the original. This is not the most efficient way of doing things, but it does what you want.
You can also try moving the .form file in
(project name)\src\(name of JFrame/JPanel).form
NetBeans will now see it as a .java file.
If you did not edit anything necessary for the GUI editor, you can move the .form file back into the src folder.
Can you subclass the class and redefine the method where this code is? Of course then you'll have to maintain the method over time as you make changes to the GUI.
You can maybe also look into AspectJ. It will modify the byte-code at build time and make the changes you define. Not sure if this gives you enough control over what code to change.
If you use APT you can rewrite source code at compile time. This way you can overwrite the code that's there at the moment. You could keep the new code in the same Java file along with comments to explain what is going on. This tutorial makes it pretty easy to get started. Copy that code, get it working, then tweak it to your liking. To find out where in the code the annotated method is call getPosition() on the Declaration.
Go to design page and select any one of the fields. Right click on any selected field and choose Customizer Code , In Customizer Code change all default code to custom creation . Now you can edit your design code as you like...
Very simple... the crappy UTF-8 did not let me compile one JasperReport (everything done by GUI). So, I needed to change Project to iso-8859-1. Result? The project is messed up and some of the things to correct are in the blocked parts of the code. So, I really NEED to edit those automatically generated parts. When they are regenerated, they will be fine because the project is now iso-8859-1.
Step # 1 Close the project in NetBeans
Step # 2 Delete the .form file from the source directory of your project
Reopen the project and it will be editable now!