Binding DOM/JTextField Swing
Dans cette section, on souhaite binder l'attribut value de l'élement input :
<html xmlns="http://www.w3.org/1999/xhtml" >
<input type="text" value="djo" />
</html>
avec la propriété Text d'un JTextField Swing.
package org.akrogen.tkui.samples.xhtml;
import java.awt.GridLayout;
import java.io.InputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.akrogen.tkui.samples.xhtml.model.Person;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.Realm;
import org.ufacekit.core.databinding.dom.DOMObservables;
import org.ufacekit.ui.swing.databinding.swing.SwingEventConstants;
import org.ufacekit.ui.swing.databinding.swing.SwingObservables;
import org.ufacekit.ui.swing.databinding.swing.SwingRealm;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
public class JFaceDatabindingDOMTest {
public static void main(String[] args) {
try {
// 1. Load XHTML content into DOM Xerces
InputStream source = XHTMLTest.class
.getResourceAsStream("sample.html");
org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.parse(new InputSource(source));
source.close();
Document document = parser.getDocument();
Element input = (Element) document.getElementsByTagName("input")
.item(0);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2, 20, 20));
JTextField text1 = new JTextField();
panel.add(text1);
frame.getContentPane().add(panel);
Person person = new Person("djo");
/* ---------- Start Swing Binding ---------- */
SwingRealm.createDefault();
Realm realm = SwingObservables.getRealm();
DataBindingContext context = new DataBindingContext(realm);
// Bind JavaBean User name getter/setter (java.lang.String) with
// Swing JTextField.
context.bindValue(SwingObservables.observeText(text1,
SwingEventConstants.Modify), DOMObservables
.observeAttrValue(input, "value", null), null, null);
/* ---------- End Swing Binding ---------- */
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}