Use of elementName while writing Plug-ins for Servoy

The below TIP explain you the use of “elementName” while writing custom Plug-ins for Servoy.

When you write any Plug-ins for Servoy, you must have a provider class, which must implement the IScriptObject Interface and the class needs to be referenced to the Servoy Application Server through the getScriptObject() method of the Plug-in’s main class. So, If you implement the IScriptObject Interface, in the provider class, you must have to override several methods, such as getAllReturnedTypes(), getParameterNames(), getSample(), getToolTip() etc.

From all those methods of the IScriptObject Interface, there is a method called, getSample(), which will get called, when you selects a method of the Servoy Plug-in and do a Move Sample. The basic implementation of the methods follows that; it will help the developer to give some sample code, explaining the use of the method. The method will return a String object, which will be written in to the Servoy code Editor.

A sample Implementation of the method shown below:

public String getSample(final String method) {

// Provides sample servoy code when move sample is used.

final StringBuffer buff = new StringBuffer();

// for method : getRecord

if (“getRecord”.equals(method) {

buff.append(“//Get the selected record value.nn”);

buff.append(“var record = plugins.RecordPlugin.getRecord();”);

return buff.toString();

}

}

In the above sample code, the Plug-in “RecordPlugin” has a method getRecord(). So, when a developer/user do a Move Sample by selecting this method, the Servoy Application Server returns a String object which will be printed to the Servoy Code Editor.

Now, if the Developer/User do a Move Sample, he will get to see the below texts in the Servoy Code Editor.

//Get the selected record value.

Var record = plugins.RecordPlugin.getRecord();

For writing the Implementation method for getSample(), “elementName” will help us a lot. We can use the “elementName” in the returned strings to automatically populate the Plug-in name. What ever may be the Plug-in name you have given, you don’t have to worry about that. Servoy Plug-in API will take care of the same. The below code shows the implementation of the above getSample() method by using “elementName”.

public String getSample(final String method) {

// Provides sample servoy code when move sample is used.

final StringBuffer buff = new StringBuffer();

// for method : getRecord

if (“getRecord”.equals(method) {

buff.append(“//Get the selected record value.nn”);

buff.append(“var record = %%elementName%%.getRecord();”);

return buff.toString();

}

}

The Method Implementation with “elementName” returns the same output as the previous implementation returns.

Hope this TIP helps you a little in writing custom Servoy Plug-ins.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!