📄 grid.java
字号:
private void clearSel() {
sel = new boolean[model.getRowCount()][model.getColumnCount()];
}
/**
* Constructor declaration
*
* @param model model
*
*/
public Grid(TableModel model) {
this();
setModel(model);
}
/**
* Set model
*
* @param model model
*/
public synchronized void setModel(TableModel model) {
if(this.model != null) {
this.model.removeTableModelListener(this);
}
this.model = model;
resetGrid();
model.addTableModelListener(this);
}
private void resetGrid() {
iColWidth = new int[model == null ? 0 : model.getColumnCount()];
for (int i = 0; i < iColWidth.length; i++) {
iColWidth[i] = 100;
}
iRowHeight = 0;
changed();
}
public synchronized void setColumnWidths(int[] colWidth) {
this.iColWidth = colWidth;
adjustScroll();
repaint();
}
public static void main(String[] args) {
Frame frame = new Frame("Test table");
TableModel model = new TestTableModel(1000);
Grid table = new Grid(model);
table.setCellRenderer(Image.class, new TestImageTableCellRenderer());
frame.setLayout(new BorderLayout());
frame.add(table, BorderLayout.CENTER);
frame.setSize(new Dimension(800, 600));
UIUtil.positionComponent(UIUtil.CENTER, frame);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
frame.setVisible(true);
}
/**
* Method declaration
*
* @param d
*/
// public void setMinimumSize(Dimension d) {
// dMinimum = d;
// }
/**
* Method declaration
*
* @param x
* @param y
* @param w
* @param h
*/
public void setBounds(int x, int y, int w, int h) {
// fredt@users 20011210 - patch 450412 by elise@users
super.setBounds(x, y, w, h);
iSbHeight = sbHoriz.getPreferredSize().height;
iSbWidth = sbVert.getPreferredSize().width;
iHeight = h - iSbHeight;
iWidth = w - iSbWidth;
sbHoriz.setBounds(0, iHeight, iWidth, iSbHeight);
sbVert.setBounds(iWidth, 0, iSbWidth, iHeight);
adjustScroll();
iImage = null;
repaint();
}
/**
* Method declaration
*
*/
public void update() {
adjustScroll();
repaint();
}
/**
* Method declaration
*
*/
void adjustScroll() {
if (iRowHeight == 0) { return; }
int w = 0;
for (int i = 0; i < model.getColumnCount(); i++) {
w += iColWidth[i];
}
iGridWidth = w;
iGridHeight = iRowHeight * (model.getRowCount() + 1);
sbHoriz.setValues(iX, iWidth, 0, iGridWidth);
int v = iY / iRowHeight, h = iHeight / iRowHeight;
sbVert.setValues(v, h, 0, model.getRowCount() + 1);
iX = sbHoriz.getValue();
iY = iRowHeight * sbVert.getValue();
}
/**
* Method declaration
*
* @param g
*/
public void paint(Graphics g) {
if (g == null) { return; }
if (model == null) {
super.paint(g);
return;
}
int cc = model.getColumnCount();
int rc = model.getRowCount();
if (iWidth <= 0 || iHeight <= 0) { return; }
g.setColor(SystemColor.control);
g.fillRect(iWidth, iHeight, iSbWidth, iSbHeight);
if (iImage == null) {
iImage = createImage(iWidth, iHeight);
gImage = iImage.getGraphics();
gImage.setFont(fFont);
if (fMetrics == null) {
fMetrics = gImage.getFontMetrics();
}
}
if (iRowHeight == 0) {
iRowHeight = fMetrics.getHeight() + 4;
adjustScroll();
}
gImage.setColor(Color.white);
gImage.fillRect(0, 0, iWidth, iHeight);
gImage.setColor(Color.darkGray);
gImage.drawLine(0, iRowHeight, iWidth, iRowHeight);
int x = -iX;
for (int i = 0; i < cc; i++) {
int w = iColWidth[i];
gImage.setColor(SystemColor.control);
gImage.fillRect(x + 1, 0, w - 2, iRowHeight);
gImage.setColor(Color.black);
gImage.drawString(model.getColumnName(i), x + 2, iRowHeight - 5);
gImage.setColor(Color.darkGray);
gImage.drawLine(x + w - 1, 0, x + w - 1, iRowHeight - 1);
gImage.setColor(Color.white);
gImage.drawLine(x + w, 0, x + w, iRowHeight - 1);
x += w;
}
gImage.setColor(SystemColor.control);
gImage.fillRect(0, 0, 1, iRowHeight);
gImage.fillRect(x + 1, 0, iWidth - x, iRowHeight);
gImage.drawLine(0, 0, 0, iRowHeight - 1);
int y = iRowHeight + 1 - iY;
int j = 0;
while (y < iRowHeight + 1) {
j++;
y += iRowHeight;
}
iFirstRow = j;
y = iRowHeight + 1;
boolean s;
Component component;
TableCellRenderer renderer;
for (; y < iHeight && j < rc; j++, y += iRowHeight) {
x = -iX;
for (int i = 0; i < cc; i++) {
int w = iColWidth[i];
s = j < sel.length && i < sel[j].length ? sel[j][i] : false;
Color b = s ? selectionBackground : getBackground(), t = s ? selectionForeground : getForeground();
gImage.setColor(b);
gImage.fillRect(x, y, w - 1, iRowHeight - 1);
gImage.setColor(t);
renderer = getRendererForColumn(i);
component = renderer.getTableCellRendererComponent(this, model.getValue(j, i), j, i, s);
// TODO Currently, ordinary components cannot be used as a renderer - something to do with the peer not be created
component.setBounds(0, 0, w - 1, iRowHeight - 1);
gImage.translate(x , y);
Shape clip = gImage.getClip();
gImage.setClip(0, 0, w -1 , iRowHeight - 1);
component.paint(gImage);
gImage.translate(-x , -y);
gImage.setClip(clip);
//gImage.drawString(getDisplay(i, j), x + 2, y + iRowHeight - 5);
if(showGrid) {
gImage.setColor(Color.lightGray);
gImage.drawLine(x + w - 1, y, x + w - 1, y + iRowHeight - 1);
gImage.drawLine(x, y + iRowHeight - 1, x + w - 1, y + iRowHeight - 1);
}
x += w;
}
gImage.setColor(getBackground());
gImage.fillRect(x, y, iWidth - x, iRowHeight - 1);
}
g.drawImage(iImage, 0, 0, this);
}
/**
* Method declaration
*
* @param g
*/
public void update(Graphics g) {
paint(g);
}
/**
* Method declaration
*
* @return
*/
public Dimension preferredSize() {
return dPreferred == null ? super.preferredSize() : dPreferred;
}
/**
* Method declaration
*
* @return
*/
public Dimension getPreferredSize() {
return dPreferred == null ? super.getPreferredSize() : dPreferred;
}
static class TestTableModel implements TableModel {
Image image;
int rows;
TestTableModel(int rows) {
this.rows = rows;
image = UIUtil.loadImage(TestTableModel.class, "test-image.png");
}
/*
* (non-Javadoc)
*
* @see com.sshtools.ui.awt.TableModel#getRowCount()
*/
public int getRowCount() {
return rows;
}
/*
* (non-Javadoc)
*
* @see com.sshtools.ui.awt.TableModel#getColumnCount()
*/
public int getColumnCount() {
return 6;
}
/*
* (non-Javadoc)
*
* @see com.sshtools.ui.awt.TableModel#getValue(int, int)
*/
public Object getValue(int r, int c) {
if(c == 0) {
return image;
}
else {
return r + "," + c;
}
}
/*
* (non-Javadoc)
*
* @see com.sshtools.ui.awt.TableModel#getColumnName(int)
*/
public String getColumnName(int c) {
if(c ==0) {
return "Image";
}
else {
return String.valueOf(c);
}
}
/*
* (non-Javadoc)
*
* @see com.sshtools.ui.awt.TableModel#getColumnClass(int)
*/
public Class getColumnClass(int r) {
if(r == 0) {
return Image.class;
}
else {
return String.class;
}
}
/* (non-Javadoc)
* @see com.sshtools.ui.awt.grid.TableModel#addTableModelListener(com.sshtools.ui.awt.grid.TableModelListener)
*/
public void addTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see com.sshtools.ui.awt.grid.TableModel#removeTableModelListener(com.sshtools.ui.awt.grid.TableModelListener)
*/
public void removeTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub
}
}
class DefaultTableCellRenderer extends ImageTextLabel implements TableCellRenderer {
public Component getTableCellRendererComponent(Grid grid, Object value, int row, int col, boolean sel) {
setBackground(sel ? grid.getSelectionBackground() : grid.getBackground());
setForeground(sel ? grid.getSelectionForeground() : grid.getForeground());
setFont(grid.getFont());
if(value instanceof Integer || value instanceof Double ||
value instanceof Long || value instanceof Float ||
value instanceof Short || value instanceof Byte) {
setHorizontalAlignment(RIGHT_ALIGNMENT);
}
else {
setHorizontalAlignment(LEFT_ALIGNMENT);
}
setText(String.valueOf(value));
return this;
}
}
static class TestImageTableCellRenderer extends ImageTextLabel implements TableCellRenderer {
public Component getTableCellRendererComponent(Grid grid, Object value, int row, int col, boolean sel) {
setBackground(sel ? grid.getSelectionBackground() : grid.getBackground());
setForeground(sel ? grid.getSelectionForeground() : grid.getForeground());
setFont(grid.getFont());
setImage((Image)value);
return this;
}
}
/* (non-Javadoc)
* @see com.sshtools.ui.awt.grid.TableModelListener#layoutChanged()
*/
public void layoutChanged() {
resetGrid();
}
/* (non-Javadoc)
* @see com.sshtools.ui.awt.grid.TableModelListener#changed()
*/
public void changed() {
sel = new boolean[model == null ? 0 : model.getRowCount()][model == null ? 0 : model.getColumnCount()];
rowsChanged();
}
/* (non-Javadoc)
* @see com.sshtools.ui.awt.grid.TableModelListener#rowInserted(int)
*/
public void rowInserted(int row) {
rowsChanged();
}
/* (non-Javadoc)
* @see com.sshtools.ui.awt.grid.TableModelListener#rowDeleted(int)
*/
public void rowDeleted(int row) {
rowsChanged();
}
/* (non-Javadoc)
* @see com.sshtools.ui.awt.grid.TableModelListener#rowChanged(int)
*/
public void rowChanged(int row) {
rowsChanged();
}
private void rowsChanged() {
lastDragSel = -1;
clearNextDrag = true;
iImage = null;
adjustScroll();
repaint();
}
/* (non-Javadoc)
* @see java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event.AdjustmentEvent)
*/
public void adjustmentValueChanged(AdjustmentEvent e) {
iX = sbHoriz.getValue();
iY = iRowHeight * sbVert.getValue();
repaint();
}
/**
* @return
*/
public int[] getSelectedRows() {
Vector v = new Vector();
for(int i = 0 ; i < sel.length; i++) {
for(int j = 0 ; j < sel[i].length; j++) {
if(sel[i][j]) {
v.addElement(new Integer(i));
break;
}
}
}
int[] sel = new int[v.size()];
int idx = 0;
for(int i = v.size() - 1; i >= 0 ; i--) {
sel[i] = ((Integer)v.elementAt(i)).intValue();
}
return sel;
}
public void setScrollPosition(int i) {
sbVert.setValue(i);
adjustmentValueChanged(null);
}
public int getScrollPosition() {
return sbVert.getValue();
}
public int getScrollBlockIncrement() {
return sbVert.getBlockIncrement();
}
public int getScrollUnitIncrement() {
return sbVert.getUnitIncrement();
}
/**
* @return
*/
public int getSelectedRowCount() {
return getSelectedRows().length;
}
/**
* @param b
*/
public void setShowGrid(boolean showGrid) {
this.showGrid = showGrid;
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -