Coverage Report - de.glossmaker.gui.gloss.main.tableview.MultiLineBasicTableUI
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiLineBasicTableUI
0%
0/158
0%
0/76
4,167
 
 1  
 package de.glossmaker.gui.gloss.main.tableview;
 2  
 
 3  
 import java.awt.*;
 4  
 import javax.swing.*;
 5  
 import javax.swing.table.*;
 6  
 import java.util.*;
 7  
 /**
 8  
  * FlexiBasicTableUI.java
 9  
  *
 10  
  * Created: Tue May 18 11:19:04 1999
 11  
  *
 12  
  * @author Thomas Wernitz, Da Vinci Communications Ltd  <thomas_wernitz@clear.net.nz>
 13  
  *
 14  
  * credit to Zafir Anjum for JTableEx and thanks to SUN for their source code ;)
 15  
  */
 16  
 @SuppressWarnings("rawtypes")
 17  
 public class MultiLineBasicTableUI extends javax.swing.plaf.basic.BasicTableUI {
 18  
   
 19  
 
 20  
   public MultiLineBasicTableUI() {
 21  0
     super();
 22  0
   }
 23  
 
 24  
   private Dimension createTableSize(long width) {
 25  0
     int numCols = table.getColumnCount();
 26  0
     int numRows = table.getRowCount();
 27  0
     TableModel tm = table.getModel();
 28  0
     int fontHeight = table.getFontMetrics(table.getFont()).getHeight();
 29  0
     int[] height = new int[numRows];
 30  0
     for (int i=0; i<numRows; i++) height[i] = fontHeight; // init to font height as minimum
 31  
 
 32  0
     Enumeration cols = table.getColumnModel().getColumns();
 33  0
     int i = 0;
 34  0
     while(cols.hasMoreElements()) {
 35  0
       TableColumn col = (TableColumn) cols.nextElement();
 36  0
       TableCellRenderer tcr = col.getCellRenderer();
 37  0
       int colWidth = col.getWidth();
 38  0
       for (int j=0; j<numRows; j++) {
 39  0
         if (tcr instanceof MultiLineCellRenderer) {
 40  0
           height[j] = Math.max(height[j], ((MultiLineTable)table).getHeight((String)tm.getValueAt(j,i), colWidth));
 41  
         }
 42  
       }
 43  0
       i++;
 44  0
     }
 45  
 
 46  0
     int totalMarginWidth =  table.getColumnModel().getColumnMargin() * numCols; 
 47  
     // Width is always positive. The call to abs() is a workaround for 
 48  
     // a bug in the 1.1.6 JIT on Windows. 
 49  0
     long widthWithMargin = Math.abs(width) + totalMarginWidth;
 50  0
     if (widthWithMargin > Integer.MAX_VALUE) {
 51  0
       widthWithMargin = Integer.MAX_VALUE;
 52  
     }
 53  0
     int totalHeight = 0;
 54  0
     for (int k=0; k<numRows; k++) totalHeight += height[k];
 55  0
     return new Dimension((int)widthWithMargin, totalHeight + numRows*table.getRowMargin());
 56  
   }
 57  
 
 58  
 
 59  
   public Dimension getMinimumSize(JComponent c) {
 60  0
     long width = 0;
 61  0
     Enumeration enumeration = table.getColumnModel().getColumns();
 62  0
     while (enumeration.hasMoreElements()) {
 63  0
       TableColumn aColumn = (TableColumn)enumeration.nextElement();
 64  0
       width = width + aColumn.getMinWidth();
 65  0
     }
 66  0
     return createTableSize(width);
 67  
   }
 68  
 
 69  
   /**
 70  
      * Return the preferred size of the table. The preferred height is the 
 71  
      * row height (plus inter-cell spacing) times the number of rows. 
 72  
      * The preferred width is the sum of the preferred widths of each column 
 73  
      * (plus inter-cell spacing).
 74  
      */
 75  
   public Dimension getPreferredSize(JComponent c) {
 76  0
     long width = 0;
 77  0
     Enumeration enumeration = table.getColumnModel().getColumns();
 78  0
     while (enumeration.hasMoreElements()) {
 79  0
       TableColumn aColumn = (TableColumn)enumeration.nextElement();
 80  0
       width = width + aColumn.getPreferredWidth();
 81  0
     }
 82  0
     return createTableSize(width);
 83  
   }
 84  
 
 85  
   /**
 86  
      * Return the maximum size of the table. The maximum height is the 
 87  
      * row height (plus inter-cell spacing) times the number of rows. 
 88  
      * The maximum width is the sum of the maximum widths of each column 
 89  
      * (plus inter-cell spacing).
 90  
      */
 91  
   public Dimension getMaximumSize(JComponent c) {
 92  0
     long width = 0;
 93  
     
 94  0
         Enumeration enumeration = table.getColumnModel().getColumns();
 95  0
     while (enumeration.hasMoreElements()) {
 96  0
       TableColumn aColumn = (TableColumn)enumeration.nextElement();
 97  0
       width = width + aColumn.getMaxWidth();
 98  0
     }
 99  0
     return createTableSize(width);
 100  
   }
 101  
 
 102  
   public void paint(Graphics g, JComponent c) {
 103  0
     Rectangle oldClipBounds = g.getClipBounds();
 104  0
     Rectangle clipBounds = new Rectangle(oldClipBounds);
 105  0
     int tableWidth = table.getColumnModel().getTotalColumnWidth();
 106  0
     clipBounds.width = Math.min(clipBounds.width, tableWidth);
 107  0
     g.setClip(clipBounds);
 108  
 
 109  
     // Paint the grid
 110  0
     paintGrid(g);
 111  
 
 112  
     // Paint the rows
 113  0
     int firstIndex = table.rowAtPoint(new Point(0, clipBounds.y));
 114  0
     int  lastIndex = lastVisibleRow(clipBounds);
 115  
 
 116  0
     int rowMargin = table.getRowMargin();
 117  0
     Rectangle rowRect = new Rectangle(0, 0,
 118  
                                       tableWidth,
 119  
                                       ((MultiLineTable)table).getRowHeight(firstIndex) +
 120  
                                       rowMargin);
 121  0
     rowRect.y = 0;
 122  0
     for (int i=0; i<firstIndex; i++) {
 123  0
       rowRect.y += ((MultiLineTable)table).getRowHeight(i) + rowMargin;
 124  
     }
 125  
 
 126  0
     for (int index = firstIndex; index <= lastIndex; index++) {
 127  
       // Paint any rows that need to be painted
 128  0
       if (rowRect.intersects(clipBounds)) {
 129  0
         paintRow(g, index);
 130  
       }
 131  0
       rowRect.y += ((MultiLineTable)table).getRowHeight(index) + rowMargin;
 132  
     }
 133  0
     g.setClip(oldClipBounds);
 134  0
   }
 135  
 
 136  
   private void paintGrid(Graphics g) {
 137  0
     g.setColor(table.getGridColor());
 138  
 
 139  0
     if (table.getShowHorizontalLines()) {
 140  0
       paintHorizontalLines(g);
 141  
     }
 142  0
     if (table.getShowVerticalLines()) {
 143  0
       paintVerticalLines(g);
 144  
     }
 145  0
   }
 146  
 
 147  
   /*
 148  
      * This method paints horizontal lines regardless of whether the
 149  
      * table is set to paint one automatically.
 150  
      */
 151  
   private void paintHorizontalLines(Graphics g) {
 152  0
     Rectangle r = g.getClipBounds();
 153  0
     Rectangle rect = r;
 154  
 
 155  0
     int firstIndex = table.rowAtPoint(new Point(0, r.y));
 156  0
     int lastIndex = lastVisibleRow(r);
 157  0
     int rowMargin = table.getRowMargin();
 158  
 
 159  0
     int y = - rowMargin;
 160  0
     for (int i=0; i<firstIndex; i++) {
 161  0
       y += ((MultiLineTable)table).getRowHeight(i) + rowMargin;
 162  
     }
 163  
 
 164  0
     for (int index = firstIndex; index <= lastIndex; index ++) {
 165  0
       y += ((MultiLineTable)table).getRowHeight(index) + rowMargin;
 166  0
       if ((y >= rect.y) && (y <= (rect.y + rect.height))) {
 167  0
         g.drawLine(rect.x, y, rect.x + rect.width - 1, y);
 168  
       }
 169  
     }
 170  0
   }
 171  
 
 172  
   /*
 173  
      * This method paints vertical lines regardless of whether the
 174  
      * table is set to paint one automatically.
 175  
      */
 176  
   private void paintVerticalLines(Graphics g) {
 177  0
     Rectangle rect = g.getClipBounds();
 178  0
     int x = 0;
 179  0
     int count = table.getColumnCount();
 180  0
     int horizontalSpacing = table.getIntercellSpacing().width;
 181  0
     for (int index = 0; index <= count; index ++) {
 182  0
       if ((x > 0) && (((x-1) >= rect.x) && ((x-1) <= (rect.x + rect.width)))){
 183  0
         g.drawLine(x - 1, rect.y, x - 1, rect.y + rect.height - 1);
 184  
       }
 185  
 
 186  0
       if (index < count)
 187  0
         x += ((TableColumn)table.getColumnModel().getColumn(index)).
 188  
           getWidth() + horizontalSpacing;
 189  
     }
 190  0
   }
 191  
 
 192  
 private void paintRow(Graphics g, int row) {
 193  0
     Rectangle rect = g.getClipBounds();
 194  0
     int column = 0;
 195  0
     boolean drawn = false;
 196  0
     int draggedColumnIndex = -1;
 197  0
     Rectangle draggedCellRect = null;
 198  0
     Dimension spacing = table.getIntercellSpacing();
 199  0
     JTableHeader header = table.getTableHeader();
 200  
 
 201  
     // Set up the cellRect
 202  0
     Rectangle cellRect = new Rectangle();
 203  0
     cellRect.height = ((MultiLineTable)table).getRowHeight(row) + spacing.height;
 204  0
     cellRect.y = 0;
 205  0
     for (int i=0; i<row; i++) {
 206  0
       cellRect.y += ((MultiLineTable)table).getRowHeight(i) + spacing.height;
 207  
     }
 208  
 
 209  0
     Enumeration enumeration = table.getColumnModel().getColumns();
 210  
 
 211  
     // Paint the non-dragged table cells first
 212  0
     while (enumeration.hasMoreElements()) {
 213  0
       TableColumn aColumn = (TableColumn)enumeration.nextElement();
 214  
 
 215  0
       cellRect.width = aColumn.getWidth() + spacing.width;
 216  0
       if (cellRect.intersects(rect)) {
 217  0
         drawn = true;
 218  0
         if ((header == null) || (aColumn != header.getDraggedColumn())) {
 219  0
           paintCell(g, cellRect, row, column);
 220  
         }
 221  
         else {
 222  
           // Paint a gray well in place of the moving column
 223  
           // This would be unnecessary if we drew the grid more cleverly
 224  0
           g.setColor(table.getParent().getBackground());
 225  0
           g.fillRect(cellRect.x, cellRect.y, cellRect.width, cellRect.height);
 226  0
           draggedCellRect = new Rectangle(cellRect);
 227  0
           draggedColumnIndex = column;
 228  
         }
 229  
       }
 230  
       else {
 231  0
         if (drawn)
 232  
           // Don't need to iterate through the rest
 233  0
           break;
 234  
       }
 235  
 
 236  0
       cellRect.x += cellRect.width;
 237  0
       column++;
 238  0
     }
 239  
 
 240  
     // paint the dragged cell if we are dragging
 241  0
     if (draggedColumnIndex != -1 && draggedCellRect != null) {
 242  0
       draggedCellRect.x += header.getDraggedDistance();
 243  
 
 244  
       // Fill the background
 245  0
       g.setColor(table.getBackground());
 246  0
       g.fillRect(draggedCellRect.x, draggedCellRect.y,
 247  
                  draggedCellRect.width, draggedCellRect.height);
 248  
 
 249  
       // paint grid if necessary.
 250  0
       g.setColor(table.getGridColor());
 251  0
       int x1 = draggedCellRect.x;
 252  0
       int y1 = draggedCellRect.y;
 253  0
       int x2 = x1 + draggedCellRect.width - 1;
 254  0
       int y2 = y1 + draggedCellRect.height - 1;
 255  0
       if (table.getShowVerticalLines()) {
 256  
         // Left
 257  
         // g.drawLine(x1-1, y1, x1-1, y2);
 258  
         // Right
 259  0
         g.drawLine(x2, y1, x2, y2);
 260  
       }
 261  
       // Bottom
 262  0
       if (table.getShowHorizontalLines()) {
 263  0
         g.drawLine(x1, y2, x2, y2);
 264  
       }
 265  
 
 266  
       // Render the cell value
 267  0
       paintCell(g, draggedCellRect, row, draggedColumnIndex);
 268  
     }
 269  0
   }
 270  
 
 271  
   private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
 272  
     // The cellRect is inset by half the intercellSpacing before painted
 273  0
     int spacingHeight = table.getRowMargin();
 274  0
     int spacingWidth = table.getColumnModel().getColumnMargin();
 275  
 
 276  
     // Round so that when the spacing is 1 the cell does not paint obscure lines.
 277  0
     cellRect.setBounds(cellRect.x + spacingWidth/2, cellRect.y + spacingHeight/2,
 278  
                        cellRect.width - spacingWidth, cellRect.height - spacingHeight);
 279  
 
 280  0
     if (table.isEditing() && table.getEditingRow()==row &&
 281  
         table.getEditingColumn()==column) {
 282  0
       Component component = table.getEditorComponent();
 283  0
       component.setBounds(cellRect);
 284  0
       component.validate();
 285  0
     }
 286  
     else {
 287  0
       TableCellRenderer renderer = table.getCellRenderer(row, column);
 288  0
       Component component = table.prepareRenderer(renderer, row, column);
 289  
 
 290  0
       if (component.getParent() == null) {
 291  0
         rendererPane.add(component);
 292  
       }
 293  0
       rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y,
 294  
                                   cellRect.width, cellRect.height, true);
 295  
     }
 296  
     // Have to restore the cellRect back to it's orginial size
 297  0
     cellRect.setBounds(cellRect.x - spacingWidth/2, cellRect.y - spacingHeight/2,
 298  
                        cellRect.width + spacingWidth, cellRect.height + spacingHeight);
 299  
 
 300  0
   }
 301  
 
 302  
   private int lastVisibleRow(Rectangle clip) {
 303  0
     int lastIndex = table.rowAtPoint(new Point(0, clip.y + clip.height - 1));
 304  
     // If the table does not have enough rows to fill the view we'll get -1.
 305  
     // Replace this with the index of the last row.
 306  0
     if (lastIndex == -1) {
 307  0
       lastIndex = table.getRowCount() -1;
 308  
     }
 309  0
     return lastIndex;
 310  
   }
 311  
 
 312  
 } // MultiLineBasicTableUI