views:

42

answers:

1

All,

I'm trying to make the properties of my node have Units associated with the measure. ( I am using the JScience.org implementation of JSR 275) So for instance,

public class Robot extends AbstractNode {
    // in kg
    float vehicleMass;

    @Override
    public Sheet createSheet() {
        Sheet s = Sheet.createDefault();
        Sheet.Set set = s.createPropertiesSet();
        try {
            PropertySupport.Reflection vehicleMassField = new PropertySupport.Reflection(this, float.class, "vehicleMass");
            vehicleMassField.setValue("units", SI.KILOGRAMS);
            vehicleMassField.setName("vehicleMass");
            set.put(vehicleMassField);

            PropertyEditorManager.registerEditor(float.class, UnitInPlaceEditor.class);
        } catch (NoSuchMethodException ex) {
            Exceptions.printStackTrace(ex);
        }
        s.put(set);
        return s;
    }
}

I want my UnitInPlaceEditor to append the units to the end of the string representation of the number, and when the field is clicked (enters edit mode) for the units to disappear and just the number becomes selected for editing. I can make the units appear, but I cannot get the units to disappear when the field enters editing mode.

public class UnitsInplaceEditor extends PropertyEditorSupport implements ExPropertyEditor {

    private PropertyEnv pe;

    @Override
    public String getAsText() {
        // Append the unit by retrieving the stored value
    }

    @Override
    public void setAsText(String s) {
        // strip off the unit, parse out the number
    }

    public void attachEnv(PropertyEnv pe) {
        this.pe = pe;
    }
}

Here's a screenshot of the display - I like it like this.. alt text

but here's the value being edited; note the unit stays there. alt text

Basically I want one value (string) to be displayed in the field when the field is NOT being edited, and a different to be displayed when user starts editing the field. Barring that, I'd like to put a constant jlabel for the units (uneditable) to the right of the text field.

Anyone know how to do this?

A: 

The best solution I found was to change getAsText() to return not the value + the units, but just the value. Same with setAsText. That way I can use the default InplaceEditor and have the units disappear on that view.

In order to get the units to show up in the nonselected view, I had to override the isPaintable and paintValue methods as follows:

@Override
public boolean isPaintable() {
    return true;
}

/**
 * Draws the number and unit in the rectangular region of the screen given
 * to this PropertyEditor.
 * @param gfx
 * @param box
 */
@Override
public void paintValue(Graphics gfx, Rectangle box) {
    Color oldColor = gfx.getColor();
    gfx.setColor(isEditable() ? EDITABLE_COLOR : NON_EDITABLE_COLOR);
    // drawString takes x, y of lower left corner of string, whereas the box
    // x, y is at the top left corner of the string; need to add to translate
    // to where the string should be drawn
    gfx.drawString(getAsText() + " " + getViewUnit(), 
            box.x + LEFT_MARGIN_PIXELS, box.y + LINE_HEIGHT_PIXELS);
    gfx.setColor(oldColor);
}

Just as a note, LEFT_MARGIN_PIXELS = 0 in order to get the text to line up with the default PropertyEditors; LINE_HEIGHT_PIXELS is 15.

I82Much