Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 
 package org.apache.cayenne.swing;
 
 import java.util.List;
 import java.util.Map;
 
 
A binding for a JTable.

Author(s):
Andrei Adamchik
 
 public class TableBinding extends BindingBase {

    
A variable exposed in the context of set/get cell value.
 
     public static final String ITEM_VAR = "item";
 
     protected JTable table;
     protected String[] headers;
     protected BindingExpression[] columns;
     protected boolean[] editableState;
     protected Class[] columnClass;
     protected List list;
 
     public TableBinding(JTable tableString listBindingString[] headers,
             BindingExpression[] columnsClass[] columnClassboolean[] editableState,
             Object[] sampleLongValues) {
 
         super(listBinding);
         this. = table;
         this. = headers;
         this. = columns;
         this. = editableState;
         this. = columnClass;
 
         table.setModel(new BoundTableModel());
         resizeColumns(sampleLongValues);
     }
 
     protected void resizeColumns(Object[] sampleLongValues) {
 
         TableCellRenderer headerRenderer = .getTableHeader().getDefaultRenderer();
         TableColumnModel columnModel = .getColumnModel();
         TableModel tableModel = .getModel();
 
         for (int i = 0; i < columnModel.getColumnCount(); i++) {
 
             TableColumn column = columnModel.getColumn(i);
 
             Component header = headerRenderer.getTableCellRendererComponent(nullcolumn
                     .getHeaderValue(), falsefalse, 0, 0);
             int headerWidth = header.getPreferredSize().;
 
             if (sampleLongValues[i] != null) {
                 Component bigCell = 
                         .getDefaultRenderer(tableModel.getColumnClass(i))
                         .getTableCellRendererComponent(
                                 ,
                                 sampleLongValues[i],
                                 false,
                                 false,
                                 0,
                                 i);
                 int cellWidth = bigCell.getPreferredSize().;
                 column.setPreferredWidth(Math.max(headerWidthcellWidth));
            }
            else {
                column.setPreferredWidth(headerWidth);
            }
        }
    }
    public void setContext(Object object) {
        super.setContext(object);
        this. = updateList();
    }
    public Component getView() {
        return ;
    }
    public void updateView() {
        this. = updateList();
    }
    int getListSize() {
        return ( != null) ? .size() : 0;
    }
    List updateList() {
        if (getContext() == null) {
            return null;
        }
        Object list = getValue();
        if (list == null) {
            return null;
        }
        if (list instanceof List) {
            return (Listlist;
        }
        if (list instanceof Object[]) {
            Object[] objects = (Object[]) list;
            return Arrays.asList(objects);
        }
        if (list instanceof Collection) {
            return new ArrayList((Collectionlist);
        }
        throw new BindingException("List expected, got - " + list);
    }
    final class BoundTableModel extends AbstractTableModel {
        // this map is used as "flyweight", providing on the spot context for Ognl
        // expression evaluation
        Map listContext = new SingletonMap(null);
        public int getColumnCount() {
            return .;
        }
        public int getRowCount() {
            return getListSize();
        }
        public boolean isCellEditable(int rowIndexint columnIndex) {
            return [columnIndex];
        }
        public Object getValueAt(int rowIndexint columnIndex) {
            Object item = .get(rowIndex);
            .put(item);
            return [columnIndex].getValue(getContext(), );
        }
        public String getColumnName(int column) {
            // per CAY-513 - if an empty string is passed for header, table header will
            // have zero height on Windows... So we have to check for this condition
            return Util.isEmptyString([column]) ? " " : [column];
        }
        public Class getColumnClass(int columnIndex) {
            return [columnIndex];
        }
        public void setValueAt(Object valueint rowIndexint columnIndex) {
            Object item = .get(rowIndex);
            .put(item);
            [columnIndex].setValue(getContext(), value);
        }
    }
New to GrepCode? Check out our FAQ X