📄 grid.java
字号:
/*
* Copyrights and Licenses
*
* This product includes Hypersonic SQL. Originally developed by Thomas Mueller
* and the Hypersonic SQL Group.
*
* Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: -
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. - Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution. - All advertising materials mentioning features or use
* of this software must display the following acknowledgment: "This product
* includes Hypersonic SQL." - Products derived from this software may not be
* called "Hypersonic SQL" nor may "Hypersonic SQL" appear in their names
* without prior written permission of the Hypersonic SQL Group. -
* Redistributions of any form whatsoever must retain the following
* acknowledgment: "This product includes Hypersonic SQL." This software is
* provided "as is" and any expressed or implied warranties, including, but not
* limited to, the implied warranties of merchantability and fitness for a
* particular purpose are disclaimed. In no event shall the Hypersonic SQL Group
* or its contributors be liable for any direct, indirect, incidental, special,
* exemplary, or consequential damages (including, but not limited to,
* procurement of substitute goods or services; loss of use, data, or profits;
* or business interruption). However caused any on any theory of liability,
* whether in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even if
* advised of the possibility of such damage. This software consists of
* voluntary contributions made by many individuals on behalf of the Hypersonic
* SQL Group.
*
*
* For work added by the HSQL Development Group:
*
* Copyright (c) 2001-2002, The HSQL Development Group All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer, including earlier license
* statements (above) and comply with all above license conditions.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution, including earlier
* license statements (above) and comply with all above license conditions.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Work added by 3SP <a href="http://3sp.com">http://3sp.com</a> for
* AWT UI components are licensed under the GPL.
* LICENSE.txt in the root of this module.
*/
package com.sshtools.ui.awt.grid;
import java.awt.AWTEventMulticaster;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Scrollbar;
import java.awt.Shape;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Hashtable;
import java.util.Vector;
import com.sshtools.ui.awt.ImageTextLabel;
import com.sshtools.ui.awt.UIUtil;
//sqlbob@users 20020401 - patch 1.7.0 by sqlbob (RMP) - enhancements
/**
* @version 1.7.0
*/
public class Grid extends Panel implements TableModelListener, AdjustmentListener {
public final static int AUTO_RESIZE_OFF = 0;
public final static int AUTO_RESIZE_EVENT = 1;
public final static int AUTO_RESIZE_FIRST_COLUMN = 2;
public final static int AUTO_RESIZE_LAST_COLUMN = 3;
// drawing
//private Dimension dMinimum;
private Dimension dPreferred;
//boucherb@users changed access for databasemanager2
protected Font fFont;
//--------------------------------------------------
private FontMetrics fMetrics;
private Graphics gImage;
private Image iImage;
// height / width
private int iWidth, iHeight;
private int iRowHeight, iFirstRow;
private int iGridWidth, iGridHeight;
private int iX, iY;
// data
//boucherb@users changed access for databasemanager2
//protected String[] sColHead = new String[0];
//protected Vector vData = new Vector();
//--------------------------------------------------
private int iColWidth[];
//boucherb@users changed access for databasemanager2
//--------------------------------------------------
// scrolling
private Scrollbar sbHoriz, sbVert;
private int iSbWidth, iSbHeight;
private boolean bDrag;
private int iXDrag, iColDrag;
private TableModel model;
private boolean[][] sel;
private int lastDragSel, lastShiftSel;
private boolean clearNextDrag;
private Color selectionBackground, selectionForeground;
private TableCellRenderer cellRenderer;
private Hashtable renderers, numberedRenderers;
private int autoResizeMode = 0;
private boolean showGrid;
private ActionListener listener;
/**
* Constructor declaration
*
*/
public Grid() {
super();
showGrid = false;
renderers = new Hashtable();
numberedRenderers = new Hashtable();
fFont = new Font("Dialog", Font.PLAIN, 12);
selectionBackground = Color.blue.darker();
selectionForeground = Color.white;
setBackground(SystemColor.text);
setForeground(SystemColor.textText);
cellRenderer = new DefaultTableCellRenderer();
setLayout(null);
sbHoriz = new Scrollbar(Scrollbar.HORIZONTAL);
sbHoriz.addAdjustmentListener(this);
add(sbHoriz);
sbVert = new Scrollbar(Scrollbar.VERTICAL);
sbVert.addAdjustmentListener(this);
dPreferred = new Dimension(480,320);
add(sbVert);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
requestFocus();
int y = e.getY();
int x = e.getX();
// get row
if(y > iRowHeight) {
int row = rowForY(y);
if(row < model.getRowCount()) {
int col = columnForX(x);
boolean ctrl = ( e.getModifiers() & MouseEvent.CTRL_MASK ) != 0;
boolean shift = ( e.getModifiers() & MouseEvent.SHIFT_MASK ) != 0;
if(!ctrl) {
clearSel();
}
// select the range if this is a shift selection.
if(shift) {
if(lastShiftSel != -1) {
int incr = lastShiftSel < row ? 1 : -1;
for(int j = lastShiftSel; j != ( row + incr) ; j += incr ) {
selRow(j, true);
}
}
else {
selRow(row, true);
lastShiftSel = row;
}
}
else {
// select entire row
boolean doSel = ctrl ? ( !sel[row][col] ) : true;
selRow(row, doSel);
lastShiftSel = doSel ? row : -1;
}
// repaint
repaint();
//
lastDragSel = -1;
}
}
}
public void mouseReleased(MouseEvent evt) {
if(lastDragSel != -1) {
clearNextDrag = true;
}
}
public void mouseExited(MouseEvent e) {
if (bDrag) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
bDrag = false;
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (bDrag && x < iWidth) {
int w = x - iXDrag;
if (w < 0) {
w = 0;
}
iColWidth[iColDrag] = w;
adjustScroll();
repaint();
}
else {
// Drag selection
if(y > iRowHeight) {
if(clearNextDrag) {
clearSel();
clearNextDrag = false;
}
int row = rowForY(y);
if(row < model.getRowCount()) {
if(lastDragSel != -1) {
// TODO allow up drag selection without clearing previous selection
if(row < lastDragSel) {
for(int j = row + 1; j <= lastDragSel; j++) {
selRow(j, false);
}
selRow(row, true);
}
else {
for(int i = lastDragSel; i <= row; i++) {
selRow(i, true);
}
}
}
else {
selRow(row, true);
}
repaint();
lastDragSel = row;
}
}
}
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (y <= iRowHeight) {
int xb = x;
x += iX - iGridWidth;
int i = model.getColumnCount() - 1;
for (; i >= 0; i--) {
if (x > -7 && x < 7) {
break;
}
x += iColWidth[i];
}
if (i >= 0) {
if (!bDrag) {
setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
bDrag = true;
iXDrag = xb - iColWidth[i];
iColDrag = i;
}
return;
}
}
if (bDrag) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
bDrag = false;
}
}
});
}
public void addActionListener(ActionListener l) {
listener = AWTEventMulticaster.add(listener, l);
}
public void removeActionListener(ActionListener l) {
listener = AWTEventMulticaster.remove(listener, l);
}
private void selRow(int r, boolean selected) {
// select entire row
boolean s[] = sel[r];
for(int i = 0 ; i < s.length; i++) {
s[i] = selected;
}
if(listener != null) {
listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "selection"));
}
}
private int columnForX(int x) {
int w = 0;
for(int i = 0 ; i < iColWidth.length; i++) {
if(x < w)
return i - 1;
else
w += iColWidth[i];
}
return -1;
}
private int rowForY(int y) {
return ( y - iRowHeight + iY ) / iRowHeight;
}
protected TableCellRenderer getRendererForColumn(int col) {
Integer i = new Integer(col);
TableCellRenderer r = (TableCellRenderer)numberedRenderers.get(i);
if(r == null) {
r = (TableCellRenderer)renderers.get(model.getColumnClass(col));
if(r == null) {
r = cellRenderer;
}
}
return r;
}
/**
* Return the first selected row, or -1 if nothing is selected
*
* @return selected row
*/
public int getSelectedRow() {
// TODO this badly needs a more efficient method
for(int i = 0 ; i < sel.length; i++) {
for(int j = 0 ; j < sel[i].length; j++) {
if(sel[i][j]) {
return i;
}
}
}
return -1;
}
/**
* Clear the selection
*
* @param model
*/
public void clearSelection() {
clearSel();
repaint();
}
/**
* Set the selection background colour
*
* @param selectionBackground selection background colour
*/
public void setSelectionBackground(Color selectionBackground) {
this.selectionBackground = selectionBackground;
repaint();
}
/**
* Get the selection background colour
*
* @return selection background colour
*/
public Color getSelectionBackground() {
return selectionBackground;
}
/**
* Set the selection foreground colour
*
* @param selectionForeground selection foreground colour
*/
public void setSelectionForeground(Color selectionForeground) {
this.selectionForeground = selectionForeground;
repaint();
}
/**
* Get the selection foreground colour
*
* @return selection foreground colour
*/
public Color getSelectionForeground() {
return selectionForeground;
}
/**
* Set the cell renderer for a given column class
*
* @param columnClass class to set renderer for
* @param renderer renderer to use
*/
public void setCellRenderer(Class columnClass, TableCellRenderer renderer) {
renderers.put(columnClass, renderer);
}
/**
* Set the cell renderer for a given number
*
* @param column column number
* @param renderer renderer to use
*/
public void setCellRenderer(int column, TableCellRenderer renderer) {
numberedRenderers.put(new Integer(column), renderer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -