JSP/JS formatting in eclipse? - eclipse

Folks is there any plugin for eclipse which provides the standard formatting/indentation.I could find basic settings
at I could find a few basic setting in Web->JSP Files->Editor but does not help much. I am looking for very basic indentation/formatting . Also it can format javascript code in jsp. For example:-
<s:if test="myManager">
<s:set var="cust" value="%{'customer'}" />
<s:set var="custMode" value="%{'custEdit'}" />
</s:if>
should be changed to below on save
<s:if test="myManager">
<s:set var="cust" value="%{'customer'}" />
<s:set var="custMode" value="%{'custEdit'}" />
</s:if>
Javascript Example
function test(){
var test1;
var test2;
if(someCondition)
{
var test3;
}
}
should be changed to below on save
function test(){
var test1;
var test2;
if(someCondition)
{
var test3;
}
}
I am not exaclty looking for above format but yes some meaningful/readable format/indentaion for jsp and javascript code. I am using eclipse helios. I did not get any open source plugin on net. i am sure there must be some utility available but somehow i am not getting through it .
It would be good if it can format scriplets also.

Related

Typo3: How to get the View exception error

I am Developing a custom extension for Typo3. now I am getting an error if the user did not include my extension from template's include section.
I want to catch this error to show a message from controller. How can I do this?
my controller action.
public function listAction()
{
$audits = $this->auditRepository->findAll();
$this->view->assign('arrDetails', $audits);
}
This could be one solution, but not the cleanest.
We first need to get the values from the field include_static_file that it is located on the sys_template table. So:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_template')->createQueryBuilder();
$result = $queryBuilder
->select('include_static_file')
->from('sys_template')
->execute()
->fetch(0);
The we need to get the string and evaluate if your extension key is present. So:
$extKey = 'your_extension_key';
if (strpos($result['include_static_file'], $extKey) !== false) {
$audits = $this->auditRepository->findAll();
$this->view->assign('arrDetails', $audits);
}
else {
$this->addFlashMessage(
'You forgot to add the static template',
$messageTitle = 'Template is missing',
$severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING,
$storeInSession = TRUE
);
}
Your HTML
<f:if condition="{arrDetails}">
<f:then>
do something with your content
</:then>
<f:else>
<f:flashMessages />
</f:else>
</f:if>
Of course you can write a static function for this or you can use the LocalizationUtility in order to get the text in multiple languages. It is up to you.
Result:
Are you sure you need your static template?
I think it is important that some values you fetch from typoscript got meaningful content.
As you inspect them anyway you can output a message if one or more values are empty.
Your plugin would work in an installation if all necessary values are set in any way. Even if your static template is not included.
And your plugin would fail if your static template is included but following typoscript would erase the settings from it.
In your error message you can note the possibility to use the values from your static template.

Making Cocoa Application Scriptable Swift

Goal
I am trying to make my Cocoa Application that has been written in Swift scriptable from Applescript.
What I've Done
I have created a SDEF file, configured my info.plist and created a class which I think is appropriate.
definition.sdef
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="SamX">
<!-- specific suite(s) for the application follow... -->
<suite name="SamX Scripting Suite" code="Samx" description="Suite for communication with the application">
<command name="savedoc" code="corecnte" description="description">
<cocoa class="ProjectName.ScriptingSaveNotification" id="BLah"/>
<parameter name="with dname" code="WTdc" type="text" optional="no" description="description">
<cocoa key="DocumentName"/>
</parameter>
<result type="boolean" description="The result of the invocation. True if it succeeds, False if it does not"/>
</command>
</suite>
</dictionary>
info.plist
ScriptingSaveNotification.swift
import Foundation
import Cocoa
class ScriptingSaveNotification: NSScriptCommand, NSUserNotificationCenterDelegate {
override func performDefaultImplementation() -> AnyObject? {
let parms = self.evaluatedArguments
var name = ""
if let args = parms {
if let DocumentName = args["DocumentName"] as? String {
name = DocumentName
}
}
debugPrint("We were prompted to save");
return "hello world"
}
func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
debugPrint("We were prompted to save");
return true
}
}
Where I Am
I have an application that launches. The application's SDEF file appears to be reflecting in the Applescript Editor. The Applescript editors also returns a dictionary definition. However when I run the command, I always get an output of 5 (int), and none of my debug lines appears to be outputting in Xcode.
It appears to me that maybe I'm referencing my class in the SDEF improperly. But I'm not 100% sure. I've tried renaming it several times. Any help would be greatly appreciated.
Applescript Dictionary
Test Script
tell application "MyApplication"
set testString to "Hello"
set returnValue to savedoc testString
display alert returnValue
end tell
Edit:
The main issue is that you don't actually use the with dname parameter in your script. It should be:
set returnValue to savedoc with dname testString
That said, the info below is still valid for creating a proper sdef and the other suggestions/examples may be helpful.
This is a basic example of passing a string in the evaluatedArguments of the NSScriptCommand and then returning that string as the result of the script command in Swift (you could return a boolean on success/failure of the command or any other type of result; and, actually, in your sdef you say you're going to return a boolean but your command is returning a string (text in sdef definitions)). Creating your sdef can be tricky. Your command's code should start with the suite's code and you can remove the id and optional parameter (if you omit the optional parameter, the default is that the parameter is required). If you do just need a single parameter you could also just use the direct-parameter instead.
You can download a demo project:
ScriptableSwift.zip
Here are the relevant bits (aside from the plist entries that you have correct in your tests).
ScriptableSwift.sdef
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="ScriptableSwift Terminology">
<suite name="ScriptableSwift Scripting Suite" code="SSss" description="Standard suite for application communication.">
<command name="save" code="SSssSave" description="Save something.">
<cocoa class="ScriptableSwift.SaveScriptCommand"/>
<parameter name="in" code="Fpat" type="text" description="The file path in which to save the document.">
<cocoa key="FilePath"/>
</parameter>
<result type="text" description="Echoes back the filepath supplied."/>
</command>
</suite>
</dictionary>
SaveScriptCommand.swift
import Foundation
import Cocoa
class SaveScriptCommand: NSScriptCommand {
override func performDefaultImplementation() -> AnyObject? {
let filePath = self.evaluatedArguments!["FilePath"] as! String
debugPrint("We were prompted to save something at: \(filePath)");
return filePath
}
}
Test AppleScript
tell application "ScriptableSwift" to save in "path/to/file"
Result:
"path/to/file"

Binding an html form action to a controller method that takes some parameters

In my Find controller I have a method like:
public Result findLatest(String repoStr) {
............
}
Which is linked through a route:
GET /latest controllers.Find.findLatest(repo: String)
Then, I have a form in a view like:
<form action="#routes.Find.findLatest()" method="get">
....
<select name="repo">....</select>
</form>
But obviously that is failing, because it is expecting some parameters that I do not fulfill in the action. What is the correct way to do this without having to end up leaving the findLatest method taking no parameters in my controller?
You could change the routes to accept an empty string:
GET /latest/:repo controllers.Find.findLatest(repo: String = "")
Then configure your controller function to handle empty string.
That way,
<form action="#routes.Find.findLatest()" method="get">
....
<select name="repo">....</select>
will evaluate repo as an empty string at the controller level.
Edit: Support for this implementation was dropped in Play v 2.1
You may be interested in Play's Optional parameters e.g. play.libs.F.Option[String]
Example: How to handle optional query parameters in Play framework
GET /latest/:repo/:artifact controllers.Find.findLatestArtifact(repo: play.libs.F.Option[String], artifact: play.libs.F.Option[String])
This will allow you flexibility in which arguments need to be provided.
Not sure which language you're using but the link above contains an example for scala and the method declaration in java would look something like:
import play.libs.F.Option;
public static Result findLatestArtifact(Option<String> repo, Option<String> artifact){ ... }
and updated implementation 2.1
Routes with optional parameter - Play 2.1 Scala
EDIT: play 2.1+ Support : Props to #RobertUdah below
Initializing to null:
GET /latest/ controllers.Find.findLatest(repo: String = null)
GET /latest/:repo controllers.Find.findLatest(repo: String)
<form action="#routes.Find.findLatest()" method="get">
Normally all form data go in the body and you can retrieve them in your action method with bindFromRequest() (see docs).
If you really want to pass one form element as a part of the URL then you have to dynamically compose your URL in JavaScript and change your route.
Your route could look like:
GET /latest/:repo controllers.Find.findLatest(repo: String)
And the JavaScript part like (I didn't actually test the code):
<form name="myform" action="javascript:composeUrl();" method="get">
....
<select name="repo">....</select>
</form>
<script>
function submitform() {
var formElement = document.getElementsByName("myform");
var repo = formElement.options[e.selectedIndex].text;
formElement.action = "/lastest/" + repo;
formElement.submit();
}
</script>
Cavice suggested something close to what I consider the best solution for this (since F.Option are not supported anymore with the default binders in Play 2.1 ).
I ended up leaving the route like:
GET /latest controllers.Find.findLatest(repo=null)
and the view like:
<form action="#routes.Find.findLatest(null)" method="get">
<select name="repo"> .... </select>
....
</form>
and in the controller:
public Result findLatest(String repoStr) {
if(repoStr==null) {
repoStr=Form.form().bindFromRequest().get("repo");
.....
This allows me to have a second route like:
GET /latest/:repo controllers.Find.findLatest(repo: String)

zk form reset is not working in MVVM

I am trying to use MVVM model in zk.
If i use form in zk and try to reset the value it is not binding
<?page title="" contentType="text/html;charset=UTF-8"?>
<zk>
<window border="none" apply="org.zkoss.bind.BindComposer"
viewModel="#id('ix') #init('com.Controller')"
form="#id('fx') #load(ix) #save(ix,before='add')" >
<textbox value="#bind(fx.title)"/>
<button onClick="#command('reverse', fx=fx, field='title')" label="Reverse"/>
</window>
</zk>
#Command
#NotifyChange("title")
public void reverse() {
String value = (String) fx.getField(fieldName);
String newValue = new StringBuilder(value).reverse().toString();
setTitle(newValue);
}
But if i use <textbox value="#bind(ix.title)"/> directly without form it works.
May i know whats wrong with this code.
I'll explain you what's wrong.
<button onClick="#command('reverse', fx=fx, field='title')" label="Reverse"/>
Here you will call the command reverse and you give 2 params with it, fx and field.
#Command
#NotifyChange("title")
public void reverse() {
Here you have your command method but you don't have parameters that you expect.
Its also not needed give the params with it cause Title is propably a global private String.
If you want to give params then this should be the code :
#Command
#NotifyChange("title")
public void reverse(#BindingParam("fx") Form fx, #BindingParam("title") String title) {
second :
form="#id('fx') #load(ix) #save(ix,before='add')" >
Not needed, you use mvvm so you call the vm by ix, this become for you absolete.
The thing what you do here is sometimes needed for grids or lists that the don't update directly by #bind but only by pushing the save button.
This is because #save is so powerfull that it will change the values in the DB without you calling a save method.
#bind(ix.title) is actually the same as #load(ix.title) #save(ix.title) but grouped in one word for sparing code.
Hopes this helps you.

Grab JSPX user input directly from a trinidad UIXComponent in my Java code?

I would like to retrieve the user input from a component within my Java code. Something akin to textbox.text in aspx/.NET. I am finding the documentation very confusing and my attempts don't compile. Is there a way?
<tr:inputDate id="date" required="true"
inlineStyle="color:rgb(0,58,117); font-weight:bold;"
value="#{processScope.benefit.serviceDate}"
immediate="false"
onchange="submit();"
label="#{mb_ResourceBean.res['claim.serviceDate.label']}">
<tr:convertDateTime pattern="yyyy/MM/dd" secondaryPattern="yyyyMMdd"
type="date"/>
<tr:validateDateTimeRange minimum="#{bk_ClaimBean.minDate}"
maximum="#{bk_ClaimBean.maxDate}"/>
</tr:inputDate>
Poor half-attempt to grab input:
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
UIXComponent component = viewRoot.findComponent("date"); //does not compile
I'm not sure what you are trying to achieve, but since you already have a value binding (#{processScope.benefit.serviceDate}) and you have onchange="submit();" in your <tr:inputDate> it looks like you want to use a valueChangeListener.
You need a method to handle the value change event in your bean, for example:
public void dateChanged(ValueChangeEvent event)
{
System.out.println("New value: "+ event.getNewValue());
System.out.println("instanceof Date: "+ (event.getNewValue() instanceof Date));
}
In your jspx you have to add the listener. Also you might want to use autoSubmit="true" instead of onchange="submit();", for example:
<tr:inputDate value="#{myBean.myDate}"
valueChangeListener="#{myBean.dateChanged}"
immediate="true" autoSubmit="true"/>
The code in your question does not compile since viewRoot.findComponent() will return a UIComponent. You need to cast it to UIXComponent.
Also, you need to take the naming containers into account. You will need to use something like: viewRoot.findComponent("formId:date");. In this case formId is the id of your <tr:form>.