View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.chainsaw;
19  
20  import java.awt.event.InputEvent;
21  import java.awt.event.MouseEvent;
22  import java.awt.event.MouseListener;
23  
24  import javax.swing.JTable;
25  import javax.swing.ListSelectionModel;
26  import javax.swing.SwingUtilities;
27  import javax.swing.table.JTableHeader;
28  import javax.swing.table.TableColumnModel;
29  
30  
31  /***
32   * A Sortable JTable implementation that allows a user to click on a
33   * specific Column and have the row information sorted by that column.
34   *
35   * @author Claude Duguay
36   * @author Scott Deboy <sdeboy@apache.org>
37   * 
38   */
39  public class JSortTable extends JTable implements MouseListener {
40    protected int sortedColumnIndex = -1;
41    protected boolean sortedColumnAscending = true;
42    private String sortedColumn;
43  
44    public JSortTable() {
45      super();
46      initSortHeader();
47    }
48    public JSortTable(SortTableModel model) {
49      super(model);
50      initSortHeader();
51    }
52  
53    public JSortTable(SortTableModel model, TableColumnModel colModel) {
54      super(model, colModel);
55      initSortHeader();
56    }
57  
58    public JSortTable(
59      SortTableModel model, TableColumnModel colModel,
60      ListSelectionModel selModel) {
61      super(model, colModel, selModel);
62      initSortHeader();
63    }
64  
65    protected void initSortHeader() {
66      JTableHeader header = getTableHeader();
67      header.setDefaultRenderer(new SortHeaderRenderer());
68      header.addMouseListener(this);
69    }
70  
71    public int getSortedColumnIndex() {
72      return sortedColumnIndex;
73    }
74  
75    public void updateSortedColumn() {
76    	if (sortedColumn != null) {
77    		try {
78  	  		sortedColumnIndex = columnModel.getColumnIndex(sortedColumn);
79              getTableHeader().resizeAndRepaint();
80    		} catch (IllegalArgumentException ie) {//nothing...column is not in the model
81    			setSortedColumnIndex(-1);
82  	  	}
83  	  }
84    }
85    
86    public void setSortedColumnIndex(int index) {
87      sortedColumnIndex = index;
88      if (sortedColumnIndex > -1) {
89          SortTableModel model = (SortTableModel) getModel();
90          model.sortColumn(sortedColumnIndex, sortedColumnAscending);
91      }
92      getTableHeader().resizeAndRepaint();
93    }
94  
95    public void scrollToRow(final int row, final int col) {
96      SwingUtilities.invokeLater(
97        new Runnable() {
98          public void run() {
99            if ((row > -1) && (row < getRowCount())) {
100             try {
101               setRowSelectionInterval(row, row);
102               scrollRectToVisible(getCellRect(row, col +1, true));
103             } catch (IllegalArgumentException iae) {
104             }
105              //ignore..out of bounds
106           }
107         }
108       });
109   }
110 
111   public void scrollToBottom(final int col) {
112     SwingUtilities.invokeLater(
113       new Runnable() {
114         public void run() {
115           int row = getRowCount() - 1;
116 
117           try {
118             setRowSelectionInterval(row, row);
119             scrollRectToVisible(getCellRect(row, col + 1, true));
120           } catch (IllegalArgumentException iae) {
121           }
122            //ignore..out of bounds
123         }
124       });
125   }
126 
127   public boolean isSortedColumnAscending() {
128     return sortedColumnAscending;
129   }
130 
131   public void mouseClicked(MouseEvent event) {
132   	
133   	if(event.getClickCount()<2 || event.isPopupTrigger()){
134   		return;
135   	}else if(event.getClickCount()>1 && ((event.getModifiers() & InputEvent.BUTTON2_MASK)>0)){
136   		return;
137   	}
138   	
139     TableColumnModel colModel = getColumnModel();
140     int index = colModel.getColumnIndexAtX(event.getX());
141     int modelIndex = colModel.getColumn(index).getModelIndex();
142     SortTableModel model = (SortTableModel) getModel();
143 
144     if (model.isSortable(modelIndex)) {
145       // toggle ascension, if already sorted
146       if (sortedColumnIndex == index) {
147         sortedColumnAscending = !sortedColumnAscending;
148       }
149 
150       sortedColumnIndex = index;
151       sortedColumn = colModel.getColumn(index).getHeaderValue().toString();
152       model.sortColumn(modelIndex, sortedColumnAscending);
153       getTableHeader().resizeAndRepaint();
154     }
155   }
156 
157   public void mousePressed(MouseEvent event) {
158   }
159 
160   public void mouseReleased(MouseEvent event) {
161   }
162 
163   public void mouseEntered(MouseEvent event) {
164   }
165 
166   public void mouseExited(MouseEvent event) {
167   }
168 }