001: /*
002: * Copyright (C) 2007 Jared Alexander Spigner
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * jspigner@openjx.org
019: *
020: * jxtextfieldCTL.java
021: *
022: * Created on June 11, 2007, 10:35 PM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.beans.PropertyChangeEvent;
029:
030: import javax.swing.event.DocumentEvent;
031: import javax.swing.event.DocumentListener;
032: import javax.swing.JTextField;
033: import javax.swing.text.BadLocationException;
034:
035: import org.openjx.core.JXObject;
036: import org.openjx.core.VirtualMachine;
037:
038: import org.openjx.display.JXMessageBox;
039:
040: /**
041: * This is the control class for the jxtextfield otherwise known as the
042: * JTextField.
043: *
044: * @author Jared Spigner
045: */
046: public class jxtextfieldCTL extends JXControl {
047:
048: /**
049: * This is the constructor for the jxtextfieldCTL class. It creates a new
050: * instance of jxtextfieldCTL.
051: *
052: * @param vm points to an instance of the Virtual Machine.
053: */
054: public jxtextfieldCTL(VirtualMachine vm) {
055: super (vm);
056: }
057:
058: /**
059: * This method is called when the compiler wishes to compile the
060: * component into Java code.
061: *
062: * @param jxObject points to the JXObject in the stack we wish to compile
063: * into Java.
064: *
065: * @return true on success, else false on failure.
066: */
067: public boolean Compile(JXObject jxObject) {
068: if (!this .instantiateObject(jxObject, "javax.swing.JTextField",
069: new Class[] {}, new Object[] {}))
070: return false;
071:
072: JTextField jTextField = (JTextField) jxObject.getObjectLink();
073:
074: jxcomponentCTL jxcomponent = new jxcomponentCTL(
075: this .virtualMachine);
076: if (!jxcomponent.Compile(jxObject))
077: return false;
078:
079: if (this .virtualMachine.isBound(jxObject.getProperty("text"))) {
080: JXMeta jxMeta = (JXMeta) this .virtualMachine
081: .getBound(jxObject.getProperty("text"));
082: jTextField.setText(jxMeta.getMetaValue());
083:
084: final JTextField bindTextField = (JTextField) jxObject
085: .getObjectLink();
086: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
087: public void propertyChange(PropertyChangeEvent evt) {
088: if (evt.getPropertyName().equals("value")) {
089: bindTextField.setText((String) evt
090: .getNewValue());
091: }
092: }
093: });
094: } else {
095: jTextField.setText(jxObject.getProperty("text"));
096: }
097:
098: final JXObject bindObject = jxObject;
099: final JTextField bindComponent = jTextField;
100:
101: /** bind JXObject properties to this JTextField. */
102: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
103: public void propertyChange(PropertyChangeEvent evt) {
104: if (evt.getPropertyName().equals("text")) {
105: bindComponent.setText((String) evt.getNewValue());
106: }
107: }
108: });
109:
110: /** bind JTextField properties to this JXObject. */
111: jTextField.addPropertyChangeListener(new JXPropertyAdapter() {
112: public void propertyChange(PropertyChangeEvent evt) {
113: }
114: });
115:
116: /** bind the JTextField document text to the JXObject. */
117: jTextField.getDocument().addDocumentListener(
118: new JXDocumentAdapter() {
119: public void removeUpdate(DocumentEvent evt) {
120: update();
121: }
122:
123: public void insertUpdate(DocumentEvent evt) {
124: update();
125: }
126:
127: public void update() {
128: update(false);
129: }
130:
131: public void update(boolean value) {
132: try {
133: bindComponent.getDocument().getText(
134: 0,
135: bindComponent.getDocument()
136: .getLength());
137: } catch (BadLocationException e) {
138: System.out.println("" + e);
139: }
140:
141: }
142: });
143:
144: this .virtualMachine.getJXCompiler().scriptEngine.put(jTextField
145: .getName(), jTextField);
146:
147: return true;
148: }
149:
150: /**
151: * This method is called when the interpreter wishes to interpret the
152: * component's script into Java code.
153: *
154: * @param jxObject points to the JXObject in the stack we wish to interpret
155: * into Java.
156: *
157: * @return true on success, else false on failure.
158: */
159: public boolean Interpret(JXObject jxObject) {
160: jxcomponentCTL jxcomponent = new jxcomponentCTL(
161: this .virtualMachine);
162: if (!jxcomponent.Interpret(jxObject))
163: return false;
164:
165: return true;
166: }
167:
168: }
|