📄 resultsetpanel.java
字号:
/** * Returns the model row count. * * @return the row ount displayed */ public int getRowCount() { return model.getRowCount(); } /** * Indicates whether the model has retained the ResultSetMetaData. * * @return true | false */ public boolean hasResultSetMetaData() { if (model == null) { return false; } else { return model.hasResultSetMetaData(); } } /** * Returns the table display. * * @return the table */ public JTable getResultsTable() { return table; } /** * Sets to display the result set meta data. */ public ResultSetMetaDataPanel getResultSetMetaDataPanel() { if (!model.hasResultSetMetaData()) { return null; } if (metaDataPanel == null) { metaDataPanel = new ResultSetMetaDataPanel(model.getResultSetMetaData()); } else { metaDataPanel.setMetaData(model.getResultSetMetaData()); } return metaDataPanel; } /** * Returns the result set table model. * * @return the table model */ public ResultSetTableModel getResultSetTableModel() { return model; } protected void printResultSet(boolean printSelection) { JTable printTable = null; if (printSelection) { TableModel _model = buildSelectedCellsModel(); if (_model != null) { printTable = new JTable(_model); } else { return; } } else { printTable = table; } Printable printable = new TablePrinter(printTable, null); PrintUtilities.print(printable, "Execute Query - editor"); } protected TableModel buildSelectedCellsModel() { int cols = table.getSelectedColumnCount(); int rows = table.getSelectedRowCount(); if (cols == 0 && rows == 0) { return null; } int[] selectedRows = table.getSelectedRows(); int[] selectedCols = table.getSelectedColumns(); Vector data = new Vector(rows); Vector columns = new Vector(cols); for (int i = 0; i < rows; i++) { Vector rowVector = new Vector(cols); for (int j = 0; j < cols; j++) { rowVector.add(table.getValueAt( selectedRows[i], selectedCols[j])); if (i == 0) { columns.add(table.getColumnName(selectedCols[j])); } } data.add(rowVector); } return new DefaultTableModel(data, columns); } protected void selectRow(Point point) { if (point != null) { table.clearSelection(); int row = table.rowAtPoint(point); table.setColumnSelectionAllowed(false); table.setRowSelectionAllowed(true); table.setRowSelectionInterval(row, row); } } protected void selectColumn(Point point) { if (point != null) { int column = table.columnAtPoint(point); table.setColumnSelectionAllowed(true); table.setRowSelectionAllowed(false); table.setColumnSelectionInterval(column, column); } } /** * Performs the cell data copy for a cell selection. */ protected void copySelectedCells() { StringBuffer sb = new StringBuffer(); int cols = table.getSelectedColumnCount(); int rows = table.getSelectedRowCount(); if (cols == 0 && rows == 0) { return; } int[] selectedRows = table.getSelectedRows(); int[] selectedCols = table.getSelectedColumns(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { sb.append(table.getValueAt(selectedRows[i], selectedCols[j])); if (j < cols - 1) { sb.append("\t"); } } sb.append("\n"); } StringSelection stsel = new StringSelection(sb.toString()); Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipBoard.setContents(stsel,stsel); } private class MouseHandler extends MouseAdapter { public MouseHandler() {} public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { if (popupMenu == null) { popupMenu = new PopMenu(); } popupMenu.lastPoint = new Point(e.getX(), e.getY()); popupMenu.show(e.getComponent(), popupMenu.lastPoint.x, popupMenu.lastPoint.y); } else { // re-enable cell selection table.setColumnSelectionAllowed(true); table.setRowSelectionAllowed(true); } } } // class MouseHandler /** The table's popup menu function */ private class PopMenu extends JPopupMenu implements ActionListener { private JMenuItem copy; private JMenuItem selectRow; private JMenuItem selectColumn; private JMenuItem exportSelection; private JMenuItem exportTable; private JMenuItem printSelection; private JMenuItem printTable; /** the last point of the popup */ protected Point lastPoint; public PopMenu() { copy = new JMenuItem("Copy Selected Cells"); copy.addActionListener(this); selectRow = new JMenuItem("Select Row"); selectRow.addActionListener(this); selectColumn = new JMenuItem("Select Column"); selectColumn.addActionListener(this); exportSelection = new JMenuItem("Export Selection"); exportSelection.addActionListener(this); exportTable = new JMenuItem("Export Table"); exportTable.addActionListener(this); // the print sub-menu JMenu printMenu = new JMenu("Print"); printSelection = new JMenuItem("Selection"); printSelection.addActionListener(this); printTable = new JMenuItem("Table"); printTable.addActionListener(this); printMenu.add(printSelection); printMenu.add(printTable); add(copy); addSeparator(); add(selectRow); add(selectColumn); addSeparator(); add(exportSelection); add(exportTable); addSeparator(); add(printMenu); } public void actionPerformed(ActionEvent e) { try { Object source = e.getSource(); if (source == copy) { copySelectedCells(); } else if (source == selectColumn) { selectColumn(lastPoint); } else if (source == selectRow) { selectRow(lastPoint); } else if (source == exportSelection) { TableModel selected = buildSelectedCellsModel(); if (selected != null) { new QueryEditorResultsExporter(selected); } } else if (source == exportTable) { new QueryEditorResultsExporter(model); } else if (source == printSelection) { printResultSet(true); } else if (source == printTable) { printResultSet(false); } } finally { lastPoint = null; } } } // class PopMenu}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -