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: * jxcheckboxCTL.java
021: *
022: * Created on October 24, 2007, 11:15 PM
023: */
024:
025: package org.openjx.jx;
026:
027: import java.awt.event.ActionEvent;
028:
029: import java.beans.PropertyChangeEvent;
030:
031: import javax.swing.JCheckBox;
032:
033: import org.openjx.core.JXObject;
034: import org.openjx.core.VirtualMachine;
035:
036: import org.openjx.display.JXMessageBox;
037:
038: /**
039: * This is the control class for the jxcheckbox otherwise known as the
040: * JCheckBox.
041: *
042: * @author Jared Spigner
043: */
044: public class jxcheckboxCTL extends JXControl {
045:
046: /**
047: * This is the constructor for the jxcheckboxCTL class. It creates a new
048: * instance of jxchecboxCTL.
049: *
050: * @param vm points to an instance of the Virtual Machine.
051: */
052: public jxcheckboxCTL(VirtualMachine vm) {
053: super (vm);
054: }
055:
056: /**
057: * This method is called when the compiler wishes to compile the
058: * component into Java code.
059: *
060: * @param jxObject points to the JXObject in the stack we wish to compile
061: * into Java.
062: *
063: * @return true on success, else false on failure.
064: */
065: public boolean Compile(JXObject jxObject) {
066: if (!this .instantiateObject(jxObject, "javax.swing.JCheckBox",
067: new Class[] {}, new Object[] {}))
068: return false;
069:
070: JCheckBox jCheckBox = (JCheckBox) jxObject.getObjectLink();
071:
072: jxcomponentCTL jxcomponent = new jxcomponentCTL(
073: this .virtualMachine);
074: if (!jxcomponent.Compile(jxObject))
075: return false;
076:
077: if (this .virtualMachine.isBound(jxObject.getProperty("value"))) {
078: JXMeta jxMeta = (JXMeta) this .virtualMachine
079: .getBound(jxObject.getProperty("text"));
080: jCheckBox.setSelected(this .jxTranslator
081: .BooleanJX2Java(jxMeta.getMetaValue()));
082:
083: final JCheckBox bindCheckBox = (JCheckBox) jxObject
084: .getObjectLink();
085: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
086: public void propertyChange(PropertyChangeEvent evt) {
087: if (evt.getPropertyName().equals("value")) {
088: bindCheckBox
089: .setText((String) evt.getNewValue());
090: }
091: }
092: });
093: } else {
094: jCheckBox.setSelected(this .jxTranslator
095: .BooleanJX2Java(jxObject.getProperty("value")));
096: }
097:
098: if (this .virtualMachine.isBound(jxObject.getProperty("text"))) {
099: JXMeta jxMeta = (JXMeta) this .virtualMachine
100: .getBound(jxObject.getProperty("text"));
101: jCheckBox.setText(jxMeta.getMetaValue());
102:
103: final JCheckBox bindCheckBox = (JCheckBox) jxObject
104: .getObjectLink();
105: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
106: public void propertyChange(PropertyChangeEvent evt) {
107: if (evt.getPropertyName().equals("value")) {
108: bindCheckBox
109: .setText((String) evt.getNewValue());
110: }
111: }
112: });
113: } else {
114: jCheckBox.setText(jxObject.getProperty("text"));
115: }
116:
117: final JXObject bindObject = jxObject;
118: final JCheckBox bindComponent = jCheckBox;
119:
120: /** bind JXObject properties to this JCheckBox. */
121: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
122: public void propertyChange(PropertyChangeEvent evt) {
123: if (evt.getPropertyName().equals("text")) {
124: bindComponent.setText((String) evt.getNewValue());
125: } else if (evt.getPropertyName().equals("value")) {
126: bindComponent
127: .setSelected(this .jxTranslator
128: .BooleanJX2Java((String) evt
129: .getNewValue()));
130: }
131: }
132: });
133:
134: /** bind JCheckBox properties to this JXObject. */
135: jCheckBox.addPropertyChangeListener(new JXPropertyAdapter() {
136: public void propertyChange(PropertyChangeEvent evt) {
137: if (evt.getPropertyName().equals("CHECKtext")) {
138: bindObject.setProperty("text", (String) evt
139: .getNewValue());
140: }
141: }
142: });
143:
144: /** bind JCheckBox properties to this JXObject. */
145: jCheckBox.addActionListener(new JXActionAdapter() {
146: public void actionPerformed(ActionEvent evt) {
147: bindObject.setProperty("value", this .jxTranslator
148: .BooleanJava2JX(bindComponent.isSelected()));
149: }
150: });
151:
152: this .virtualMachine.getJXCompiler().scriptEngine.put(jCheckBox
153: .getName(), jCheckBox);
154:
155: return true;
156: }
157:
158: /**
159: * This method is called when the interpreter wishes to interpret the
160: * component's script into Java code.
161: *
162: * @param jxObject points to the JXObject in the stack we wish to interpret
163: * into Java.
164: *
165: * @return true on success, else false on failure.
166: */
167: public boolean Interpret(JXObject jxObject) {
168: jxcomponentCTL jxcomponent = new jxcomponentCTL(
169: this .virtualMachine);
170: if (!jxcomponent.Interpret(jxObject))
171: return false;
172:
173: String myScript;
174:
175: if (jxObject.getProperty("onChange") != null) {
176: myScript = jxObject.getName() + ".addItemListener("
177: + "function(event,methodName){"
178: + jxObject.getProperty("onChange") + "}" + ");";
179:
180: return this .virtualMachine.getJXInterpreter()
181: .executeScript(myScript);
182: }
183:
184: return true;
185: }
186:
187: }
|