001: package org.swingml.treetablebrowser.ext;
002:
003: import java.awt.event.*;
004: import java.net.*;
005: import java.util.*;
006:
007: import javax.swing.*;
008: import javax.swing.table.*;
009: import javax.swing.tree.*;
010:
011: import org.swingml.*;
012: import org.swingml.component.ext.*;
013:
014: /**
015: * @author dpitt
016: */
017: public class TreeTableBrowser implements MouseListener, ActionListener {
018:
019: private static final Map sorts = new HashMap();
020: Filter filter = new Filter();
021: TreeTableBrowserModel model;
022: TableSort sort;
023: TreeTableComponent table;
024:
025: public TreeTableBrowser(final TreeTableComponent ttc,
026: final TreeTableBrowserModel ttbm) {
027: setTable(ttc);
028: setModel(ttbm);
029: getFilter().setModel(ttbm);
030: setSort((TableSort) sorts.get(ttbm.getName()));
031: if (getSort() == null) {
032: setSort(new TableSort());
033: getSort().calculateSortFromModel(ttbm);
034: }
035: getSort().setModel(ttbm);
036: }
037:
038: /**
039: * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
040: */
041: public void actionPerformed(ActionEvent e) {
042: }
043:
044: public void addFolder(String folder) {
045: TreePath path = getTable().tree.getLeadSelectionPath();
046: TreePath parent = path.getParentPath();
047: TreeTableBrowserTreeModel tree = new TreeTableBrowserTreeModel(
048: getModel().getContainer());
049: tree.setName(folder);
050: tree.setText(folder);
051: TreeTableBrowserTreeModel parentModel = (TreeTableBrowserTreeModel) parent
052: .getLastPathComponent();
053: parentModel.addChild(tree);
054: renderTable();
055: return;
056: }
057:
058: public void applyColumnWidths() {
059: for (int i = 0; i < getModel().getColumnCount(); i++) {
060: TreeTableBrowserColumnModel m = (TreeTableBrowserColumnModel) getModel()
061: .getTreeTableColumns().get(i);
062: getTable().setColumnWidth(i, m.getWidth());
063: }
064: }
065:
066: public void applySortDirectionArrows() {
067: URL ascendingUrlImagePath = this .getClass().getResource(
068: Constants.SORTARROWDOWN);
069: URL descendingUrlImagePath = this .getClass().getResource(
070: Constants.SORTARROWUP);
071: ArrayList theColumnSortOrder = getSort().getSortOrder();
072: if (theColumnSortOrder != null) {
073: for (int column = 0; column < getTable().getColumnModel()
074: .getColumnCount(); column++) {
075: ImageIcon arrowIcon = null;
076: String order = null;
077: String sortType = "a";
078: TreeTableBrowserColumnModel tbcm = null;
079: if (getModel().getColumns() != null) {
080: tbcm = (TreeTableBrowserColumnModel) getModel()
081: .getColumns().get(column);
082: }
083: TableCellRenderer headerCellRenderer = getTable()
084: .getColumnModel().getColumn(
085: getTable().convertColumnIndexToView(
086: column)).getHeaderRenderer();
087: if (headerCellRenderer == null) {
088: headerCellRenderer = new ColumnHeadingCellRenderer(
089: tbcm);
090: }
091:
092: if (theColumnSortOrder.contains(new Integer(column))) {
093: if (getSort().direction(column)) {
094: if (ascendingUrlImagePath != null) {
095: arrowIcon = new ImageIcon(
096: ascendingUrlImagePath);
097: } else {
098: order = "a";
099: }
100: } else {
101: if (descendingUrlImagePath != null) {
102: arrowIcon = new ImageIcon(
103: descendingUrlImagePath);
104: sortType = "d";
105: } else {
106: order = "d";
107: sortType = "d";
108: }
109: }
110:
111: ((ColumnHeadingCellRenderer) headerCellRenderer)
112: .setIcon(arrowIcon);
113: ((ColumnHeadingCellRenderer) headerCellRenderer)
114: .setSortType(sortType);
115: ((ColumnHeadingCellRenderer) headerCellRenderer)
116: .setSort(theColumnSortOrder
117: .indexOf(new Integer(column)) + 1);
118: ((ColumnHeadingCellRenderer) headerCellRenderer)
119: .setDirection(order);
120: } else {
121: ((ColumnHeadingCellRenderer) headerCellRenderer)
122: .reset();
123: }
124:
125: getTable().getColumnModel().getColumn(
126: getTable().convertColumnIndexToView(column))
127: .setHeaderRenderer(headerCellRenderer);
128: }
129: }
130: }
131:
132: public Filter getFilter() {
133: return filter;
134: }
135:
136: public TreeTableBrowserModel getModel() {
137: return model;
138: }
139:
140: public TableSort getSort() {
141: return sort;
142: }
143:
144: /**
145: * Returns the table.
146: *
147: * @return JTable
148: */
149: public TreeTableComponent getTable() {
150: return table;
151: }
152:
153: public boolean isEditable(int row) {
154: return true;
155: }
156:
157: /**
158: * @see java.awt.event.MouseListener#mouseClicked(MouseEvent)
159: */
160: public void mouseClicked(final MouseEvent e) {
161: if (e.getSource().getClass().equals(JTableHeader.class)) {
162: JTableHeader theHeader = getTable().getTableHeader();
163: TableColumnModel theColumnModel = theHeader
164: .getColumnModel();
165: int theSelectedColumn = theColumnModel.getColumnIndexAtX(e
166: .getX());
167: // Changed to pull the column out by identifier
168: // int theSelectedColumnNumber =
169: // m.getColumn(theSelectedColumn).getModelIndex();
170: int theSelectedColumnNumber = theColumnModel
171: .getColumnIndex(theColumnModel.getColumn(
172: theSelectedColumn).getIdentifier());
173: int modelIndex = getTable().convertColumnIndexToModel(
174: theSelectedColumnNumber);
175: if (((TreeTableBrowserColumnModel) getModel()
176: .getTreeTableColumns().get(modelIndex))
177: .isSortable()) {
178: // int theSortedColumn = getTheSortedColumn();
179: if (e.getModifiers() == 18) {
180: getSort().removeColumn(modelIndex);
181: } else if (getSort().isSortColumn(modelIndex)) { // ctrl
182: getSort().toggleSortDirection(modelIndex);
183: } else {
184: getSort().addColumn(modelIndex);
185: }
186: sorts.put(getModel().getName(), getSort());
187: }
188: refresh();
189: }
190: }
191:
192: /**
193: * @see java.awt.event.MouseListener#mouseEntered(MouseEvent)
194: */
195: public void mouseEntered(MouseEvent e) {
196: }
197:
198: /**
199: * @see java.awt.event.MouseListener#mouseExited(MouseEvent)
200: */
201: public void mouseExited(MouseEvent e) {
202: }
203:
204: /**
205: * @see java.awt.event.MouseListener#mousePressed(MouseEvent)
206: */
207: public void mousePressed(MouseEvent e) {
208: }
209:
210: /**
211: * @see java.awt.event.MouseListener#mouseReleased(MouseEvent)
212: */
213: public void mouseReleased(MouseEvent e) {
214: }
215:
216: public void refresh() {
217: // apply column headings
218: // getTable().createDefaultColumnsFromModel();
219: // filter
220: getTable().applyFilters();
221: // sort
222: applySortDirectionArrows();
223: // applyColumnWidths();
224: getTable().sort();
225: }
226:
227: public void renderTable() {
228: getTable().getTableHeader().addMouseListener(this );
229: refresh();
230: }
231:
232: public void setFilter(Filter aFilter) {
233: this .filter = aFilter;
234: }
235:
236: public void setModel(TreeTableBrowserModel aModel) {
237: this .model = aModel;
238: }
239:
240: public void setSort(TableSort aSort) {
241: this .sort = aSort;
242: }
243:
244: public void setTable(TreeTableComponent aTable) {
245: this.table = aTable;
246: }
247: }
|