CharacterEditor.java :  » Workflow-Engines » osbl-1_0 » org » conform » wings » editor » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » conform » wings » editor » CharacterEditor.java
/* $Id: CharacterEditor.java 746 2006-11-08 13:34:23Z hengels $ */
package org.conform.wings.editor;

import javax.swing.text.BadLocationException;

import org.conform.*;
import org.conform.wings.PropertyAdapter;
import org.conform.wings.Editor;
import org.wings.SComponent;
import org.wings.STextComponent;
import org.wings.STextField;
import org.wings.text.DefaultDocument;

public class CharacterEditor
    implements Editor
{
    public SComponent createComponent(PropertyMeta propertyMeta) {
        STextField textField = new STextField();
        textField.setDocument(new DocumentPropertyAdapter(propertyMeta));
        configureComponent(propertyMeta, textField, false);
        return textField;
    }

    public void configureComponent(PropertyMeta propertyMeta, SComponent component, boolean erroneous) {
        STextField textField = (STextField)component;
        textField.setColumns(2);
        textField.setMaxColumns(1);
        textField.setEnabled(propertyMeta.isWritable());
        textField.setVisible(propertyMeta.isReadable());
        DefaultEditorStyles.applyEditorAlignment(propertyMeta, component);
        DefaultEditorStyles.applyEditorState(propertyMeta, component, erroneous);
    }

    public void setPropertyData(SComponent component, PropertyData propertyData) {
        BeanData beanData = propertyData.getBeanData();
        if (beanData != null)
            beanData.addPropertyChangeListener(propertyData.getPropertyMeta().getName(), new ComponentInvalidator(component));
        STextComponent textComponent = (STextComponent)component;
        ((DocumentPropertyAdapter)textComponent.getDocument()).setPropertyData(propertyData);
    }

    public PropertyData getPropertyData(SComponent component) {
        STextComponent textComponent = (STextComponent)component;
        return ((DocumentPropertyAdapter)textComponent.getDocument()).getPropertyData();
    }

    static class DocumentPropertyAdapter
        extends DefaultDocument
        implements PropertyAdapter
    {
        private PropertyData propertyData;
        private PropertyMeta field;

        public DocumentPropertyAdapter(PropertyMeta field) {
            this.field = field;
        }

        public void setPropertyData(PropertyData propertyData) {
            this.propertyData = propertyData;
        }

        public PropertyData getPropertyData() {
            return propertyData;
        }

        public void setText(String text) {
            int length = (text == null) ? 0 : text.length();
            if (length == 0)
                text = null;

            propertyData.setValue(new Character(text.charAt(0)));
            fireChangeUpdate(0, length);
        }

        public String getText() {
            Object value = propertyData.getValue();
            return value != null ? value.toString() : "";
        }

        public int getLength() {
            return (getText() == null) ? 0 : getText().length();
        }

        public String getText(int offset, int length) throws BadLocationException {
            throw new UnsupportedOperationException("this meant to be an adapter");
        }

        public void remove(int offset, int length) throws BadLocationException {
            throw new UnsupportedOperationException("this meant to be an adapter");
        }

        public void insert(int pos, String s) throws BadLocationException {
            throw new UnsupportedOperationException("this meant to be an adapter");
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.