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: * jximageCTL.java
021: *
022: * Created on June 9, 2007, 11:03 PM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.beans.PropertyChangeEvent;
029:
030: import java.io.StringReader;
031:
032: import javax.swing.ImageIcon;
033:
034: import org.openjx.core.JXObject;
035: import org.openjx.core.VirtualMachine;
036:
037: import org.openjx.util.Base64;
038:
039: /**
040: * This is the control class for the jximage otherwise known as the
041: * JXImage.
042: * @author Jared Spigner
043: */
044: public class jximageCTL extends JXControl {
045:
046: /**
047: * This is the constructor for the jximageCTL class. It creates a
048: * new instance of jximageCTL.
049: *
050: * @param vm points to an instance of the Virtual Machine.
051: */
052: public jximageCTL(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: Base64 decodedImage;
067: StringReader stringReader;
068:
069: if (!this .instantiateObject(jxObject, "org.openjx.jx.JXImage",
070: new Class[] {}, new Object[] {}))
071: return false;
072:
073: JXImage jxImage = (JXImage) jxObject.getObjectLink();
074:
075: jxImage.setName(jxObject.getName());
076:
077: decodedImage = new Base64();
078: stringReader = new StringReader(jxObject.getProperty("cdata"));
079:
080: jxImage.setImage(new ImageIcon(decodedImage
081: .decodeBase64(stringReader)));
082:
083: final JXObject bindObject = jxObject;
084: final JXImage bindComponent = jxImage;
085:
086: /** bind JXObject properties to this JXImage. */
087: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
088: public void propertyChange(PropertyChangeEvent evt) {
089: if (evt.getPropertyName().equals("cdata")) {
090: Base64 decodedImage = new Base64();
091: StringReader stringReader = new StringReader(
092: (String) evt.getNewValue());
093: bindComponent.setImage(new ImageIcon(decodedImage
094: .decodeBase64(stringReader)));
095: } else if (evt.getPropertyName().equals("name")) {
096: bindComponent.setName(evt.getPropertyName());
097: }
098: }
099: });
100:
101: /** bind JXImage properties to this JXObject. */
102: jxImage.addPropertyChangeListener(new JXPropertyAdapter() {
103: public void propertyChange(PropertyChangeEvent evt) {
104: if (evt.getPropertyName().equals("value")) {
105: //Base64 encodedImage = new Base64();
106: //StringReader stringReader = new StringReader(
107: //bindObject.setProperty("cdata",encodedImage.encodeBase64((String)evt.getNewValue());
108: } else if (evt.getPropertyName().equals("name")) {
109: bindObject.setProperty("name", (String) evt
110: .getNewValue());
111: }
112: }
113: });
114:
115: this .virtualMachine.getJXCompiler().scriptEngine.put(jxImage
116: .getName(), jxImage);
117:
118: return true;
119: }
120:
121: /**
122: * This method is called when the interpreter wishes to interpret the
123: * component's script into Java code.
124: *
125: * @param jxObject points to the JXObject in the stack we wish to interpret
126: * into Java.
127: *
128: * @return true on success, else false on failure.
129: */
130: public boolean Interpret(JXObject jxObject) {
131: return true;
132: }
133:
134: }
|