synthtableheaderui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 587 行 · 第 1/2 页

JAVA
587
字号
    protected void installKeyboardActions() { }// Uninstall methods    public void uninstallUI(JComponent c) {        uninstallDefaults();        uninstallListeners();        uninstallKeyboardActions();        header.remove(rendererPane);        rendererPane = null;        header = null;    }    protected void uninstallDefaults() {        SynthContext context = getContext(header, ENABLED);        style.uninstallDefaults(context);        context.dispose();        style = null;    }    protected void uninstallListeners() {        header.removeMouseListener(mouseInputListener);        header.removeMouseMotionListener(mouseInputListener);        header.removePropertyChangeListener(this);        mouseInputListener = null;    }    protected void uninstallKeyboardActions() {}//// Paint Methods and support//    public void update(Graphics g, JComponent c) {        SynthContext context = getContext(c);        SynthLookAndFeel.update(context, g);        paint(context, g);        context.dispose();    }    public void paint(Graphics g, JComponent c) {        SynthContext context = getContext(c);        paint(context, g);        context.dispose();    }    protected void paint(SynthContext context, Graphics g) {	if (header.getColumnModel().getColumnCount() <= 0) { 	    return; 	}        boolean ltr = header.getComponentOrientation().isLeftToRight();	Rectangle clip = g.getClipBounds();         Point left = clip.getLocation();        Point right = new Point( clip.x + clip.width - 1, clip.y );	TableColumnModel cm = header.getColumnModel();         int cMin = header.columnAtPoint( ltr ? left : right );        int cMax = header.columnAtPoint( ltr ? right : left );        // This should never happen.         if (cMin == -1) {	    cMin =  0;        }        // If the table does not have enough columns to fill the view we'll get -1.        // Replace this with the index of the last column.        if (cMax == -1) {	    cMax = cm.getColumnCount()-1;          }	TableColumn draggedColumn = header.getDraggedColumn(); 	int columnWidth;	int columnMargin = cm.getColumnMargin();        Rectangle cellRect = header.getHeaderRect(cMin); 	TableColumn aColumn;	if (ltr) {	    for(int column = cMin; column <= cMax ; column++) { 		aColumn = cm.getColumn(column); 		columnWidth = aColumn.getWidth();		cellRect.width = columnWidth - columnMargin;		if (aColumn != draggedColumn) {		    paintCell(g, cellRect, column);		} 		cellRect.x += columnWidth;	    }	} else {	    aColumn = cm.getColumn(cMin);	    if (aColumn != draggedColumn) {		columnWidth = aColumn.getWidth();		cellRect.width = columnWidth - columnMargin;		cellRect.x += columnMargin;		paintCell(g, cellRect, cMin);	    }	    for(int column = cMin+1; column <= cMax; column++) {		aColumn = cm.getColumn(column);		columnWidth = aColumn.getWidth();		cellRect.width = columnWidth - columnMargin;		cellRect.x -= columnWidth;		if (aColumn != draggedColumn) {		    paintCell(g, cellRect, column);		}	    }	}         // Paint the dragged column if we are dragging.         if (draggedColumn != null) {             int draggedColumnIndex = viewIndexForColumn(draggedColumn); 	    Rectangle draggedCellRect = header.getHeaderRect(draggedColumnIndex);                         // Draw a gray well in place of the moving column.             g.setColor(header.getParent().getBackground());            g.fillRect(draggedCellRect.x, draggedCellRect.y,                               draggedCellRect.width, draggedCellRect.height);            draggedCellRect.x += header.getDraggedDistance();	    // Fill the background.             g.setColor(context.getStyle().getColor(context,                                                   ColorType.BACKGROUND));	    g.fillRect(draggedCellRect.x, draggedCellRect.y,		       draggedCellRect.width, draggedCellRect.height);             paintCell(g, draggedCellRect, draggedColumnIndex);        }	// Remove all components in the rendererPane. 	rendererPane.removeAll();     }    private Component getHeaderRenderer(int columnIndex) {         TableColumn aColumn = header.getColumnModel().getColumn(columnIndex); 	TableCellRenderer renderer = aColumn.getHeaderRenderer();         if (renderer == null) { 	    renderer = header.getDefaultRenderer(); 	}	return renderer.getTableCellRendererComponent(header.getTable(), 						aColumn.getHeaderValue(), false, false,                                                 -1, columnIndex);    }    private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {        Component component = getHeaderRenderer(columnIndex);         rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y,                            cellRect.width, cellRect.height, true);    }    private int viewIndexForColumn(TableColumn aColumn) {        TableColumnModel cm = header.getColumnModel();        for (int column = 0; column < cm.getColumnCount(); column++) {            if (cm.getColumn(column) == aColumn) {                return column;            }        }        return -1;    }//// SynthUI//    public SynthContext getContext(JComponent c) {        return getContext(c, getComponentState(c));    }    private SynthContext getContext(JComponent c, int state) {        return SynthContext.getContext(SynthContext.class, c,                    SynthLookAndFeel.getRegion(c), style, state);    }    private Region getRegion(JComponent c) {        return SynthLookAndFeel.getRegion(c);    }    private int getComponentState(JComponent c) {        return SynthLookAndFeel.getComponentState(c);    }//// Size Methods//    private int getHeaderHeight() {        int height = 0; 	boolean accomodatedDefault = false;         TableColumnModel columnModel = header.getColumnModel();        for(int column = 0; column < columnModel.getColumnCount(); column++) { 	    TableColumn aColumn = columnModel.getColumn(column); 	    // Configuring the header renderer to calculate its preferred size is expensive. 	    // Optimise this by assuming the default renderer always has the same height. 	    if (aColumn.getHeaderRenderer() != null || !accomodatedDefault) { 		Component comp = getHeaderRenderer(column); 		int rendererHeight = comp.getPreferredSize().height; 		height = Math.max(height, rendererHeight); 		// If the header value is empty (== "") in the 		// first column (and this column is set up 		// to use the default renderer) we will 		// return zero from this routine and the header 		// will disappear altogether. Avoiding the calculation 		// of the preferred size is such a performance win for 		// most applications that we will continue to 		// use this cheaper calculation, handling these 		// issues as `edge cases'. 		if (rendererHeight > 0) { 		    accomodatedDefault = true; 		}	    }        }        return height;    }    private Dimension createHeaderSize(long width) {        TableColumnModel columnModel = header.getColumnModel();        // None of the callers include the intercell spacing, do it here.        if (width > Integer.MAX_VALUE) {            width = Integer.MAX_VALUE;        }        return new Dimension((int)width, getHeaderHeight());    }    /**     * Return the minimum size of the header. The minimum width is the sum      * of the minimum widths of each column (plus inter-cell spacing).     */    public Dimension getMinimumSize(JComponent c) {        long width = 0;        Enumeration enumeration = header.getColumnModel().getColumns();        while (enumeration.hasMoreElements()) {            TableColumn aColumn = (TableColumn)enumeration.nextElement();            width = width + aColumn.getMinWidth();        }        return createHeaderSize(width);    }    /**     * Return the preferred size of the header. The preferred height is the      * maximum of the preferred heights of all of the components provided      * by the header renderers. The preferred width is the sum of the      * preferred widths of each column (plus inter-cell spacing).     */    public Dimension getPreferredSize(JComponent c) {        long width = 0;        Enumeration enumeration = header.getColumnModel().getColumns();        while (enumeration.hasMoreElements()) {            TableColumn aColumn = (TableColumn)enumeration.nextElement();            width = width + aColumn.getPreferredWidth();        }        return createHeaderSize(width);    }    /**     * Return the maximum size of the header. The maximum width is the sum      * of the maximum widths of each column (plus inter-cell spacing).     */    public Dimension getMaximumSize(JComponent c) {        long width = 0;        Enumeration enumeration = header.getColumnModel().getColumns();        while (enumeration.hasMoreElements()) {            TableColumn aColumn = (TableColumn)enumeration.nextElement();            width = width + aColumn.getMaxWidth();        }        return createHeaderSize(width);    }    public void propertyChange(PropertyChangeEvent evt) {        if (SynthLookAndFeel.shouldUpdateStyle(evt)) {            fetchStyle((JTableHeader)evt.getSource());        }    }    private class HeadererRenderer extends DefaultTableCellRenderer                                 implements UIResource {        HeadererRenderer() {            setName("TableHeader.renderer");            setBorder(null);            setHorizontalAlignment(JLabel.LEADING);            // Recreate the UI to trigger refetching of the style.            updateUI();        }        public Component getTableCellRendererComponent(                            JTable table, Object value, boolean isSelected,                            boolean hasFocus, int row, int column) {            setText((value == null) ? "" : value.toString());            return this;        }    }}  // End of Class SynthTableHeaderUI

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?