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.
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)
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?
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.
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.
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.)
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.
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.)