at.spardat.xma.gui.mapper.WidgetCreationWizPProps.java Source code

Java tutorial

Introduction

Here is the source code for at.spardat.xma.gui.mapper.WidgetCreationWizPProps.java

Source

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: WidgetCreationWizPProps.java 2106 2007-11-28 16:57:42Z s3460 $
package at.spardat.xma.gui.mapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * Wizard page that allows to specify the most common properties for
 * the widgets to be created.
 *
 * @author YSD, 16.07.2003 16:33:17
 */
public class WidgetCreationWizPProps extends WizardPage {

    static {
        String[] images = { "checked.gif", "unchecked.gif" };
        for (int i = 0; i < images.length; i++) {
            MapperPlugin.registerImage(images[i], ImageDescriptor.createFromFile(MapperPlugin.class, images[i]));
        }
    }

    private Table table_;
    private TableViewer tableViewer_;
    private WidgetCreationWiz wiz_;
    private MdlAttribute[] attrs_;
    private boolean createTableCols_;

    public WidgetCreationWizPProps(boolean createTableCols) {
        super("props");
        createTableCols_ = createTableCols;
        if (createTableCols_) {
            setTitle("Table Columns");
            setDescription("Specify the values of the most important table column attributes.");
        } else {
            setTitle("Widget Details");
            setDescription("Specify what widget types are created and provide initial values for some properties.");
        }
    }

