001: // DoubleAttributeEditor.java
002: // $Id: DoubleAttributeEditor.java,v 1.7 2000/08/16 21:37:27 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadm.editors;
007:
008: import java.awt.Component;
009: import java.awt.TextComponent;
010: import java.awt.TextField;
011:
012: import java.awt.event.InputEvent;
013: import java.awt.event.KeyAdapter;
014: import java.awt.event.KeyEvent;
015:
016: import java.util.Properties;
017:
018: import org.w3c.tools.resources.Attribute;
019:
020: import org.w3c.jigsaw.admin.RemoteAccessException;
021: import org.w3c.jigsaw.admin.RemoteResource;
022:
023: import org.w3c.jigadm.RemoteResourceWrapper;
024:
025: public class DoubleAttributeEditor extends AttributeEditor {
026:
027: class DoubleListener extends KeyAdapter {
028:
029: // filter the non-numeric char
030: public void keyPressed(KeyEvent ke) {
031: if (ke.getKeyCode() == KeyEvent.VK_DELETE
032: || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE
033: || ke.getKeyCode() == KeyEvent.VK_UP
034: || ke.getKeyCode() == KeyEvent.VK_DOWN
035: || ke.getKeyCode() == KeyEvent.VK_LEFT
036: || ke.getKeyCode() == KeyEvent.VK_RIGHT)
037: return;
038: if (!(ke.getKeyChar() >= '0' && ke.getKeyChar() <= '9')
039: && (ke.getKeyChar() != '.')
040: && ke.getKeyChar() != ',') {
041: ke.consume();
042: }
043: }
044: }
045:
046: private String origs;
047: TextField widget;
048:
049: /**
050: * Tells if the edited value has changed
051: * @return true if the value changed.
052: */
053:
054: public boolean hasChanged() {
055: return !origs.equals(widget.getText());
056: }
057:
058: /**
059: * set the current value to be the original value, ie: changed
060: * must return <strong>false</strong> after a reset.
061: */
062:
063: public void clearChanged() {
064: origs = widget.getText();
065: }
066:
067: /**
068: * reset the changes (if any)
069: */
070:
071: public void resetChanges() {
072: widget.setText(origs);
073: }
074:
075: /**
076: * Get the current value of the edited value
077: * @return an object or <strong>null</strong> if the object was not
078: * initialized
079: */
080:
081: public Object getValue() {
082: return new Double(widget.getText());
083: }
084:
085: /**
086: * Set the value of the edited value
087: * @param o the new value.
088: */
089:
090: public void setValue(Object o) {
091: widget.setText(o.toString());
092: }
093:
094: /**
095: * get the Component created by the editor.
096: * @return a Component
097: */
098:
099: public Component getComponent() {
100: return widget;
101: }
102:
103: /**
104: * Initialize the editor
105: * @param w the ResourceWrapper father of the attribute
106: * @param a the Attribute we are editing
107: * @param o the value of the above attribute
108: * @param p some Properties, used to fine-tune the editor
109: * @exception RemoteAccessException if a remote access error occurs.
110: */
111:
112: public void initialize(RemoteResourceWrapper w, Attribute a,
113: Object o, Properties p) throws RemoteAccessException {
114: RemoteResource r = w.getResource();
115: if (o == null) {
116: String v = null;
117: // FIXME
118: v = (String) r.getValue(a.getName());
119:
120: if (v == null)
121: if (a.getDefault() != null)
122: v = a.getDefault().toString();
123: if (v != null) {
124: origs = v;
125: widget.setText(origs);
126: }
127: } else {
128: origs = o.toString();
129: }
130: widget.setText(origs);
131: }
132:
133: public DoubleAttributeEditor() {
134: widget = new TextField();
135: widget.addKeyListener(new DoubleListener());
136: origs = "";
137: }
138: }
|