📄 jxtable.java
字号:
return null; return getFilters().getSorter(); } /** * Removes the interactive sorter. * Used by headerListener. * */ protected void resetSorter() { // JW PENDING: think about notification instead of manual repaint. setInteractiveSorter(null); getTableHeader().repaint(); } public void columnRemoved(TableColumnModelEvent e) { // JW - old problem: need access to removed column // to get hold of removed modelIndex // to remove interactive sorter if any // no way // int modelIndex = convertColumnIndexToModel(e.getFromIndex()); updateSorterAfterColumnRemoved(); super.columnRemoved(e); } /** * guarantee that the interactive sorter is removed if its column * is removed. * */ private void updateSorterAfterColumnRemoved() { // bloody hack: get sorter and check if there's a column with it // available Sorter sorter = getInteractiveSorter(); if (sorter != null) { int sorterColumn = sorter.getColumnIndex(); List columns = getColumns(true); for (Iterator iter = columns.iterator(); iter.hasNext();) { TableColumn column = (TableColumn) iter.next(); if (column.getModelIndex() == sorterColumn) return; } // didn't find a column with the sorter's index - remove resetSorter(); } } /** * * request to sort the column at columnIndex in view coordinates. if there * is already an interactive sorter for this column it's sort order is * reversed. Otherwise the columns sorter is used as is. * Used by headerListener. * */ protected void setSorter(int columnIndex) { if (!isSortable()) return; Sorter sorter = getInteractiveSorter(); if ((sorter != null) && (sorter.getColumnIndex() == convertColumnIndexToModel(columnIndex))) { sorter.toggle(); } else { TableColumnExt column = getColumnExt(columnIndex); getFilters().setSorter(column != null ? column.getSorter() : null); } } /** * Returns the interactive sorter if it is set from the given column. * Used by ColumnHeaderRenderer.getTableCellRendererComponent(). * * @param columnIndex the column index in view coordinates. * @return the interactive sorter if matches the column or null. */ public Sorter getSorter(int columnIndex) { Sorter sorter = getInteractiveSorter(); return sorter == null ? null : sorter.getColumnIndex() == convertColumnIndexToModel(columnIndex) ? sorter : null; } //---------------------- enhanced TableColumn/Model support /** * Remove all columns, make sure to include hidden. * */ protected void removeColumns() { /** * @todo promote this method to superclass, and change * createDefaultColumnsFromModel() to call this method */ List columns = getColumns(true); for (Iterator iter = columns.iterator(); iter.hasNext();) { getColumnModel().removeColumn((TableColumn) iter.next()); } } /** * returns a list of all visible TableColumns. * * @return */ public List getColumns() { return Collections.list(getColumnModel().getColumns()); } /** * returns a list of TableColumns including hidden if the parameter is set * to true. * * @param includeHidden * @return */ public List getColumns(boolean includeHidden) { if (includeHidden && (getColumnModel() instanceof TableColumnModelExt)) { return ((TableColumnModelExt) getColumnModel()) .getColumns(includeHidden); } return getColumns(); } /** * returns the number of TableColumns including hidden if the parameter is set * to true. * * @param includeHidden * @return */ public int getColumnCount(boolean includeHidden) { if (getColumnModel() instanceof TableColumnModelExt) { return ((TableColumnModelExt) getColumnModel()) .getColumnCount(includeHidden); } return getColumnCount(); } /** * reorders the columns in the sequence given array. Logical names that do * not correspond to any column in the model will be ignored. Columns with * logical names not contained are added at the end. * * @param columnNames * array of logical column names */ public void setColumnSequence(Object[] identifiers) { List columns = getColumns(true); Map map = new HashMap(); for (Iterator iter = columns.iterator(); iter.hasNext();) { // PENDING: handle duplicate identifiers ... TableColumn column = (TableColumn) iter.next(); map.put(column.getIdentifier(), column); getColumnModel().removeColumn(column); } for (int i = 0; i < identifiers.length; i++) { TableColumn column = (TableColumn) map.get(identifiers[i]); if (column != null) { getColumnModel().addColumn(column); columns.remove(column); } } for (Iterator iter = columns.iterator(); iter.hasNext();) { TableColumn column = (TableColumn) iter.next(); getColumnModel().addColumn(column); } } /** * Returns the <code>TableColumnExt</code> object for the column in the * table whose identifier is equal to <code>identifier</code>, when * compared using <code>equals</code>. The returned TableColumn is * guaranteed to be part of the current ColumnModel but may be hidden, that * is * * <pre> <code> * TableColumnExt column = table.getColumnExt(id); * if (column != null) { * int viewIndex = table.convertColumnIndexToView(column.getModelIndex()); * assertEquals(column.isVisible(), viewIndex >= 0); * } * </code> </pre> * * @param identifier * the identifier object * * @return the <code>TableColumnExt</code> object that matches the * identifier or null if none is found. */ public TableColumnExt getColumnExt(Object identifier) { if (getColumnModel() instanceof TableColumnModelExt) { return ((TableColumnModelExt) getColumnModel()) .getColumnExt(identifier); } else { // PENDING: not tested! try { TableColumn column = getColumn(identifier); if (column instanceof TableColumnExt) { return (TableColumnExt) column; } } catch (Exception e) { // TODO: handle exception } } return null; } /** * Returns the <code>TableColumnExt</code> object for the column in the * table whose column index is equal to <code>viewColumnIndex</code> * * @param viewColumnIndex * index of the column with the object in question * * @return the <code>TableColumnExt</code> object that matches the column * index * @exception IllegalArgumentException * if no <code>TableColumn</code> has this identifier */ public TableColumnExt getColumnExt(int viewColumnIndex) { return (TableColumnExt) getColumnModel().getColumn(viewColumnIndex); } public void createDefaultColumnsFromModel() { TableModel model = getModel(); if (model != null) { // Create new columns from the data model info // Note: it's critical to create the new columns before // deleting the old ones. Why? // JW PENDING: the reason is somewhere in the early forums - search! int modelColumnCount = model.getColumnCount(); TableColumn newColumns[] = new TableColumn[modelColumnCount]; for (int i = 0; i < newColumns.length; i++) { newColumns[i] = createAndConfigureColumn(model, i); } // Remove any current columns removeColumns(); // Now add the new columns to the column model for (int i = 0; i < newColumns.length; i++) { addColumn(newColumns[i]); } } } protected TableColumn createAndConfigureColumn(TableModel model, int modelColumn) { return getColumnFactory().createAndConfigureTableColumn(model, modelColumn); } protected ColumnFactory getColumnFactory() { if (columnFactory == null) { columnFactory = ColumnFactory.getInstance(); } return columnFactory; } //----------------------- delegating methods?? from super /** * Returns the margin between columns. * * @return the margin between columns */ public int getColumnMargin() { return getColumnModel().getColumnMargin(); } /** * Sets the margin between columns. * * @param value * margin between columns; must be greater than or equal to zero. */ public void setColumnMargin(int value) { getColumnModel().setColumnMargin(value); } /** * Returns the selection mode used by this table's selection model. * * @return the selection mode used by this table's selection model */ public int getSelectionMode() { return getSelectionModel().getSelectionMode(); }//----------------------- Search support /** Opens the find widget for the table. */ private void find() { SearchFactory.getInstance().showFindInput(this, getSearchable()); } /** * * @returns a not-null Searchable for this editor. */ public Searchable getSearchable() { if (searchable == null) { searchable = new TableSearchable(); } return searchable; } /** * sets the Searchable for this editor. If null, a default * searchable will be used. * * @param searchable */ public void setSearchable(Searchable searchable) { this.searchable = searchable; } public class TableSearchable extends AbstractSearchable { private SearchHighlighter searchHighlighter; @Override protected void findMatchAndUpdateState(Pattern pattern, int startRow, boolean backwards) { SearchResult matchRow = null; if (backwards) { // CHECK: off-one end still needed? // Probably not - the findXX don't have side-effects any longer // hmmm... still needed: even without side-effects we need to // guarantee calling the notfound update at the very end of the // loop. for (int r = startRow; r >= -1 && matchRow == null; r--) { matchRow = findMatchBackwardsInRow(pattern, r); updateState(matchRow); } } else { for (int r = startRow; r <= getSize() && matchRow == null; r++) { matchRow = findMatchForwardInRow(pattern, r); updateState(matchRow); } } // JW: Needed to update if loop wasn't entered! // the alternative is to go one off in the loop. Hmm - which is // preferable? // updateState(matchRow); } /** * called if sameRowIndex && !hasEqualRegEx. Matches the cell at * row/lastFoundColumn against the pattern. PRE: lastFoundColumn valid. * * @param pattern * @param row * @return */ protected SearchResult findExtendedMatch(Pattern pattern, int row) { return findMatchAt(pattern, row, lastSearchResult.foundColumn); } /** * 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -