Reputation: 122719
I would like to describe the expected inputs of a Java class aimed to perform a certain task, within the Java class itself. The aim is to be able to generate the user interface directly be loading this class.
For example, assuming the base interface is Task
(implementing Runnable
):
public class BuildRectangleTask implements Task {
public void setLength(double val) { /* ... */ }
public void setWidth(double val) { /* ... */ }
public void run() { /* Build a rectangle */ }
}
I could use some reflection to get the parameter names from this, and then build the relevant part of a user interface from this, for example:
<form>
<label for="length">Length: </label>
<input type="text"name="length" id="length" /><br />
<label for="width">Width: </label>
<input type="text"name="width" id="width" />
</form>
This would probably work fine for very simple examples, but accessor names can't always translate properly into natural language without sounding awkward (they may also need to be translated). In addition, I would also like to be able to have additional information (e.g. which ones would be optional or have default values).
I'm thinking of using annotations to describe this:
@Description(text="This tasks builds a rectangle")
public class BuildRectangleTask implements Task {
@Input(label="Length", optional=false)
public void setLength(double val) { /* ... */ }
@Input(label="Width", optional=true, default=10,
help="This is the width of the rectangle")
public void setWidth(double val) { /* ... */ }
public void run() { /* Build a rectangle */ }
}
This would also be useful for extra information, for example multiple choices:
@Input(label="Colour", options={"Red", "Green", "Blue"})
I would also like to be able to generate other types of user interfaces, for example a command-line, get-opt style like this based on the information:
java TaskProcessor BuildRectangle --help
usage: BuildRectangle --height= [ --width=10 ] [ --color= ]
Options:
--height= ...
--width= This is the width of the rectangle (defaults to 10).
--color= Red, Blue, Green.
Is there any standard set of annotations or usual framework for achieving this (or perhaps a different approach altogether), preferably compatible with Java and Groovy?
I would like the class to be self-contained as much as possible (i.e. avoid configuration files). (I'm not necessarily interested in defining what the output of such a task would be and how it should be displayed.)
Upvotes: 1
Views: 329
Reputation: 336
You could try http://today.java.net/pub/a/today/2008/06/24/automatic-user-interface-with-openxava.html which is the sort of thing you want to do but probably the wrong framework! Might be a bit overkill though.
Upvotes: 1
Reputation: 66089
You might want to take a look at javabuilders. It has a lot of overlap with the ideas you've got. In particular, it allows you specify your UI layout declaratively and bind data from the UI to Java objects automatically.
Upvotes: 1