How do you set up new class templates in Eclipse? - eclipse

I mainly program Android, and one of the things I (and the rest of android programmers I'm sure) use with tenacity is android.util.Log. Every class I create has a
private static final String TAG = "ClassName";
tag that I write up as soon as I create the class, before I work on anything else. So, since I always create the tag I figured it would just save me on time (albeit a small amount) to just have it as part of the template for an empty class. My problem is, I don't know how or where to create code templates. Can anyone inform me on how to manage and create them?

What you're referring to are called "code templates" in Eclipse.
Go to Eclipse Preferences, search for "templates", you'll find them under Java -> Code Style -> Code Templates.

Related

Can eclipse connect Java editing to JSF files, and if yes, how to enable it?

In using Eclipse to edit a web application. When I edit a Java class, put the cursor into a method name and use “search for references” from the mouse menu, Eclipse shows me all other places in the Java code, where the method is used. When I rename the method in one Java class, the rename is executed all across the code in any other Java classes where the method is used.
However, in our web application, there are also HTML files in JSF-XML format (EL), and they can also use the Java code. Example:
// BaseForm.java
#Named("BaseForm")
#SessionScoped
public class BaseForm
public String getMessage() {
return "Lorem ipsum dolor sit amet";
}
}
<!-- baseForm.xhtml -->
<ui:composition ...>
<ui:define name="contentHeader">
<h3>#{BaseForm.message}</h3>
^^^^^^^ this will invoke getMessage()!
Eclipse does detects the connection between the Java and the HTML: If there is no BaseForm, or if BaseForm doesn’t have a getMessage(), it will show an error in the HTML editor.
I am not editing the HTML files normally, I am only working on the Java code. But, if I rename something, it maybe that the rename must be aligned with a change somewhere in the HTML. I often miss this, and it doesn’t get noticed until a user clicks on the page where the error is, and the application crashes. (So to say, there is no compile-time error.) My question is, is there any support for me in Eclipse when renaming, and if yes, how can I enable it?
My observed behaviour is, when I “search for references”, Eclipes gives me only the matches in Java, not in HTML (even though it knows something about the relationship of the two, as I wrote before.) If I rename getMessage(), for example to getFallbackMessage(), it will change all function calls in all Java classes, taking into account many kinds of dependencies such as inheritance, and not accidentally renaming an unrelated getMessage() function that belongs to another class. So this is more powerful than a text editor’s Edit/Replace in all files. However, Eclipse doesn’t change the HTML where the function is called.
I could imagine that, if the one part of this function is so elaborated, that there is also some support to port such renames to HTML, but it doesn’t yet. I wonder if that exists and if yes, how can I enable it.
My desired behaviour is, when I “search for references”, Eclipse shows me references in Java files and references in XHTML files. When I rename getMessage() to getFallbackMessage(), the HTML will be edited, and #{FormClass.message} will be changed to #{FormClass.fallbackMessage}.
So my question is composed in two parts:
does such function exist in Eclipse?
if yes, what may cause that it is disabled, and how maybe I can change it to be enabled?

Customizing content proposal in Xtext for web editors

