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: * jxpasswordfieldCTL.java
021: *
022: * Created on October 12, 2007, 12:00 AM
023: *
024: */
025:
026: package org.openjx.jx;
027:
028: import java.beans.PropertyChangeEvent;
029:
030: import javax.swing.event.DocumentEvent;
031: import javax.swing.event.DocumentListener;
032: import javax.swing.JPasswordField;
033: import javax.swing.text.BadLocationException;
034:
035: import org.openjx.core.JXObject;
036: import org.openjx.core.VirtualMachine;
037:
038: import org.openjx.display.JXMessageBox;
039:
040: /**
041: * This is the control class for the jxpasswordfield otherwise known as the
042: * JPasswordField.
043: * @author Jared Spigner
044: */
045: public class jxpasswordfieldCTL extends JXControl {
046:
047: /**
048: * This is the constructor for the jxpasswordfieldCTL class. It creates a new
049: * instance of jxpasswordfieldCTL.
050: *
051: * @param vm points to an instance of the Virtual Machine.
052: */
053: public jxpasswordfieldCTL(VirtualMachine vm) {
054: super (vm);
055: }
056:
057: /**
058: * This method is called when the compiler wishes to compile the
059: * component into Java code.
060: *
061: * @param jxObject points to the JXObject in the stack we wish to compile
062: * into Java.
063: *
064: * @return true on success, else false on failure.
065: */
066: public boolean Compile(JXObject jxObject) {
067: if (!this .instantiateObject(jxObject,
068: "javax.swing.JPasswordField", new Class[] {},
069: new Object[] {}))
070: return false;
071:
072: JPasswordField jPasswordField = (JPasswordField) jxObject
073: .getObjectLink();
074:
075: jxcomponentCTL jxcomponent = new jxcomponentCTL(
076: this .virtualMachine);
077: if (!jxcomponent.Compile(jxObject))
078: return false;
079:
080: if (this .virtualMachine.isBound(jxObject.getProperty("text"))) {
081: JXMeta jxMeta = (JXMeta) this .virtualMachine
082: .getBound(jxObject.getProperty("text"));
083: jPasswordField.setText(jxMeta.getMetaValue());
084:
085: final JPasswordField bindPasswordField = (JPasswordField) jxObject
086: .getObjectLink();
087: jxMeta.addPropertyChangeListener(new JXPropertyAdapter() {
088: public void propertyChange(PropertyChangeEvent evt) {
089: if (evt.getPropertyName().equals("value")) {
090: bindPasswordField.setText((String) evt
091: .getNewValue());
092: }
093: }
094: });
095: } else {
096: jPasswordField.setText(jxObject.getProperty("text"));
097: }
098:
099: final JXObject bindObject = jxObject;
100: final JPasswordField bindComponent = jPasswordField;
101:
102: /** bind JXObject properties to this JPasswordField. */
103: jxObject.addPropertyChangeListener(new JXPropertyAdapter() {
104: public void propertyChange(PropertyChangeEvent evt) {
105: if (evt.getPropertyName().equals("text")) {
106: bindComponent.setText((String) evt.getNewValue());
107: } else if (evt.getPropertyName().equals("echocar")) {
108: bindComponent.setEchoChar(((String) evt
109: .getNewValue()).charAt(0));
110: }
111: }
112: });
113:
114: /** bind JPasswordField properties to this JXObject. */
115: jPasswordField
116: .addPropertyChangeListener(new JXPropertyAdapter() {
117: public void propertyChange(PropertyChangeEvent evt) {
118: }
119: });
120:
121: /** bind the JPasswordField document text to the JXObject. */
122: jPasswordField.getDocument().addDocumentListener(
123: new JXDocumentAdapter() {
124: public void insertUpdate(DocumentEvent evt) {
125: update();
126: }
127:
128: public void removeUpdate(DocumentEvent evt) {
129: update();
130: }
131:
132: public void update() {
133: update(false);
134: }
135:
136: public void update(boolean value) {
137: try {
138: bindComponent.getDocument().getText(
139: 0,
140: bindComponent.getDocument()
141: .getLength());
142: } catch (BadLocationException e) {
143: System.out.println("" + e);
144: }
145:
146: }
147: });
148:
149: this .virtualMachine.getJXCompiler().scriptEngine.put(
150: jPasswordField.getName(), jPasswordField);
151:
152: return true;
153: }
154:
155: /**
156: * This method is called when the interpreter wishes to interpret the
157: * component's script into Java code.
158: *
159: * @param jxObject points to the JXObject in the stack we wish to interpret
160: * into Java.
161: *
162: * @return true on success, else false on failure.
163: */
164: public boolean Interpret(JXObject jxObject) {
165: jxcomponentCTL jxcomponent = new jxcomponentCTL(
166: this .virtualMachine);
167: if (!jxcomponent.Interpret(jxObject))
168: return false;
169:
170: return true;
171: }
172:
173: }
|