Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretable / table / renders / TextAreaCellRenderer.java @ 42775

History | View | Annotate | Download (6.64 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.fmap.dal.swing.impl.featuretable.table.renders;
29

    
30
import java.awt.Component;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.Map;
34

    
35
import javax.swing.JTable;
36
import javax.swing.JTextArea;
37
import javax.swing.table.DefaultTableCellRenderer;
38
import javax.swing.table.TableCellRenderer;
39
import javax.swing.table.TableColumnModel;
40

    
41
/**
42
 * Renders a text value into a text area.
43
 * <p>
44
 * Taken and adapted from the Article <strong>"Multi-line cells in JTable in JDK
45
 * 1.4+"</strong> by Dr. Heinz M. Kabutz:
46
 * </p>
47
 * <p>
48
 * http://www.javaspecialists.eu/archive/Issue106.html
49
 * </p>
50
 * 
51
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
52
 */
53
public class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
54

    
55
    private static final long serialVersionUID = 3269365116036190589L;
56

    
57
    private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
58
    /** map from table to map of rows to map of column heights */
59
    private final Map<JTable, Map<Integer, Map<Integer, Integer>>> cellSizes = new HashMap<JTable, Map<Integer, Map<Integer, Integer>>>();
60
    
61
    private int maxTxtLength = Integer.MAX_VALUE;
62
    
63
    private int maxRowHeight = Integer.MAX_VALUE;
64

    
65
    /**
66
     * Creates a new TextAreaRenderer.
67
     */
68
    public TextAreaCellRenderer() {
69
        this(Integer.MAX_VALUE, Integer.MAX_VALUE);
70
    }
71

    
72
    /**
73
     * Creates a new TextAreaRenderer.
74
     * 
75
     * @param maxTXTLength
76
     *            the maximum text length to be rendered.
77
     * @param maxRowHeight
78
     *            the maximum row height for the rows with cells rendered with
79
     *            this component
80
     */
81
    public TextAreaCellRenderer(int maxTxtLength, int maxRowHeight) {
82
        setLineWrap(true);
83
        setWrapStyleWord(true);
84
        this.maxTxtLength = maxTxtLength;
85
        this.maxRowHeight = maxRowHeight;
86
    }
87

    
88
    public Component getTableCellRendererComponent(
89
            JTable table, Object obj, boolean isSelected, boolean hasFocus,
90
            int row, int column) {
91
        // set the colours, etc. using the standard for that platform
92
        adaptee.getTableCellRendererComponent(table, obj, isSelected, hasFocus,
93
                row, column);
94
        setForeground(adaptee.getForeground());
95
        setBackground(adaptee.getBackground());
96
        setBorder(adaptee.getBorder());
97
        setFont(adaptee.getFont());
98

    
99
        String cellTxt = getCellText(obj, row, column);
100
        if (cellTxt.length() > maxTxtLength) {
101
            cellTxt = cellTxt.substring(0, maxTxtLength - 4).concat(" ...");
102
        }
103

    
104
        setText(cellTxt);
105

    
106
        // This line was very important to get it working with JDK1.4
107
        TableColumnModel columnModel = table.getColumnModel();
108
        setSize(columnModel.getColumn(column).getWidth(), 100000);
109
        int height_wanted = (int) getPreferredSize().getHeight();
110
        
111
        height_wanted = height_wanted > maxRowHeight ? maxRowHeight
112
                : height_wanted;
113
        
114
        addSize(table, row, column, height_wanted);
115
        height_wanted = findTotalMaximumRowSize(table, row);
116
        if (height_wanted != table.getRowHeight(row)) {
117
            int previousRowHeight = table.getRowHeight();
118
            table.setRowHeight(row, height_wanted);
119
            table.firePropertyChange("rowheight", previousRowHeight, height_wanted);
120
        }
121
        return this;
122
    }
123

    
124
    /**
125
     * Returns the text for the cell.
126
     * 
127
     * @param value
128
     *            the cell object value
129
     * @return the text to render on the cell
130
     */
131
    protected String getCellText(Object value, int row, int column) {
132
        return adaptee.getText();
133
    }
134

    
135
    private void addSize(JTable table, int row, int column, int height) {
136
        Map<Integer, Map<Integer, Integer>> rows = cellSizes.get(table);
137
        if (rows == null) {
138
            cellSizes.put(table,
139
                    rows = new HashMap<Integer, Map<Integer, Integer>>());
140
        }
141
        Map<Integer, Integer> rowheights = rows.get(new Integer(row));
142
        if (rowheights == null) {
143
            rows.put(new Integer(row),
144
                    rowheights = new HashMap<Integer, Integer>());
145
        }
146
        rowheights.put(new Integer(column), new Integer(height));
147
    }
148

    
149
    /**
150
     * Look through all columns and get the renderer. If it is also a
151
     * TextAreaRenderer, we look at the maximum height in its hash table for
152
     * this row.
153
     */
154
    private int findTotalMaximumRowSize(JTable table, int row) {
155
        int maximum_height = 0;
156
        for (int column = 0; column < table.getColumnCount(); column++) {
157
            TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
158
            if (cellRenderer instanceof TextAreaCellRenderer) {
159
                TextAreaCellRenderer tar = (TextAreaCellRenderer) cellRenderer;
160
                maximum_height = Math.max(maximum_height, tar
161
                        .findMaximumRowSize(table, row));
162
            }
163
        }
164
        return maximum_height;
165
    }
166

    
167
    private int findMaximumRowSize(JTable table, int row) {
168
        Map<Integer, Map<Integer, Integer>> rows = cellSizes.get(table);
169
        if (rows == null)
170
            return 0;
171
        Map<Integer, Integer> rowheights = rows.get(new Integer(row));
172
        if (rowheights == null)
173
            return 0;
174
        int maximum_height = 0;
175
        for (Iterator<Map.Entry<Integer, Integer>> it = rowheights.entrySet()
176
                .iterator(); it.hasNext();) {
177
            Map.Entry<Integer, Integer> entry = it.next();
178
            int cellHeight = ((Integer) entry.getValue()).intValue();
179
            maximum_height = Math.max(maximum_height, cellHeight);
180
        }
181
        return maximum_height;
182
    }
183

    
184
}