synthtableui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,411 行 · 第 1/4 页
JAVA
1,411 行
} /** * 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() { fetchStyle(table); TransferHandler th = table.getTransferHandler(); if (th == null || th instanceof UIResource) { table.setTransferHandler(defaultTransferHandler); } DropTarget dropTarget = table.getDropTarget(); if (dropTarget instanceof UIResource) { if (defaultDropTargetListener == null) { defaultDropTargetListener = new TableDropTargetListener(); } try { dropTarget.addDropTargetListener(defaultDropTargetListener); } catch (TooManyListenersException tmle) { // should not happen... swing drop target is multicast } } } private void fetchStyle(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); } } context.dispose(); } /** * Attaches listeners to the JTable. */ protected void installListeners() { focusListener = createFocusListener(); keyListener = createKeyListener(); mouseInputListener = createMouseInputListener(); propertyChangeListener = createPropertyChangeListener(); table.addFocusListener(focusListener); table.addKeyListener(keyListener); table.addMouseListener(defaultDragRecognizer); table.addMouseMotionListener(defaultDragRecognizer); table.addMouseListener(mouseInputListener); table.addMouseMotionListener(mouseInputListener); table.addPropertyChangeListener(propertyChangeListener); } /** * Register all keyboard actions on the JTable. */ protected void installKeyboardActions() { LazyActionMap.installLazyActionMap(table, SynthTableUI.class, "Table.actionMap"); InputMap inputMap = getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(table, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { SynthContext context = getContext(table, ENABLED); InputMap keyMap = (InputMap)style.get(context, "Table.ancestorInputMap"); InputMap rtlKeyMap; if (!table.getComponentOrientation().isLeftToRight() && ((rtlKeyMap = (InputMap)style.get(context, "Table.ancestorInputMap.RightToLeft")) == null)) { rtlKeyMap.setParent(keyMap); keyMap = rtlKeyMap; } context.dispose(); return keyMap; } return null; }// Uninstallation public void uninstallUI(JComponent c) { uninstallDefaults(); uninstallListeners(); uninstallKeyboardActions(); table.remove(rendererPane); rendererPane = null; table = null; } protected void uninstallDefaults() { if (table.getTransferHandler() instanceof UIResource) { table.setTransferHandler(null); } SynthContext context = getContext(table, ENABLED); style.uninstallDefaults(context); context.dispose(); style = null; } protected void uninstallListeners() { table.removeFocusListener(focusListener); table.removeKeyListener(keyListener); table.removeMouseListener(defaultDragRecognizer); table.removeMouseMotionListener(defaultDragRecognizer); table.removeMouseListener(mouseInputListener); table.removeMouseMotionListener(mouseInputListener); table.removePropertyChangeListener(propertyChangeListener); focusListener = null; keyListener = null; mouseInputListener = null; propertyChangeListener = null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIInputMap(table, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIActionMap(table, null); } // // 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 Dimension createTableSize(long width) { int height = 0; int rowCount = table.getRowCount(); if (rowCount > 0 && table.getColumnCount() > 0) { Rectangle r = table.getCellRect(rowCount-1, 0, true); height = r.y + r.height; } // Width is always positive. The call to abs() is a workaround for // a bug in the 1.1.6 JIT on Windows. long tmp = Math.abs(width); if (tmp > Integer.MAX_VALUE) { tmp = Integer.MAX_VALUE; } return new Dimension((int)tmp, height); } /** * Return the minimum size of the table. The minimum height is the * row height times the number of rows. * The minimum width is the sum of the minimum widths of each column. */ public Dimension getMinimumSize(JComponent c) { long width = 0; Enumeration enumeration = table.getColumnModel().getColumns(); while (enumeration.hasMoreElements()) { TableColumn aColumn = (TableColumn)enumeration.nextElement(); width = width + aColumn.getMinWidth(); } return createTableSize(width); } /** * Return the preferred size of the table. The preferred height is the * row height times the number of rows. * The preferred width is the sum of the preferred widths of each column. */ public Dimension getPreferredSize(JComponent c) { long width = 0; Enumeration enumeration = table.getColumnModel().getColumns(); while (enumeration.hasMoreElements()) { TableColumn aColumn = (TableColumn)enumeration.nextElement(); width = width + aColumn.getPreferredWidth(); } return createTableSize(width); } /** * Return the maximum size of the table. The maximum height is the * row heighttimes the number of rows. * The maximum width is the sum of the maximum widths of each column. */ public Dimension getMaximumSize(JComponent c) { long width = 0; Enumeration enumeration = table.getColumnModel().getColumns(); while (enumeration.hasMoreElements()) { TableColumn aColumn = (TableColumn)enumeration.nextElement(); width = width + aColumn.getMaxWidth(); } return createTableSize(width); }//// 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 (table.getRowCount() <= 0 || table.getColumnCount() <= 0) { return; } Rectangle clip = g.getClipBounds(); Point upperLeft = clip.getLocation(); Point lowerRight = new Point(clip.x + clip.width - 1, clip.y + clip.height - 1); int rMin = table.rowAtPoint(upperLeft); int rMax = table.rowAtPoint(lowerRight); // This should never happen. if (rMin == -1) { rMin = 0; } // If the table does not have enough rows to fill the view we'll get -1. // Replace this with the index of the last row. if (rMax == -1) { rMax = table.getRowCount()-1; } boolean ltr = table.getComponentOrientation().isLeftToRight(); 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); // PENDING: We may want to add a special Renderer for painting // the background of the selected cells. } /* * Paints the grid lines within <I>aRect</I>, using the grid * color set with <I>setGridColor</I>. Paints vertical lines * if <code>getShowVerticalLines()</code> returns true and paints * horizontal lines if <code>getShowHorizontalLines()</code> * returns true. */ private void paintGrid(SynthContext context, Graphics g, int rMin, int rMax, int cMin, int cMax) { g.setColor(table.getGridColor()); Rectangle minCell = table.getCellRect(rMin, cMin, true); Rectangle maxCell = table.getCellRect(rMax, cMax, true); Rectangle damagedArea = minCell.union( maxCell ); SynthGraphics synthG = context.getStyle().getSynthGraphics(context); if (table.getShowHorizontalLines()) { int tableWidth = damagedArea.x + damagedArea.width; int y = damagedArea.y; for (int row = rMin; row <= rMax; row++) { y += table.getRowHeight(row); synthG.drawLine(context, "Table.grid", g, damagedArea.x, y - 1, tableWidth - 1,y - 1); } } if (table.getShowVerticalLines()) { TableColumnModel cm = table.getColumnModel(); int tableHeight = damagedArea.y + damagedArea.height; int x; if (table.getComponentOrientation().isLeftToRight()) { x = damagedArea.x; for (int column = cMin; column <= cMax; column++) { int w = cm.getColumn(column).getWidth(); x += w; synthG.drawLine(context, "Table.grid", g, x - 1, 0, x - 1, tableHeight - 1); } } else { x = damagedArea.x + damagedArea.width; for (int column = cMin; column < cMax; column++) { int w = cm.getColumn(column).getWidth(); x -= w; synthG.drawLine(context, "Table.grid", g, x - 1, 0, x - 1, tableHeight - 1); } x -= cm.getColumn(cMax).getWidth(); synthG.drawLine(context, "Table.grid", g, x, 0, x, tableHeight - 1); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?