/* * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.table; import sun.swing.table.DefaultTableCellHeaderRenderer; import java.util.*;import java.awt.*;import java.awt.event.*; import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.*;import javax.accessibility.*; import java.beans.PropertyChangeListener;import java.beans.Transient; import java.io.ObjectOutputStream;import java.io.ObjectInputStream;import java.io.IOException; /** * This is the object which manages the header of the <code>JTable</code>. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @author Alan Chung * @author Philip Milne * @see javax.swing.JTable */public class JTableHeader extends JComponent implements TableColumnModelListener, Accessible{ /** * @see #getUIClassID * @see #readObject */ private static final String uiClassID = "TableHeaderUI"; //// Instance Variables// /** * The table for which this object is the header; * the default is <code>null</code>. */ protected JTable table; /** * The <code>TableColumnModel</code> of the table header. */ protected TableColumnModel columnModel; /** * If true, reordering of columns are allowed by the user; * the default is true. */ protected boolean reorderingAllowed; /** * If true, resizing of columns are allowed by the user; * the default is true. */ protected boolean resizingAllowed; /** * Obsolete as of Java 2 platform v1.3. Real time repaints, in response * to column dragging or resizing, are now unconditional. */ /* * If this flag is true, then the header will repaint the table as * a column is dragged or resized; the default is true. */ protected boolean updateTableInRealTime; /** The index of the column being resized. <code>null</code> if not resizing. */ transient protected TableColumn resizingColumn; /** The index of the column being dragged. <code>null</code> if not dragging. */ transient protected TableColumn draggedColumn; /** The distance from its original position the column has been dragged. */ transient protected int draggedDistance; /** * The default renderer to be used when a <code>TableColumn</code> * does not define a <code>headerRenderer</code>. */ private TableCellRenderer defaultRenderer; //// Constructors// /** * Constructs a <code>JTableHeader</code> with a default * <code>TableColumnModel</code>. * * @see #createDefaultColumnModel */ public JTableHeader() { this(null); } /** * Constructs a <code>JTableHeader</code> which is initialized with * <code>cm</code> as the column model. If <code>cm</code> is * <code>null</code> this method will initialize the table header * with a default <code>TableColumnModel</code>. * * @param cm the column model for the table * @see #createDefaultColumnModel */ public JTableHeader(TableColumnModel cm) { super(); //setFocusable(false); // for strict win/mac compatibility mode, // this method should be invoked if (cm == null) cm = createDefaultColumnModel(); setColumnModel(cm); // Initialize local ivars initializeLocalVars(); // Get UI going updateUI(); } //// Local behavior attributes// /** * Sets the table associated with this header. * @param table the new table * @beaninfo * bound: true * description: The table associated with this header. */ public void setTable(JTable table) { JTable old = this.table; this.table = table; firePropertyChange("table", old, table); } /** * Returns the table associated with this header. * @return the <code>table</code> property */ public JTable getTable() { return table; } /** * Sets whether the user can drag column headers to reorder columns. * * @param reorderingAllowed true if the table view should allow * reordering; otherwise false * @see #getReorderingAllowed * @beaninfo * bound: true * description: Whether the user can drag column headers to reorder columns. */ public void setReorderingAllowed(boolean reorderingAllowed) { boolean old = this.reorderingAllowed; this.reorderingAllowed = reorderingAllowed; firePropertyChange("reorderingAllowed", old, reorderingAllowed); } /** * Returns true if the user is allowed to rearrange columns by * dragging their headers, false otherwise. The default is true. You can * rearrange columns programmatically regardless of this setting. * * @return the <code>reorderingAllowed</code> property * @see #setReorderingAllowed */ public boolean getReorderingAllowed() { return reorderingAllowed; } /** * Sets whether the user can resize columns by dragging between headers. * * @param resizingAllowed true if table view should allow * resizing * @see #getResizingAllowed * @beaninfo * bound: true * description: Whether the user can resize columns by dragging between headers. */ public void setResizingAllowed(boolean resizingAllowed) { boolean old = this.resizingAllowed; this.resizingAllowed = resizingAllowed; firePropertyChange("resizingAllowed", old, resizingAllowed); } /** * Returns true if the user is allowed to resize columns by dragging * between their headers, false otherwise. The default is true. You can * resize columns programmatically regardless of this setting. * * @return the <code>resizingAllowed</code> property * @see #setResizingAllowed */ public boolean getResizingAllowed() { return resizingAllowed; } /** * Returns the the dragged column, if and only if, a drag is in * process, otherwise returns <code>null</code>. * * @return the dragged column, if a drag is in * process, otherwise returns <code>null</code> * @see #getDraggedDistance */ public TableColumn getDraggedColumn() { return draggedColumn; } /** * Returns the column's horizontal distance from its original * position, if and only if, a drag is in process. Otherwise, the * the return value is meaningless. * * @return the column's horizontal distance from its original * position, if a drag is in process, otherwise the return * value is meaningless * @see #getDraggedColumn */ public int getDraggedDistance() { return draggedDistance; } /** * Returns the resizing column. If no column is being * resized this method returns <code>null</code>. * * @return the resizing column, if a resize is in process, otherwise * returns <code>null</code> */ public TableColumn getResizingColumn() { return resizingColumn; } /** * Obsolete as of Java 2 platform v1.3. Real time repaints, in response to * column dragging or resizing, are now unconditional. */ /* * Sets whether the body of the table updates in real time when * a column is resized or dragged. * * @param flag true if tableView should update * the body of the table in real time * @see #getUpdateTableInRealTime */ public void setUpdateTableInRealTime(boolean flag) { updateTableInRealTime = flag; } /** * Obsolete as of Java 2 platform v1.3. Real time repaints, in response to * column dragging or resizing, are now unconditional. */ /* * Returns true if the body of the table view updates in real * time when a column is resized or dragged. User can set this flag to * false to speed up the table's response to user resize or drag actions. * The default is true. * * @return true if the table updates in real time * @see #setUpdateTableInRealTime */ public boolean getUpdateTableInRealTime() { return updateTableInRealTime; } /** * Sets the default renderer to be used when no <code>headerRenderer</code> * is defined by a <code>TableColumn</code>. * @param defaultRenderer the default renderer * @since 1.3 */ public void setDefaultRenderer(TableCellRenderer defaultRenderer) { this.defaultRenderer = defaultRenderer; } /** * Returns the default renderer used when no <code>headerRenderer</code> * is defined by a <code>TableColumn</code>. * @return the default renderer * @since 1.3 */ @Transient public TableCellRenderer getDefaultRenderer() { return defaultRenderer; } /** * Returns the index of the column that <code>point</code> lies in, or -1 if it * lies out of bounds. * * @return the index of the column that <code>point</code> lies in, or -1 if it * lies out of bounds */ public int columnAtPoint(Point point) { int x = point.x; if (!getComponentOrientation().isLeftToRight()) { x = getWidthInRightToLeft() - x - 1; } return getColumnModel().getColumnIndexAtX(x); } /** * Returns the rectangle containing the header tile at <code>column</code>. * When the <code>column</code> parameter is out of bounds this method uses the * same conventions as the <code>JTable</code> method <code>getCellRect</code>. * * @return the rectangle containing the header tile at <code>column</code> * @see JTable#getCellRect */ public Rectangle getHeaderRect(int column) { Rectangle r = new Rectangle(); TableColumnModel cm = getColumnModel(); r.height = getHeight(); if (column < 0) { // x = width = 0; if( !getComponentOrientation().isLeftToRight() ) { r.x = getWidthInRightToLeft(); } } else if (column >= cm.getColumnCount()) { if( getComponentOrientation().isLeftToRight() ) { r.x = getWidth(); } } else { for(int i = 0; i < column; i++) { r.x += cm.getColumn(i).getWidth(); } if( !getComponentOrientation().isLeftToRight() ) { r.x = getWidthInRightToLeft() - r.x - cm.getColumn(column).getWidth(); } r.width = cm.getColumn(column).getWidth(); } return r; } /** * Allows the renderer's tips to be used if there is text set. * @param event the location of the event identifies the proper * renderer and, therefore, the proper tip * @return the tool tip for this component */ public String getToolTipText(MouseEvent event) { String tip = null; Point p = event.getPoint(); int column; // Locate the renderer under the event location if ((column = columnAtPoint(p)) != -1) { TableColumn aColumn = columnModel.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { renderer = defaultRenderer; } Component component = renderer.getTableCellRendererComponent( getTable(), aColumn.getHeaderValue(), false, false, -1, column); // Now have to see if the component is a JComponent before // getting the tip if (component instanceof JComponent) { // Convert the event to the renderer's coordinate system MouseEvent newEvent; Rectangle cellRect = getHeaderRect(column); p.translate(-cellRect.x, -cellRect.y); newEvent = new MouseEvent(component, event.getID(), event.getWhen(), event.getModifiers(), p.x, p.y, event.getXOnScreen(), event.getYOnScreen(), event.getClickCount(), event.isPopupTrigger(), MouseEvent.NOBUTTON); tip = ((JComponent)component).getToolTipText(newEvent); } } // No tip from the renderer get our own tip if (tip == null) tip = getToolTipText(); return tip; } //// Managing TableHeaderUI// /** * Returns the look and feel (L&F) object that renders this component. * * @return the <code>TableHeaderUI</code> object that renders this component */ public TableHeaderUI getUI() { return (TableHeaderUI)ui; } /** * Sets the look and feel (L&F) object that renders this component. * * @param ui the <code>TableHeaderUI</code> L&F object * @see UIDefaults#getUI */ public void setUI(TableHeaderUI ui){ if (this.ui != ui) { super.setUI(ui); repaint(); } } /** * Notification from the <code>UIManager</code> that the look and feel * (L&F) has changed. * Replaces the current UI object with the latest version from the * <code>UIManager</code>. * * @see JComponent#updateUI */ public void updateUI(){ setUI((TableHeaderUI)UIManager.getUI(this)); TableCellRenderer renderer = getDefaultRenderer(); if (renderer instanceof Component) { SwingUtilities.updateComponentTreeUI((Component)renderer); } } /** * Returns the suffix used to construct the name of the look and feel * (L&F) class used to render this component. * @return the string "TableHeaderUI" * * @return "TableHeaderUI" * @see JComponent#getUIClassID * @see UIDefaults#getUI */ public String getUIClassID() { return uiClassID; } //// Managing models// /** * Sets the column model for this table to <code>newModel</code> and registers * for listener notifications from the new column model.Lines 1–500 of 1472. Load more
/* * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.table; import sun.swing.table.DefaultTableCellHeaderRenderer; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.*; import javax.accessibility.*; import java.beans.PropertyChangeListener; import java.beans.Transient; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.IOException; /** * This is the object which manages the header of the <code>JTable</code>. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @author Alan Chung * @author Philip Milne * @see javax.swing.JTable */ public class JTableHeader extends JComponent implements TableColumnModelListener, Accessible { /** * @see #getUIClassID * @see #readObject */ private static final String uiClassID = "TableHeaderUI"; // // Instance Variables // /** * The table for which this object is the header; * the default is <code>null</code>. */ protected JTable table; /** * The <code>TableColumnModel</code> of the table header. */ protected TableColumnModel columnModel; /** * If true, reordering of columns are allowed by the user; * the default is true. */ protected boolean reorderingAllowed; /** * If true, resizing of columns are allowed by the user; * the default is true. */ protected boolean resizingAllowed; /** * Obsolete as of Java 2 platform v1.3. Real time repaints, in response * to column dragging or resizing, are now unconditional. */ /* * If this flag is true, then the header will repaint the table as * a column is dragged or resized; the default is true. */ protected boolean updateTableInRealTime; /** The index of the column being resized. <code>null</code> if not resizing. */ transient protected TableColumn resizingColumn; /** The index of the column being dragged. <code>null</code> if not dragging. */ transient protected TableColumn draggedColumn; /** The distance from its original position the column has been dragged. */ transient protected int draggedDistance; /** * The default renderer to be used when a <code>TableColumn</code> * does not define a <code>headerRenderer</code>. */ private TableCellRenderer defaultRenderer; // // Constructors // /** * Constructs a <code>JTableHeader</code> with a default * <code>TableColumnModel</code>. * * @see #createDefaultColumnModel */ public JTableHeader() { this(null); } /** * Constructs a <code>JTableHeader</code> which is initialized with * <code>cm</code> as the column model. If <code>cm</code> is * <code>null</code> this method will initialize the table header * with a default <code>TableColumnModel</code>. * * @param cm the column model for the table * @see #createDefaultColumnModel */ public JTableHeader(TableColumnModel cm) { super(); //setFocusable(false); // for strict win/mac compatibility mode, // this method should be invoked if (cm == null) cm = createDefaultColumnModel(); setColumnModel(cm); // Initialize local ivars initializeLocalVars(); // Get UI going updateUI(); } // // Local behavior attributes // /** * Sets the table associated with this header. * @param table the new table * @beaninfo * bound: true * description: The table associated with this header. */ public void setTable(JTable table) { JTable old = this.table; this.table = table; firePropertyChange("table", old, table); } /** * Returns the table associated with this header. * @return the <code>table</code> property */ public JTable getTable() { return table; } /** * Sets whether the user can drag column headers to reorder columns. * * @param reorderingAllowed true if the table view should allow * reordering; otherwise false * @see #getReorderingAllowed * @beaninfo * bound: true * description: Whether the user can drag column headers to reorder columns. */ public void setReorderingAllowed(boolean reorderingAllowed) { boolean old = this.reorderingAllowed; this.reorderingAllowed = reorderingAllowed; firePropertyChange("reorderingAllowed", old, reorderingAllowed); } /** * Returns true if the user is allowed to rearrange columns by * dragging their headers, false otherwise. The default is true. You can * rearrange columns programmatically regardless of this setting. * * @return the <code>reorderingAllowed</code> property * @see #setReorderingAllowed */ public boolean getReorderingAllowed() { return reorderingAllowed; } /** * Sets whether the user can resize columns by dragging between headers. * * @param resizingAllowed true if table view should allow * resizing * @see #getResizingAllowed * @beaninfo * bound: true * description: Whether the user can resize columns by dragging between headers. */ public void setResizingAllowed(boolean resizingAllowed) { boolean old = this.resizingAllowed; this.resizingAllowed = resizingAllowed; firePropertyChange("resizingAllowed", old, resizingAllowed); } /** * Returns true if the user is allowed to resize columns by dragging * between their headers, false otherwise. The default is true. You can * resize columns programmatically regardless of this setting. * * @return the <code>resizingAllowed</code> property * @see #setResizingAllowed */ public boolean getResizingAllowed() { return resizingAllowed; } /** * Returns the the dragged column, if and only if, a drag is in * process, otherwise returns <code>null</code>. * * @return the dragged column, if a drag is in * process, otherwise returns <code>null</code> * @see #getDraggedDistance */ public TableColumn getDraggedColumn() { return draggedColumn; } /** * Returns the column's horizontal distance from its original * position, if and only if, a drag is in process. Otherwise, the * the return value is meaningless. * * @return the column's horizontal distance from its original * position, if a drag is in process, otherwise the return * value is meaningless * @see #getDraggedColumn */ public int getDraggedDistance() { return draggedDistance; } /** * Returns the resizing column. If no column is being * resized this method returns <code>null</code>. * * @return the resizing column, if a resize is in process, otherwise * returns <code>null</code> */ public TableColumn getResizingColumn() { return resizingColumn; } /** * Obsolete as of Java 2 platform v1.3. Real time repaints, in response to * column dragging or resizing, are now unconditional. */ /* * Sets whether the body of the table updates in real time when * a column is resized or dragged. * * @param flag true if tableView should update * the body of the table in real time * @see #getUpdateTableInRealTime */ public void setUpdateTableInRealTime(boolean flag) { updateTableInRealTime = flag; } /** * Obsolete as of Java 2 platform v1.3. Real time repaints, in response to * column dragging or resizing, are now unconditional. */ /* * Returns true if the body of the table view updates in real * time when a column is resized or dragged. User can set this flag to * false to speed up the table's response to user resize or drag actions. * The default is true. * * @return true if the table updates in real time * @see #setUpdateTableInRealTime */ public boolean getUpdateTableInRealTime() { return updateTableInRealTime; } /** * Sets the default renderer to be used when no <code>headerRenderer</code> * is defined by a <code>TableColumn</code>. * @param defaultRenderer the default renderer * @since 1.3 */ public void setDefaultRenderer(TableCellRenderer defaultRenderer) { this.defaultRenderer = defaultRenderer; } /** * Returns the default renderer used when no <code>headerRenderer</code> * is defined by a <code>TableColumn</code>. * @return the default renderer * @since 1.3 */ @Transient public TableCellRenderer getDefaultRenderer() { return defaultRenderer; } /** * Returns the index of the column that <code>point</code> lies in, or -1 if it * lies out of bounds. * * @return the index of the column that <code>point</code> lies in, or -1 if it * lies out of bounds */ public int columnAtPoint(Point point) { int x = point.x; if (!getComponentOrientation().isLeftToRight()) { x = getWidthInRightToLeft() - x - 1; } return getColumnModel().getColumnIndexAtX(x); } /** * Returns the rectangle containing the header tile at <code>column</code>. * When the <code>column</code> parameter is out of bounds this method uses the * same conventions as the <code>JTable</code> method <code>getCellRect</code>. * * @return the rectangle containing the header tile at <code>column</code> * @see JTable#getCellRect */ public Rectangle getHeaderRect(int column) { Rectangle r = new Rectangle(); TableColumnModel cm = getColumnModel(); r.height = getHeight(); if (column < 0) { // x = width = 0; if( !getComponentOrientation().isLeftToRight() ) { r.x = getWidthInRightToLeft(); } } else if (column >= cm.getColumnCount()) { if( getComponentOrientation().isLeftToRight() ) { r.x = getWidth(); } } else { for(int i = 0; i < column; i++) { r.x += cm.getColumn(i).getWidth(); } if( !getComponentOrientation().isLeftToRight() ) { r.x = getWidthInRightToLeft() - r.x - cm.getColumn(column).getWidth(); } r.width = cm.getColumn(column).getWidth(); } return r; } /** * Allows the renderer's tips to be used if there is text set. * @param event the location of the event identifies the proper * renderer and, therefore, the proper tip * @return the tool tip for this component */ public String getToolTipText(MouseEvent event) { String tip = null; Point p = event.getPoint(); int column; // Locate the renderer under the event location if ((column = columnAtPoint(p)) != -1) { TableColumn aColumn = columnModel.getColumn(column); TableCellRenderer renderer = aColumn.getHeaderRenderer(); if (renderer == null) { renderer = defaultRenderer; } Component component = renderer.getTableCellRendererComponent( getTable(), aColumn.getHeaderValue(), false, false, -1, column); // Now have to see if the component is a JComponent before // getting the tip if (component instanceof JComponent) { // Convert the event to the renderer's coordinate system MouseEvent newEvent; Rectangle cellRect = getHeaderRect(column); p.translate(-cellRect.x, -cellRect.y); newEvent = new MouseEvent(component, event.getID(), event.getWhen(), event.getModifiers(), p.x, p.y, event.getXOnScreen(), event.getYOnScreen(), event.getClickCount(), event.isPopupTrigger(), MouseEvent.NOBUTTON); tip = ((JComponent)component).getToolTipText(newEvent); } } // No tip from the renderer get our own tip if (tip == null) tip = getToolTipText(); return tip; } // // Managing TableHeaderUI // /** * Returns the look and feel (L&F) object that renders this component. * * @return the <code>TableHeaderUI</code> object that renders this component */ public TableHeaderUI getUI() { return (TableHeaderUI)ui; } /** * Sets the look and feel (L&F) object that renders this component. * * @param ui the <code>TableHeaderUI</code> L&F object * @see UIDefaults#getUI */ public void setUI(TableHeaderUI ui){ if (this.ui != ui) { super.setUI(ui); repaint(); } } /** * Notification from the <code>UIManager</code> that the look and feel * (L&F) has changed. * Replaces the current UI object with the latest version from the * <code>UIManager</code>. * * @see JComponent#updateUI */ public void updateUI(){ setUI((TableHeaderUI)UIManager.getUI(this)); TableCellRenderer renderer = getDefaultRenderer(); if (renderer instanceof Component) { SwingUtilities.updateComponentTreeUI((Component)renderer); } } /** * Returns the suffix used to construct the name of the look and feel * (L&F) class used to render this component. * @return the string "TableHeaderUI" * * @return "TableHeaderUI" * @see JComponent#getUIClassID * @see UIDefaults#getUI */ public String getUIClassID() { return uiClassID; } // // Managing models // /** * Sets the column model for this table to <code>newModel</code> and registers * for listener notifications from the new column model.