summaryrefslogtreecommitdiff
path: root/javax/swing/plaf/basic/BasicTableHeaderUI.java
blob: 8a8eeb837feb9c0bf25750665dcb5860f9b7caa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* BasicTableHeaderUI.java --
   Copyright (C) 2004 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing.plaf.basic;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.TableHeaderUI;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * Basic pluggable look and feel interface for JTableHeader.
 */
public class BasicTableHeaderUI extends TableHeaderUI
{
  /**
   * The width of the space (in both direction) around the column boundary,
   * where mouse cursor changes shape into "resize"
   */
  static int COLUMN_BOUNDARY_TOLERANCE = 3;
  
  public static ComponentUI createUI(JComponent h)
  {
    return new BasicTableHeaderUI();
  }
  
  /**
   * The table header that is using this interface.
   */
  protected JTableHeader header;
  
  /**
   * The mouse input listener, responsible for mouse manipulations with
   * the table header.
   */
  protected MouseInputListener mouseInputListener;
  
  /**
   * Paint the header cell.
   */
  protected CellRendererPane rendererPane;
  
  /**
   * The header cell border.
   */
  private Border cellBorder;

  /**
   * Original mouse cursor prior to resizing.
   */
  private Cursor originalCursor;
  
  /**
   * If not null, one of the columns is currently being dragged.
   */
  Rectangle draggingHeaderRect;
  
