Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Custom intelligent java template
Custom intelligent java template [message #664750] Mon, 11 April 2011 20:14 Go to next message
Aleck Agakhan is currently offline Aleck AgakhanFriend
Messages: 4
Registered: April 2011
Junior Member
I decided to make a template for generation getter and setter wrappers for the field being typed. For instance:
private int myprop;
public int getMyprop()
{
	return myprop;
}
public void setMyprop(int myprop)
{
	this.myprop=myprop;
}

That is I need to create TemplateVariableResolver which intercepts field name while user is typing it and generates getter and setter names. The resolver must capitalize first letter of the field name and prepend it with prefix, for getter - "get"/"is" (it depends on the field type) and "set" for setter.
I delved into the sources of org.eclipse.pdi.ui plugin and discovered that the class ElementTypeResolver resorts to use of JavaContext.addDependency() and JavaContext.getTemplateVariable() where the former is restricted to the project org.eclipse.pdi.ui and the latter isn't public at all.
Could somebody give me a tip about the implementation of the idea.
Re: Custom intelligent java template [message #664792 is a reply to message #664750] Tue, 12 April 2011 03:17 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 4/12/2011 1:44 AM, Aleck Agakhan wrote:
> I decided to make a template for generation getter and setter wrappers
> for the field being typed. For instance:
>
> private int myprop;
> public int getMyprop()
> {
> return myprop;
> }
> public void setMyprop(int myprop)
> {
> this.myprop=myprop;
> }
>
> That is I need to create TemplateVariableResolver which intercepts field
> name while user is typing it and generates getter and setter names. The
> resolver must capitalize first letter of the field name and prepend it
> with prefix, for getter - "get"/"is" (it depends on the field type) and
> "set" for setter.
> I delved into the sources of org.eclipse.pdi.ui plugin and discovered
> that the class ElementTypeResolver resorts to use of
> JavaContext.addDependency() and JavaContext.getTemplateVariable() where
> the former is restricted to the project org.eclipse.pdi.ui and the
> latter isn't public at all.
> Could somebody give me a tip about the implementation of the idea.

Is there a reason why you cannot use the quick assist to create getter
and setter ? (Type the field, and with cursor in the field name press
Ctrl+1, select 'Create getter and setter' quick assist)
Re: Custom intelligent java template [message #665615 is a reply to message #664792] Fri, 15 April 2011 09:31 Go to previous messageGo to next message
Aleck Agakhan is currently offline Aleck AgakhanFriend
Messages: 4
Registered: April 2011
Junior Member
I didn't know about the quick assist. If I had known I'd choose another example to describe the abilities which I need from eclipse templates. I'm interested in the approach of how to make templates with the functionality like that. Thanks Deepak Azad for advice, nevertheless the question is more general and still open.
By the way, can I somehow configure the quick assist?
Re: Custom intelligent java template [message #665705 is a reply to message #665615] Fri, 15 April 2011 14:38 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 4/15/2011 3:01 PM, Aleck Agakhan wrote:
> I didn't know about the quick assist. If I had known I'd choose another
> example to describe the abilities which I need from eclipse templates.
> I'm interested in the approach of how to make templates with the
> functionality like that. Thanks Deepak Azad for advice, nevertheless the
> question is more general and still open.

In the original question you said 'That is I need to create
TemplateVariableResolver which intercepts field name while user is
typing it'. Is 'while user is typing it' part important?

It would help if you can describe another example of what you want to
achieve.

Also you had mentioned 'org.eclipse.pdi.ui' - I guess you wanted to say
'org.eclipse.jdt.ui', no?

> By the way, can I somehow configure the quick assist?
You can configure field prefixes - Java > Code Style.

btw for the future, questions on JDT should go in JDT forum. Though we
can continue this thread here itself.
Re: Custom intelligent java template [message #665816 is a reply to message #665705] Sat, 16 April 2011 12:29 Go to previous messageGo to next message
Aleck Agakhan is currently offline Aleck AgakhanFriend
Messages: 4
Registered: April 2011
Junior Member
You're right, I wanted to say org.eclipse.jdt.ui.

Take a look at this template

private int ${myprop};
public int get${myprop}()
{
	return ${myprop};
}
public void set${myprop}(int ${myprop})
{
	this.${myprop}=${myprop};
}


When someone is typing the value for ${myprop} the entries of this var are changing within the entire template. The question is: Is it possible to create a sort of TemplateVariableResolver which intercepts the value of ${myprop} while it's being typed or after the user has finished with it and set a modified values for other entries of this or another variables within the template.
Re: Custom intelligent java template [message #665890 is a reply to message #665816] Mon, 18 April 2011 03:51 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
On 4/16/2011 6:00 PM, Aleck Agakhan wrote:
> You're right, I wanted to say org.eclipse.jdt.ui.
>
> Take a look at this template
>
>
> private int ${myprop};
> public int get${myprop}()
> {
> return ${myprop};
> }
> public void set${myprop}(int ${myprop})
> {
> this.${myprop}=${myprop};
> }
>
>
> When someone is typing the value for ${myprop} the entries of this var
> are changing within the entire template. The question is: Is it possible
> to create a sort of TemplateVariableResolver which intercepts the value
> of ${myprop} while it's being typed or after the user has finished with
> it and set a modified values for other entries of this or another
> variables within the template.

This should already work.

- Create this template (Java > Editor > Templates) and call it say
'createProperty'.
- Now when you type 'createProp' in the Java editor and invoke content
assist (Ctrl+Space), this template should be offered.
- When you insert this template, the code will be inserted and all
locations of the variable will be surrounded by a rectangle (this is
called as linked mode). Typing in one of the rectangles will affect all
other rectangles as well.

Is this what you were looking for ?

(More information on editor templates can be found in Eclipse help under
Java Development user guide.)
Re: Custom intelligent java template [message #666041 is a reply to message #665890] Mon, 18 April 2011 17:57 Go to previous messageGo to next message
Aleck Agakhan is currently offline Aleck AgakhanFriend
Messages: 4
Registered: April 2011
Junior Member
Deepak Azad wrote on Sun, 17 April 2011 23:51

Is this what you were looking for ?


Of course no.
becouse while I'm typing in this line: "private int ${myprop};" for instance "private int somefield;" the line "public int get${myprop}()" turns into "public int getsomefield()" whereas I need custom resolver which can turn the latter line into something like "public int getSomeFIELD()" that is to apply some changes on the value of ${myprop} before placing it in the latter line.

Let's look to the next template:
private int ${myprop};

public int get${myAuxVar}()
{
	return ${myprop};
}

Is it possible to intercept the value of ${myprop} while it's being typed or after the user has finished with it and generate a value for ${myAuxVar} so that the new value would be placed at the required position.
Re: Custom intelligent java template [message #666572 is a reply to message #666041] Thu, 21 April 2011 04:36 Go to previous message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
Registered: July 2009
Senior Member
Ha! Sorry, my bad. I see the problem now.

I guess you will have to open a bug with JDT/UI about providing
JavaContext.addDependency() and other required things as APIs. You are
also welcome to provide a patch! Since there is not much time left for
3.7, this can be looked into in 3.8.

(Though if the only scenario you have in mind is creating getters and
setters, you can simply use the quick assist.)
Previous Topic:Problem with running as configurations on Eclipse HELIOS
Next Topic:How to build Eclipse for SPARC/Linux from source code
Goto Forum:
  


Current Time: Thu Mar 28 12:14:31 GMT 2024

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

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

Back to the top