2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
42
package org.netbeans.modules.tasklist.ui;
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;
59
class TaskListTableUI extends BasicTableUI {
61
/** Creates a new instance of TaskListTableUI */
62
public TaskListTableUI() {
65
/** Paint a representation of the <code>table</code> instance
66
* that was set in installUI().
68
* (copy & paste from BasicTableUI)
70
public void paint(Graphics g, JComponent c) {
71
Rectangle clip = g.getClipBounds();
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;
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)) {
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).
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.
100
rMax = table.getRowCount()-1;
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.
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.
113
cMax = table.getColumnCount()-1;
117
paintGrid(g, rMin, rMax, cMin, cMax);
120
paintCells(g, rMin, rMax, cMin, cMax);
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>
130
* (copy & paste from BasicTableUI)
132
private void paintGrid(Graphics g, int rMin, int rMax, int cMin, int cMax) {
133
g.setColor(table.getGridColor());
135
Rectangle minCell = table.getCellRect(rMin, cMin, true);
136
Rectangle maxCell = table.getCellRect(rMax, cMax, true);
137
Rectangle damagedArea = minCell.union( maxCell );
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);
147
if (table.getShowVerticalLines()) {
148
TableColumnModel cm = table.getColumnModel();
149
int tableHeight = damagedArea.y + damagedArea.height;
151
if (table.getComponentOrientation().isLeftToRight()) {
153
for (int column = cMin; column <= cMax; column++) {
154
int w = cm.getColumn(column).getWidth();
156
g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
159
x = damagedArea.x + damagedArea.width;
160
for (int column = cMin; column < cMax; column++) {
161
int w = cm.getColumn(column).getWidth();
163
g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
165
x -= cm.getColumn(cMax).getWidth();
166
g.drawLine(x, 0, x, tableHeight - 1);
172
* (copy & paste from BasicTableUI)
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) {
185
* (copy & paste from BasicTableUI)
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();
191
TableColumnModel cm = table.getColumnModel();
192
int columnMargin = cm.getColumnMargin();
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
203
cellRect.width = table.getColumnModel().getTotalColumnWidth()-columnMargin;
204
paintCell( g, cellRect, row, 0 );
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);
213
cellRect.x += columnWidth;
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
223
cellRect.width = table.getColumnModel().getTotalColumnWidth()-columnMargin;
224
paintCell( g, cellRect, row, 0 );
226
aColumn = cm.getColumn(cMin);
227
if (aColumn != draggedColumn) {
228
columnWidth = aColumn.getWidth();
229
cellRect.width = columnWidth - columnMargin;
230
paintCell(g, cellRect, row, cMin);
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);
245
// Paint the dragged column if we are dragging.
246
if (draggedColumn != null) {
247
paintDraggedArea(g, rMin, rMax, draggedColumn, header.getDraggedDistance());
250
// Remove any renderers that may be left in the rendererPane.
251
rendererPane.removeAll();
255
* (copy & paste from BasicTableUI)
257
private void paintDraggedArea(Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) {
258
int draggedColumnIndex = viewIndexForColumn(draggedColumn);
260
Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true);
261
Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true);
263
Rectangle vacatedColumnRect = minCell.union(maxCell);
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);
270
// Move to the where the cell has been dragged.
271
vacatedColumnRect.x += distance;
273
// Fill the background.
274
g.setColor(table.getBackground());
275
g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
276
vacatedColumnRect.width, vacatedColumnRect.height);
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;
286
g.drawLine(x1-1, y1, x1-1, y2);
288
g.drawLine(x2, y1, x2, y2);
291
for(int row = rMin; row <= rMax; row++) {
292
// Render the cell value
293
Rectangle r = table.getCellRect(row, draggedColumnIndex, false);
295
paintCell(g, r, row, draggedColumnIndex);
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);
304
int x2 = x1 + rcr.width - 1;
305
int y2 = y1 + rcr.height - 1;
306
g.drawLine(x1, y2, x2, y2);
312
* (copy & paste from BasicTableUI)
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();
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);
329
private boolean isFoldingRow( int row ) {
331
if( table.getModel() instanceof FoldingTaskListModel ) {
332
res = ((FoldingTaskListModel)table.getModel()).isGroupRow( row );