    /**
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    public void createControl(Composite cmp) {
        Composite myComposite = new Composite(cmp, SWT.NULL);
        myComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        myComposite.setLayout(new FormLayout());

        wiz_ = (WidgetCreationWiz) getWizard();
        attrs_ = wiz_.getAttributes();

        Table table_ = new Table(myComposite,
                SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
        table_.setLinesVisible(true);
        table_.setHeaderVisible(true);

        FormData fd = new FormData();
        fd.top = new FormAttachment(0, 0);
        fd.left = new FormAttachment(0, 0);
        fd.right = new FormAttachment(100, 0);
        fd.bottom = new FormAttachment(100, 0);
        table_.setLayoutData(fd);

        tableViewer_ = new TableViewer(table_);

        TableProvider tP = new TableProvider(tableViewer_);
        tP.add(new Column("Instance Name", 100, true, SWT.LEFT) {
            public CellEditor getEditor() {
                return new TextCellEditor(getTable());
            }

            public Object getValue(Object row) {
                return ((MdlAttribute.WidgetInitData) row).getInstanceName();
            }

            public void setValue(Object row, Object cell) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                if (wi.isValidInstanceName((String) cell))
                    wi.setInstanceName((String) cell);
            }

            public Image getImage(Object row) {
                return MapperPlugin.getImage("method.gif");
            }
        });

        tP.add(new Column("Type", 70, false, SWT.LEFT) {
            public CellEditor getEditor() {
                return new TextCellEditor(getTable());
            }

            public Object getValue(Object row) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                return wi.getAttribute().getTypeName();
            }
        });

        if (!createTableCols_) {
            // widget type may be selected if we do not create table columns
            tP.add(new Column("Widget Type", 100, true, SWT.LEFT) {
                public CellEditor getEditor() {
                    return new ComboBoxCellEditor(getTable(), MdlAttribute.widgetNames, SWT.READ_ONLY);
                }

                public Object getValue(Object row) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    return new Integer(wi.getWType());
                }

                public void setValue(Object row, Object cell) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    int val = ((Integer) cell).intValue();
                    if (wi.isValidWType(val))
                        wi.setWType(val);
                }

                public String getText(Object row) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    return MdlAttribute.widgetNames[wi.getWType()];
                }

                public Image getImage(Object row) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    String imageName = "unknownWidget.gif";
                    switch (wi.getWType()) {
                    case MdlAttribute.WIDG_CHECK:
                        imageName = "check.gif";
                        break;
                    case MdlAttribute.WIDG_COMBO:
                        imageName = "combo.gif";
                        break;
                    case MdlAttribute.WIDG_TEXT:
                        imageName = "text.gif";
                        break;
                    case MdlAttribute.WIDG_PICKER:
                        imageName = "DatePicker.gif";
                        break;
                    }
                    return MapperPlugin.getImage(imageName);
                }

            });
        }

        tP.add(new Column(createTableCols_ ? "Column Header" : "Label", 100, true, SWT.LEFT) {
            public CellEditor getEditor() {
                return new TextCellEditor(getTable());
            }

            public Object getValue(Object row) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                return wi.getLabel();
            }

            public void setValue(Object row, Object cell) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                wi.setLabel((String) cell);
            }
        });

        tP.add(new Column("Alignment", 70, true, SWT.LEFT) {
            public CellEditor getEditor() {
                return new ComboBoxCellEditor(getTable(), MdlAttribute.alignmentNames, SWT.READ_ONLY);
            }

            public Object getValue(Object row) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                return new Integer(wi.getAlignment());
            }

            public String getText(Object row) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                return MdlAttribute.alignmentNames[wi.getAlignment()];
            }

            public void setValue(Object row, Object cell) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                int val = ((Integer) cell).intValue();
                wi.setAlignment(val);
            }

            public boolean canModify(Object row) {
                MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                return wi.getWType() != MdlAttribute.WIDG_COMBO;
            }
        });

        if (createTableCols_) {
            // width of column only if we are creating table columns
            tP.add(new Column("Width", 60, true, SWT.RIGHT) {
                public CellEditor getEditor() {
                    return new TextCellEditor(getTable());
                }

                public Object getValue(Object row) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    return String.valueOf(wi.getWidth());
                }

                public void setValue(Object row, Object cell) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    try {
                        wi.setWidth(Integer.parseInt((String) cell));
                    } catch (Exception ex) {
                    }
                }
            });
        }

        if (!createTableCols_) {
            //            // not supported yet since this would require to add a validator to the model
            //            // Mandatory property with Texts and Combos
            //            tP.add(new BooleanColumn ("Mandatory", 70, true, SWT.CENTER) {
            //                public boolean getBooleanValue (Object row) {
            //                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData)row;
            //                    return wi.isMandatory();
            //                }
            //                public boolean canModify (Object row) {
            //                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData)row;
            //                    return wi.getWType() == MdlAttribute.WIDG_TEXT || wi.getWType() == MdlAttribute.WIDG_COMBO;
            //                }
            //                public void setBooleanValue (Object row, boolean value) {
            //                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData)row;
            //                    wi.setMandatory(value);
            //                }
            //            });

            // Readonly property with Texts
            tP.add(new BooleanColumn("Editable", 70, true, SWT.CENTER) {
                public boolean getBooleanValue(Object row) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    return wi.isEditable();
                }

                public boolean canModify(Object row) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    //return wi.getWType() == MdlAttribute.WIDG_TEXT;
                    return true;
                }

                public void setBooleanValue(Object row, boolean value) {
                    MdlAttribute.WidgetInitData wi = (MdlAttribute.WidgetInitData) row;
                    wi.setEditable(value);
                }
            });

        }

        tP.createSWTColumns();
        tP.setCellEditors();
        tableViewer_.setUseHashlookup(true);
        tableViewer_.setColumnProperties(tP.getColumnNames());
        tableViewer_.setCellModifier(tP);
        tableViewer_.setLabelProvider(tP);
        tableViewer_.setContentProvider(new IStructuredContentProvider() {
            public Object[] getElements(Object inputElement) {
                MdlAttribute.WidgetInitData[] data = new MdlAttribute.WidgetInitData[attrs_.length];
                for (int i = 0; i < data.length; i++) {
                    data[i] = attrs_[i].getWidgetInitData(createTableCols_);
                }
                return data;
            }

            public void dispose() {
            }

            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            }
        });
        tableViewer_.setInput(tP); // does not really matter

        setControl(myComposite);

    }

    /**
     * Descriptive information for a table column
     *
     * @author YSD, 16.07.2003 18:56:00
     */
    private abstract class Column {
        String name_;
        int width_;
        boolean modifyable_;
        TableViewer tv_;
        int align_; // one of SWT.LEFT, SWT.CENTER, SWT.RIGHT

        public Column(String name, int width, boolean modifyable, int align) {
            name_ = name;
            width_ = width;
            modifyable_ = modifyable;
        }

        public abstract CellEditor getEditor();

        public abstract Object getValue(Object row);

