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: * jxlistCTL.java
021: *
022: * Created on October 13, 2007, 12:29 AM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.beans.PropertyChangeEvent;
029:
030: import javax.swing.JList;
031:
032: import org.openjx.core.JXObject;
033: import org.openjx.core.VirtualMachine;
034:
035: import org.openjx.display.JXMessageBox;
036:
037: /**
038: * This is the control class for the jxlist otherwise known as the
039: * JList.
040: *
041: * @author Jared Spigner
042: */
043: public class jxlistCTL extends JXControl {
044:
045: /**
046: * This is the constructor for the jxpasswordfieldCTL class. It creates a
047: * new instance of jxlistCTL.
048: *
049: * @param vm points to an instance of the Virtual Machine.
050: */
051: public jxlistCTL(VirtualMachine vm) {
052: super (vm);
053: }
054:
055: /**
056: * This method is called when the compiler wishes to compile the
057: * component into Java code.
058: *
059: * @param jxObject points to the JXObject in the stack we wish to compile
060: * into Java.
061: *
062: * @return true on success, else false on failure.
063: */
064: public boolean Compile(JXObject jxObject) {
065: if (!this .instantiateObject(jxObject, "javax.swing.JList",
066: new Class[] {}, new Object[] {}))
067: return false;
068:
069: JList jList = (JList) jxObject.getObjectLink();
070:
071: jxcomponentCTL jxcomponent = new jxcomponentCTL(
072: this .virtualMachine);
073: if (!jxcomponent.Compile(jxObject))
074: return false;
075:
076: jList.setSelectionMode(this .jxTranslator
077: .listSelectionModelJX2Java(jxObject
078: .getProperty("selectMode")));
079:
080: if (jxObject.getProperty("selectIndices") != null
081: && jxObject.getProperty("selectMode").equals("single"))
082: jList.setSelectedIndex(Integer.parseInt(jxObject
083: .getProperty("selectIndices")));
084: else if (jxObject.getProperty("selectIndices") != null
085: && jxObject.getProperty("selectMode")
086: .equals("multiple")) {
087: String str[] = jxObject.getProperty("selectIndices").split(
088: ",");
089: int tmp[] = new int[str.length];
090:
091: for (int i = 0; i < str.length; i++) {
092: tmp[i] = Integer.parseInt(str[i]);
093: }
094:
095: jList.setSelectedIndices(tmp);
096: }
097:
098: jList.setSelectionBackground(this .jxTranslator
099: .ColorJX2Java(jxObject.getProperty("selectBGColor")));
100: jList.setSelectionForeground(this .jxTranslator
101: .ColorJX2Java(jxObject.getProperty("selectFGColor")));
102:
103: if (jxObject.getProperty("layoutOrientation")
104: .equals("vertical"))
105: jList.setLayoutOrientation(JList.VERTICAL);
106: else if (jxObject.getProperty("layoutOrientation").equals(
107: "vertical_wrap"))
108: jList.setLayoutOrientation(JList.VERTICAL_WRAP);
109: else if (jxObject.getProperty("layoutOrientation").equals(
110: "horizontal_wrap"))
111: jList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
112:
113: if (this .virtualMachine.isBound(jxObject.getProperty("data"))) {
114: JXMeta jxMeta = (JXMeta) this .virtualMachine
115: .getBound(jxObject.getProperty("data"));
116: jList.setListData(jxMeta.getMetaValue().split(","));
117:
118: final JList bindList = (JList) jxObject.getObjectLink();
119: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
120: public void propertyChange(PropertyChangeEvent evt) {
121: if (evt.getPropertyName().equals("value")) {
122: bindList.setListData(((String) evt
123: .getNewValue()).split(","));
124: }
125: }
126: });
127: }
128:
129: final JXObject bindObject = jxObject;
130: final JList bindComponent = jList;
131:
132: /** bind JXObject properties to this JList. */
133: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
134: public void propertyChange(PropertyChangeEvent evt) {
135: if (evt.getPropertyName().equals("data")) {
136: bindComponent.setListData(((String) evt
137: .getNewValue()).split(","));
138: } else if (evt.getPropertyName().equals("selectMode")) {
139: bindComponent.setSelectionMode(this .jxTranslator
140: .listSelectionModelJX2Java((String) evt
141: .getNewValue()));
142: } else if (evt.getPropertyName()
143: .equals("selectIndices")) {
144: if (bindObject.getProperty("selectMode").equals(
145: "single"))
146: bindComponent
147: .setSelectedIndex(Integer
148: .parseInt(((String) evt
149: .getNewValue())));
150: else if (bindObject.getProperty("selectMode")
151: .equals("multiple")) {
152: String str[] = ((String) evt.getNewValue())
153: .split(",");
154: int tmp[] = new int[str.length];
155:
156: for (int i = 0; i < str.length; i++) {
157: tmp[i] = Integer.parseInt(str[i]);
158: }
159:
160: bindComponent.setSelectedIndices(tmp);
161: }
162: } else if (evt.getPropertyName()
163: .equals("selectBGColor")) {
164: bindComponent
165: .setSelectionBackground(this .jxTranslator
166: .ColorJX2Java((String) evt
167: .getNewValue()));
168: } else if (evt.getPropertyName()
169: .equals("selectFGColor")) {
170: bindComponent
171: .setSelectionForeground(this .jxTranslator
172: .ColorJX2Java((String) evt
173: .getNewValue()));
174: } else if (evt.getPropertyName().equals(
175: "layoutOrientation")) {
176: if (((String) evt.getNewValue()).equals("vertical"))
177: bindComponent
178: .setLayoutOrientation(JList.VERTICAL);
179: else if (((String) evt.getNewValue())
180: .equals("vertical_wrap"))
181: bindComponent
182: .setLayoutOrientation(JList.VERTICAL_WRAP);
183: else if (((String) evt.getNewValue())
184: .equals("horizontal_wrap"))
185: bindComponent
186: .setLayoutOrientation(JList.HORIZONTAL_WRAP);
187: }
188: }
189: });
190:
191: /** bind JList properties to this JXObject. */
192: jList.addPropertyChangeListener(new JXPropertyAdapter() {
193: public void propertyChange(PropertyChangeEvent evt) {
194: System.out.println(evt.getPropertyName());
195: if (evt.getPropertyName().equals("data")) {
196: bindObject.setProperty("data", (String) evt
197: .getNewValue());
198: } else if (evt.getPropertyName().equals(
199: "selectionBackground")) {
200: bindObject.setProperty("selectBGColor",
201: this .jxTranslator
202: .ColorJava2JX(bindComponent
203: .getSelectionBackground()));
204: } else if (evt.getPropertyName().equals(
205: "selectionForeground")) {
206: bindObject.setProperty("selectFGColor",
207: this .jxTranslator
208: .ColorJava2JX(bindComponent
209: .getSelectionForeground()));
210: }
211: }
212: });
213:
214: this .virtualMachine.getJXCompiler().scriptEngine.put(jList
215: .getName(), jList);
216:
217: return true;
218: }
219:
220: /**
221: * This method is called when the interpreter wishes to interpret the
222: * component's script into Java code.
223: *
224: * @param jxObject points to the JXObject in the stack we wish to interpret
225: * into Java.
226: *
227: * @return true on success, else false on failure.
228: */
229: public boolean Interpret(JXObject jxObject) {
230: jxcomponentCTL jxcomponent = new jxcomponentCTL(
231: this .virtualMachine);
232: if (!jxcomponent.Interpret(jxObject))
233: return false;
234:
235: String myScript;
236:
237: if (jxObject.getProperty("onSelect") != null) {
238: myScript = jxObject.getName()
239: + ".addListSelectionListener("
240: + "function(event,methodName){"
241: + jxObject.getProperty("onSelect") + "}" + ");";
242:
243: return this .virtualMachine.getJXInterpreter()
244: .executeScript(myScript);
245: }
246:
247: return true;
248: }
249:
250: }
|