~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to core/tasklist/ui/src/org/netbeans/modules/tasklist/ui/TaskListTableUI.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.tasklist.ui;
 
43
 
 
44
import java.awt.Component;
 
45
import java.awt.Graphics;
 
46
import java.awt.Point;
 
47
import java.awt.Rectangle;
 
48
import javax.swing.JComponent;
 
49
import javax.swing.plaf.basic.BasicTableUI;
 
50
import javax.swing.table.JTableHeader;
 
51
import javax.swing.table.TableCellRenderer;
 
52
import javax.swing.table.TableColumn;
 
53
import javax.swing.table.TableColumnModel;
 
54
 
 
55
/**
 
56
 *
 
57
 * @author S. Aubrecht
 
58
 */
 
59
class TaskListTableUI extends BasicTableUI {
 
60
    
 
61
    /** Creates a new instance of TaskListTableUI */
 
62
    public TaskListTableUI() {
 
63
    }
 
64
 
 
65
    /** Paint a representation of the <code>table</code> instance
 
66
     * that was set in installUI().
 
67
     * 
 
68
     * (copy & paste from BasicTableUI)
 
69
     */
 
70
    public void paint(Graphics g, JComponent c) {
 
71
        Rectangle clip = g.getClipBounds();
 
72
 
 
73
        Rectangle bounds = table.getBounds();
 
74
        // account for the fact that the graphics has already been translated
 
75
        // into the table's bounds
 
76
        bounds.x = bounds.y = 0;
 
77
 
 
78
        if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 ||
 
79
                // this check prevents us from painting the entire table
 
80
                // when the clip doesn't intersect our bounds at all
 
81
                !bounds.intersects(clip)) {
 
82
 
 
83
            return;
 
84
        }
 
85
 
 
86
        Point upperLeft = clip.getLocation();
 
87
        Point lowerRight = new Point(clip.x + clip.width - 1, clip.y + clip.height - 1);
 
88
        int rMin = table.rowAtPoint(upperLeft);
 
89
        int rMax = table.rowAtPoint(lowerRight);
 
90
        // This should never happen (as long as our bounds intersect the clip,
 
91
        // which is why we bail above if that is the case).
 
92
        if (rMin == -1) {
 
93
            rMin = 0;
 
94
        }
 
95
        // If the table does not have enough rows to fill the view we'll get -1.
 
96
        // (We could also get -1 if our bounds don't intersect the clip,
 
97
        // which is why we bail above if that is the case).
 
98
        // Replace this with the index of the last row.
 
99
        if (rMax == -1) {
 
100
            rMax = table.getRowCount()-1;
 
101
        }
 
102
 
 
103
        boolean ltr = table.getComponentOrientation().isLeftToRight();
 
104
        int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight); 
 
105
        int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft);        
 
106
        // This should never happen.
 
107
        if (cMin == -1) {
 
108
            cMin = 0;
 
109
        }
 
110
        // If the table does not have enough columns to fill the view we'll get -1.
 
111
        // Replace this with the index of the last column.
 
112
        if (cMax == -1) {
 
113
            cMax = table.getColumnCount()-1;
 
114
        }
 
115
 
 
116
        // Paint the grid.
 
117
        paintGrid(g, rMin, rMax, cMin, cMax);
 
118
 
 
119
        // Paint the cells.
 
120
        paintCells(g, rMin, rMax, cMin, cMax);
 
121
    }
 
122
    
 
123
    /*
 
124
     * Paints the grid lines within <I>aRect</I>, using the grid
 
125
     * color set with <I>setGridColor</I>. Paints vertical lines
 
126
     * if <code>getShowVerticalLines()</code> returns true and paints
 
127
     * horizontal lines if <code>getShowHorizontalLines()</code>
 
128
     * returns true.
 
129
     * 
 
130
     * (copy & paste from BasicTableUI)
 
131
     */
 
