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: * jxbuttonCTL.java
021: *
022: * Created on June 8, 2007, 11:10 PM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.beans.PropertyChangeEvent;
029:
030: import javax.swing.JButton;
031:
032: import org.openjx.core.JXInterpreter;
033: import org.openjx.core.JXObject;
034: import org.openjx.core.VirtualMachine;
035:
036: /**
037: * This is the control class for the jxbutton otherwise known as the
038: * JButton.
039: *
040: * @author Jared Spigner
041: */
042: public class jxbuttonCTL extends JXControl {
043:
044: /**
045: * This is the constructor for the jxbuttonCTL class. It creates a
046: * new instance of jxbuttonCTL.
047: *
048: * @param vm points to an instance of the Virtual Machine.
049: */
050: public jxbuttonCTL(VirtualMachine vm) {
051: super (vm);
052: }
053:
054: /**
055: * This method is called when the compiler wishes to compile the
056: * component into Java code.
057: *
058: * @param jxObject points to the JXObject in the stack we wish to compile
059: * into Java.
060: *
061: * @return true on success, else false on failure.
062: */
063: public boolean Compile(JXObject jxObject) {
064: if (!this .instantiateObject(jxObject, "javax.swing.JButton",
065: new Class[] {}, new Object[] {}))
066: return false;
067:
068: JButton jButton = (JButton) jxObject.getObjectLink();
069:
070: jxcomponentCTL jxcomponent = new jxcomponentCTL(
071: this .virtualMachine);
072: if (!jxcomponent.Compile(jxObject))
073: return false;
074:
075: jButton.setText(jxObject.getProperty("text"));
076:
077: final JXObject bindObject = jxObject;
078: final JButton bindComponent = jButton;
079:
080: /** bind JXObject properties to this JButton. */
081: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
082: public void propertyChange(PropertyChangeEvent evt) {
083: if (evt.getPropertyName().equals("text")) {
084: bindComponent.setText((String) evt.getNewValue());
085: }
086: }
087: });
088:
089: /** bind JButton properties to this JXObject. */
090: jButton.addPropertyChangeListener(new JXPropertyAdapter() {
091: public void propertyChange(PropertyChangeEvent evt) {
092: if (evt.getPropertyName().equals("text")) {
093: bindObject.setProperty("text", (String) evt
094: .getNewValue());
095: }
096: }
097: });
098:
099: this .virtualMachine.getJXCompiler().scriptEngine.put(jButton
100: .getName(), jButton);
101:
102: return true;
103: }
104:
105: /**
106: * This method is called when the interpreter wishes to interpret the
107: * component's script into Java code.
108: *
109: * @return true on success, else false on failure.
110: */
111: public boolean Interpret(JXObject jxObject) {
112: jxcomponentCTL jxcomponent = new jxcomponentCTL(
113: this .virtualMachine);
114: if (!jxcomponent.Interpret(jxObject))
115: return false;
116:
117: String myScript;
118:
119: if (jxObject.getProperty("onClick") != null) {
120: myScript = jxObject.getName() + ".addActionListener("
121: + "function(event,methodName){"
122: + jxObject.getProperty("onClick") + "}" + ");";
123:
124: return this .virtualMachine.getJXInterpreter()
125: .executeScript(myScript);
126: }
127:
128: return true;
129: }
130:
131: }
|