package chrriis.common.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
/**
* TableSorter is a decorator for TableModels; adding sorting
* functionality to a supplied TableModel. TableSorter does
* not store or copy the data in its TableModel; instead it maintains
* a map from the row indexes of the view to the row indexes of the
* model. As requests are made of the sorter (like getValueAt(row, col))
* they are passed to the underlying model after the row numbers
* have been translated via the internal mapping array. This way,
* the TableSorter appears to hold another copy of the table
* with the rows in a different order.
* <p/>
* TableSorter registers itself as a listener to the underlying model,
* just as the JTable itself would. Events recieved from the model
* are examined, sometimes manipulated (typically widened), and then
* passed on to the TableSorter's listeners (typically the JTable).
* If a change to the model has invalidated the order of TableSorter's
* rows, a note of this is made and the sorter will resort the
* rows the next time a value is requested.
* <p/>
* When the tableHeader property is set, either by using the
* setTableHeader() method or the two argument constructor, the
* table header may be used as a complete UI for TableSorter.
* The default renderer of the tableHeader is decorated with a renderer
* that indicates the sorting status of each column. In addition,
* a mouse listener is installed with the following behavior:
* <ul>
* <li>
* Mouse-click: Clears the sorting status of all other columns
* and advances the sorting status of that column through three
* values: {NOT_SORTED, ASCENDING, DESCENDING} (then back to
* NOT_SORTED again).
* <li>
* SHIFT-mouse-click: Clears the sorting status of all other columns
* and cycles the sorting status of the column through the same
* three values, in the opposite order: {NOT_SORTED, DESCENDING, ASCENDING}.
* <li>
* CONTROL-mouse-click and CONTROL-SHIFT-mouse-click: as above except
* that the changes to the column do not cancel the statuses of columns
* that are already sorting - giving a way to initiate a compound
* sort.
* </ul>
* <p/>
* This is a long overdue rewrite of a class of the same name that
* first appeared in the swing table demos in 1997.
*
* @author Philip Milne
* @author Brendon McLean
* @author Dan van Enckevort
* @author Parwinder Sekhon
* @version 2.0 02/27/04
*/
public class TableSorter extends AbstractTableModel {
protected TableModel tableModel;
public static final int DESCENDING = -1;
public static final int NOT_SORTED = 0;
public static final int ASCENDING = 1;
private static Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED);
public static final Comparator<Object> COMPARABLE_COMPARATOR = new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
if(o1 instanceof String && o2 instanceof String) {
return ((String)o1).toLowerCase(Locale.ENGLISH).compareTo(((String)o2).toLowerCase(Locale.ENGLISH));
}
return ((Comparable) o1).compareTo(o2);
}
};
public static final Comparator<Object> LEXICAL_COMPARATOR = new Comparator<Object>() {
public int compare(Object o1, Object o2) {
return o1.toString().toLowerCase(Locale.ENGLISH).compareTo(o2.toString().toLowerCase(Locale.ENGLISH));
}
};
private Row[] viewToModel;
private int[] modelToView;
private JTableHeader tableHeader;
private MouseListener mouseListener;
private TableModelListener tableModelListener;
private Map<Class<?>, Comparator<Object>> columnComparators = new HashMap<Class<?>, Comparator<Object>>();
private List<Directive> sortingColumns = new ArrayList<Directive>();
public TableSorter() {
mouseListener = new MouseHandler();
tableModelListener = new TableModelHandler();
}
public TableSorter(TableModel tableModel) {
this();
setTableModel(tableModel);
}
public TableSorter(TableModel tableModel, JTableHeader tableHeader) {
this();
setTableHeader(tableHeader);
setTableModel(tableModel);
}
private void clearSortingState() {
viewToModel = null;
modelToView = null;
}
public TableModel getTableModel() {
return tableModel;
}
public void setTableModel(TableModel tableModel) {
if (this.tableModel != null) {
this.tableModel.removeTableModelListener(tableModelListener);
}
this.tableModel = tableModel;
if (this.tableModel != null) {
this.tableModel.addTableModelListener(tableModelListener);
}
clearSortingState();
fireTableStructureChanged();
}
public JTableHeader getTableHeader() {
return tableHeader;
}
public void setTableHeader(JTableHeader tableHeader) {
if (this.tableHeader != null) {
this.tableHeader.removeMouseListener(mouseListener);
TableCellRenderer defaultRenderer = this.tableHeader.getDefaultRenderer();
if (defaultRenderer instanceof SortableHeaderRenderer) {
this.tableHeader.setDefaultRenderer(((SortableHeaderRenderer) defaultRenderer).tableCellRenderer);
}
}
this.tableHeader = tableHeader;
if (this.tableHeader != null) {
this.tableHeader.addMouseListener(mouseListener);
this.tableHeader.setDefaultRenderer(
new SortableHeaderRenderer(this.tableHeader.getDefaultRenderer()));
}
}
public boolean isSorting() {
return sortingColumns.size() != 0;
}
private Directive getDirective(int column) {
for (int i = 0; i < sortingColumns.size(); i++) {
Directive directive = sortingColumns.get(i);
if (directive.column == column) {
return directive;
}
}
return EMPTY_DIRECTIVE;
}
public int getSortingStatus(int column) {
return getDirective(column).direction;
}
private void sortingStatusChanged() {
clearSortingState();
fireTableDataChanged();
if (tableHeader != null) {
tableHeader.repaint();
}
}
public void setSortingStatus(int column, int status) {
Directive directive = getDirective(column);
if (directive != EMPTY_DIRECTIVE) {
sortingColumns.remove(directive);
}
if (status != NOT_SORTED) {
sortingColumns.add(new Directive(column, status));
}
sortingStatusChanged();
}
protected Icon getHeaderRendererIcon(int column, int size) {
Directive directive = getDirective(column);
if (directive == EMPTY_DIRECTIVE) {
return null;
}
return new Arrow(directive.direction == DESCENDING, size, sortingColumns.indexOf(directive));
}
private void cancelSorting() {
sortingColumns.clear();
sortingStatusChanged();
}
public void setColumnComparator(Class<?> type, Comparator<Object> comparator) {
if (comparator == null) {
columnComparators.remove(type
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java浏览器+swing嵌套网页+源码免费 (299个子文件)
java.policy.applet 141B
DemoExampleDefinitionLoader.class 11KB
TableSorter.class 9KB
FileAssociations$2$1.class 6KB
ComponentLifeCycle.class 6KB
CKEditorExample.class 5KB
XPCOMDownloadManager$2.class 5KB
FCKEditorExample.class 5KB
TinyMCEExample.class 5KB
FullPageCaptureExample$1.class 5KB
DemoPane$2$1$1.class 5KB
EditorDirtyExample.class 5KB
WebBrowserFunctionsExample.class 5KB
JavascriptExecution.class 4KB
Cookies.class 4KB
ConfirmedDisposal.class 4KB
SimpleWMediaPlayerExample.class 4KB
SendingCommands.class 4KB
ConstrainVisibility.class 4KB
FunctionCalls.class 4KB
SimpleWShellExplorerExample.class 4KB
OptionsAndPlaylistExample.class 4KB
HierarchyProxying.class 4KB
SimpleVLCPlayerExample.class 4KB
NativeDialogs$1.class 4KB
DemoPane$2$1$1$2.class 4KB
PseudoTransparency.class 4KB
VariablesAndFlow.class 4KB
NavigationParameters.class 4KB
SettingContent.class 4KB
ThumbnailCreation.class 4KB
InputEventsExample.class 3KB
Cookies$4.class 3KB
NavigationControl.class 3KB
NativeIntegration.class 3KB
CustomDecorators.class 3KB
NavigationParameters$2.class 3KB
WindowsAsTabs.class 3KB
TheSolution.class 3KB
MyWebWindow.class 3KB
ThumbnailCreation$3.class 3KB
NavigationControl$1$3.class 3KB
Codewise.class 3KB
NavigationParameters$1.class 3KB
FullPageCaptureExample.class 3KB
NativeDialogs.class 3KB
CustomDecorators$4.class 3KB
NavigationControl$1.class 3KB
ThumbnailCreation$ThumbnailPane.class 3KB
XPCOMDownloadManager.class 3KB
WebBrowserFunctionsExample$1.class 3KB
DeferredDestruction.class 3KB
WindowsAsTabs$1.class 3KB
XPCOMToggleEditionMode.class 3KB
SimpleSyntaxHighlighterExample.class 2KB
FileAssociations$2$1$8.class 2KB
SimpleWebBrowserExample.class 2KB
XPCOMToggleEditionMode$1.class 2KB
DemoPane.class 2KB
WebStartAndApplets.class 2KB
SendingCommands$1.class 2KB
TableSorter$Row.class 2KB
DemoTree.class 2KB
DemoFrame$1.class 2KB
XPCOMDownloadManager$1.class 2KB
ComponentLifeCycle$1.class 2KB
DemoPane$2.class 2KB
XPCOMDownloadManager$2$1.class 2KB
DemoPane$2$1$1$1.class 2KB
FileAssociations.class 2KB
TableSorter$Arrow.class 2KB
FileAssociations$2$1$1.class 2KB
Example.class 2KB
ConfirmedDisposal$1.class 2KB
XPCOMDownloadManager$1$1.class 2KB
TableSorter$SortableHeaderRenderer.class 2KB
PseudoTransparency$3.class 2KB
TableSorter$MouseHandler.class 2KB
NavigationControl$1$1.class 2KB
FileAssociations$2$1$3$1.class 2KB
FunctionCalls$3.class 2KB
FileAssociations$2$1$5.class 2KB
SimpleWMediaPlayerExample$3.class 2KB
SimpleWShellExplorerExample$3.class 2KB
FileAssociations$2$1$4.class 2KB
InputEventsExample$1.class 2KB
WebBrowserFunctionsExample$2.class 2KB
OptionsAndPlaylistExample$3.class 2KB
TableSorter$TableModelHandler.class 2KB
PseudoTransparency$2.class 2KB
ClasspathPages.class 2KB
SimpleVLCPlayerExample$3.class 2KB
NavigationControl$1$2.class 2KB
FileAssociations$2.class 1KB
OptionsAndPlaylistExample$1.class 1KB
DemoTree$1.class 1KB
ThumbnailCreation$3$1.class 1KB
CustomDecorators$4$1.class 1KB
DemoPane$2$1.class 1KB
FileAssociations$2$1$7.class 1KB
共 299 条
- 1
- 2
- 3
yjflinchong
- 粉丝: 671
- 资源: 210
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页