132
    private void paintGrid(Graphics g, int rMin, int rMax, int cMin, int cMax) {
 
133
        g.setColor(table.getGridColor());
 
134
 
 
135
        Rectangle minCell = table.getCellRect(rMin, cMin, true);
 
136
        Rectangle maxCell = table.getCellRect(rMax, cMax, true);
 
137
        Rectangle damagedArea = minCell.union( maxCell );
 
138
 
 
139
        if (table.getShowHorizontalLines()) {
 
140
            int tableWidth = damagedArea.x + damagedArea.width;
 
141
            int y = damagedArea.y;
 
142
            for (int row = rMin; row <= rMax; row++) {
 
143
                y += table.getRowHeight(row);
 
144
                g.drawLine(damagedArea.x, y - 1, tableWidth - 1, y - 1);
 
145
            }
 
146
        }
 
147
        if (table.getShowVerticalLines()) {
 
148
            TableColumnModel cm = table.getColumnModel();
 
149
            int tableHeight = damagedArea.y + damagedArea.height;
 
150
            int x;
 
151
            if (table.getComponentOrientation().isLeftToRight()) {
 
152
                x = damagedArea.x;
 
153
                for (int column = cMin; column <= cMax; column++) {
 
154
                    int w = cm.getColumn(column).getWidth();
 
155
                    x += w;
 
156
                    g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
 
157
                }
 
158
            } else {
 
159
                x = damagedArea.x + damagedArea.width;
 
160
                for (int column = cMin; column < cMax; column++) {
 
161
                    int w = cm.getColumn(column).getWidth();
 
162
                    x -= w;
 
163
                    g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
 
164
                }
 
165
                x -= cm.getColumn(cMax).getWidth();
 
166
                g.drawLine(x, 0, x, tableHeight - 1);
 
167
            }
 
168
        }
 
169
    }
 
170
 
 
171
    /**
 
172
     * (copy & paste from BasicTableUI)
 
173
     */
 
174
    private int viewIndexForColumn(TableColumn aColumn) {
 
175
        TableColumnModel cm = table.getColumnModel();
 
176
        for (int column = 0; column < cm.getColumnCount(); column++) {
 
177
            if (cm.getColumn(column) == aColumn) {
 
178
                return column;
 
179
            }
 
180
        }
 
181
        return -1;
 
182
    }
 
183
 
 
184
    /**
 
185
     * (copy & paste from BasicTableUI)
 
186
     */
 
187
    private void paintCells(Graphics g, int rMin, int rMax, int cMin, int cMax) {
 
188
        JTableHeader header = table.getTableHeader();
 
189
        TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();
 
190
 
 
191
        TableColumnModel cm = table.getColumnModel();
 
192
        int columnMargin = cm.getColumnMargin();
 
193
 
 
194
        Rectangle cellRect;
 
195
        TableColumn aColumn;
 
196
        int columnWidth;
 
197
        if (table.getComponentOrientation().isLeftToRight()) {
 
198
            for(int row = rMin; row <= rMax; row++) {
 
199
                cellRect = table.getCellRect(row, cMin, false);
 
200
                if( isFoldingRow( row ) ) {
 
201
                    //paint the cell across the whole table
 
202
                    cellRect.x = 0;
 
203
                    cellRect.width = table.getColumnModel().getTotalColumnWidth()-columnMargin;
 
204
                    paintCell( g, cellRect, row, 0 );
 
205
                } else {
 
206
                    for(int column = cMin; column <= cMax; column++) {
 
207
                        aColumn = cm.getColumn(column);
 
208
                        columnWidth = aColumn.getWidth();
 
209
                        cellRect.width = columnWidth - columnMargin;
 
210
                        if (aColumn != draggedColumn) {
 
211
                            paintCell(g, cellRect, row, column);
 
212
                        }
 
213
                        cellRect.x += columnWidth;
 
214
                    }
 
215
                }
 
216
            }
 
217
        } else {
 
218
            for(int row = rMin; row <= rMax; row++) {
 
219
                cellRect = table.getCellRect(row, cMin, false);
 
220
                if( isFoldingRow( row ) ) {
 
221
                    //paint the cell across the whole table
 
222
                    cellRect.x = 0;
 
223
                    cellRect.width = table.getColumnModel().getTotalColumnWidth()-columnMargin;
 
224
                    paintCell( g, cellRect, row, 0 );
 
225
                } else {
 
226
                    aColumn = cm.getColumn(cMin);
 
227
                    if (aColumn != draggedColumn) {
 
228
                        columnWidth = aColumn.getWidth();
 
229
                        cellRect.width = columnWidth - columnMargin;
 
230
                        paintCell(g, cellRect, row, cMin);
 
231
                    }
 
232
                    for(int column = cMin+1; column <= cMax; column++) {
 
233
                        aColumn = cm.getColumn(column);
 
234
                        columnWidth = aColumn.getWidth();
 
235
                        cellRect.width = columnWidth - columnMargin;
 
236
                        cellRect.x -= columnWidth;
 
237
                        if (aColumn != draggedColumn) {
 
238
                            paintCell(g, cellRect, row, column);
 
239
                        }
 
240
                    }
 
241
                }
 
242
            }
 
243
        }
 
244
 
 
245
        // Paint the dragged column if we are dragging.
 
246
        if (draggedColumn != null) {
 
247
            paintDraggedArea(g, rMin, rMax, draggedColumn, header.getDraggedDistance());
 
248
        }
 
249
 
 
250
        // Remove any renderers that may be left in the rendererPane.
 
251
        rendererPane.removeAll();
 
252
    }
 
