📄 jxtable.java
字号:
//------------------ bound action callback methods /** * This resizes all columns to fit the viewport; if horizontal scrolling is * enabled, all columns will get their preferred width. This can be * triggered by the "packAll" BoundAction on the table as well. */ public void packAll() { packTable(getDefaultPackMargin()); } /** * This resizes selected columns to fit the viewport; if horizontal * scrolling is enabled, selected columns will get their preferred width. * This can be triggered by the "packSelected" BoundAction on the table as * well. */ public void packSelected() { int selected = getSelectedColumn(); if (selected >= 0) { packColumn(selected, getDefaultPackMargin()); } } /** * Controls horizontal scrolling in the viewport, and works in coordination * with column sizing. * * @param enabled * If true, the scrollpane will allow the table to scroll * horizontally, and columns will resize to their preferred * width. If false, columns will resize to fit the viewport. */ public void setHorizontalScrollEnabled(boolean enabled) { if (enabled == (isHorizontalScrollEnabled())) return; if (enabled) { oldAutoResizeMode = getAutoResizeMode(); setAutoResizeMode(AUTO_RESIZE_OFF); } else { setAutoResizeMode(oldAutoResizeMode); } } /** Returns the current setting for horizontal scrolling. */ private boolean isHorizontalScrollEnabled() { return getAutoResizeMode() == AUTO_RESIZE_OFF; } /** Returns the default margin for packing columns. */ private int getDefaultPackMargin() { return 4; } /** Notifies the table that a new column has been selected. * overridden to update the enabled state of the packSelected * action. */ public void columnSelectionChanged(ListSelectionEvent e) { super.columnSelectionChanged(e); if (e.getValueIsAdjusting()) return; Action packSelected = getActionMap().get(PACKSELECTED_ACTION_COMMAND); if ((packSelected != null)) { packSelected.setEnabled(!((ListSelectionModel) e.getSource()) .isSelectionEmpty()); } } /** * overridden to update the show horizontal scrollbar action's * selected state. */ public void setAutoResizeMode(int mode) { super.setAutoResizeMode(mode); Action showHorizontal = getActionMap().get( HORIZONTALSCROLL_ACTION_COMMAND); if (showHorizontal instanceof BoundAction) { ((BoundAction) showHorizontal) .setSelected(isHorizontalScrollEnabled()); } }//------------------------ override super because of filter-awareness /** * Returns the row count in the table; if filters are applied, this is the * filtered row count. */ @Override public int getRowCount() { // RG: If there are no filters, call superclass version rather than // accessing model directly return filters == null ? super.getRowCount() : filters.getOutputSize(); } public boolean isHierarchical(int column) { return false; } /** * Convert row index from view coordinates to model coordinates accounting * for the presence of sorters and filters. * * @param row * row index in view coordinates * @return row index in model coordinates */ public int convertRowIndexToModel(int row) { return getFilters().convertRowIndexToModel(row); } /** * Convert row index from model coordinates to view coordinates accounting * for the presence of sorters and filters. * * @param row * row index in model coordinates * @return row index in view coordinates */ public int convertRowIndexToView(int row) { return getFilters().convertRowIndexToView(row); } /** * {@inheritDoc} */ public Object getValueAt(int row, int column) { return getModel().getValueAt(convertRowIndexToModel(row), convertColumnIndexToModel(column)); } /** * {@inheritDoc} */ public void setValueAt(Object aValue, int row, int column) { getModel().setValueAt(aValue, convertRowIndexToModel(row), convertColumnIndexToModel(column)); } /** * {@inheritDoc} */ public boolean isCellEditable(int row, int column) { return getModel().isCellEditable(convertRowIndexToModel(row), convertColumnIndexToModel(column)); } /** * {@inheritDoc} */ public void setModel(TableModel newModel) { // JW: need to look here? is done in tableChanged as well. getSelection().lock(); super.setModel(newModel); } /** * additionally updates filtered state. * {@inheritDoc} */ public void tableChanged(TableModelEvent e) { // JW: make Selection deaf ... super doesn't know about row // mapping and sets rowSelection in model coordinates // causing complete confusion. getSelection().lock(); super.tableChanged(e); updateSelectionAndRowModel(e); use(filters); } /** * reset model selection coordinates in Selection after * model events. * * @param e */ private void updateSelectionAndRowModel(TableModelEvent e) { // JW: c&p from JTable // JW: still missing: checkLeadAnchor (#172-swingx) // super checkLeadAnchor is subtly buggy in lead/anchor update // because it calls model.getRowCount() instead of getRowCount!! if (e.getType() == TableModelEvent.INSERT) { int start = e.getFirstRow(); int end = e.getLastRow(); if (start < 0) { start = 0; } if (end < 0) { end = getModel().getRowCount() - 1; } // Adjust the selection to account for the new rows. int length = end - start + 1; getSelection().insertIndexInterval(start, length, true); getRowSizing().insertIndexInterval(start, length, getRowHeight()); } else if (e.getType() == TableModelEvent.DELETE) { int start = e.getFirstRow(); int end = e.getLastRow(); if (start < 0) { start = 0; } if (end < 0) { end = getModel().getRowCount() - 1; } int deletedCount = end - start + 1; int previousRowCount = getModel().getRowCount() + deletedCount; // Adjust the selection to account for the new rows getSelection().removeIndexInterval(start, end); getRowSizing().removeIndexInterval(start, deletedCount); } else if (getSelectionModel().isSelectionEmpty()) { // JW: this is incomplete! see #167-swingx // possibly got a dataChanged or structureChanged // super will have cleared selection getSelection().clearModelSelection(); getRowSizing().clearModelSizes(); updateViewSizeSequence(); } } /** * Called if individual row height mapping need to be updated. * This implementation guards against unnessary access of * super's private rowModel field. */ protected void updateViewSizeSequence() { SizeSequence sizeSequence = null; if (isRowHeightEnabled()) { sizeSequence = getSuperRowModel(); } getRowSizing().setViewSizeSequence(sizeSequence, getRowHeight()); } private Selection getSelection() { if (selection == null) { selection = new Selection(filters, getSelectionModel()); } return selection; }//----------------------------- filters /** Returns the FilterPipeline for the table. */ public FilterPipeline getFilters() { // PENDING: this is guaranteed to be != null because // init calls setFilters(null) which enforces an empty // pipeline return filters; } /** * setModel() and setFilters() may be called in either order. * * @param pipeline */ private void use(FilterPipeline pipeline) { if (pipeline != null) { // check JW: adding listener multiple times (after setModel)? if (initialUse(pipeline)) { pipeline.addPipelineListener(getFilterPipelineListener()); pipeline.assign(getComponentAdapter()); } else { pipeline.flush(); } } } /** * @return true is not yet used in this JXTable, false otherwise */ private boolean initialUse(FilterPipeline pipeline) { if (pipelineListener == null) return true; PipelineListener[] l = pipeline.getPipelineListeners(); for (int i = 0; i < l.length; i++) { if (pipelineListener.equals(l[i])) return false; } return true; } /** Sets the FilterPipeline for filtering table rows. */ public void setFilters(FilterPipeline pipeline) { FilterPipeline old = getFilters(); Sorter sorter = null; if (old != null) { old.removePipelineListener(pipelineListener); sorter = old.getSorter(); } if (pipeline == null) { pipeline = new FilterPipeline(); } filters = pipeline; filters.setSorter(sorter); // JW: first assign to prevent (short?) illegal internal state // #173-swingx use(filters); getRowSizing().setFilters(filters); getSelection().setFilters(filters); } /** returns the listener for changes in filters. */ protected PipelineListener getFilterPipelineListener() { if (pipelineListener == null) { pipelineListener = createPipelineListener(); } return pipelineListener; } /** creates the listener for changes in filters. */ protected PipelineListener createPipelineListener() { PipelineListener l = new PipelineListener() { public void contentsChanged(PipelineEvent e) { updateOnFilterContentChanged(); } }; return l; } /** * method called on change notification from filterpipeline. */ protected void updateOnFilterContentChanged() { revalidate(); repaint(); }//-------------------------------- sorting /** * Sets "sortable" property indicating whether or not this table * supports sortable columns. If <code>sortable</code> is * <code>true</code> then sorting will be enabled on all columns whose * <code>sortable</code> property is <code>true</code>. If * <code>sortable</code> is <code>false</code> then sorting will be * disabled for all columns, regardless of each column's individual * <code>sorting</code> property. The default is <code>true</code>. * * @see TableColumnExt#isSortable() * @param sortable * boolean indicating whether or not this table supports sortable * columns */ public void setSortable(boolean sortable) { if (sortable == isSortable()) return; this.sortable = sortable; if (!isSortable()) resetSorter(); firePropertyChange("sortable", !sortable, sortable); } /** Returns true if the table is sortable. */ public boolean isSortable() { return sortable; } private void setInteractiveSorter(Sorter sorter) { // this check is for the sake of the very first call after instantiation if (filters == null) return; getFilters().setSorter(sorter); } private Sorter getInteractiveSorter() { // this check is for the sake of the very first call after instantiation if (filters == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -