001: package net.xoetrope.samples.controls;
002:
003: import java.io.BufferedWriter;
004: import java.io.File;
005: import java.io.FileOutputStream;
006: import java.io.OutputStreamWriter;
007:
008: import java.awt.Frame;
009:
010: import net.xoetrope.awt.XButton;
011: import net.xoetrope.awt.XComboBox;
012: import net.xoetrope.awt.XEdit;
013: import net.xoetrope.data.XDataSource;
014: import net.xoetrope.xui.XPage;
015: import net.xoetrope.xui.XResourceManager;
016: import net.xoetrope.xui.data.XDataBinding;
017: import net.xoetrope.xui.data.XListBinding;
018: import net.xoetrope.xui.data.XTextBinding;
019: import net.xoetrope.xui.helper.BuddyHelper;
020: import net.xoetrope.xui.style.XStyleFactory;
021:
022: /**
023: * <p>Title: Xui</p>
024: * <p>Description: </p>
025: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
026: * <p>Company: Xoetrope Ltd.</p>
027: * @author not attributable
028: * @version 1.0
029: */
030:
031: public class DataSample extends XPage {
032: XButton closeButton, saveButton;
033: Frame frame;
034: BuddyHelper buddy;
035: XEdit txtFirstname, txtSurname, txtAge;
036: XComboBox cmbOccupations;
037: private boolean pageValidation = false;
038: private String validationText = "";
039: private XDataSource modelDataSource;
040:
041: public DataSample() {
042: buddy = new BuddyHelper((XStyleFactory) componentFactory);
043:
044: frame = new Frame("Data Sample");
045: frame.setLayout(null);
046: frame.setSize(640, 580);
047: String desc = "This example shows how data can be bound to input controls. First create the components and then";
048: desc += " bind them to the controls you wish to populate. Load the data using the modelDataSource and call the";
049: desc += " updateBoundComponentValues function in order to do the population. To save the data the saveBoundComponentValues";
050: desc += " must first be called. and then the save will output the data in the format it was read.";
051: desc += " In the more complete examples the transfer of data between the controls and the model is automated";
052: desc += " making the data handling more transparent";
053:
054: componentFactory.addComponent(XPage.LABEL, 10, 50, 530, 120,
055: desc);
056:
057: txtFirstname = (XEdit) buddy.addComponent(XPage.EDIT, 20, 250,
058: 150, 25, "Firstname", "", null);
059: txtSurname = (XEdit) buddy.addComponent(XPage.EDIT, 20, 280,
060: 150, 25, "Surname", "", null);
061: txtAge = (XEdit) buddy.addComponent(XPage.EDIT, 20, 95, 130,
062: 310, 355, 25, "Age", "", "Years", null);
063: cmbOccupations = (XComboBox) buddy.addComponent(XPage.COMBO,
064: 20, 340, 150, 25, "Profession", "", null);
065:
066: saveButton = (XButton) componentFactory.addComponent(
067: XPage.BUTTON, 10, 440, 130, 25, "Save");
068: closeButton = (XButton) componentFactory.addComponent(
069: XPage.BUTTON, 10, 540, 130, 25, "Close");
070: setSize(frame.getSize());
071: mapEvents();
072: loadData();
073: populateComponents();
074: updateBoundComponentValues();
075:
076: frame.add(this );
077: frame.setVisible(true);
078: frame.show();
079: }
080:
081: public void populateComponents() {
082: addBinding(new XTextBinding(txtFirstname,
083: "personaldata/firstname"));
084: addBinding(new XTextBinding(txtSurname, "personaldata/surname"));
085: addBinding(new XTextBinding(txtAge, "personaldata/age"));
086: addBinding(new XListBinding(cmbOccupations,
087: "personaldata/professions"));
088: }
089:
090: public void updateBoundComponentValues() {
091: // iterate over components and update their values
092: int numBindings = modelBindings.size();
093: for (int i = 0; i < numBindings; i++)
094: ((XDataBinding) modelBindings.elementAt(i)).get();
095: }
096:
097: public void saveBoundComponentValues() {
098: // iterate over components and save their values
099: int numBindings = modelBindings.size();
100: for (int i = 0; i < numBindings; i++)
101: ((XDataBinding) modelBindings.elementAt(i)).set();
102: }
103:
104: private void mapEvents() {
105: addMouseHandler(closeButton, "Close");
106: addMouseHandler(saveButton, "save");
107: }
108:
109: public static void main(String args[]) {
110: ValidationSample compSample = new ValidationSample();
111: }
112:
113: public void Close() {
114: if (wasMouseClicked()) {
115: frame.setVisible(false);
116: }
117: }
118:
119: private void loadData() {
120: modelDataSource = new XDataSource();
121: try {
122: String fileName = "sampledatasets.xml";
123: File source = new File(fileName);
124: if (source.exists())
125: modelDataSource.read(XResourceManager
126: .getBufferedReader(source, null));
127: else
128: modelDataSource.read(XResourceManager
129: .getBufferedReader(fileName, null));
130: } catch (Exception ex) {
131: ex.printStackTrace();
132: }
133: }
134:
135: public void save() {
136: if (wasMouseClicked()) {
137: saveBoundComponentValues();
138: try {
139: String fileName = "SampleData.xml";
140: FileOutputStream fos = new FileOutputStream(fileName);
141: OutputStreamWriter osw = new OutputStreamWriter(fos,
142: "ISO-8859-1");
143: BufferedWriter bw = new BufferedWriter(osw);
144: modelDataSource.write(bw);
145: } catch (Exception ex) {
146: ex.printStackTrace();
147: }
148: }
149: }
150: }
|