JavaStates Simple Tutorial

21
HOW TO USE JAVASTATES

description

This document provides a simple tutorial on how to use JavaStates, https://sourceforge.net/projects/javastates/ http://laurent.henocque.free.fr

Transcript of JavaStates Simple Tutorial

Page 1: JavaStates Simple Tutorial

HOW TO USE JAVASTATES

Page 2: JavaStates Simple Tutorial

CREATIVE COMMONS

• This document is shared under the terms of the Attribution-Share Alike 2.0 Generic Creative Commons license

• http://creativecommons.org/licenses/by-sa/2.0/

Page 3: JavaStates Simple Tutorial

WHAT IS JAVASTATES?

• JavaStates is a Java library that allows for defining the internal dynamics of a man machine interface declaratively in terms of states rather than in programmatic ways.

• JavaStates can be used to implement Java Swing applications, but also any program based upon objects obeying the Java Bean conventions (“resource”->getResource/setResource/isResource), or the Listener API (addListener,removeListener,getListeners)

Page 4: JavaStates Simple Tutorial

WHERE DO I GET JAVASTATES?

• JavaStates is shared under the terms of a BSD license.

• https://sourceforge.net/projects/javastates/

• http://laurent.henocque.free.fr/javastates.php

Page 5: JavaStates Simple Tutorial

STATE INTEGRITY

• The main feature of JavaStates is that the library will take care for all the cleanup needed when switching from state to state

• JavaStates also cares for this integrity in the presence of concurrent (independent) sub regions in states.

• The main advantage is that the interface designer/implementor concentrates on the specification, under the warranty that the resulting application will be defect free.

Page 6: JavaStates Simple Tutorial

THE SIMPLEST TWO STATE JAVA SWING APPLICATION

public class HelloWorld { public static void main(String[] args) { try { new HelloWorld(); } catch(Exception e) { System.out.println(e);}} HelloWorld() throws Exception{ JFrame mainFrame= new JFrame("HelloWorld"); JButton hellobutton=new JButton("Hello ?"); mainFrame.add(hellobutton); JavaStates.MAIN_("Main"). req(mainFrame, "Visible", true). req(mainFrame, "Size", new Dimension(500,100)). req(mainFrame, "Title", "Hello! World, powered with JavaStates"). lis(mainFrame, WindowListener.class, new WindowAdapter(){

public void windowClosing(WindowEvent e) {System.exit(0); }}). set(hellobutton,"world"). STATE_("world"). req(hellobutton,"Label","World !"). set(hellobutton,"root"). _STATE("world"). _MAIN().start(); }}

Page 7: JavaStates Simple Tutorial

HELLO WORLD RUNNING

Page 8: JavaStates Simple Tutorial

INITIALIZING JAVASTATES

• Two initialize JavaStates, and at the same time create a main region, use the static function JavaStates.MAIN_(String name)

JavaStates.MAIN_("Main")

• The name parameter is the name of the most global region.

• The root state in this region is called “root”.

Page 9: JavaStates Simple Tutorial

STATE REQUIREMENTS AS TRIPLES

• A state requirement is a triple: a mandatory setting of a given resource, of a given object to a certain value. The function

req(Object, String resource, Object value)

• creates such a requirement. For example:

• req(hellobutton, “Label”, “World!”)

• JavaStates expects a Java Bean api to exist on target object:

Page 10: JavaStates Simple Tutorial

CREATING STATES

• States are declared using the function

STATE_(String name)

• State declarations must be closed at the end, like parentheses, using the function

_STATE(String name)

• JavaStates checks the identity of the names. This prevents from copy/paste errors or forgotten/extraneous closures.

Page 11: JavaStates Simple Tutorial

STATES AS PLACEHOLDERS

• States are placeholders for requirements and other states.There are no limits to the embedding of states.

• Embedded states inherit, and may override the requirements from enclosing states.

• Embedding states obeys the UML State Diagrams conventions, borrowed from Harel Statecharts

• State requirements have no equivalent in UML

Page 12: JavaStates Simple Tutorial

LISTENER REQUIREMENTS

• Interface dynamics are implemented in Java by using listeners. A listener requirement is set using the functionlis(Object, Class<?>listener, Object value)• creates such a requirement. For example:

lis(mainFrame, WindowListener.class, new WindowAdapter(){...}).• JavaStates supports listener requirements by fully relying upon

introspection. Any class having a resource that obeys the Listener convention can be used: if there exist three functions getListener, addListener, getListeners.

Page 13: JavaStates Simple Tutorial

CHANGING STATE

• A function for creating Action Listeners that change state is predefined:

• set(Object target, String name)

• ”set” expects target to implement the Java AbstractButton interface. It does not use introspection.

• In a state that specifies ‘set(hellobutton,"world")’, the action listener of object ‘hello button’ has the effect of changing to state “world”.

Page 14: JavaStates Simple Tutorial

SETTING THE ROOT STATE

• Every region owns a “root” state, placeholder for all other states

•When the state containing this region is set, the region state is by default set to this root state.

•When explicit setting to this root state is needed, one can use the function reset(Object target). This is almost equivalent to set(Object target,”root”), with the advantage of being independent from a change in the root state name

Page 15: JavaStates Simple Tutorial

CREATING REGIONS

• Regions are declared using the function

REGION_(String name)

• Region declarations must be closed at the end, like parentheses, using the function

_REGION(String name)

• JavaStates checks the identity of the names. This prevents from copy/paste errors or forgotten/extraneous closures.

Page 16: JavaStates Simple Tutorial

THE TWO REGION DEMO

JavaStates.MAIN_("Main").req(mainFrame, "Visible", true).req(mainFrame, "Size", new Dimension(500,100)).req(mainFrame, "Title", "Two Region demo, powered with JavaStates").lis(mainFrame, WindowListener.class, new WindowAdapter() {...}).set(button1,"world").STATE_("world").

req(button1,"Label","World Region 1").reset(button1).

_STATE("world").REGION_("2").

set(button2,"world").STATE_("world").

req(button2,"Label","World Region 2").reset(button2).

_STATE("world"). _REGION("2").

_MAIN().start();

Page 17: JavaStates Simple Tutorial

THE RUNNING DEMO

Page 18: JavaStates Simple Tutorial

LAUNCHING JAVASTATES

• Launching JavaStates is performed using the function

start()

• This call sets the main region to its root state. Any subregion in this state gets started as well, and so on recursively.

Page 19: JavaStates Simple Tutorial

SETTING AN INITIAL STATE

• It often happens that the desired initial state in a region is not its root state.

• To specify the root state in a region, use the function

initial (String name)

• The name parameter must match a valid state name in the region at run time. “initial” may be called before the state is declared (usually just after the region is created, and before any requirement or state is declared.

Page 20: JavaStates Simple Tutorial

TO CONTINUE• You may check and then adapt the full featured Editor demo.

Page 21: JavaStates Simple Tutorial

CONCLUSION, FURTHER LINKS

• JavaStates offers state support in Java Interface development.

• Further details, and the programs are present at

• http://sourceforge.net/projects/javastates

• http://laurent.henocque.free.fr