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: * JXControl.java
021: *
022: * Created on June 8, 2007, 8:39 PM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.lang.reflect.Constructor;
029: import java.lang.reflect.Field;
030: import java.lang.reflect.Method;
031:
032: import javax.swing.JOptionPane;
033:
034: import org.openjx.display.JXMessageBox;
035:
036: import org.openjx.core.JXObject;
037: import org.openjx.core.JXTranslator;
038: import org.openjx.core.VirtualMachine;
039:
040: /**
041: * This class is an abstract class representing the methods which must be
042: * defined by a component or plugin component.
043: *
044: * @author Jared Spigner
045: */
046: public abstract class JXControl {
047:
048: /** This is a reference to the Virtual Machine. */
049: protected VirtualMachine virtualMachine;
050:
051: /** This is a reference to the JXTranslator. */
052: protected JXTranslator jxTranslator;
053:
054: /**
055: * This is the constructor for the JXControl class. It creates a new
056: * instance of JXControl.
057: *
058: * @param vm points to an instance of the Virtual Machine.
059: */
060: public JXControl(VirtualMachine vm) {
061: this .virtualMachine = vm;
062: this .jxTranslator = new JXTranslator();
063: }
064:
065: /**
066: * This method is called when the compiler wishes to compile the
067: * component into Java code.
068: *
069: * @param jxObject points to the JXObject in the stack we wish to compile
070: * into Java.
071: *
072: * @return true on success, else false on failure.
073: */
074: public abstract boolean Compile(JXObject jxObject);
075:
076: /**
077: * This is a generalized utility method which uses the Reflection API to
078: * instantiate a class type to an object.
079: *
080: * @param jxObject is the object we are trying to build for.
081: * @param className is the name of the class we are trying to instantiate.
082: * @param parameterTypes is the paramter types, this should always be an
083: * empty array since this particular method does not support constructors
084: * with arguments.
085: * @param objectTypes are the parameters.
086: *
087: * @return true on success, else false on failure.
088: */
089: public boolean instantiateObject(JXObject jxObject,
090: String className, Class[] parameterTypes,
091: Object[] objectTypes) {
092: Class objectClass;
093: Constructor objectConstructor;
094:
095: try {
096: objectClass = Class.forName(className);
097:
098: objectConstructor = objectClass
099: .getConstructor(parameterTypes);
100:
101: jxObject.setObjectLink(objectConstructor
102: .newInstance(objectTypes));
103: } catch (Exception e) {
104: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
105: "JXObject Instance Failure", "" + e,
106: this .virtualMachine.getViewer());
107: return false;
108: }
109:
110: return true;
111: }
112:
113: /**
114: * This method is called when the interpreter wishes to interpret the
115: * component's script into Java code.
116: *
117: * @param jxObject points to the JXObject in the stack we wish to interpret
118: * into Java.
119: *
120: * @return true on success, else false on failure.
121: */
122: public abstract boolean Interpret(JXObject jxObject);
123:
124: }
|