📄 exacttable.java
字号:
/**
* Overridden to stop editing
*/
public void selectAll() {
stopEditing();
super.selectAll();
}
/**
* Updates the state of the actions. called whenever the selection state of the table changes.
*/
protected void updateActions() {
int cols = getSelectedColumnCount();
int rows = getSelectedRowCount();
boolean somethingSelected = (rows > 0 || cols > 0);
boolean moreSelected = (rows > 1 || cols > 1);
COPY_ACTION.setEnabled(clipboardTransferEnabled && somethingSelected);
CUT_ACTION.setEnabled(clipboardTransferEnabled && somethingSelected);
CLEAR_ACTION.setEnabled(somethingSelected);
PASTE_ACTION.setEnabled(somethingSelected && clipboardTransferEnabled && canPaste());
FILL_ACTION.setEnabled(fillCellsEnabled && moreSelected);
}
private void updateScrollLabels() {
Rectangle vr = getVisibleRect();
moreColumnsLabel.setVisible(vr.x + vr.width < getWidth());
moreRowsLabel.setVisible(vr.y + vr.height < getHeight());
}
public void updateStructure() {
tableChanged(new TableModelEvent(dataModel, TableModelEvent.HEADER_ROW));
if (rowHeader != null) rowHeader.update();
}
public void update() {
tableChanged(new TableModelEvent(dataModel));
if (rowHeader != null) rowHeader.update();
}
public void updateRow(int row) {
updateRows(row, row);
}
public void updateRows(int firstRow, int lastRow) {
tableChanged(new TableModelEvent(dataModel, firstRow, lastRow));
if (rowHeader != null) rowHeader.updateRows(firstRow, lastRow);
}
public int getRowHeaderWidth() {
return rowHeaderWidth;
}
public void setRowHeaderWidth(int rowHeaderWidth) {
this.rowHeaderWidth = rowHeaderWidth;
if (rowHeader != null) {
rowHeader.setWidth(rowHeaderWidth);
}
}
public String getSelectAllTooltip() {
return selectAllTooltip;
}
public void setSelectAllTooltip(String selectAllTooltip) {
this.selectAllTooltip = selectAllTooltip;
selectAction.putValue(Action.SHORT_DESCRIPTION, selectAllTooltip);
}
public String getMoreRowsTooltip() {
return moreRowsTooltip;
}
public void setMoreRowsTooltip(String moreRowsTooltip) {
this.moreRowsTooltip = moreRowsTooltip;
if (moreRowsLabel != null) moreRowsLabel.setToolTipText(moreRowsTooltip);
}
public String getMoreColumnsTooltip() {
return moreColumnsTooltip;
}
public void setMoreColumnsTooltip(String moreColumnsTooltip) {
this.moreColumnsTooltip = moreColumnsTooltip;
if (moreColumnsLabel != null) moreColumnsLabel.setToolTipText(moreColumnsTooltip);
}
public boolean displaysScrollLabels() {
return displaysScrollLabels;
}
public void setDisplaysScrollLabels(boolean displaysScrollLabels) {
this.displaysScrollLabels = displaysScrollLabels;
if (!displaysScrollLabels) {
moreColumnsLabel.setVisible(false);
moreRowsLabel.setVisible(false);
} else {
updateScrollLabels();
}
}
/**
* Try to keep the viewport aligned on column boundaries in the direction of interest
*/
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
int direction) {
if (orientation == SwingConstants.HORIZONTAL) {
return 80;
}
return getRowHeight();
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation,
int direction) {
/* borrowed from JTable */
if (orientation == SwingConstants.HORIZONTAL) {
return 5 * getScrollableUnitIncrement(visibleRect, orientation, direction);
}
return super.getScrollableBlockIncrement(visibleRect, orientation, direction);
}
public boolean isClipboardTransferEnabled() {
return clipboardTransferEnabled;
}
public void setClipboardTransferEnabled(boolean clipboardTransferEnabled) {
this.clipboardTransferEnabled = clipboardTransferEnabled;
}
public boolean isFillCellsEnabled() {
return fillCellsEnabled;
}
public void setFillCellsEnabled(boolean fillCellsEnabled) {
this.fillCellsEnabled = fillCellsEnabled;
}
public boolean isMouseMenuEnabled() {
return mouseMenuEnabled;
}
public void setMouseMenuEnabled(boolean mouseMenuEnabled) {
this.mouseMenuEnabled = mouseMenuEnabled;
}
/* copy/cut/paste/delete stuff -------------------------------------*/
public void copyAnchorToSelection() {
if ((getSelectedRowCount() == 0) && (getSelectedColumnCount() == 0)) return;
if (dataModel instanceof ExactTableModel) {
stopEditing();
int rowFrom = selectionModel.getMinSelectionIndex();
int rowTo = selectionModel.getMaxSelectionIndex();
ListSelectionModel csm = columnModel.getSelectionModel();
int colFrom = csm.getMinSelectionIndex();
int colTo = csm.getMaxSelectionIndex();
int anchorRow = selectionModel.getAnchorSelectionIndex();
int anchorCol = csm.getAnchorSelectionIndex();
((ExactTableModel) dataModel).copyCellToArea(anchorRow, anchorCol, rowFrom, rowTo, colFrom, colTo);
updateRows(rowFrom, rowTo);
requestFocus();
}
}
public void copyCells() {
StringBuffer sbf = new StringBuffer();
// Check to ensure we have selected only a contiguous block of
// cells
int numcols = getSelectedColumnCount();
int numrows = getSelectedRowCount();
int[] rowsselected = getSelectedRows();
int[] colsselected = getSelectedColumns();
if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0] &&
numrows == rowsselected.length) &&
(numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0] &&
numcols == colsselected.length))) {
JOptionPane.showMessageDialog(null, "Invalid Copy Selection",
"Invalid Copy Selection",
JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < numrows; i++) {
for (int j = 0; j < numcols; j++) {
sbf.append(getValueAt(rowsselected[i], colsselected[j]));
if (j < numcols - 1) sbf.append("\t");
}
sbf.append("\n");
}
stsel = new StringSelection(sbf.toString()) {
public void lostOwnership(Clipboard clipboard, Transferable contents) {
canPaste = false;
}
};
clip.setContents(stsel, stsel);
canPaste = true;
}
public void clearCells() {
if (DEBUG) System.out.println("clearCells");
boolean hasClear = false;
ExactTableModel etm = null;
if (dataModel instanceof ExactTableModel) {
hasClear = true;
etm = (ExactTableModel) dataModel;
}
int numcols = getSelectedColumnCount();
int numrows = getSelectedRowCount();
int[] rowsselected = getSelectedRows();
int[] colsselected = getSelectedColumns();
if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0] &&
numrows == rowsselected.length) &&
(numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0] &&
numcols == colsselected.length))) {
JOptionPane.showMessageDialog(null, "Invalid Clear Selection",
"Invalid Clear Selection",
JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < numrows; i++) {
for (int j = 0; j < numcols; j++) {
if (hasClear) {
etm.clear(rowsselected[i], colsselected[j]);
} else {
setValueAt(null, rowsselected[i], colsselected[j]);
}
}
}
updateRows(rowsselected[0], rowsselected[rowsselected.length - 1]);
}
public void pasteCells() {
pasteCellsAt(selectionModel.getAnchorSelectionIndex(), columnModel.getSelectionModel().getAnchorSelectionIndex());
}
public void pasteCellsAt(int startRow, int startCol) {
if (!canPaste()) return;
String rowstring, value;
int i = 0,j = 0;
if (DEBUG) System.out.println("Trying to Paste");
try {
String trstring = (String) (clip.getContents(this).getTransferData(DataFlavor.stringFlavor));
if (DEBUG) System.out.println("String is:" + trstring);
StringTokenizer st1 = new StringTokenizer(trstring, "\n");
for (i = 0; st1.hasMoreTokens(); i++) {
rowstring = st1.nextToken();
StringTokenizer st2 = new StringTokenizer(rowstring, "\t");
for (j = 0; st2.hasMoreTokens(); j++) {
value = st2.nextToken();
if (startRow + i < getRowCount() &&
startCol + j < getColumnCount())
setValueAt(value, startRow + i, startCol + j);
if (DEBUG) System.out.println("Putting " + value + "at row=" + startRow + i + "column=" + startCol + j);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
update();
setRowSelectionInterval(startRow, Math.min(getRowCount(), startRow + i - 1));
setColumnSelectionInterval(startCol, Math.min(getColumnCount(), startCol + j - 1));
}
/**
* As of now does not allow importing data that has not been copied by us. Ideas are welcome...
*/
public boolean canPaste() {
return canPaste;
}
public AbstractAction COPY_ACTION = new AbstractAction("Copy") {
{
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false));
putValue(Action.SHORT_DESCRIPTION, "Copies selected cells to the system clipboard");
}
public void actionPerformed(ActionEvent e) {
copyCells();
}
};
public AbstractAction CUT_ACTION = new AbstractAction("Cut") {
{
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK, false));
putValue(Action.SHORT_DESCRIPTION, "Copies selected cells to the system clipboard and clears them");
}
public void actionPerformed(ActionEvent e) {
copyCells();
clearCells();
}
};
public AbstractAction PASTE_ACTION = new AbstractAction("Paste") {
{
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, false));
putValue(Action.SHORT_DESCRIPTION, "Pastes cells from the system clipboard, starting from the currently focused cell");
}
public void actionPerformed(ActionEvent e) {
pasteCells();
}
};
public AbstractAction CLEAR_ACTION = new AbstractAction("Clear") {
{
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false));
putValue(Action.SHORT_DESCRIPTION, "Clears selected cells");
}
public void actionPerformed(ActionEvent e) {
clearCells();
}
};
public AbstractAction FILL_ACTION = new AbstractAction("Fill") {
{
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK, false));
putValue(Action.SHORT_DESCRIPTION, "Fills selected cells with the value of the focused cell");
}
public void actionPerformed(ActionEvent e) {
copyAnchorToSelection();
}
};
protected class MouseHandler extends MouseAdapter {
private boolean isInstalled;
private JPopupMenu menu;
public MouseHandler(JPopupMenu menu) {
this.menu = menu;
}
public void install() {
if (!isInstalled) {
addMouseListener(this);
isInstalled = true;
}
}
public void uninstall() {
if (isInstalled) {
removeMouseListener(this);
isInstalled = false;
}
}
public void mousePressed(MouseEvent e) {
processME(e);
}
public void mouseReleased(MouseEvent e) {
processME(e);
}
protected void processME(MouseEvent e) {
if (!mouseMenuEnabled) return;
if (e.isPopupTrigger()) {
updateActions();
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
public JPopupMenu getMenu() {
return menu;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -