From 60a02f3c390cb4e64c60e8f33a1ac290c049fb98 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 7 Jun 2002 17:34:37 +0000 Subject: [PATCH] Wired up the field editor to generate and receive attr changed events. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1429 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/admin/client/FieldEditor.java | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/src/java/com/threerings/admin/client/FieldEditor.java b/src/java/com/threerings/admin/client/FieldEditor.java index 7071dfd31..70416c82a 100644 --- a/src/java/com/threerings/admin/client/FieldEditor.java +++ b/src/java/com/threerings/admin/client/FieldEditor.java @@ -1,5 +1,5 @@ // -// $Id: FieldEditor.java,v 1.1 2002/06/07 06:22:24 mdb Exp $ +// $Id: FieldEditor.java,v 1.2 2002/06/07 17:34:37 mdb Exp $ package com.threerings.admin.client; @@ -12,14 +12,18 @@ import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; +import javax.swing.event.AncestorEvent; import com.samskivert.swing.HGroupLayout; +import com.samskivert.swing.event.AncestorAdapter; import com.threerings.presents.dobj.AttributeChangeListener; import com.threerings.presents.dobj.AttributeChangedEvent; import com.threerings.presents.dobj.DObject; import com.threerings.presents.util.PresentsContext; +import com.threerings.admin.Log; + /** * Used to display and edit a particular distributed object field. */ @@ -42,36 +46,67 @@ public class FieldEditor extends JPanel add(_label = new JLabel(_field.getName()), HGroupLayout.FIXED); // and a text entry field to display the field value - String value = ""; - try { - value = String.valueOf(_field.get(_object)); - } catch (IllegalAccessException iae) { - value = ""; - } - add(_value = new JTextField(value)); + add(_value = new JTextField()); _value.addActionListener(this); + + // listen while we're shown and not when we're not + addAncestorListener(new AncestorAdapter () { + public void ancestorAdded (AncestorEvent event) { + _object.addListener(FieldEditor.this); + readValue(); + } + public void ancestorRemoved (AncestorEvent event) { + _object.removeListener(FieldEditor.this); + } + }); } // documentation inherited from interface public void attributeChanged (AttributeChangedEvent event) { - _value.setText(String.valueOf(event.getValue())); - updateBorder(false); + readValue(); + } + + /** + * Reads the value from the distributed object field and updates the + * editor field appropriately. + */ + protected void readValue () + { + String value = ""; + try { + value = String.valueOf(_field.get(_object)); + updateBorder(false); + } catch (IllegalAccessException iae) { + value = ""; + updateBorder(true); + } + _value.setText(String.valueOf(value)); } // documentation inherited from interface public void actionPerformed (ActionEvent event) { + Object value = null; + + // parse the new value if (_field.getType().equals(Integer.class) || _field.getType().equals(Integer.TYPE)) { try { - Integer value = new Integer(_value.getText()); - AttributeChangedEvent ace = new AttributeChangedEvent( - _object.getOid(), _field.getName(), value); - _ctx.getDObjectManager().postEvent(ace); + value = new Integer(_value.getText()); } catch (NumberFormatException nfe) { updateBorder(true); } + + } else if (_field.getType().equals(String.class)) { + value = _value.getText(); + } + + // submit an attribute changed event with the new value + if (value != null) { + AttributeChangedEvent ace = new AttributeChangedEvent( + _object.getOid(), _field.getName(), value); + _ctx.getDObjectManager().postEvent(ace); } } @@ -91,7 +126,6 @@ public class FieldEditor extends JPanel protected PresentsContext _ctx; protected Field _field; protected DObject _object; - protected JLabel _label; protected JTextField _value; }