        public void setValue(Object row, Object cell) {
        }

        public boolean canModify(Object row) {
            return modifyable_;
        }

        public Image getImage(Object row) {
            return null;
        }

        public String getText(Object row) {
            Object obj = getValue(row);
            return obj == null ? null : obj.toString();
        }

        public Table getTable() {
            return (Table) tv_.getControl();
        }
    }

    /**
     * Boolean Column that returns gifs instead of a text
     *
     * @author YSD, 16.07.2003 21:28:02
     */
    private abstract class BooleanColumn extends Column {
        public BooleanColumn(String name, int width, boolean modifyable, int align) {
            super(name, width, modifyable, align);
        }

        public CellEditor getEditor() {
            return new CheckboxCellEditor(getTable());
        }

        public abstract boolean getBooleanValue(Object row);

        public abstract void setBooleanValue(Object row, boolean what);

        public Object getValue(Object row) {
            return new Boolean(getBooleanValue(row));
        }

        public void setValue(Object row, Object cell) {
            setBooleanValue(row, ((Boolean) cell).booleanValue());
        }

        public String getText(Object row) {
            return null;
        }

        public Image getImage(Object row) {
            return MapperPlugin.getImage(getBooleanValue(row) ? "checked.gif" : "unchecked.gif");
        }
    }

    /**
     * Implements all the nasty providers we need for a TableViewer
     *
     * @author YSD, 16.07.2003 18:55:40
     */
    private class TableProvider extends LabelProvider implements ITableLabelProvider, ICellModifier {
        private ArrayList cols_ = new ArrayList();
        private HashMap colsHashed_ = new HashMap();
        private TableViewer tv_;
        private Table table_;

        public TableProvider(TableViewer tv) {
            tv_ = tv;
            table_ = (Table) tv.getControl();
        }

        /**
         * Adds a column
         */
        public void add(Column c) {
            cols_.add(c);
            colsHashed_.put(c.name_, c);
            c.tv_ = tv_;
        }

        /**
         * Finds a column by name or returns <tt>null</tt> if non found
         */
        public Column getCol(String name) {
            return (Column) colsHashed_.get(name);
        }

        public Column getCol(int index) {
            return (Column) cols_.get(index);
        }

        /**
         * Creates the TableColumns of the SWT table.
         */
        public void createSWTColumns() {
            for (Iterator iter = cols_.iterator(); iter.hasNext();) {
                Column col = (Column) iter.next();
                TableColumn c = new TableColumn(table_, col.align_);
                c.setWidth(col.width_);
                c.setResizable(true);
                c.setText(col.name_);
            }
        }

        public void setCellEditors() {
            CellEditor[] editors = new CellEditor[cols_.size()];
            for (int i = 0; i < cols_.size(); i++) {
                Column element = (Column) cols_.get(i);
                editors[i] = element.getEditor();
            }
            tv_.setCellEditors(editors);
        }

        public String[] getColumnNames() {
            String[] names = new String[cols_.size()];
            for (int i = 0; i < cols_.size(); i++) {
                Column element = (Column) cols_.get(i);
                names[i] = element.name_;
            }
            return names;
        }

        /**
         * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
         */
        public Image getColumnImage(Object element, int columnIndex) {
            Column c = getCol(columnIndex);
            return c.getImage(element);
        }

        /**
         * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
         */
        public String getColumnText(Object element, int columnIndex) {
            Column c = getCol(columnIndex);
            String text = c.getText(element);
            return text == null ? "" : text;
        }

        /**
         * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
         */
        public boolean canModify(Object element, String property) {
            Column c = getCol(property);
            return c.canModify(element);
        }

        /**
         * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
         */
        public Object getValue(Object element, String property) {
            Column c = getCol(property);
            return c.getValue(element);
        }

        /**
         * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
         */
        public void modify(Object element, String property, Object value) {
            if (element instanceof Item) {
                element = ((Item) element).getData();
            }
            Column c = getCol(property);
            c.setValue(element, value);
            tv_.update(element, null);
        }

    }

    /**
     * The attribute array did change; refresh all
     */
    public void attributesChanged() {
        attrs_ = wiz_.getAttributes();
        tableViewer_.refresh();
    }

}