  /**
   * Handles column movement and rearrangement by mouse. The same instance works
   * both as mouse listener and the mouse motion listner.
   */
  public class MouseInputHandler
      implements MouseInputListener
  {
    /**
     * If true, the cursor is being already shown in the alternative "resize"
     * shape. 
     */
    boolean showingResizeCursor;

    /**
     * The position, from where the cursor is dragged during resizing. Double
     * purpose field (absolute value during resizing and relative offset during
     * column dragging).
     */
    int draggingFrom = - 1;
    
    /**
     * The number of the column being dragged.
     */
    int draggingColumnNumber;    

    /**
     * The previous preferred width of the column.
     */
    int prevPrefWidth = - 1;

    /**
     * The timer to coalesce column resizing events.
     */
    Timer timer;

    /**
     * Returns without action, part of the MouseInputListener interface.
     */
    public void mouseClicked(MouseEvent e)
    {
      // Nothing to do.
    }

    /**
     * If being in the resizing mode, handle resizing.
     */
    public void mouseDragged(MouseEvent e)
    {
      TableColumn resizeIt = header.getResizingColumn();
      if (resizeIt != null && header.getResizingAllowed())
        {
          // The timer is intialised on demand.
          if (timer == null)
            {
              // The purpose of timer is to coalesce events. If the queue
              // is free, the repaint event is fired immediately.
              timer = new Timer(1, new ActionListener()
              {
                public void actionPerformed(ActionEvent e)
                {
                  header.getTable().doLayout();
                }
              });
              timer.setRepeats(false);
              timer.setCoalesce(true);
            }
          resizeIt.setPreferredWidth(prevPrefWidth + e.getX() - draggingFrom);
          timer.restart();
        }
      else if (draggingHeaderRect != null && header.getReorderingAllowed())
        {
          draggingHeaderRect.x = e.getX() + draggingFrom;
          header.repaint();
        }
    }

    /**
     * Returns without action, part of the MouseInputListener interface.
     */
    public void mouseEntered(MouseEvent e)
    {
      // Nothing to do.
    }

    /**
     * Reset drag information of the column resizing.
     */
    public void mouseExited(MouseEvent e)
    {
      // Nothing to do.
    }

    /**
     * Change the mouse cursor if the mouse if above the column boundary.
     */
    public void mouseMoved(MouseEvent e)
    {
      // When dragging, the functionality is handled by the mouseDragged.
      if (e.getButton() == 0 && header.getResizingAllowed())
        {
          TableColumnModel model = header.getColumnModel();
          int n = model.getColumnCount();
          if (n < 2)
            // It must be at least two columns to have at least one boundary.
            // Otherwise, nothing to do.
            return;

          boolean onBoundary = false;

          int x = e.getX();
          int a = x - COLUMN_BOUNDARY_TOLERANCE;
          int b = x + COLUMN_BOUNDARY_TOLERANCE;

          int p = 0;

          Scan: for (int i = 0; i < n - 1; i++)
            {
              p += model.getColumn(i).getWidth();

              if (p >= a && p <= b)
                {
                  TableColumn column = model.getColumn(i);
                  onBoundary = true;

                  draggingFrom = x;
                  prevPrefWidth = column.getWidth();
                  header.setResizingColumn(column);
                  break Scan;
                }
            }

          if (onBoundary != showingResizeCursor)
            {
              // Change the cursor shape, if needed.
              if (onBoundary)
                {

		  originalCursor = header.getCursor();
                  if (p < x)
                    header.setCursor(Cursor.getPredefinedCursor(
                        Cursor.W_RESIZE_CURSOR));
                  else
                    header.setCursor(Cursor.getPredefinedCursor(
                        Cursor.E_RESIZE_CURSOR));
                }
              else
                {
                  header.setCursor(originalCursor);
                  header.setResizingColumn(null);
                }

              showingResizeCursor = onBoundary;
            }
        }
    }

    /**
     * Starts the dragging/resizing procedure.
     */
    public void mousePressed(MouseEvent e)
    {
      if (header.getResizingAllowed())
        {
          TableColumn resizingColumn = header.getResizingColumn();
          if (resizingColumn != null)
            {
              resizingColumn.setPreferredWidth(resizingColumn.getWidth());
              return;
            }
        }

      if (header.getReorderingAllowed())
        {
          TableColumnModel model = header.getColumnModel();
          int n = model.getColumnCount();
          if (n < 2)
            // It must be at least two columns to change the column location.
            return;

          boolean onBoundary = false;

          int x = e.getX();
          int p = 0;
          int col = - 1;

          Scan: for (int i = 0; i < n; i++)
            {
              p += model.getColumn(i).getWidth();
              if (p > x)
                {
                  col = i;
                  break Scan;
                }
            }
          if (col < 0)
            return;

          TableColumn dragIt = model.getColumn(col);
          header.setDraggedColumn(dragIt);

          draggingFrom = (p - dragIt.getWidth()) - x;
          draggingHeaderRect = new Rectangle(header.getHeaderRect(col));
          draggingColumnNumber = col;
        }
    }

    /**
     * Set all column preferred width to the current width to prevend abrupt
     * width changes during the next resize.
     */
    public void mouseReleased(MouseEvent e)
    {
      if (header.getResizingColumn() != null && header.getResizingAllowed())
        endResizing();
      if (header.getDraggedColumn() != null &&  header.getReorderingAllowed())
        endDragging(e);
    }

    /**
     * Stop resizing session.
     */
    void endResizing()
    {
      TableColumnModel model = header.getColumnModel();
      int n = model.getColumnCount();
      if (n > 2)
        {
          TableColumn c;
          for (int i = 0; i < n; i++)
            {
              c = model.getColumn(i);
              c.setPreferredWidth(c.getWidth());
            }
        }
      header.setResizingColumn(null);
      showingResizeCursor = false;
      if (timer != null)
        timer.stop();
      header.setCursor(originalCursor);
    }

    /**
     * Stop the dragging session.
     * 
     * @param e the "mouse release" mouse event, needed to determing the final
     *          location for the dragged column.
     */
    void endDragging(MouseEvent e)
    {
      header.setDraggedColumn(null);
      draggingHeaderRect = null;

      TableColumnModel model = header.getColumnModel();

      // Find where have we dragged the column.
      int x = e.getX();
      int p = 0;
      
      int col = model.getColumnCount() - 1;
      int n = model.getColumnCount();

      // This loop does not find the column if the mouse if out of the 
      // right boundary of the table header. Then we make this column the
      // rightmost column.
      Scan: for (int i = 0; i < n; i++)
        {
          p += model.getColumn(i).getWidth();
          if (p > x)
            {
              col = i;
              break Scan;
            }
        }

      header.getTable().moveColumn(draggingColumnNumber, col);
    }
  }
 
  /**
   * Create and return the mouse input listener.
   * 
   * @return the mouse listener ({@link MouseInputHandler}, if not overridden.
   */
  protected MouseInputListener createMouseInputListener()
  {
    return new MouseInputHandler();
  }
  
  /**
   * Construct a new BasicTableHeaderUI, create mouse listeners.
   */
  public BasicTableHeaderUI()
  {
    mouseInputListener = createMouseInputListener();
  }

  protected void installDefaults()
  {
    LookAndFeel.installColorsAndFont(header, "TableHeader.background",
                                     "TableHeader.foreground",
                                     "TableHeader.font");
    cellBorder = UIManager.getBorder("TableHeader.cellBorder");
  }

  protected void installKeyboardActions()
  {
    // AFAICS, the RI does nothing here.
  }

