PropertyChangeEvents
.
There are two predefined target actions, PropertySyncAction and
DelegateMethodAction.
The first is used to synchronize component properties of any component
with any other component using standard JavaBeans
getter and setter methods.
The second is used to invoke a delegate method
in case the link is activated. For example, this can be used to
invoke a specific method if a user presses a button on the gui.
Or you can run it directly using Java. Note that you need to set the class path accordingly.java -jar swlink.jar
This will open a window with a selection list that contains the various examples.java com.iternum.swlink.examples.Launcher
myAcceptBtn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent pEvt) {
getController().okPressed();
}
}
);
This is a lot of code to create something very simplistic.
In Swlink, this logic is reduced to a single line of code.
Linker.createDelegateLink(myAcceptBtn, ActionEventActivator.class, null,
getController(), "okPressed", false, false, false);
myNameTf
to the field called
lastName
in the model. Binding should occur,
whenever the text field loses its focus and whenever the
value in the model changes (assuming lastName
is a bound
property), as shown in the diagram below.
getModel().addPropertyChangeListener(this);
...
myNameTf.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent evt) {
getModel().setLastName(myNameTf.getText());
}
});
...
...
public void propertyChange(PropertyChangeEvent pEvt) {
if (pEvt.propertyName().equals("lastName")) {
myNameTf.setText(getModel().getLastName());
}
}
Again, a lot of boilerplate code for a standard task. Since
Swlink supports the notion of bidirectional connections,
setting up the same synchronization looks a lot tidier.
Linker.createPropertySyncLink(myNameTf, "text", FocusLostActivator.class,
null, getModel(), "lastName", null, null, true,false);
Of course it is also possible to synchronize two properties in two arbitrary
JavaBeans. Consider two JavaBeans with properties propertyOne and
propertyTwo respectively. A change in propertyOne should synchronize
propertyOne with propertyTwo in its opposite and vice versa.
Linker.createPropertySyncLink(theBeanOne, theBeanTwo,
"propertyOne", "propertyTwo",true,false);
EndpointProxy
can be used to proxy both source and
target of a link. The following code snippet uses an EndpointProxy
to proxy the source of a link.
TestBean theBeanOne = new TestBean();
TestBean theBeanTwo = new TestBean();
EndpointProxy theSource = new DefaultEndpointProxy(theBeanOne);
Linker.createPropertySyncLink(theSource,theBeanThree,
"propertyTwo","propertyTwo",
true,false);
...
// Now swap the source of the link
TestBean theBeanThree = new TestBean();
theSource.setValue(theBeanThree)
propertyOne
of TestBeanOne
with propertyTwo
of
TestBeanTwo
for property synchronization.
Link
between propertyOne
in TestBeanOne
and a nested property beanTwo.propertyTwo
in
TestBeanThree
. The code to setup such a Link is
again very compact.
Linker.createPropertySyncLink(beanOne, beanThree,
"propertyOne", "beanTwo.propertyTwo",
true, false);
Nested properties are especially useful if they are combined with an
EndpointProxy.
As a general rule, EndpointProxies are
resolved before nested properties. By calling setValue()
in an
EndpointProxy
, the links with nested properties are
recreated using the newly supplied value object.
// Create a beanutils locale sensitve converter
ConvertUtils.register(new DateLocaleConverter(null, Locale.GERMANY,
"dd.MM.yyyy"), java.util.Date.class);
Swlink builds on this mechanism to provide support for formatting conversions.
Formatting conversions are typically required when you need to transform
some data type into a String. Swlink provides out of the box support for
the classes derived from java.text.Format
. It is also easy to
provide your own format classes to format domain specific objects, for
example order numbers or airline codes. The example below shows to format
a date accoring to a specific date format.
// Create a format converter for a date
FormatConverter formatConverter = new FormatConverter();
formatConverter.setFormat(java.util.Date.class,
new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY));
// Register with BeanUtils
ConvertUtils.register(formatConverter, java.lang.String.class);
Links
. The main
extension points of the system are to create new Activator
and
Action
.
Activator
when you need to define a new
way to activate a connection. Consider to derive your new Activator
from AbstractActivator
and implement the abstract methods.
Action
if you need to define a new action in
order a link is activated. Consider to derive your new Action
from AbstractAction
as this provides boilerplate implementation
for handling reentrancy. Before creating a new Action
take some time to
determine if you cannot use one of the existing Actions
. Actually the
existing action types should suffice most of the time.