home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.Action;
- import com.sun.java.swing.CellRendererPane;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JScrollPane;
- import com.sun.java.swing.JTable;
- import com.sun.java.swing.JTextField;
- import com.sun.java.swing.JViewport;
- import com.sun.java.swing.KeyStroke;
- import com.sun.java.swing.ListSelectionModel;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.SwingUtilities;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.TableUI;
- import com.sun.java.swing.plaf.UIResource;
- import com.sun.java.swing.table.JTableHeader;
- import com.sun.java.swing.table.TableCellEditor;
- import com.sun.java.swing.table.TableCellRenderer;
- import com.sun.java.swing.table.TableColumn;
- import com.sun.java.swing.text.JTextComponent;
- import com.sun.java.swing.text.Keymap;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.io.Serializable;
- import java.util.Enumeration;
-
- public class BasicTableUI extends TableUI implements MouseListener, MouseMotionListener, FocusListener, Serializable {
- protected JTable table;
- private boolean phantomMousePressed = false;
- private KeyListener tableKeyListener;
- protected transient Component dispatchComponent;
- protected CellRendererPane rendererPane;
-
- private void clearSelection() {
- this.clearSelection(this.table.getColumnModel().getSelectionModel());
- this.clearSelection(this.table.getSelectionModel());
- }
-
- private void clearSelection(ListSelectionModel sm) {
- sm.removeSelectionInterval(sm.getMinSelectionIndex(), sm.getMaxSelectionIndex());
- }
-
- private void configureTable() {
- LookAndFeel.installColorsAndFont(this.table, "Table.background", "Table.foreground", "Table.font");
- Color sbg = this.table.getSelectionBackground();
- if (sbg == null || sbg instanceof UIResource) {
- this.table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
- }
-
- Color sfg = this.table.getSelectionForeground();
- if (sfg == null || sfg instanceof UIResource) {
- this.table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
- }
-
- Color gridColor = this.table.getGridColor();
- if (gridColor == null || gridColor instanceof UIResource) {
- this.table.setGridColor(UIManager.getColor("Table.gridColor"));
- }
-
- Container parent = this.table.getParent();
- if (parent != null) {
- parent = ((Component)parent).getParent();
- if (parent != null && parent instanceof JScrollPane) {
- LookAndFeel.installBorder((JScrollPane)parent, "Table.scrollPaneBorder");
- }
- }
-
- }
-
- public static ComponentUI createUI(JComponent c) {
- return new BasicTableUI();
- }
-
- private void dispatchKeyboardEvent(KeyEvent e) {
- int selectedRow = this.table.getSelectedRow();
- int selectedColumn = this.table.getSelectedColumn();
- if (selectedRow != -1 && selectedColumn != -1 && !this.table.isEditing()) {
- boolean editing = this.table.editCellAt(selectedRow, selectedColumn);
- this.table.requestFocus();
- if (!editing) {
- return;
- }
- }
-
- Component editorComp = this.table.getEditorComponent();
- if (this.table.isEditing() && editorComp != null) {
- char keyChar = e.getKeyChar();
- if (editorComp instanceof JTextField) {
- JTextField textField = (JTextField)editorComp;
- Keymap keyMap = ((JTextComponent)textField).getKeymap();
- KeyStroke key = KeyStroke.getKeyStroke(keyChar, 0);
- Action action = keyMap.getAction(key);
- if (action == null) {
- action = keyMap.getDefaultAction();
- }
-
- if (action != null) {
- ActionEvent ae = new ActionEvent(textField, 1001, String.valueOf(keyChar));
- action.actionPerformed(ae);
- }
- }
- }
-
- }
-
- protected void drawGridInClipRect(Rectangle rect, Graphics g) {
- g.setColor(this.table.getGridColor());
- if (this.table.getShowHorizontalLines()) {
- this.drawHorizontalLinesInClipRect(rect, g);
- }
-
- if (this.table.getShowVerticalLines()) {
- this.drawVerticalLinesInClipRect(rect, g);
- }
-
- }
-
- protected void drawHorizontalLinesInClipRect(Rectangle rect, Graphics g) {
- int delta = this.table.getRowHeight() + this.table.getIntercellSpacing().height;
- Rectangle r = g.getClipBounds();
- int firstIndex = this.table.rowAtPoint(new Point(0, r.y));
- int lastIndex = this.lastVisibleRow(r);
- int y = delta * firstIndex + (delta - 1);
-
- for(int index = firstIndex; index <= lastIndex; ++index) {
- if (y >= rect.y && y <= rect.y + rect.height) {
- g.drawLine(rect.x, y, rect.x + rect.width - 1, y);
- }
-
- y += delta;
- }
-
- }
-
- protected void drawRowInClipRect(int row, Rectangle rect, Graphics g) {
- int column = 0;
- boolean drawn = false;
- Rectangle draggedCellRect = null;
- int draggedColumnIndex = -1;
- Enumeration enumeration = this.table.getColumnModel().getColumns();
- Dimension spacing = this.table.getIntercellSpacing();
- JTableHeader header = this.table.getTableHeader();
- Rectangle cellRect = new Rectangle();
- cellRect.height = this.table.getRowHeight() + spacing.height;
-
- for(cellRect.y = row * cellRect.height; enumeration.hasMoreElements(); ++column) {
- TableColumn aColumn = (TableColumn)enumeration.nextElement();
- cellRect.width = aColumn.getWidth() + spacing.width;
- if (cellRect.intersects(rect)) {
- drawn = true;
- if (header != null && aColumn == header.getDraggedColumn()) {
- g.setColor(this.table.getParent().getBackground());
- g.fillRect(cellRect.x, cellRect.y, cellRect.width, cellRect.height);
- draggedCellRect = new Rectangle(cellRect);
- draggedColumnIndex = column;
- } else {
- TableCellRenderer renderer = this.getCellRenderer(column);
- Component component = this.prepareRenderer(renderer, this.table, row, column);
- this.drawWithComponent(g, component, cellRect);
- }
- } else if (drawn) {
- break;
- }
-
- cellRect.x += cellRect.width;
- }
-
- if (draggedColumnIndex != -1 && draggedCellRect != null) {
- TableCellRenderer var20 = this.getCellRenderer(draggedColumnIndex);
- if (var20 != null) {
- Component component = this.prepareRenderer(var20, this.table, row, draggedColumnIndex);
- draggedCellRect.x += header.getDraggedDistance();
- Color bColor = this.table.getBackground();
- if (bColor != null) {
- g.setColor(bColor);
- g.fillRect(draggedCellRect.x, draggedCellRect.y, draggedCellRect.width, draggedCellRect.height);
- }
-
- g.setColor(this.table.getGridColor());
- int x1 = draggedCellRect.x;
- int y1 = draggedCellRect.y;
- int x2 = x1 + draggedCellRect.width - 1;
- int y2 = y1 + draggedCellRect.height - 1;
- if (this.table.getShowVerticalLines()) {
- g.drawLine(x2, y1, x2, y2);
- }
-
- if (this.table.getShowHorizontalLines()) {
- g.drawLine(x1, y2, x2, y2);
- }
-
- this.drawWithComponent(g, component, draggedCellRect);
- }
- }
-
- }
-
- protected void drawVerticalLinesInClipRect(Rectangle rect, Graphics g) {
- int x = 0;
- int count = this.table.getColumnCount();
-
- for(int index = 0; index <= count; ++index) {
- if (x > 0 && x - 1 >= rect.x && x - 1 <= rect.x + rect.width) {
- g.drawLine(x - 1, rect.y, x - 1, rect.y + rect.height - 1);
- }
-
- if (index < count) {
- x += this.table.getColumnModel().getColumn(index).getWidth() + this.table.getIntercellSpacing().width;
- }
- }
-
- }
-
- protected void drawWithComponent(Graphics g, Component component, Rectangle cellRect) {
- Dimension spacing = this.table.getIntercellSpacing();
- cellRect.setBounds(cellRect.x + spacing.width / 2, cellRect.y + spacing.height / 2, cellRect.width - spacing.width, cellRect.height - spacing.height);
- if (component.getParent() == null) {
- this.rendererPane.add(component);
- }
-
- this.rendererPane.paintComponent(g, component, this.table, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
- cellRect.setBounds(cellRect.x - spacing.width / 2, cellRect.y - spacing.height / 2, cellRect.width + spacing.width, cellRect.height + spacing.height);
- }
-
- public void focusGained(FocusEvent e) {
- if (this.table.getSelectedColumn() == -1) {
- this.table.setColumnSelectionInterval(0, 0);
- }
-
- if (this.table.getSelectedRow() == -1) {
- this.table.setRowSelectionInterval(0, 0);
- }
-
- this.repaintAnchorCell();
- }
-
- public void focusLost(FocusEvent e) {
- this.repaintAnchorCell();
- }
-
- protected TableCellRenderer getCellRenderer(int column) {
- TableColumn tableColumn = this.table.getColumnModel().getColumn(column);
- TableCellRenderer renderer = tableColumn.getCellRenderer();
- if (renderer == null) {
- Class columnClass = this.table.getColumnClass(column);
- renderer = this.table.getDefaultRenderer(columnClass);
- }
-
- return renderer;
- }
-
- public Dimension getMaximumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getMinimumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- JTable table = (JTable)c;
- Dimension size = new Dimension();
- int mode = table.getAutoResizeMode();
- Component parent = ((Component)table).getParent();
- if (mode != 0 && parent instanceof JViewport) {
- size.width = parent.getBounds().width;
- table.sizeColumnsToFit(mode == 1);
- } else {
- size.width = table.getColumnModel().getTotalColumnWidth();
- }
-
- size.height = table.getRowCount() * (table.getRowHeight() + table.getIntercellSpacing().height);
- return size;
- }
-
- public void installUI(JComponent c) {
- this.table = (JTable)c;
- ((Component)c).addMouseListener(this);
- ((Component)c).addMouseMotionListener(this);
- ((Component)c).addFocusListener(this);
- this.rendererPane = new CellRendererPane();
- this.table.add(this.rendererPane);
- this.configureTable();
- this.registerKeyboardActions();
- }
-
- private int lastVisibleRow(Rectangle clipRect) {
- int lastIndex = this.table.rowAtPoint(new Point(0, clipRect.y + clipRect.height - 1));
- if (lastIndex == -1) {
- lastIndex = this.table.getRowCount() - 1;
- }
-
- return lastIndex;
- }
-
- public void mouseClicked(MouseEvent e) {
- }
-
- public void mouseDragged(MouseEvent e) {
- if (this.table.isEditing()) {
- if (this.dispatchComponent != null) {
- this.dispatchComponent.dispatchEvent(SwingUtilities.convertMouseEvent(this.table, e, this.dispatchComponent));
- }
-
- } else {
- Point p = e.getPoint();
- int hitRowIndex = this.table.rowAtPoint(p);
- int hitColumnIndex = this.table.columnAtPoint(p);
- if (hitColumnIndex != -1 && hitRowIndex != -1) {
- this.updateSelection(hitRowIndex, hitColumnIndex, false, false);
- }
- }
- }
-
- public void mouseEntered(MouseEvent e) {
- if (this.dispatchComponent != null) {
- this.dispatchComponent = null;
- }
-
- }
-
- public void mouseExited(MouseEvent e) {
- if (this.dispatchComponent != null) {
- this.dispatchComponent = null;
- }
-
- }
-
- public void mouseMoved(MouseEvent e) {
- if (this.dispatchComponent != null) {
- this.dispatchComponent = null;
- }
-
- }
-
- public void mousePressed(MouseEvent e) {
- if (!this.phantomMousePressed) {
- this.phantomMousePressed = true;
- Point p = e.getPoint();
- int hitRowIndex = this.table.rowAtPoint(p);
- int hitColumnIndex = this.table.columnAtPoint(p);
- if (hitColumnIndex != -1 && hitRowIndex != -1) {
- Rectangle cellRect = this.table.getCellRect(hitRowIndex, hitColumnIndex, false);
- if (cellRect.contains(p)) {
- this.table.editCellAt(hitRowIndex, hitColumnIndex, e);
- }
-
- if (!this.table.isEditing()) {
- this.table.requestFocus();
- boolean controlKeyDown = ((InputEvent)e).isControlDown();
- boolean shiftKeyDown = ((InputEvent)e).isShiftDown();
- boolean deselect = controlKeyDown && this.table.isCellSelected(hitRowIndex, hitColumnIndex);
- if (!controlKeyDown && !shiftKeyDown) {
- this.clearSelection();
- }
-
- this.updateSelection(hitRowIndex, hitColumnIndex, deselect, !shiftKeyDown || deselect);
- } else {
- Component editorComp = this.table.getEditorComponent();
- Point componentPoint = SwingUtilities.convertPoint(this.table, new Point(e.getX(), e.getY()), editorComp);
- this.dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComp, componentPoint.x, componentPoint.y);
- this.dispatchComponent.dispatchEvent(SwingUtilities.convertMouseEvent(this.table, e, this.dispatchComponent));
- }
-
- }
- }
- }
-
- public void mouseReleased(MouseEvent e) {
- this.phantomMousePressed = false;
- if (this.table.isEditing()) {
- if (this.dispatchComponent != null) {
- this.dispatchComponent.dispatchEvent(SwingUtilities.convertMouseEvent(this.table, e, this.dispatchComponent));
- }
-
- this.dispatchComponent = null;
- }
- }
-
- private void moveAnchor(int row, int column) {
- if (this.table.isEditing()) {
- TableCellEditor cellEditor = this.table.getCellEditor();
- if (cellEditor != null) {
- boolean stopped = cellEditor.stopCellEditing();
- if (!stopped) {
- return;
- }
- }
- }
-
- this.updateSelection(row, column, false, true);
- }
-
- public void paint(Graphics g, JComponent c) {
- JTable table = (JTable)c;
- Rectangle paintBounds = g.getClipBounds();
- ((Component)table).getSize();
- if (table.getColumnModel() != null) {
- Dimension pSize = this.getPreferredSize(table);
- Rectangle tableRect = new Rectangle(0, 0, pSize.width, pSize.height);
- tableRect = tableRect.intersection(paintBounds);
- g.setColor(((Component)table).getParent().getBackground());
- g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
- if (tableRect.height >= 0 && tableRect.width >= 0) {
- Color bColor = ((Component)table).getBackground();
- if (bColor != null) {
- g.setColor(bColor);
- g.fillRect(tableRect.x, tableRect.y, tableRect.width, tableRect.height);
- }
-
- this.drawGridInClipRect(tableRect, g);
- Rectangle r = g.getClipBounds();
- int firstIndex = table.rowAtPoint(new Point(0, r.y));
- int lastIndex = this.lastVisibleRow(r);
- Rectangle rowRect = new Rectangle(0, 0, table.getColumnModel().getTotalColumnWidth(), table.getRowHeight() + table.getIntercellSpacing().height);
- rowRect.y = firstIndex * rowRect.height;
-
- for(int index = firstIndex; index <= lastIndex; ++index) {
- if (rowRect.intersects(tableRect)) {
- Rectangle intersection = rowRect.intersection(tableRect);
- this.drawRowInClipRect(index, intersection, g);
- }
-
- rowRect.y += rowRect.height;
- }
-
- }
- }
- }
-
- protected Component prepareRenderer(TableCellRenderer renderer, JTable table, int row, int column) {
- Object value = table.getValueAt(row, column);
- boolean isSelected = table.isCellSelected(row, column);
- boolean rowIsAnchor = table.getSelectedRow() == row;
- boolean columnIsAnchor = table.getSelectedColumn() == column;
- boolean hasFocus = rowIsAnchor && columnIsAnchor && ((JComponent)table).hasFocus();
- return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
- }
-
- private void registerArrowKey(int keyEvent, int dx, int dy) {
- this.table.registerKeyboardAction(new ArrowKeyAction(this, dx, dy), KeyStroke.getKeyStroke(keyEvent, 0), 0);
- }
-
- private void registerKeyboardActions() {
- this.registerArrowKey(39, 1, 0);
- this.registerArrowKey(37, -1, 0);
- this.registerArrowKey(38, 0, -1);
- this.registerArrowKey(40, 0, 1);
- this.tableKeyListener = new TableKeyListener(this);
- this.table.addKeyListener(this.tableKeyListener);
- }
-
- private void repaintAnchorCell() {
- int anchorRow = this.table.getSelectedRow();
- int anchorColumn = this.table.getSelectedColumn();
- Rectangle dirtyRect = this.table.getCellRect(anchorRow, anchorColumn, false);
- this.table.repaint(dirtyRect);
- }
-
- public void uninstallUI(JComponent c) {
- this.table.remove(this.rendererPane);
- ((Component)c).removeMouseListener(this);
- ((Component)c).removeMouseMotionListener(this);
- ((Component)c).removeFocusListener(this);
- this.unregisterKeyboardActions();
- this.rendererPane = null;
- this.table = null;
- }
-
- private void unregisterKeyboardActions() {
- this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(38, 0));
- this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(40, 0));
- this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(39, 0));
- this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(37, 0));
- this.table.removeKeyListener(this.tableKeyListener);
- }
-
- private void updateSelection(int rowIndex, int columnIndex, boolean deselect, boolean reAnchor) {
- Rectangle cellRect = this.table.getCellRect(rowIndex, columnIndex, false);
- if (cellRect != null) {
- this.table.scrollRectToVisible(cellRect);
- }
-
- ListSelectionModel rsm = this.table.getSelectionModel();
- ListSelectionModel csm = this.table.getColumnModel().getSelectionModel();
- this.updateSelectionModel(csm, columnIndex, deselect, reAnchor);
- this.updateSelectionModel(rsm, rowIndex, deselect, reAnchor);
- }
-
- private void updateSelectionModel(ListSelectionModel sm, int index, boolean deselect, boolean reAnchor) {
- if (reAnchor) {
- if (deselect) {
- sm.removeSelectionInterval(index, index);
- return;
- }
-
- sm.addSelectionInterval(index, index);
- }
-
- sm.setLeadSelectionIndex(index);
- }
-
- static void access$clearSelection(BasicTableUI var0, ListSelectionModel var1) {
- var0.clearSelection(var1);
- }
-
- static void access$moveAnchor(BasicTableUI var0, int var1, int var2) {
- var0.moveAnchor(var1, var2);
- }
-
- static void access$dispatchKeyboardEvent(BasicTableUI var0, KeyEvent var1) {
- var0.dispatchKeyboardEvent(var1);
- }
- }
-