  /**
   * Add the mouse listener and the mouse motion listener to the table
   * header. The listeners support table column resizing and rearrangement
   * by mouse.
   */
  protected void installListeners()
  {
    header.addMouseListener(mouseInputListener);
    header.addMouseMotionListener(mouseInputListener);
  }

  public void installUI(JComponent c)
  {
    header = (JTableHeader) c;
    rendererPane = new CellRendererPane();
    installDefaults();
    installKeyboardActions();
    installListeners();
  }

  protected void uninstallDefaults()
  {
    header.setBackground(null);
    header.setForeground(null);
    header.setFont(null);
  }

  protected void uninstallKeyboardActions()
  {
    // AFAICS, the RI does nothing here.
  }
  
  /**
   * Remove the previously installed listeners.
   */
  protected void uninstallListeners()
  {
    header.removeMouseListener(mouseInputListener);
    header.removeMouseMotionListener(mouseInputListener);
  }

  public void uninstallUI(JComponent c)
  {
    uninstallListeners();
    uninstallKeyboardActions();
    uninstallDefaults();
  }
  
  /**
   * Repaint the table header. 
   */
  public void paint(Graphics gfx, JComponent c)
  {
    TableColumnModel cmod = header.getColumnModel();
    int ncols = cmod.getColumnCount();
    if (ncols == 0)
      return;
    
    Rectangle clip = gfx.getClipBounds();
    TableCellRenderer defaultRend = header.getDefaultRenderer();

    for (int i = 0; i < ncols; ++i)
      {
        Rectangle bounds = header.getHeaderRect(i);
        if (bounds.intersects(clip))
          {
            Rectangle oldClip = gfx.getClipBounds();
            TableColumn col = cmod.getColumn(i);
            TableCellRenderer rend = col.getHeaderRenderer();
            if (rend == null)
              rend = defaultRend;
            Object val = col.getHeaderValue();
            Component comp = rend.getTableCellRendererComponent(header.getTable(),
                                                                val,
                                                                false, // isSelected
                                                                false, // isFocused
                                                                -1, i);
            // FIXME: The following settings should be performed in
            // rend.getTableCellRendererComponent().
            comp.setFont(header.getFont());
            comp.setBackground(header.getBackground());
            comp.setForeground(header.getForeground());
            if (comp instanceof JComponent)
              ((JComponent) comp).setBorder(cellBorder);
            rendererPane.paintComponent(gfx, comp, header, bounds.x, bounds.y,
                                        bounds.width, bounds.height);
          }
      }
    
    // This displays a running rectangle that is much simplier than the total
    // animation, as it is seen in Sun's application.
    // TODO animate the collumn dragging like in Sun's jre.
    if (draggingHeaderRect != null)
      {
        gfx.setColor(header.getForeground()); 
        gfx.drawRect(draggingHeaderRect.x, draggingHeaderRect.y + 2,
            draggingHeaderRect.width - 1, draggingHeaderRect.height - 6);
      }
  }
  
  /**
   * Get the preferred header size.
   * 
   * @param ignored unused
   * 
   * @return the preferred size of the associated header.
   */
  public Dimension getPreferredSize(JComponent ignored)
  {
    TableColumnModel cmod = header.getColumnModel();
    TableCellRenderer defaultRend = header.getDefaultRenderer();
    int ncols = cmod.getColumnCount();    
    Dimension ret = new Dimension(0, 0);
    int spacing = 0;

    if (header.getTable() != null 
        && header.getTable().getIntercellSpacing() != null)
      spacing = header.getTable().getIntercellSpacing().width;
    
    for (int i = 0; i < ncols; ++i)      
      {
        TableColumn col = cmod.getColumn(i);
        TableCellRenderer rend = col.getHeaderRenderer();
        if (rend == null)
          rend = defaultRend;
        Object val = col.getHeaderValue();
        Component comp = rend.getTableCellRendererComponent(header.getTable(),
                                                            val,
                                                            false, // isSelected
                                                            false, // isFocused
                                                            -1, i);
        comp.setFont(header.getFont());
        comp.setBackground(header.getBackground());
        comp.setForeground(header.getForeground());
        if (comp instanceof JComponent)
          ((JComponent) comp).setBorder(cellBorder);

        Dimension d = comp.getPreferredSize();
        ret.width += spacing;
        ret.height = Math.max(d.height, ret.height);        
      }
    ret.width = cmod.getTotalColumnWidth();
    return ret;
  }
  
  
}