01: /*
02: * Copyright 2006 Ethan Nicholas. All rights reserved.
03: * Use is subject to license terms.
04: */
05: package jaxx.compiler;
06:
07: import jaxx.*;
08: import jaxx.types.*;
09:
10: /** Represents a data binding in a JAXX file. <code>DataBinding</code> uses {@link DataSource} to
11: * track changes to a source expression and update the destination.
12: */
13: public class DataBinding {
14: private String id;
15:
16: /** The DatSource which tracks source expression changes. */
17: private DataSource dataSource;
18:
19: /** The data binding destination in the form <code><id>.<propertyName></code>. */
20: private String dest;
21:
22: /** A Java snippet which will cause the destination property to be updated with the current value of
23: * the binding.
24: */
25: private String assignment;
26:
27: /** The current <code>JAXXCompiler</code>. */
28: private JAXXCompiler compiler;
29:
30: /** Creates a new data binding.
31: *
32: *@param source the Java source code for the data binding expression
33: *@param dest the data binding destination in the form <code><id>.<propertyName></code>
34: *@param assignment Java snippet which will cause the destination property to be updated with the current value of the binding
35: *@param compiler the current <code>JAXXCompiler</code>
36: */
37: public DataBinding(String source, String dest, String assignment,
38: JAXXCompiler compiler) {
39: this .id = dest;
40: this .dataSource = new DataSource(id, source, compiler);
41: this .dest = dest;
42: this .assignment = assignment;
43: this .compiler = compiler;
44: }
45:
46: public String getId() {
47: return id;
48: }
49:
50: /** Compiles the data binding expression. This method calls methods in <code>JAXXCompiler</code>
51: * to add the Java code that performs the data binding setup.
52: *
53: *@param quickNoDependencies true to optimize bindings with no dependencies by simply running them at startup time
54: *@return <code>true</code> if the expression has dependencies, <code>false</code> otherwise
55: *@throws CompilerException if a compilation error occurs
56: */
57: public boolean compile(boolean quickNoDependencies)
58: throws CompilerException {
59: // DataSource.compile handles all of the listener additions
60: boolean result = dataSource
61: .compile("new jaxx.runtime.DataBindingListener("
62: + compiler.getRootObject().getJavaCode() + ", "
63: + TypeManager.getJavaCode(id) + ")");
64:
65: if (!result && quickNoDependencies) {
66: if (!dest.endsWith(".layout")) // layout is specially handled early in the chain
67: compiler.initDataBindings.append(assignment
68: + JAXXCompiler.getLineSeparator());
69: return false; // no dependencies, just a static expression
70: } else {
71: if (compiler.processDataBinding.length() > 0)
72: compiler.processDataBinding.append("else ");
73: compiler.processDataBinding.append("if ($dest.equals("
74: + TypeManager.getJavaCode(id) + ")) { "
75: + assignment + "}\n"
76: + JAXXCompiler.getLineSeparator());
77: return true;
78: }
79: }
80: }
|