001: package org.enhydra.jawe.base.panel.panels.tablesorting;
002:
003: import java.awt.Component;
004: import java.awt.event.MouseEvent;
005: import java.util.Vector;
006:
007: import javax.swing.Icon;
008: import javax.swing.JTable;
009: import javax.swing.UIManager;
010: import javax.swing.table.DefaultTableCellRenderer;
011: import javax.swing.table.JTableHeader;
012: import javax.swing.table.TableColumnModel;
013:
014: import org.enhydra.jawe.base.panel.panels.XMLBasicTablePanel;
015: import org.enhydra.shark.xpdl.XMLCollection;
016: import org.enhydra.shark.xpdl.XMLElement;
017:
018: public class BasicSortingTable extends JTable {
019:
020: protected int sortedColIndex = -1;
021: protected boolean ascending = true;
022:
023: XMLBasicTablePanel owner;
024:
025: public BasicSortingTable(XMLBasicTablePanel p, Vector data,
026: Vector names) {
027: super (new SortingTableModel((XMLCollection) p.getOwner(), data,
028: names));
029: this .owner = p;
030:
031: JTableHeader h = getTableHeader();
032: h.setDefaultRenderer(new SHRenderer());
033:
034: // TODO SORTING IS DISABLED
035: // MouseListener ml=new MouseAdapter () {
036: // public void mouseReleased(MouseEvent event) {
037: // performSorting(event);
038: // }
039: // };
040: // h.addMouseListener(ml);
041:
042: }
043:
044: public int getSortedColumnIndex() {
045: return sortedColIndex;
046: }
047:
048: public boolean isSortedColumnAscending() {
049: return ascending;
050: }
051:
052: public void performSorting(MouseEvent event) {
053: if (owner.getOwner().isReadOnly()
054: && ((XMLCollection) owner.getOwner()).getParent()
055: .isReadOnly())
056: return;
057: if (event == null && sortedColIndex == -1)
058: return;
059:
060: TableColumnModel colModel = getColumnModel();
061: int index = sortedColIndex;
062: if (event != null) {
063: index = colModel.getColumnIndexAtX(event.getX());
064: }
065: int modelIndex = colModel.getColumn(index).getModelIndex();
066:
067: SortingTableModel model = (SortingTableModel) getModel();
068:
069: Object selEl = null;
070: int sr = getSelectedRow();
071: if (sr >= 0) {
072: selEl = model.getValueAt(sr, 0);
073: }
074:
075: if (sortedColIndex == index && event != null) {
076: ascending = !ascending;
077: }
078: sortedColIndex = index;
079:
080: model.sortByColumn(modelIndex, ascending);
081:
082: XMLCollection c = (XMLCollection) owner.getOwner();
083: for (int i = 0; i < model.getRowCount(); i++) {
084: XMLElement el = (XMLElement) model.getValueAt(i, 0);
085: if (c.contains(el)) {
086: c.reposition(el, i);
087: }
088: }
089:
090: if (selEl != null) {
091: try {
092: owner.setSelectedElement(selEl);
093: } catch (Exception ex) {
094: }
095: }
096: update(getGraphics());
097: getTableHeader().update(getTableHeader().getGraphics());
098: }
099:
100: static class SHRenderer extends DefaultTableCellRenderer {
101:
102: static Icon NONSORTED = null;
103: static Icon ASCENDING = null;
104: static Icon DESCENDING = null;
105:
106: // static {
107: // URL url = XMLUtil.getResource("ASCENDINGImage");
108: // if ( url != null ) {
109: // ASCENDING = new ImageIcon( url );
110: // }
111: // url = XMLUtil.getResource("DESCENDINGImage");
112: // if ( url != null ) {
113: // DESCENDING = new ImageIcon( url );
114: // }
115: // }
116:
117: public SHRenderer() {
118: setHorizontalTextPosition(LEFT);
119: setHorizontalAlignment(CENTER);
120: }
121:
122: public Component getTableCellRendererComponent(JTable table,
123: Object value, boolean isSelected, boolean hasFocus,
124: int row, int col) {
125: int index = -1;
126: boolean ascending = true;
127: BasicSortingTable sortTable = (BasicSortingTable) table;
128: index = sortTable.getSortedColumnIndex();
129: ascending = sortTable.isSortedColumnAscending();
130:
131: if (table != null) {
132: JTableHeader header = table.getTableHeader();
133: if (header != null) {
134: setForeground(header.getForeground());
135: setBackground(header.getBackground());
136: setFont(header.getFont());
137: }
138: }
139:
140: Icon icon = null;
141: if (ascending) {
142: icon = ASCENDING;
143: } else {
144: icon = DESCENDING;
145: }
146:
147: if (col == index) {
148: setIcon(icon);
149: } else {
150: setIcon(NONSORTED);
151: }
152:
153: if (value == null) {
154: setText("");
155: } else {
156: setText(value.toString());
157: }
158:
159: setBorder(UIManager.getBorder("TableHeader.cellBorder"));
160: return this;
161: }
162: }
163:
164: }
|