I have a DSL written in Xtext. In order to add custom content proposal, I have edited the MyDslProposalProvider class in the ui project. The new proposals are present when I debug the plugin in Eclipse, but not in the web editor, which is ultimately what I want. I want to set the custom proposals at a single place and all generated editors to use them. Is it possible to do that with Xtext?
As I had the same problem and struggled a little bit with the solution (as I would like to use both, Eclipse editor and Web editor) I would like to provide some more detailed feedback on a possible solution here, which worked well for me.
In my solution I did the following steps.
Implement a MyDslIdeContentProposalProvider in my.dsl.ide sub project extending from IdeContentProposalProvider, package my.dsl.ide.contentassistant (newly created); an example of such an implementation may be found here.
The implementation is not such convenient as at the well known UI proposal provider for Eclipse. I had to implement switch cases based on MyDslGrammarAccess elements instead of structural grammar elements like in the UI proposal provider. On the other hand, I have only one proposal implementation for all editor cases that way (DRY principle!).
Register the MyDslIdeContentProposalProvider at MyDslIdeModule in the same project (only with that it works already in the web editor).
def Class<? extends IdeContentProposalProvider> bindIdeContentProposalProvider() {
MyDslIdeContentProposalProvider
}
Register the new MyDslIdeContentProposalProvider and the forwarding class UiToIdeContentProposalProvider at the MyDslUiModule in the my.dsl.ui sub project. (That's what took the longest investigation as it cannot be logically derived.)
override Class<? extends IContentProposalProvider> bindIContentProposalProvider() {
return UiToIdeContentProposalProvider
}
def Class<? extends IdeContentProposalProvider> bindIdeContentProposalProvider() {
return JavaPOSConfigLanguageIdeContentProposalProvider
}
For an full example see here.
As I had implemented the MyDslIdeContentProposalProvider in the newly created package my.dsl.ide.contentassistant, this package has to be exported in the MANIFEST.MF file of the sub project my.dsl.ide (the subsequent 2 exports were already there). Otherwise i would get an an error Access restriction: The type is not accessible due to restriction on required project in the MyDslUiModule.
Export-Package: my.dsl.ide.contentassist,
my.dsl.ide.contentassist.antlr,
my.dsl.ide.contentassist.antlr.internal
That way it worked well for for both editors, Eclipse and web.
Thanks Christian again for the initial hint!
... If I could make a wish, I would like to have the same structural grammar element access in the MyDslIdeContentProposalProvider as we have it today in UI proposal provider.
you need to subclass org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalProvider and bind it in YourDslIdeModule and YourDslUiModule. Then (in Xtext 2.13) you can use org.eclipse.xtext.ui.editor.contentassist.UiToIdeContentProposalProvider bound in YourDslUiModule to delegate to that in eclipse ui.

adding python interpreters programmatically

Is there a simple way to add and change interpreters using the Pydev plugin interface? I'm running pydev 1.6.1 and I'd like to be able to add and use a given interpreter based on a list of available interpreters in my environment.
Right now I can see the PythonInterpreterManager has a createInterpreterInfo call, but that doesn't seem to do anything. Looking at the source for pydev, it seems like I have to actually work with the preference pages to keep track of all of them.
Is there a simpler set of functions I can call to add these?
PythonInterpreterManager manager = (PythonInterpreterManager)PydevPlugin.getPythonInterpreterManager(true);
IInterpreterInfo info = manager.createInterpreterInfo(execPath, new NullProgressMonitor());
manager.addInterpreterInfo(info);
I can already do the above, but that only caches it, but doesn't display it as a valid interpreter option in the preferences.
I've even tried doing reflection to add these without much luck. I can call addNewInput on the editor as the Add button does, but then it says it doesn't have any knowledge of that interpreter. I've tried creating a popup preferences page and adding the values to the various members via reflection as getNewInput() would, but still don't see any more options in the preference page. I'm not sure if this is because I'm missing something or the popup preference page I make is totally unrelated to the page that pops up using the Window->"Preferences" pulldown.
The API is something as:
IInterpreterManager iMan = PydevPlugin.getPythonInterpreterManager(true);
IInterpreterInfo interpreterInfo = iMan.createInterpreterInfo("c:/python/python.exe", monitor, false);
iMan.setInfos(new IInterpreterInfo[]{interpreterInfo}, null, null);
Note that if you have 'manager.addInterpreterInfo' in there, you probably have an old version of PyDev... (and at that call you set all the interpreters available, so, if you want to keep some configuration, you should query it and add them back).
You can use: org.python.pydev.editor.codecompletion.revisited.javaintegration.AbstractWorkbenchTestCase.createPythonInterpreterManager(NullProgressMonitor) as a reference.

How do I get a view part instance in an Eclipse e4 application?

I'm trying to get the instance of a view part in an Eclipse e4 application but I can't find the PlatformUI class. Has the name changed since Eclipse 3 or is it located in a different package?
When looking at Eclipse e4 Parts:
bugs like 371405 can be instructive:
org.eclipse.ui.presentations
This API no longer works in 4.2, and we never intend to make it work.
It is incompatible with the pluggable rendering story in 4.2. Decisions that could once be made by the presentation extensions are now up to the renderer.
Affected API that needs deprecation:
Entire API package: org.eclipse.ui.presentations
Extension point:org.eclipse.ui.presentationFactories
org.eclipse.ui.IWorkbenchPreferenceConstants#PRESENTATION_FACTORY_ID
org.eclipse.ui.IWorkbenchWindowConfigurer#getPresentationFactory
org.eclipse.ui.IWorkbenchWindowConfigurer#setPresentationFactory
The rest of the Tutorial explains how to declare "parts" (editors or views)
The OP August Karlstrom mentions:
This used to work:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("‌​some view");
Using a singleton like PlatformUI is a bad practice and one of the reason of the introduction, in e4, of Context. See this presentation on Context.
Paul Webster (IBM Eclipse Platform team member) comments:
In Eclipse4 you would use org.eclipse.e4.ui.workbench.modeling.EPartService.findPart(String) to find an MPart by ID.
The MPart contains the injected part in its object property.
As the page Workbench_Services details:
In e4, the notion of a workbench page will not be present.
The part service API will essentially be a merge of the existing 3.x IPartService and WorkbenchPage interfaces.
Note that this isn't ideal, as bug 372488 illustrates (following this thread):
An MPart for an MPartDescriptor is created with EPartService.createPart(descriptor_id), where descriptor_id is the identifier of the MPartDescriptor.
This part can be found again with EPartService.findPart(descriptor_id) -- if there is only one.
The problem is, that one may need do create more than one MPart for one MPartDescriptor.
An editor may be one example: one may want to edit different instances of one and the same kind.
The creation of more than one MPart for a given MPartDescriptor is possible, but there is no convenient method to find these parts.
EPartService.findPart(descriptor_id) will return the first MPart created for a particular MPartDescriptor, even if there is more than one.
So there are three problems, for a given MPartDescriptor:
EPartService.findPart(id) does not tell that there is more than one MPart.
There is no convenient way to get all MParts for this descriptor.
There is no API-way to get the particular MPart for given descriptor and "content" or "reference".
Currently the way to go is using EPartService.getParts() which unfortunately
returns all MParts, not only those corresponding to one particular
MPartDescriptor.
Then one would need to check, whether there is one MPart for the particular MPartDescriptor having a particular "content".
So something is missing that will find an MPart for a given MPartDescriptor
with particular "content" or "reference".
Just have the same question. After found this thread and tried with:
MPart mPart = epartService.findPart("MyPart");
MyPart myPart = (MyPart)mPart.getObject();
then I got my view part.

Netbeans: using GUI Builder on regular Java class file

I'm using Netbeans. When I create a Java class, I sometimes want to change it to be a GUI component so that I can visually edit it using the GUI Builder.
What is the necessary step to transform a regular Java class to a GUI component so that Netbeans would recognize it and allow me to use GUI Builder ? (i.e. switch between Source and Design)
NetBeans' Gui builder, Matisse, works off a .form xml file located adjacent to the source file. Matisse creates and maintains the .form file and the code generator creates/updates methods in the java source to reflect changes to the form.
Unfortunately, there is no support in NetBeans for free-form GUI construction.
The closest I've seen is FormGenerator. It's a contributed NetBeans module that adds a right click action to .java files that will attempt to generate a .form file from the .java source. It's very limited, but it's better than nothing. It works best if you've followed the coding style employed by Matisse.
http://netbeans.org/projects/contrib/downloads/download/Readme.txt
http://netbeans.org/projects/contrib/downloads/download/FormGeneratorModule.zip
To add a class to the Palette, all that's needed is for your class to conform to the Java Beans model. That is, your class must:
be serializable
have a public, no-argument constructor.
All fields that have getter and setter methods that are named properly, i.e.:
int count
int getCount()
void setCount(int c)
should by default be recognized as a property.
For a finer control of what properties should and should not be exposed to the GUI Builder, you can associate your class with an implementation of the BeanInfo interface. See this Sun tutorial for more details.
However, NetBeans has several tools to help you in designing a custom bean. You can create new beans using the built-in templates available in the new file dialog, under the "JavaBeans Objects" folder.
This tutorial will guide you through creating an Image Bean.
What you could do is create one from scratch, design it as you wish, and then look at the generated code to understand how you can modify your existing class.
Try to use properties (Java bean!) for properties which should be changed from the ui designer and look here for more info.