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: * jxcomboboxCTL.java
021: *
022: * Created on October 25, 2007, 12:02 AM
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.JComboBox;
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 jxcombobox otherwise known as the
040: * JComboBox.
041: *
042: * @author Jared Spigner
043: */
044: public class jxcomboboxCTL extends JXControl {
045:
046: /**
047: * This is the constructor for the jxcomboboxCTL class. It creates a new
048: * instance of jxchecboxCTL.
049: *
050: * @param vm points to an instance of the Virtual Machine.
051: */
052: public jxcomboboxCTL(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.JComboBox",
067: new Class[] {}, new Object[] {}))
068: return false;
069:
070: JComboBox jComboBox = (JComboBox) 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
078: .getProperty("selectIndex"))) {
079: JXMeta jxMeta = (JXMeta) this .virtualMachine
080: .getBound(jxObject.getProperty("selectIndex"));
081:
082: jComboBox.setSelectedIndex(Integer.parseInt(jxMeta
083: .getMetaValue()));
084:
085: final JComboBox bindComboBox = (JComboBox) jxObject
086: .getObjectLink();
087: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
088: public void propertyChange(PropertyChangeEvent evt) {
089: if (evt.getPropertyName().equals("value")) {
090: bindComboBox.setSelectedIndex(Integer
091: .parseInt((String) evt.getNewValue()));
092: }
093: }
094: });
095: } else {
096: jComboBox.setSelectedIndex(Integer.parseInt(jxObject
097: .getProperty("selectIndex")));
098: }
099:
100: if (this .virtualMachine.isBound(jxObject.getProperty("data"))) {
101: JXMeta jxMeta = (JXMeta) this .virtualMachine
102: .getBound(jxObject.getProperty("data"));
103: jComboBox.removeAllItems();
104: String str[] = jxMeta.getMetaValue().split(",");
105: for (int i = 0; i < str.length; i++)
106: jComboBox.addItem(str[i]);
107:
108: final JComboBox bindComboBox = (JComboBox) jxObject
109: .getObjectLink();
110: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
111: public void propertyChange(PropertyChangeEvent evt) {
112: if (evt.getPropertyName().equals("value")) {
113: String str[] = ((String) evt.getNewValue())
114: .split(",");
115: for (int i = 0; i < str.length; i++)
116: bindComboBox.addItem(str[i]);
117: }
118: }
119: });
120: } else {
121: String str[] = jxObject.getProperty("data").split(",");
122: for (int i = 0; i < str.length; i++)
123: jComboBox.addItem(str[i]);
124: }
125:
126: final JXObject bindObject = jxObject;
127: final JComboBox bindComponent = jComboBox;
128:
129: /** bind JXObject properties to this JComboBox. */
130: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
131: public void propertyChange(PropertyChangeEvent evt) {
132: if (evt.getPropertyName().equals("data")) {
133: bindComponent.removeAllItems();
134: String str[] = ((String) evt.getNewValue())
135: .split(",");
136: for (int i = 0; i < str.length; i++)
137: bindComponent.addItem(str[i]);
138: }
139: }
140: });
141:
142: /** bind JComboBox properties to this JXObject. */
143: jComboBox.addPropertyChangeListener(new JXPropertyAdapter() {
144: public void propertyChange(PropertyChangeEvent evt) {
145: }
146: });
147:
148: /** bind JComboBox properties to this JXObject. */
149: jComboBox.addActionListener(new JXActionAdapter() {
150: public void actionPerformed(ActionEvent evt) {
151: //bindObject.setProperty("value",this.jxTranslator.BooleanJava2JX(bindComponent.isSelected()));
152: }
153: });
154:
155: this .virtualMachine.getJXCompiler().scriptEngine.put(jComboBox
156: .getName(), jComboBox);
157:
158: return true;
159: }
160:
161: /**
162: * This method is called when the interpreter wishes to interpret the
163: * component's script into Java code.
164: *
165: * @param jxObject points to the JXObject in the stack we wish to interpret
166: * into Java.
167: *
168: * @return true on success, else false on failure.
169: */
170: public boolean Interpret(JXObject jxObject) {
171: jxcomponentCTL jxcomponent = new jxcomponentCTL(
172: this .virtualMachine);
173: if (!jxcomponent.Interpret(jxObject))
174: return false;
175:
176: String myScript;
177:
178: if (jxObject.getProperty("onClick") != null) {
179: myScript = jxObject.getName() + ".addActionListener("
180: + "function(event,methodName){"
181: + jxObject.getProperty("onClick") + "}" + ");";
182:
183: return this .virtualMachine.getJXInterpreter()
184: .executeScript(myScript);
185: }
186:
187: return true;
188: }
189:
190: }
|