View Javadoc

1   package org.wcb.util;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   * 
19   *  Abstract: little arrow button showing sort direction for Jtable
20   */
21  
22  import java.util.Hashtable;
23  import java.awt.Component;
24  import java.awt.Insets;
25  import javax.swing.JButton;
26  import javax.swing.table.TableCellRenderer;
27  import javax.swing.JTable;
28  
29  public class SortButtonRenderer extends JButton implements TableCellRenderer {
30    public static final int NONE = 0;
31    public static final int DOWN = 1;
32    public static final int UP   = 2;
33    
34    int pushedColumn;
35    Hashtable state;
36    JButton downButton,upButton;
37    
38    public SortButtonRenderer() {
39      pushedColumn   = -1;
40      state = new Hashtable();
41      
42      setMargin(new Insets(0,0,0,0));
43      setHorizontalTextPosition(LEFT);
44      setIcon(new BlankIcon());
45          
46      downButton = new JButton();
47      downButton.setMargin(new Insets(0,0,0,0));
48      downButton.setHorizontalTextPosition(LEFT);
49      downButton.setIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false));
50      downButton.setPressedIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, true));
51      
52      upButton = new JButton();
53      upButton.setMargin(new Insets(0,0,0,0));
54      upButton.setHorizontalTextPosition(LEFT);
55      upButton.setIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, false));
56      upButton.setPressedIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, true));
57      
58    }
59  
60    public void setSelectedColumn(int col) {
61      if (col < 0) return;
62      Integer value = null;
63      Object obj = state.get(new Integer(col));
64      if (obj == null) {
65        value = new Integer(DOWN);
66      } else {
67        if (((Integer)obj).intValue() == DOWN) {
68          value = new Integer(UP);
69        } else {
70          value = new Integer(DOWN);
71        }
72      }
73      state.clear();
74      state.put(new Integer(col), value);
75    } 
76    
77    public Component getTableCellRendererComponent(JTable table, Object value,
78                     boolean isSelected, boolean hasFocus, int row, int column) {
79      JButton button = this;
80      Object obj = state.get(new Integer(column));
81      if (obj != null) {
82        if (((Integer)obj).intValue() == DOWN) {
83          button = downButton;
84        } else {
85          button = upButton;
86        }
87      }
88      button.setText((value ==null) ? "" : value.toString());
89      boolean isPressed = (column == pushedColumn);
90      button.getModel().setPressed(isPressed);
91      button.getModel().setArmed(isPressed);
92      return button;
93    }
94    
95    public void setPressedColumn(int col) {
96      pushedColumn = col;
97    }
98      
99    public int getState(int col) {
100     int retValue;
101     Object obj = state.get(new Integer(col));
102     if (obj == null) {
103       retValue = NONE;
104     } else {
105       if (((Integer)obj).intValue() == DOWN) {
106         retValue = DOWN;
107       } else {
108         retValue = UP;
109       }
110     }
111     return retValue;
112   } 
113 }
114 
115 
116