253
 
 
254
    /**
 
255
     * (copy & paste from BasicTableUI)
 
256
     */
 
257
    private void paintDraggedArea(Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) {
 
258
        int draggedColumnIndex = viewIndexForColumn(draggedColumn);
 
259
 
 
260
        Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true);
 
261
        Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true);
 
262
 
 
263
        Rectangle vacatedColumnRect = minCell.union(maxCell);
 
264
 
 
265
        // Paint a gray well in place of the moving column.
 
266
        g.setColor(table.getParent().getBackground());
 
267
        g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
 
268
                   vacatedColumnRect.width, vacatedColumnRect.height);
 
269
 
 
270
        // Move to the where the cell has been dragged.
 
271
        vacatedColumnRect.x += distance;
 
272
 
 
273
        // Fill the background.
 
274
        g.setColor(table.getBackground());
 
275
        g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
 
276
                   vacatedColumnRect.width, vacatedColumnRect.height);
 
277
 
 
278
        // Paint the vertical grid lines if necessary.
 
279
        if (table.getShowVerticalLines()) {
 
280
            g.setColor(table.getGridColor());
 
281
            int x1 = vacatedColumnRect.x;
 
282
            int y1 = vacatedColumnRect.y;
 
283
            int x2 = x1 + vacatedColumnRect.width - 1;
 
284
            int y2 = y1 + vacatedColumnRect.height - 1;
 
285
            // Left
 
286
            g.drawLine(x1-1, y1, x1-1, y2);
 
287
            // Right
 
288
            g.drawLine(x2, y1, x2, y2);
 
289
        }
 
290
 
 
291
        for(int row = rMin; row <= rMax; row++) {
 
292
            // Render the cell value
 
293
            Rectangle r = table.getCellRect(row, draggedColumnIndex, false);
 
294
            r.x += distance;
 
295
            paintCell(g, r, row, draggedColumnIndex);
 
296
 
 
297
            // Paint the (lower) horizontal grid line if necessary.
 
298
            if (table.getShowHorizontalLines()) {
 
299
                g.setColor(table.getGridColor());
 
300
                Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true);
 
301
                rcr.x += distance;
 
302
                int x1 = rcr.x;
 
303
                int y1 = rcr.y;
 
304
                int x2 = x1 + rcr.width - 1;
 
305
                int y2 = y1 + rcr.height - 1;
 
306
                g.drawLine(x1, y2, x2, y2);
 
307
            }
 
308
        }
 
309
    }
 
310
 
 
311
    /**
 
312
     * (copy & paste from BasicTableUI)
 
313
     */
 
314
    private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
 
315
        if (table.isEditing() && table.getEditingRow()==row &&
 
316
                                 table.getEditingColumn()==column) {
 
317
            Component component = table.getEditorComponent();
 
318
            component.setBounds(cellRect);
 
319
            component.validate();
 
320
        }
 
321
        else {
 
322
            TableCellRenderer renderer = table.getCellRenderer(row, column);
 
323
            Component component = table.prepareRenderer(renderer, row, column);
 
324
            rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y,
 
325
                                        cellRect.width, cellRect.height, true);
 
326
        }
 
327
    }
 
328
    
 
329
    private boolean isFoldingRow( int row ) {
 
330
        boolean res = false;
 
331
        if( table.getModel() instanceof FoldingTaskListModel ) {
 
332
            res = ((FoldingTaskListModel)table.getModel()).isGroupRow( row );
 
333
        }
 
334
        return res;
 
335
    }
 
336
}