⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jtablebinding.java

📁 java属性邦定的(JSR-295)的一个实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        this.editable = editable;
    }

    /**
     * Returns whether or not the cells of the table should be editable.
     * The default for this property is {@code true}.
     * See this <a href="#EDITABILITY">paragraph</a> in the class level
     * documentation on editability.
     *
     * @return whether or not the cells of the table should be editable
     */
    public boolean isEditable() {
        return editable;
    }

    /**
     * Creates a {@code ColumnBinding} and adds it to the end of the list of {@code ColumnBindings}
     * maintained by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @param columnProperty the property with which to derive cell values from the
     *                       elements of the source {@code List}
     * @return the {@code ColumnBinding}
     * @throws IllegalArgumentException if {@code columnProperty} is {@code null}
     * @see org.jdesktop.swingbinding.JTableBinding.ColumnBinding
     */
    public ColumnBinding addColumnBinding(Property<E, ?> columnProperty) {
        return addColumnBinding(columnProperty, null);
    }

    /**
     * Creates a named {@code ColumnBinding} and adds it to the end of the list of {@code ColumnBindings}
     * maintained by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @param columnProperty the property with which to derive cell values from the
     *                       elements of the source {@code List}
     * @param name a name for the column binding
     * @return the {@code ColumnBinding}
     * @throws IllegalArgumentException if {@code columnProperty} is {@code null}
     * @see org.jdesktop.swingbinding.JTableBinding.ColumnBinding
     */
    public ColumnBinding addColumnBinding(Property<E, ?> columnProperty, String name) {
        throwIfBound();

        if (columnProperty == null) {
            throw new IllegalArgumentException("can't have null column property");
        }

        if (name == null && JTableBinding.this.getName() != null) {
            name = JTableBinding.this.getName() + ".COLUMN_BINDING";
        }

        ColumnBinding binding = new ColumnBinding(columnBindings.size(), columnProperty, name);
        columnBindings.add(binding);
        return binding;
    }

    /**
     * Creates a {@code ColumnBinding} and inserts it at the given index into the list
     * of {@code ColumnBindings} maintained by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @param index the index at which to insert the {@code ColumnBinding}
     * @param columnProperty the property with which to derive cell values from the
     *                       elements of the source {@code List}
     * @return the {@code ColumnBinding}
     * @throws IllegalArgumentException if {@code columnProperty} is {@code null}
     * @see org.jdesktop.swingbinding.JTableBinding.ColumnBinding
     */
    public ColumnBinding addColumnBinding(int index, Property<E, ?> columnProperty) {
        return addColumnBinding(index, columnProperty, null);
    }

    /**
     * Creates a {@code ColumnBinding} and inserts it at the given index into the list
     * of {@code ColumnBindings} maintained by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @param index the index at which to insert the {@code ColumnBinding}
     * @param columnProperty the property with which to derive cell values from the
     *                       elements of the source {@code List}
     * @param name a name for the {@code ColumnBinding}
     * @return the {@code ColumnBinding}
     * @throws IllegalArgumentException if {@code columnProperty} is {@code null}
     * @see org.jdesktop.swingbinding.JTableBinding.ColumnBinding
     */
    public ColumnBinding addColumnBinding(int index, Property<E, ?> columnProperty, String name) {
        throwIfBound();

        if (columnProperty == null) {
            throw new IllegalArgumentException("can't have null column property");
        }

        if (name == null && JTableBinding.this.getName() != null) {
            name = JTableBinding.this.getName() + ".COLUMN_BINDING";
        }
        
        ColumnBinding binding = new ColumnBinding(index, columnProperty, name);
        columnBindings.add(index, binding);
        adjustIndices(index + 1, true);
        return binding;
    }

    /**
     * Removes the given {@code ColumnBinding} from the list maintained
     * by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @param binding the {@code ColumnBinding} to remove
     * @see #addColumnBinding(Property, String)
     */
    public boolean removeColumnBinding(ColumnBinding binding) {
        throwIfBound();
        boolean retVal = columnBindings.remove(binding);

        if (retVal) {
            adjustIndices(binding.getColumn(), false);
        }

        return retVal;
    }

    /**
     * Removes the {@code ColumnBinding} with the given index from the list maintained
     * by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @param index the index of the {@code ColumnBinding} to remove
     * @see #addColumnBinding(Property, String)
     */
    public ColumnBinding removeColumnBinding(int index) {
        throwIfBound();
        ColumnBinding retVal = columnBindings.remove(index);
        
        if (retVal != null) {
            adjustIndices(index, false);
        }

        return retVal;
    }

    /**
     * Returns the {@code ColumnBinding} with the given index in the list maintained
     * by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @param index the index of the {@code ColumnBinding} to return
     * @return the {@code ColumnBinding} at the given index
     * @see #addColumnBinding(Property, String)
     */
    public ColumnBinding getColumnBinding(int index) {
        return columnBindings.get(index);
    }

    /**
     * Returns an unmodifiable copy of the list of {@code ColumnBindings} maintained
     * by this {@code JTableBinding}.
     * <p>
     * The list of {@code ColumnBindings} dictates the columns to be displayed in the
     * {@code JTable}, with a {@code ColumnBinding's} order in the list determining its
     * table model index.
     *
     * @return the list of {@code ColumnBindings}
     * @see #addColumnBinding(Property, String)
     */
    public List<ColumnBinding> getColumnBindings() {
        return Collections.unmodifiableList(columnBindings);
    }

    private void adjustIndices(int start, boolean up) {
        int size = columnBindings.size();
        for (int i = start; i < size; i++) {
            ColumnBinding cb = columnBindings.get(i);
            cb.adjustColumn(cb.getColumn() + (up ? 1 : -1));
        }
    }
    
    private final class ColumnProperty extends Property {
        private ColumnBinding binding;

        public Class<? extends Object> getWriteType(Object source) {
            return binding.columnClass == null ? Object.class : binding.columnClass;
        }

        public Object getValue(Object source) {
            if (binding.isBound()) {
                return binding.editingObject;
            }

            throw new UnsupportedOperationException();
        }

        public void setValue(Object source, Object value) {
            throw new UnsupportedOperationException();
        }

        public boolean isReadable(Object source) {
            return binding.isBound();
        }

        public boolean isWriteable(Object source) {
            return true;
        }

        public void addPropertyStateListener(Object source, PropertyStateListener listener) {
        }

        public void removePropertyStateListener(Object source, PropertyStateListener listener) {
        }

        public PropertyStateListener[] getPropertyStateListeners(Object source) {
            return new PropertyStateListener[0];
        }
    }

    /**
     * {@code ColumnBinding} represents a binding between a property of the elements
     * in the {@code JTableBinding's} source {@code List}, and a column in the table. Each
     * {@code ColumnBinding} added to a {@code JTableBinding} represents a column
     * to be displayed by the {@code JTable}. A value for any given row in a column
     * is aquired by fetching the value of the associated {@code ColumnBinding's}
     * source property for the element in the source {@code List} representing that row.
     * <p>
     * A {@code Converter} may be specified on a {@code ColumnBinding}, as may be
     * a {@code Validator}. Validation occurs at the time a cell value is to be
     * committed back to the source {@code List}.
     * <p>
     * {@code BindingListeners} registered on
     * a {@code ColumnBinding} are notified of successful {@code sync} or
     * {@code syncFailure}. These notifications are also sent to the
     * {@code JTableBinding's} {@code BindingListeners}.
     * <p>
     * {@code ColumnBindings} are managed by their {@code JTableBinding}. They are not
     * to be explicitly bound, unbound, added to a {@code BindingGroup}, or accessed
     * in a way that is not allowed for a managed binding.
     *
     * @see org.jdesktop.swingbinding.JTableBinding#addColumnBinding(Property, String)
     */
    public final class ColumnBinding extends AbstractColumnBinding {
        private Class<?> columnClass;
        private boolean editable = true;
        private boolean editableSet;
        private String columnName;
        private Object editingObject;

        private ColumnBinding(int column, Property<E, ?> columnProperty, String name) {
            super(column, columnProperty, new ColumnProperty(), name);
            ((ColumnProperty) getTargetProperty()).binding = this;
        }

        private void setEditingObject(Object editingObject) {
            this.editingObject = editingObject;
        }
        
        private void adjustColumn(int newCol) {
            setColumn(newCol);
        }

        /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -