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: * jxcomponentCTL.java
021: *
022: * Created on June 8, 2007, 8:47 PM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.awt.Color;
029: import java.awt.Font;
030:
031: import java.awt.event.ComponentEvent;
032: import java.awt.event.ComponentAdapter;
033:
034: import java.beans.PropertyChangeEvent;
035:
036: import javax.swing.border.Border;
037: import javax.swing.border.EtchedBorder;
038: import javax.swing.border.TitledBorder;
039: import javax.swing.BorderFactory;
040:
041: import javax.swing.JComponent;
042:
043: import org.openjx.core.JXObject;
044: import org.openjx.core.VirtualMachine;
045:
046: /**
047: * This is the control class for the jxcomponent otherwise known as the
048: * JComponent.
049: *
050: * @author Jared Spigner
051: */
052: public class jxcomponentCTL extends JXControl {
053:
054: /**
055: * This is the constructor for the jxcomponentCTL class. It creates a new
056: * instance of jxcomponentCTL.
057: *
058: * @param vm points to an instance of the Virtual Machine.
059: */
060: public jxcomponentCTL(VirtualMachine vm) {
061: super (vm);
062: }
063:
064: /**
065: * This method is called when the compiler wishes to compile the
066: * component into Java code.
067: *
068: * @param jxObject points to the JXObject in the stack we wish to compile
069: * into Java.
070: *
071: * @return true on success, else false on failure.
072: */
073: public boolean Compile(JXObject jxObject) {
074: if (jxObject.getObjectLink() == null) {
075: if (!this .instantiateObject(jxObject,
076: "javax.swing.JComponent", new Class[] {},
077: new Object[] {}))
078: return false;
079: }
080:
081: JComponent jComponent = (JComponent) jxObject.getObjectLink();
082:
083: jComponent.setName(jxObject.getProperty("name"));
084: jComponent.setBackground(this .jxTranslator
085: .ColorJX2Java(jxObject.getProperty("bgcolor")));
086: jComponent.setForeground(this .jxTranslator
087: .ColorJX2Java(jxObject.getProperty("fgcolor")));
088: jComponent.setSize(Integer.parseInt(jxObject
089: .getProperty("width")), Integer.parseInt(jxObject
090: .getProperty("height")));
091: jComponent.setLocation(Integer.parseInt(jxObject
092: .getProperty("x")), Integer.parseInt(jxObject
093: .getProperty("y")));
094: jComponent.setFont(new Font(jxObject.getProperty("fontFamily"),
095: this .jxTranslator.FontStyleJX2Java(jxObject
096: .getProperty("fontWeight")), Integer
097: .parseInt(jxObject.getProperty("fontSize"))));
098:
099: if (!jxObject.getProperty("border").equals("none")) {
100: Color borderColor = this .jxTranslator.ColorJX2Java(jxObject
101: .getProperty("borderColor"));
102: String borderTitle = jxObject.getProperty("borderTitle");
103: String borderFontFamily = jxObject
104: .getProperty("borderTitleFontFamily");
105: int borderFontSize = Integer.parseInt(jxObject
106: .getProperty("borderTitleFontSize"));
107: int borderFontWeight = this .jxTranslator
108: .FontStyleJX2Java(jxObject
109: .getProperty("borderTitleFontWeight"));
110: int borderTitleAlign = this .jxTranslator
111: .BorderTitleHAlignJX2Java(jxObject
112: .getProperty("borderTitleAlign"));
113: int borderTitleVAlign = this .jxTranslator
114: .BorderTitleVAlignJX2Java(jxObject
115: .getProperty("borderTitleVAlign"));
116:
117: if (jxObject.getProperty("border").equals("line"))
118: jComponent.setBorder(BorderFactory
119: .createLineBorder(borderColor));
120: else if (jxObject.getProperty("border").equals("raised"))
121: jComponent.setBorder(BorderFactory
122: .createEtchedBorder(EtchedBorder.RAISED));
123: else if (jxObject.getProperty("border").equals("lowered"))
124: jComponent.setBorder(BorderFactory
125: .createEtchedBorder(EtchedBorder.LOWERED));
126: else if (jxObject.getProperty("border").equals("titled")) {
127: jComponent.setBorder(BorderFactory.createTitledBorder(
128: BorderFactory.createLineBorder(borderColor),
129: borderTitle, borderTitleAlign,
130: borderTitleVAlign, new Font(borderFontFamily,
131: borderFontWeight, borderFontSize)));
132: } else if (jxObject.getProperty("border").equals(
133: "raised-lowered")) {
134: jComponent
135: .setBorder(BorderFactory
136: .createCompoundBorder(
137: BorderFactory
138: .createEtchedBorder(EtchedBorder.RAISED),
139: BorderFactory
140: .createEtchedBorder(EtchedBorder.LOWERED)));
141: }
142: }
143:
144: final JXObject bindObject = jxObject;
145: final JComponent bindComponent = jComponent;
146:
147: /** bind JXObject properties to this JComponent. */
148: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
149: public void propertyChange(PropertyChangeEvent evt) {
150: if (evt.getPropertyName().equals("name")) {
151: bindComponent.setName((String) evt.getNewValue());
152: } else if (evt.getPropertyName().equals("bgcolor")) {
153: bindComponent.setBackground(this .jxTranslator
154: .ColorJX2Java((String) evt.getNewValue()));
155: } else if (evt.getPropertyName().equals("fgcolor")) {
156: bindComponent.setForeground(this .jxTranslator
157: .ColorJX2Java((String) evt.getNewValue()));
158: } else if (evt.getPropertyName().equals("fontFamily")) {
159: bindComponent.setFont(new Font((String) evt
160: .getNewValue(), bindComponent.getFont()
161: .getStyle(), bindComponent.getFont()
162: .getSize()));
163: } else if (evt.getPropertyName().equals("fontWeight")) {
164: bindComponent.setFont(new Font(bindComponent
165: .getFont().getFamily(), this .jxTranslator
166: .FontStyleJX2Java(((String) evt
167: .getNewValue())), bindComponent
168: .getFont().getSize()));
169: } else if (evt.getPropertyName().equals("fontSize")) {
170: bindComponent.setFont(new Font(bindComponent
171: .getFont().getFamily(), bindComponent
172: .getFont().getStyle(), Integer
173: .parseInt(((String) evt.getNewValue()))));
174: }
175: }
176: });
177:
178: /** bind JComponent properties to this JXObject. */
179: jComponent.addPropertyChangeListener(new JXPropertyAdapter() {
180: public void propertyChange(PropertyChangeEvent evt) {
181: if (evt.getPropertyName().equals("background")) {
182: bindObject.setProperty("background",
183: this .jxTranslator
184: .ColorJava2JX(bindComponent
185: .getBackground()));
186: } else if (evt.getPropertyName().equals("foreground")) {
187: bindObject.setProperty("background",
188: this .jxTranslator
189: .ColorJava2JX(bindComponent
190: .getForeground()));
191: } else if (evt.getPropertyName().equals("font")) {
192: bindObject.setProperty("fontFamily", bindComponent
193: .getFont().getFamily());
194: bindObject.setProperty("fontSize", ""
195: + bindComponent.getFont().getSize());
196: bindObject.setProperty("fontWeight",
197: this .jxTranslator
198: .FontStyleJava2JX(bindComponent
199: .getFont()));
200: } else if (evt.getPropertyName().equals("name")) {
201: bindObject.setProperty("name", bindComponent
202: .getName());
203: }
204: }
205: });
206:
207: jComponent.addComponentListener(new ComponentAdapter() {
208: public void componentMoved(ComponentEvent e) {
209: bindObject.setProperty("x", String
210: .valueOf(bindComponent.getX()));
211: bindObject.setProperty("y", String
212: .valueOf(bindComponent.getY()));
213: }
214:
215: public void componentResized(ComponentEvent e) {
216: bindObject.setProperty("width", String
217: .valueOf(bindComponent.getWidth()));
218: bindObject.setProperty("height", String
219: .valueOf(bindComponent.getHeight()));
220: }
221:
222: public void componentShown(ComponentEvent e) {
223: System.out.println("Shown");
224: }
225:
226: public void componentHidden(ComponentEvent e) {
227: System.out.println("Hidden");
228: }
229: });
230:
231: this .virtualMachine.getJXCompiler().scriptEngine.put(jComponent
232: .getName(), jComponent);
233:
234: return true;
235: }
236:
237: /**
238: * This method is called when the interpreter wishes to interpret the
239: * component's script into Java code.
240: *
241: * @param jxObject points to the JXObject in the stack we wish to interpret
242: * into Java.
243: *
244: * @return true on success, else false on failure.
245: */
246: public boolean Interpret(JXObject jxObject) {
247: return true;
248: }
249:
250: }
|