📄 tableheaderui.java
字号:
// 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);
}
/**
* This inner class is marked "public" due to a compiler bug.
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of BasicTableUI.
*/
public class MouseInputHandler implements MouseInputListener
{
private int mouseXOffset;
private Cursor otherCursor=resizeCursor;
public void mouseClicked(MouseEvent e) {}
private boolean canResize(TableColumn column)
{
return (column!=null) && header.getResizingAllowed()
&& column.getResizable();
}
private TableColumn getResizingColumn(Point p)
{
return getResizingColumn(p, header.columnAtPoint(p));
}
private TableColumn getResizingColumn(Point p, int column)
{
if(column==-1)
{
return null;
}
Rectangle r=header.getHeaderRect(column);
r.grow(-3, 0);
if(r.contains(p))
{
return null;
}
int midPoint=r.x+r.width/2;
int columnIndex;
if(header.getComponentOrientation().isLeftToRight())
{
columnIndex=(p.x<midPoint) ? column-1 : column;
}
else
{
columnIndex=(p.x<midPoint) ? column : column-1;
}
if(columnIndex==-1)
{
return null;
}
return header.getColumnModel().getColumn(columnIndex);
}
public void mousePressed(MouseEvent e)
{
header.setDraggedColumn(null);
header.setResizingColumn(null);
header.setDraggedDistance(0);
Point p=e.getPoint();
// First find which header cell was hit
TableColumnModel columnModel=header.getColumnModel();
int index=header.columnAtPoint(p);
if(index!=-1)
{
// The last 3 pixels + 3 pixels of next column are for resizing
TableColumn resizingColumn=getResizingColumn(p, index);
if(canResize(resizingColumn))
{
header.setResizingColumn(resizingColumn);
if(header.getComponentOrientation().isLeftToRight())
{
mouseXOffset=p.x-resizingColumn.getWidth();
}
else
{
mouseXOffset=p.x+resizingColumn.getWidth();
}
}
else if(header.getReorderingAllowed())
{
TableColumn hitColumn=columnModel.getColumn(index);
header.setDraggedColumn(hitColumn);
mouseXOffset=p.x;
}
}
}
private void swapCursor()
{
Cursor tmp=header.getCursor();
header.setCursor(otherCursor);
otherCursor=tmp;
}
public void mouseMoved(MouseEvent e)
{
if(canResize(getResizingColumn(e.getPoint()))!=(header.getCursor()==resizeCursor))
{
swapCursor();
}
}
public void mouseDragged(MouseEvent e)
{
int mouseX=e.getX();
TableColumn resizingColumn=header.getResizingColumn();
TableColumn draggedColumn=header.getDraggedColumn();
boolean headerLeftToRight=header.getComponentOrientation()
.isLeftToRight();
if(resizingColumn!=null)
{
int oldWidth=resizingColumn.getWidth();
int newWidth;
if(headerLeftToRight)
{
newWidth=mouseX-mouseXOffset;
}
else
{
newWidth=mouseXOffset-mouseX;
}
resizingColumn.setWidth(newWidth);
Container container;
if((header.getParent()==null)
|| ((container=header.getParent().getParent())==null)
|| !(container instanceof JScrollPane))
{
return;
}
if(!container.getComponentOrientation().isLeftToRight()
&& !headerLeftToRight)
{
JTable table=header.getTable();
if(table!=null)
{
JViewport viewport=((JScrollPane)container).getViewport();
int viewportWidth=viewport.getWidth();
int diff=newWidth-oldWidth;
int newHeaderWidth=table.getWidth()+diff;
/* Resize a table */
Dimension tableSize=table.getSize();
tableSize.width+=diff;
table.setSize(tableSize);
/* If this table is in AUTO_RESIZE_OFF mode and
* has a horizontal scrollbar, we need to update
* a view's position.
*/
if((newHeaderWidth>=viewportWidth)
&& (table.getAutoResizeMode()==JTable.AUTO_RESIZE_OFF))
{
Point p=viewport.getViewPosition();
p.x=Math.max(0,
Math.min(newHeaderWidth-viewportWidth,
p.x+diff));
viewport.setViewPosition(p);
/* Update the original X offset value. */
mouseXOffset+=diff;
}
}
}
}
else if(draggedColumn!=null)
{
TableColumnModel cm=header.getColumnModel();
int draggedDistance=mouseX-mouseXOffset;
int direction=(draggedDistance<0) ? -1 : 1;
int columnIndex=viewIndexForColumn(draggedColumn);
int newColumnIndex=columnIndex
+(headerLeftToRight ? direction
: -direction);
if(0<=newColumnIndex && newColumnIndex<cm.getColumnCount())
{
int width=cm.getColumn(newColumnIndex).getWidth();
if(Math.abs(draggedDistance)>(width/2))
{
mouseXOffset=mouseXOffset+direction*width;
header.setDraggedDistance(draggedDistance-direction*width);
cm.moveColumn(columnIndex, newColumnIndex);
return;
}
}
setDraggedDistance(draggedDistance, columnIndex);
}
}
public void mouseReleased(MouseEvent e)
{
setDraggedDistance(0, viewIndexForColumn(header.getDraggedColumn()));
header.setResizingColumn(null);
header.setDraggedColumn(null);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
//
// Protected & Private Methods
//
private void setDraggedDistance(int draggedDistance, int column)
{
header.setDraggedDistance(draggedDistance);
if(column!=-1)
{
header.getColumnModel().moveColumn(column, column);
}
}
}
}
// End of Class BasicTableHeaderUI
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -