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: * jxlabelCTL.java
021: *
022: * Created on June 8, 2007, 11:52 PM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.awt.Image;
029: import java.awt.Toolkit;
030:
031: import java.beans.PropertyChangeEvent;
032:
033: import javax.swing.ImageIcon;
034: import javax.swing.JLabel;
035: import javax.swing.JOptionPane;
036:
037: import org.openjx.core.JXObject;
038: import org.openjx.core.JXTranslator;
039: import org.openjx.core.VirtualMachine;
040:
041: import org.openjx.display.JXMessageBox;
042:
043: /**
044: * This is the control class for the jxlabel otherwise known as the
045: * JLabel.
046: *
047: * @author Jared Spigner
048: */
049: public class jxlabelCTL extends JXControl {
050:
051: /** This is a reference to the JXTranslator class. */
052: private JXTranslator jxTranslator;
053:
054: /**
055: * This is the constructor for the jxlabelCTL class. It creates a new
056: * instance of jxlabelCTL.
057: *
058: * @param vm points to an instance of the Virtual Machine.
059: */
060: public jxlabelCTL(VirtualMachine vm) {
061: super (vm);
062:
063: this .jxTranslator = new JXTranslator();
064: }
065:
066: /**
067: * This method is called when the compiler wishes to compile the
068: * component into Java code.
069: *
070: * @param jxObject points to the JXObject in the stack we wish to compile
071: * into Java.
072: *
073: * @return true on success, else false on failure.
074: */
075: public boolean Compile(JXObject jxObject) {
076: if (!this .instantiateObject(jxObject, "javax.swing.JLabel",
077: new Class[] {}, new Object[] {}))
078: return false;
079:
080: JLabel jLabel = (JLabel) jxObject.getObjectLink();
081:
082: jxcomponentCTL jxcomponent = new jxcomponentCTL(
083: this .virtualMachine);
084: if (!jxcomponent.Compile(jxObject))
085: return false;
086:
087: if (this .virtualMachine.isBound(jxObject.getProperty("text"))) {
088: JXMeta jxMeta = (JXMeta) this .virtualMachine
089: .getBound(jxObject.getProperty("text"));
090: jLabel.setText(jxMeta.getMetaValue());
091:
092: final JLabel bindLabel = (JLabel) jxObject.getObjectLink();
093: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
094: public void propertyChange(PropertyChangeEvent evt) {
095: if (evt.getPropertyName().equals("value")) {
096: bindLabel.setText((String) evt.getNewValue());
097: }
098: }
099: });
100: } else {
101: jLabel.setText(jxObject.getProperty("text"));
102: }
103:
104: JXTranslator jxTranslator = new JXTranslator();
105:
106: jLabel.setHorizontalAlignment(jxTranslator
107: .HAlignJX2Java(jxObject.getProperty("align")));
108: jLabel.setVerticalAlignment(jxTranslator.VAlignJX2Java(jxObject
109: .getProperty("valign")));
110:
111: if (jxObject.getProperty("image") != null) {
112: // Check to see if the property is bindable starts with "@".
113: if (this .virtualMachine.isBound(jxObject
114: .getProperty("image"))) {
115: JXImage jxImage = (JXImage) this .virtualMachine
116: .getBound(jxObject.getProperty("image"));
117: jLabel.setIcon(jxImage.getImageIcon());
118:
119: final JLabel bindLabel = (JLabel) jxObject
120: .getObjectLink();
121: jxImage
122: .addPropertyChangeListener(new JXPropertyAdapter() {
123: public void propertyChange(
124: PropertyChangeEvent evt) {
125: bindLabel.setIcon((ImageIcon) evt
126: .getNewValue());
127: }
128: });
129: } else {
130: try {
131: Image myImage = Toolkit
132: .getDefaultToolkit()
133: .getImage(
134: this
135: .getClass()
136: .getClassLoader()
137: .getResource(
138: jxObject
139: .getProperty("image")));
140:
141: jLabel.setIcon(new ImageIcon(myImage));
142: } catch (Exception e) {
143: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
144: "Failed to Bind Label Image", e
145: .getMessage(), this .virtualMachine
146: .getViewer());
147: return false;
148: }
149: }
150: }
151:
152: final JXObject bindObject = jxObject;
153: final JLabel bindComponent = jLabel;
154:
155: /** bind JXObject properties to this JLabel. */
156: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
157: public void propertyChange(PropertyChangeEvent evt) {
158: if (evt.getPropertyName().equals("text")) {
159: bindComponent.setText((String) evt.getNewValue());
160: } else if (evt.getPropertyName().equals("align")) {
161: bindComponent
162: .setHorizontalAlignment(this .jxTranslator
163: .HAlignJX2Java(evt
164: .getPropertyName()));
165: } else if (evt.getPropertyName().equals("valign")) {
166: bindComponent
167: .setVerticalAlignment(this .jxTranslator
168: .VAlignJX2Java(evt
169: .getPropertyName()));
170: }
171: }
172: });
173:
174: /** bind JLabel properties to this JXObject. */
175: jLabel.addPropertyChangeListener(new JXPropertyAdapter() {
176: public void propertyChange(PropertyChangeEvent evt) {
177: if (evt.getPropertyName().equals("text")) {
178: bindObject.setProperty("text", (String) evt
179: .getNewValue());
180: } else if (evt.getPropertyName().equals(
181: "horizontalAlignment")) {
182: bindObject.setProperty("align", this .jxTranslator
183: .HAlignJava2JX(bindComponent
184: .getHorizontalAlignment()));
185: } else if (evt.getPropertyName().equals(
186: "verticalAlignment")) {
187: bindObject.setProperty("valign", this .jxTranslator
188: .VAlignJava2JX(bindComponent
189: .getVerticalAlignment()));
190: }
191: }
192: });
193:
194: this .virtualMachine.getJXCompiler().scriptEngine.put(jLabel
195: .getName(), jLabel);
196:
197: return true;
198: }
199:
200: /**
201: * This method is called when the interpreter wishes to interpret the
202: * component's script into Java code.
203: *
204: * @param jxObject points to the JXObject in the stack we wish to interpret
205: * into Java.
206: *
207: * @return true on success, else false on failure.
208: */
209: public boolean Interpret(JXObject jxObject) {
210: jxcomponentCTL jxcomponent = new jxcomponentCTL(
211: this .virtualMachine);
212: if (!jxcomponent.Interpret(jxObject))
213: return false;
214:
215: return true;
216: }
217:
218: }
|