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

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

import org.conform.*;
import org.wings.*;
import org.wings.table.STableCellEditor;

import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import java.io.Serializable;

public class PropertyCellEditor
    implements STableCellEditor
{
    /**
     * The default ok button icon.
     */
    private static final SIcon OK_BUTTON_ICON = new SResourceIcon("toolbarButtonGraphics/general/Save16.gif");

    /**
     * The default cancel button icon.
     */
    private static final SIcon CANCEL_BUTTON_ICON = new SResourceIcon("toolbarButtonGraphics/general/Stop16.gif");

    protected final SLabel messageLabel = new SLabel();
    protected final SPanel editorForm = new SPanel();
    protected final SButton ok = new SButton();
    protected final SButton cancel = new SButton();
    protected final EventListenerList listenerList = new EventListenerList();
    protected boolean fireStoppedEvent = false;
    protected boolean fireCanceledEvent = false;

    private final ActionListener fireEventListener = new FireEventListener();

    /**
     * Fast edit support is editing with reduced interaction. E.g. a boolean
     * value can only have to states, true or false. So if editing is started,
     * the editor just flips the state and fires editing stopped.
     */
    private boolean fastEditSupport = true;

    protected EditorDelegate delegate;

    protected SComponent editorComponent;
    private PropertyData propertyData;
    private Editor editor;

    public PropertyCellEditor(Editor editor, final PropertyMeta propertyMeta) {
        this.editor = editor;
        editorForm.add(messageLabel);
        this.editorComponent = editor.createComponent(propertyMeta);
        editorForm.add(this.editorComponent);
        propertyData = new EditorPropertyData(propertyMeta);
        this.editor.setPropertyData(this.editorComponent, propertyData);

        this.delegate = new EditorDelegate() {
                public void setValue(Object v) {
                    super.setValue(v);
                    propertyData.setValue(v);
                }

                public Object getCellEditorValue() {
                    return propertyData.getValue();
                }

                public boolean stopCellEditing() {
                    return true;
                }

                public boolean shouldSelectCell(EventObject anEvent) {
                    return true;
                }
        };

        initButtons();
    }

    protected void initButtons() {
        ok.addActionListener(fireEventListener);
        ok.setIcon(OK_BUTTON_ICON);
        ok.setToolTipText("ok");

        cancel.addActionListener(fireEventListener);
        cancel.setIcon(CANCEL_BUTTON_ICON);
        cancel.setToolTipText("cancel");

        editorForm.add(ok);
        editorForm.add(cancel);
    }

    /**
     * Returns a reference to the editor component.
     *
     * @return the editor Component
     */
    public final SComponent getComponent() {
        return editorComponent;
    }

    public final SButton getOKButton() {
        return ok;
    }

    public final SButton getCancelButton() {
        return cancel;
    }

    /**
     * Fast edit support is editing with reduced interaction. E.g. a boolean
     * value can only have to states, true or false. So if editing is started,
     * the editor just flips the state and fires editing stopped.
     *
     * @param b a <code>boolean</code> value
     */
    public final void setFastEdit(boolean b) {
        fastEditSupport = b;
    }

    /**
     * Return if fast edit is activated.
     *
     * @return a <code>boolean</code> value
     * @see #setFastEdit
     */
    public final boolean getFastEdit() {
        return fastEditSupport;
    }

    public Object getCellEditorValue() {
        return delegate.getCellEditorValue();
    }

    public boolean isCellEditable(EventObject anEvent) {
        return delegate.isCellEditable(anEvent);
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return delegate.shouldSelectCell(anEvent);
    }

    public boolean stopCellEditing() {
        if (delegate.stopCellEditing()) {
            fireEditingStopped();
            return true;
        }

        return false;
    }

    public void cancelCellEditing() {
        delegate.cancelCellEditing();
        fireEditingCanceled();
    }

    public void addCellEditorListener(CellEditorListener l) {
        listenerList.add(CellEditorListener.class, l);
    }

    public void removeCellEditorListener(CellEditorListener l) {
        listenerList.remove(CellEditorListener.class, l);
    }

    private ChangeEvent changeEvent = null;

    /*
     * Notify all listeners that have registered interest for
     * notification on this event type.  The event instance
     * is lazily created using the parameters passed into
     * the fire method.
     * @see EventListenerList
     */
    protected void fireEditingStopped() {
        Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == CellEditorListener.class) {
                if (changeEvent == null)
                    changeEvent = new ChangeEvent(this);
                ((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
            }
        }
    }


    /*
     * Notify all listeners that have registered interest for
     * notification on this event type.  The event instance
     * is lazily created using the parameters passed into
     * the fire method.
     * @see EventListenerList
     */
    protected void fireEditingCanceled() {
        Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == CellEditorListener.class) {
                if (changeEvent == null)
                    changeEvent = new ChangeEvent(this);
                ((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
            }
        }
    }

    public boolean containedInForm(SComponent c) {
        while (c != null) {
            if (c instanceof SForm)
                return true;
            c = c.getParent();
        }
        return false;
    }

    /*
    public SComponent getTreeCellEditorComponent(STree tree, Object value,
                                                 boolean isSelected,
                                                 boolean expanded,
                                                 boolean leaf, int row) {

        String stringValue = (value != null)?value.toString():"";

        delegate.setValue(stringValue);
        return editorForm;
    }
    */

    public SComponent getTableCellEditorComponent(STable table, Object value, boolean isSelected, int row, int column) {
        delegate.setValue(value);
        return editorForm;
    }


    protected static class EditorDelegate {
        protected Object value;

        public Object getCellEditorValue() {
            return value;
        }

        /**
         * Set the Editors value. The task of this method is to
         * pass the value to the editor component so that editing
         * can be started.
         *
         * @param x the value to be edited.
         */
        public void setValue(Object x) {
            this.value = x;
        }

        public boolean isCellEditable(EventObject anEvent) {
            return true;
        }

        public boolean stopCellEditing() {
            return true;
        }

        public void cancelCellEditing() {
        }

        public boolean shouldSelectCell(EventObject anEvent) {
            return true;
        }
    }

    private final class FireEventListener implements ActionListener, Serializable {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == ok) {
                stopCellEditing();
            } else if (e.getSource() == cancel) {
                cancelCellEditing();
            }
        }
    }

    class EditorPropertyData
        extends AbstractPropertyData
    {
        private Object value;
        private Object invalidValue;

        protected EditorPropertyData(PropertyMeta propertyMeta) {
            super(null, propertyMeta);
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public Object getValue() {
            return value;
        }

        public Data getRelationData() {
            return null;
        }

        public void setInvalidValue(Object text) {
            invalidValue = text;
        }

        protected void doSetValue(Object value) {
        }

        protected Object doGetValue() {
            return null;
        }

        public Object getInvalidValue() {
            return invalidValue;
        }
    }
}
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.