📄 synthtableui.java
字号:
/* * @(#)SynthTableUI.java 1.28 08/06/16 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.synth;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Point;import java.awt.Rectangle;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.text.DateFormat;import java.text.Format;import java.text.NumberFormat;import java.util.Date;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JCheckBox;import javax.swing.JComponent;import javax.swing.JLabel;import javax.swing.JTable;import javax.swing.LookAndFeel;import javax.swing.border.Border;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.UIResource;import javax.swing.plaf.basic.BasicTableUI;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.JTableHeader;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;import sun.swing.plaf.synth.SynthUI;/** * SynthTableUI implementation * * @version 1.28, 06/16/08 * @author Philip Milne */class SynthTableUI extends BasicTableUI implements SynthUI, PropertyChangeListener {//// Instance Variables// private SynthStyle style; private boolean useTableColors; private boolean useUIBorder; private Color alternateColor; //the background color to use for cells for alternate cells // TableCellRenderer installed on the JTable at the time we're installed, // cached so that we can reinstall them at uninstallUI time. private TableCellRenderer dateRenderer; private TableCellRenderer numberRenderer; private TableCellRenderer doubleRender; private TableCellRenderer floatRenderer; private TableCellRenderer iconRenderer; private TableCellRenderer imageIconRenderer; private TableCellRenderer booleanRenderer; private TableCellRenderer objectRenderer;//// The installation/uninstall procedures and support// public static ComponentUI createUI(JComponent c) { return new SynthTableUI(); } /** * Initialize JTable properties, e.g. font, foreground, and background. * The font, foreground, and background properties are only set if their * current value is either null or a UIResource, other properties are set * if the current value is null. * * @see #installUI */ protected void installDefaults() { dateRenderer = installRendererIfPossible(Date.class, null); numberRenderer = installRendererIfPossible(Number.class, null); doubleRender = installRendererIfPossible(Double.class, null); floatRenderer = installRendererIfPossible(Float.class, null); iconRenderer = installRendererIfPossible(Icon.class, null); imageIconRenderer = installRendererIfPossible(ImageIcon.class, null); booleanRenderer = installRendererIfPossible(Boolean.class, new SynthBooleanTableCellRenderer()); objectRenderer = installRendererIfPossible(Object.class, new SynthTableCellRenderer()); updateStyle(table); } private TableCellRenderer installRendererIfPossible(Class objectClass, TableCellRenderer renderer) { TableCellRenderer currentRenderer = table.getDefaultRenderer( objectClass); if (currentRenderer instanceof UIResource) { table.setDefaultRenderer(objectClass, renderer); } return currentRenderer; } private void updateStyle(JTable c) { SynthContext context = getContext(c, ENABLED); SynthStyle oldStyle = style; style = SynthLookAndFeel.updateStyle(context, this); if (style != oldStyle) { context.setComponentState(ENABLED | SELECTED); Color sbg = table.getSelectionBackground(); if (sbg == null || sbg instanceof UIResource) { table.setSelectionBackground(style.getColor( context, ColorType.TEXT_BACKGROUND)); } Color sfg = table.getSelectionForeground(); if (sfg == null || sfg instanceof UIResource) { table.setSelectionForeground(style.getColor( context, ColorType.TEXT_FOREGROUND)); } context.setComponentState(ENABLED); Color gridColor = table.getGridColor(); if (gridColor == null || gridColor instanceof UIResource) { gridColor = (Color)style.get(context, "Table.gridColor"); if (gridColor == null) { gridColor = style.getColor(context, ColorType.FOREGROUND); } table.setGridColor(gridColor); } useTableColors = style.getBoolean(context, "Table.rendererUseTableColors", true); useUIBorder = style.getBoolean(context, "Table.rendererUseUIBorder", true); Object rowHeight = style.get(context, "Table.rowHeight"); if (rowHeight != null) { LookAndFeel.installProperty(table, "rowHeight", rowHeight); } boolean showGrid = style.getBoolean(context, "Table.showGrid", true); if (!showGrid) { table.setShowGrid(false); } Dimension d = table.getIntercellSpacing();// if (d == null || d instanceof UIResource) { if (d != null) { d = (Dimension)style.get(context, "Table.intercellSpacing"); } alternateColor = (Color)style.get(context, "Table.alternateRowColor"); if (d != null) { table.setIntercellSpacing(d); } if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } } context.dispose(); } /** * Attaches listeners to the JTable. */ protected void installListeners() { super.installListeners(); table.addPropertyChangeListener(this); } protected void uninstallDefaults() { table.setDefaultRenderer(Date.class, dateRenderer); table.setDefaultRenderer(Number.class, numberRenderer); table.setDefaultRenderer(Double.class, doubleRender); table.setDefaultRenderer(Float.class, floatRenderer); table.setDefaultRenderer(Icon.class, iconRenderer); table.setDefaultRenderer(ImageIcon.class, imageIconRenderer); table.setDefaultRenderer(Boolean.class, booleanRenderer); table.setDefaultRenderer(Object.class, objectRenderer); if (table.getTransferHandler() instanceof UIResource) { table.setTransferHandler(null); } SynthContext context = getContext(table, ENABLED); style.uninstallDefaults(context); context.dispose(); style = null; } protected void uninstallListeners() { table.removePropertyChangeListener(this); super.uninstallListeners(); } // // 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); }//// Paint methods and support// public void update(Graphics g, JComponent c) { SynthContext context = getContext(c); SynthLookAndFeel.update(context, g); context.getPainter().paintTableBackground(context, g, 0, 0, c.getWidth(), c.getHeight()); paint(context, g); context.dispose(); } public void paintBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { context.getPainter().paintTableBorder(context, g, x, y, w, h); } public void paint(Graphics g, JComponent c) { SynthContext context = getContext(c); paint(context, g); context.dispose(); } protected void paint(SynthContext context, Graphics g) { Rectangle clip = g.getClipBounds(); Rectangle bounds = table.getBounds(); // account for the fact that the graphics has already been translated // into the table's bounds bounds.x = bounds.y = 0; if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 || // this check prevents us from painting the entire table // when the clip doesn't intersect our bounds at all !bounds.intersects(clip)) { paintDropLines(context, g); return; } boolean ltr = table.getComponentOrientation().isLeftToRight(); Point upperLeft = clip.getLocation(); if (!ltr) { upperLeft.x++; } Point lowerRight = new Point(clip.x + clip.width - (ltr ? 1 : 0), clip.y + clip.height); int rMin = table.rowAtPoint(upperLeft); int rMax = table.rowAtPoint(lowerRight); // This should never happen (as long as our bounds intersect the clip, // which is why we bail above if that is the case). if (rMin == -1) { rMin = 0; } // If the table does not have enough rows to fill the view we'll get -1. // (We could also get -1 if our bounds don't intersect the clip, // which is why we bail above if that is the case). // Replace this with the index of the last row. if (rMax == -1) { rMax = table.getRowCount()-1; } int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight); int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft); // 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 = table.getColumnCount()-1; } // Paint the grid. paintGrid(context, g, rMin, rMax, cMin, cMax); // Paint the cells. paintCells(context, g, rMin, rMax, cMin, cMax); paintDropLines(context, g); } private void paintDropLines(SynthContext context, Graphics g) { JTable.DropLocation loc = table.getDropLocation(); if (loc == null) { return; } Color color = (Color)style.get(context, "Table.dropLineColor"); Color shortColor = (Color)style.get(context, "Table.dropLineShortColor"); if (color == null && shortColor == null) { return; } Rectangle rect; rect = getHDropLineRect(loc); if (rect != null) { int x = rect.x; int w = rect.width; if (color != null) { extendRect(rect, true); g.setColor(color); g.fillRect(rect.x, rect.y, rect.width, rect.height); } if (!loc.isInsertColumn() && shortColor != null) { g.setColor(shortColor); g.fillRect(x, rect.y, w, rect.height); } } rect = getVDropLineRect(loc); if (rect != null) { int y = rect.y; int h = rect.height; if (color != null) { extendRect(rect, false); g.setColor(color); g.fillRect(rect.x, rect.y, rect.width, rect.height); } if (!loc.isInsertRow() && shortColor != null) { g.setColor(shortColor); g.fillRect(rect.x, y, rect.width, h); } } } private Rectangle getHDropLineRect(JTable.DropLocation loc) { if (!loc.isInsertRow()) { return null; } int row = loc.getRow(); int col = loc.getColumn(); if (col >= table.getColumnCount()) { col--; } Rectangle rect = table.getCellRect(row, col, true); if (row >= table.getRowCount()) { row--; Rectangle prevRect = table.getCellRect(row, col, true); rect.y = prevRect.y + prevRect.height; } if (rect.y == 0) { rect.y = -1; } else { rect.y -= 2; } rect.height = 3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -