📄 jxtable.java
字号:
private SearchResult findMatchForwardInRow(Pattern pattern, int row) { int startColumn = (lastSearchResult.foundColumn < 0) ? 0 : lastSearchResult.foundColumn; if (isValidIndex(row)) { for (int column = startColumn; column < getColumnCount(); column++) { SearchResult result = findMatchAt(pattern, row, column); if (result != null) return result; } } return null; } /** * Searches forward through columns of the given row. Starts at * lastFoundColumn or first column if lastFoundColumn < 0. returns an * appropriate SearchResult if a matching cell is found in this row or * null if no match is found. A row index out off range results in a * no-match. * * @param pattern * @param row * the row to search * @return */ private SearchResult findMatchBackwardsInRow(Pattern pattern, int row) { int startColumn = (lastSearchResult.foundColumn < 0) ? getColumnCount() - 1 : lastSearchResult.foundColumn; if (isValidIndex(row)) { for (int column = startColumn; column >= 0; column--) { SearchResult result = findMatchAt(pattern, row, column); if (result != null) return result; } } return null; } /** * Matches the cell content at row/col against the given Pattern. * Returns an appropriate SearchResult if matching or null if no * matching * * @param pattern * @param row * a valid row index in view coordinates * @param column * a valid column index in view coordinates * @return */ protected SearchResult findMatchAt(Pattern pattern, int row, int column) { Object value = getValueAt(row, column); if (value != null) { Matcher matcher = pattern.matcher(value.toString()); if (matcher.find()) { return createSearchResult(matcher, row, column); } } return null; } /** * Called if startIndex is different from last search, reset the column * to -1 and make sure a backwards/forwards search starts at last/first * row, respectively. * * @param startIndex * @param backwards * @return */ protected int adjustStartPosition(int startIndex, boolean backwards) { lastSearchResult.foundColumn = -1; return super.adjustStartPosition(startIndex, backwards); } /** * Moves the internal start for matching as appropriate and returns the * new startIndex to use. Called if search was messaged with the same * startIndex as previously. * * @param startRow * @param backwards * @return */ @Override protected int moveStartPosition(int startRow, boolean backwards) { if (backwards) { lastSearchResult.foundColumn--; if (lastSearchResult.foundColumn < 0) { startRow--; } } else { lastSearchResult.foundColumn++; if (lastSearchResult.foundColumn >= getColumnCount()) { lastSearchResult.foundColumn = -1; startRow++; } } return startRow; } /** * Checks if the startIndex is a candidate for trying a re-match. * * * @param startIndex * @return true if the startIndex should be re-matched, false if not. */ protected boolean isEqualStartIndex(final int startIndex) { return super.isEqualStartIndex(startIndex) && isValidColumn(lastSearchResult.foundColumn); } /** * checks if row is in range: 0 <= row < getRowCount(). * * @param column * @return */ private boolean isValidColumn(int column) { return column >= 0 && column < getColumnCount(); } @Override protected int getSize() { return getRowCount(); } @Override protected void moveMatchMarker() { int row = lastSearchResult.foundRow; int column = lastSearchResult.foundColumn; Pattern pattern = lastSearchResult.pattern; if (markByHighlighter()) { Rectangle cellRect = getCellRect(row, column, true); if (cellRect != null) { scrollRectToVisible(cellRect); } ensureInsertedSearchHighlighters(); // TODO (JW) - cleanup SearchHighlighter state management if ((row >= 0) && (column >= 0)) { getSearchHighlighter().setPattern(pattern); int modelColumn = convertColumnIndexToModel(column); getSearchHighlighter().setHighlightCell(row, modelColumn); } else { getSearchHighlighter().setPattern(null); } } else { // use selection changeSelection(row, column, false, false); if (!getAutoscrolls()) { // scrolling not handled by moving selection Rectangle cellRect = getCellRect(row, column, true); if (cellRect != null) { scrollRectToVisible(cellRect); } } } } private boolean markByHighlighter() { return Boolean.TRUE.equals(getClientProperty(MATCH_HIGHLIGHTER)); } private SearchHighlighter getSearchHighlighter() { if (searchHighlighter == null) { searchHighlighter = createSearchHighlighter(); } return searchHighlighter; } private void ensureInsertedSearchHighlighters() { if (getHighlighters() == null) { setHighlighters(new HighlighterPipeline( new Highlighter[] { getSearchHighlighter() })); } else if (!isInPipeline(getSearchHighlighter())) { getHighlighters().addHighlighter(getSearchHighlighter()); } } private boolean isInPipeline(PatternHighlighter searchHighlighter) { Highlighter[] inPipeline = getHighlighters().getHighlighters(); if ((inPipeline.length > 0) && (searchHighlighter.equals(inPipeline[inPipeline.length -1]))) { return true; } getHighlighters().removeHighlighter(searchHighlighter); return false; } protected SearchHighlighter createSearchHighlighter() { return new SearchHighlighter(); } }//-------------------------------- sizing support /** ? */ public void setVisibleRowCount(int visibleRowCount) { this.visibleRowCount = visibleRowCount; } /** ? */ public int getVisibleRowCount() { return visibleRowCount; } public Dimension getPreferredScrollableViewportSize() { Dimension prefSize = super.getPreferredScrollableViewportSize(); // JTable hardcodes this to 450 X 400, so we'll calculate it // based on the preferred widths of the columns and the // visibleRowCount property instead... if (prefSize.getWidth() == 450 && prefSize.getHeight() == 400) { TableColumnModel columnModel = getColumnModel(); int columnCount = columnModel.getColumnCount(); int w = 0; for (int i = 0; i < columnCount; i++) { TableColumn column = columnModel.getColumn(i); initializeColumnPreferredWidth(column); w += column.getPreferredWidth(); } prefSize.width = w; JTableHeader header = getTableHeader(); // remind(aim): height is still off...??? int rowCount = getVisibleRowCount(); prefSize.height = rowCount * getRowHeight() + (header != null ? header.getPreferredSize().height : 0); setPreferredScrollableViewportSize(prefSize); } return prefSize; } /** * Packs all the columns to their optimal size. Works best with auto * resizing turned off. * * Contributed by M. Hillary (Issue #60) * * @param margin * the margin to apply to each column. */ public void packTable(int margin) { for (int c = 0; c < getColumnCount(); c++) packColumn(c, margin, -1); } /** * Packs an indivudal column in the table. Contributed by M. Hillary (Issue * #60) * * @param column * The Column index to pack in View Coordinates * @param margin * The Margin to apply to the column width. */ public void packColumn(int column, int margin) { packColumn(column, margin, -1); } /** * Packs an indivual column in the table to less than or equal to the * maximum witdth. If maximun is -1 then the column is made as wide as it * needs. Contributed by M. Hillary (Issue #60) * * @param column * The Column index to pack in View Coordinates * @param margin * The margin to apply to the column * @param max * The maximum width the column can be resized to. -1 mean any * size. */ public void packColumn(int column, int margin, int max) { getColumnFactory().packColumn(this, getColumnExt(column), margin, max); } /** * Initialize the preferredWidth of the specified column based on the * column's prototypeValue property. If the column is not an instance of * <code>TableColumnExt</code> or prototypeValue is <code>null</code> * then the preferredWidth is left unmodified. * * @see org.jdesktop.swingx.table.TableColumnExt#setPrototypeValue * @param column * TableColumn object representing view column */ protected void initializeColumnPreferredWidth(TableColumn column) { if (column instanceof TableColumnExt) { getColumnFactory().configureColumnWidths(this, (TableColumnExt) column); } } //----------------------------------- uniform data model access protected ComponentAdapter getComponentAdapter() { if (dataAdapter == null) { dataAdapter = new TableAdapter(this); } return dataAdapter; } protected static class TableAdapter extends ComponentAdapter { private final JXTable table; /** * Constructs a <code>TableDataAdapter</code> for the specified target * component. * * @param component * the target component */ public TableAdapter(JXTable component) { super(component); table = component; } /** * Typesafe accessor for the target component. * * @return the target component as a {@link javax.swing.JTable} */ public JXTable getTable() { return table; } public String getColumnName(int columnIndex) { TableColumn column = getColumnByModelIndex(columnIndex); return column == null ? "" : column.getHeaderValue().toString(); } protected TableColumn getColumnByModelIndex(int modelColumn) { List columns = table.getColumns(true); for (Iterator iter = columns.iterator(); iter.hasNext();) { TableColumn column = (TableColumn) iter.next(); if (column.getModelIndex() == modelColumn) { return column; } } return null; } public String getColumnIdentifier(int columnIndex) { TableColumn column = getColumnByModelIndex(columnIndex); Object identifier = column != null ? column.getIdentifier() : null; return identifier != null ? identifier.toString() : null; } public int getColumnCount() { return table.getModel().getColumnCount(); } public int getRowCount() { return table.getModel().getRowCount(); } /** * {@inheritDoc} */ public Object getValueAt(int row, int column) { return table.getModel().getValueAt(row, column); } public void setValueAt(Object aValue, int row, int column) { table.getModel().setValueAt(aValue, row, column); } public
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -