|
|
@ -6,7 +6,11 @@ import com.fr.design.gui.itable.TableSorter; |
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
import com.fr.stable.os.OperatingSystem; |
|
|
|
import com.fr.stable.os.OperatingSystem; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.*; |
|
|
|
|
|
|
|
import javax.swing.table.DefaultTableCellRenderer; |
|
|
|
|
|
|
|
import javax.swing.table.JTableHeader; |
|
|
|
import javax.swing.table.TableCellRenderer; |
|
|
|
import javax.swing.table.TableCellRenderer; |
|
|
|
|
|
|
|
import javax.swing.table.TableColumnModel; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.datatransfer.Clipboard; |
|
|
|
import java.awt.datatransfer.Clipboard; |
|
|
|
import java.awt.datatransfer.StringSelection; |
|
|
|
import java.awt.datatransfer.StringSelection; |
|
|
@ -40,27 +44,104 @@ public class CopyableJTable extends SortableJTable { |
|
|
|
int commandKeyCode = 157; |
|
|
|
int commandKeyCode = 157; |
|
|
|
//选中单元格的背景色
|
|
|
|
//选中单元格的背景色
|
|
|
|
Color selectBackGround = new Color(54, 133, 242, 63); |
|
|
|
Color selectBackGround = new Color(54, 133, 242, 63); |
|
|
|
|
|
|
|
Color headerBackGround = new Color(229, 229, 229); |
|
|
|
|
|
|
|
boolean mouseDrag = false; |
|
|
|
|
|
|
|
boolean headerSelect = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DefaultTableCellRenderer tableHeaderCellRenderer = new DefaultTableCellRenderer() { |
|
|
|
|
|
|
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
|
|
|
|
|
|
|
JComponent comp = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
|
|
|
|
|
|
|
if (isChoose(row, column)) { |
|
|
|
|
|
|
|
comp.setBackground(selectBackGround); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
comp.setBackground(headerBackGround); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return comp; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CopyableJTable(TableSorter tableModel) { |
|
|
|
public CopyableJTable(TableSorter tableModel) { |
|
|
|
super(tableModel); |
|
|
|
super(tableModel); |
|
|
|
initListener(); |
|
|
|
initListener(); |
|
|
|
|
|
|
|
this.getTableHeader().setDefaultRenderer(tableHeaderCellRenderer); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initListener() { |
|
|
|
private void initListener() { |
|
|
|
CopyableJTable self = this; |
|
|
|
CopyableJTable self = this; |
|
|
|
|
|
|
|
this.getTableHeader().addMouseListener(new MouseAdapter() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseEntered(MouseEvent e) { |
|
|
|
|
|
|
|
if (mouseDrag) { |
|
|
|
|
|
|
|
headerSelect = true; |
|
|
|
|
|
|
|
int column = getColumn(e); |
|
|
|
|
|
|
|
self.updateEndPoint(-1, column); |
|
|
|
|
|
|
|
self.getTableHeader().repaint(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseExited(MouseEvent e) { |
|
|
|
|
|
|
|
if (mouseDrag) { |
|
|
|
|
|
|
|
headerSelect = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseClicked(MouseEvent e) { |
|
|
|
|
|
|
|
headerSelect = true; |
|
|
|
|
|
|
|
int column = getColumn(e); |
|
|
|
|
|
|
|
if (column != -1) { |
|
|
|
|
|
|
|
self.clearPoint(); |
|
|
|
|
|
|
|
self.addPoint(-1, column); |
|
|
|
|
|
|
|
self.updateStartPoint(-1, column); |
|
|
|
|
|
|
|
self.updateEndPoint(-1, column); |
|
|
|
|
|
|
|
self.refreshTable(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int getColumn(MouseEvent e) { |
|
|
|
|
|
|
|
JTableHeader h = (JTableHeader) e.getSource(); |
|
|
|
|
|
|
|
TableColumnModel columnModel = h.getColumnModel(); |
|
|
|
|
|
|
|
int viewColumn = columnModel.getColumnIndexAtX(e.getX()); |
|
|
|
|
|
|
|
return viewColumn; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
this.getTableHeader().addMouseMotionListener(new MouseAdapter() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseMoved(MouseEvent e) { |
|
|
|
|
|
|
|
mouseDrag = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void mouseDragged(MouseEvent e) { |
|
|
|
|
|
|
|
self.clearPoint(); |
|
|
|
|
|
|
|
self.updateStartPoint(-1, -1); |
|
|
|
|
|
|
|
self.updateEndPoint(-1, -1); |
|
|
|
|
|
|
|
self.refreshTable(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.addMouseMotionListener(new java.awt.event.MouseAdapter() { |
|
|
|
this.addMouseMotionListener(new java.awt.event.MouseAdapter() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void mouseDragged(MouseEvent evt) { |
|
|
|
public void mouseDragged(MouseEvent evt) { |
|
|
|
|
|
|
|
mouseDrag = true; |
|
|
|
int row = self.rowAtPoint(evt.getPoint()); |
|
|
|
int row = self.rowAtPoint(evt.getPoint()); |
|
|
|
int col = self.columnAtPoint(evt.getPoint()); |
|
|
|
int col = self.columnAtPoint(evt.getPoint()); |
|
|
|
if (self.updateEndPoint(row, col)) { |
|
|
|
if (self.updateEndPoint(row, col)) { |
|
|
|
self.repaint(); |
|
|
|
self.refreshTable(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void mouseMoved(MouseEvent e) { |
|
|
|
|
|
|
|
mouseDrag = false; |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
this.addMouseListener(new MouseAdapter() { |
|
|
|
this.addMouseListener(new MouseAdapter() { |
|
|
|
public void mousePressed(MouseEvent e) { |
|
|
|
public void mousePressed(MouseEvent e) { |
|
|
|
|
|
|
|
headerSelect = false; |
|
|
|
int row = self.rowAtPoint(e.getPoint()); |
|
|
|
int row = self.rowAtPoint(e.getPoint()); |
|
|
|
int col = self.columnAtPoint(e.getPoint()); |
|
|
|
int col = self.columnAtPoint(e.getPoint()); |
|
|
|
if (!self.isControlDown) { |
|
|
|
if (!self.isControlDown) { |
|
|
@ -74,7 +155,7 @@ public class CopyableJTable extends SortableJTable { |
|
|
|
self.addPoint(row, col); |
|
|
|
self.addPoint(row, col); |
|
|
|
self.updateEndPoint(row, col); |
|
|
|
self.updateEndPoint(row, col); |
|
|
|
|
|
|
|
|
|
|
|
self.repaint(); |
|
|
|
self.refreshTable(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
@ -129,6 +210,8 @@ public class CopyableJTable extends SortableJTable { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean updateEndPoint(int row, int col) { |
|
|
|
private boolean updateEndPoint(int row, int col) { |
|
|
|
|
|
|
|
if (headerSelect && row != -1) |
|
|
|
|
|
|
|
return false; |
|
|
|
if (endRow != row || endCol != col) { |
|
|
|
if (endRow != row || endCol != col) { |
|
|
|
endRow = row; |
|
|
|
endRow = row; |
|
|
|
endCol = col; |
|
|
|
endCol = col; |
|
|
@ -157,13 +240,12 @@ public class CopyableJTable extends SortableJTable { |
|
|
|
private void copy() { |
|
|
|
private void copy() { |
|
|
|
FineLoggerFactory.getLogger().info("copy cell value"); |
|
|
|
FineLoggerFactory.getLogger().info("copy cell value"); |
|
|
|
java.util.List<java.util.List<Object>> table = new ArrayList<>(); |
|
|
|
java.util.List<java.util.List<Object>> table = new ArrayList<>(); |
|
|
|
if ((startRow != endRow || startCol != endCol) && |
|
|
|
if ((startRow != endRow || startCol != endCol) && Math.min(startCol, endCol) > -1) { |
|
|
|
Math.min(startRow, endRow) > -1 && Math.min(startCol, endCol) > -1) { |
|
|
|
|
|
|
|
for (int i = Math.min(startRow, endRow); i <= Math.max(startRow, endRow); i++) { |
|
|
|
for (int i = Math.min(startRow, endRow); i <= Math.max(startRow, endRow); i++) { |
|
|
|
table.add(new ArrayList<>()); |
|
|
|
table.add(new ArrayList<>()); |
|
|
|
for (int j = Math.min(startCol, endCol); j <= Math.max(startCol, endCol); j++) { |
|
|
|
for (int j = Math.min(startCol, endCol); j <= Math.max(startCol, endCol); j++) { |
|
|
|
Object text = this.getValueAt(i, j); |
|
|
|
Object text = this.getTableValue(i, j); |
|
|
|
table.get(i - Math.min(startRow, endRow)).add(text); |
|
|
|
table.get(table.size() - 1).add(text); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (pointList.size() > 0) { |
|
|
|
} else if (pointList.size() > 0) { |
|
|
@ -176,8 +258,8 @@ public class CopyableJTable extends SortableJTable { |
|
|
|
table.add(new ArrayList<>()); |
|
|
|
table.add(new ArrayList<>()); |
|
|
|
currentRow++; |
|
|
|
currentRow++; |
|
|
|
} |
|
|
|
} |
|
|
|
Object text = this.getValueAt(point.x, point.y); |
|
|
|
Object text = this.getTableValue(point.x, point.y); |
|
|
|
table.get(currentRow - startRow).add(text); |
|
|
|
table.get(table.size() - 1).add(text); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -186,6 +268,24 @@ public class CopyableJTable extends SortableJTable { |
|
|
|
clip.setContents(tText, null); |
|
|
|
clip.setContents(tText, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Object getTableValue(int row, int col) { |
|
|
|
|
|
|
|
Object value = null; |
|
|
|
|
|
|
|
if (col > -1) { |
|
|
|
|
|
|
|
if (row > -1) { |
|
|
|
|
|
|
|
value = this.getValueAt(row, col); |
|
|
|
|
|
|
|
} else if (row == -1) { |
|
|
|
|
|
|
|
col = columnModel.getColumn(col).getModelIndex(); |
|
|
|
|
|
|
|
value = this.getModel().getColumnName(col); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void refreshTable() { |
|
|
|
|
|
|
|
this.repaint(); |
|
|
|
|
|
|
|
this.getTableHeader().repaint(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean isChoose(int row, int col) { |
|
|
|
private boolean isChoose(int row, int col) { |
|
|
|
if (row >= Math.min(startRow, endRow) && row <= Math.max(startRow, endRow)) { |
|
|
|
if (row >= Math.min(startRow, endRow) && row <= Math.max(startRow, endRow)) { |
|
|
|
if (col >= Math.min(startCol, endCol) && col <= Math.max(startCol, endCol)) { |
|
|
|
if (col >= Math.min(startCol, endCol) && col <= Math.max(startCol, endCol)) { |
|
|
|