KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > ToolBarToggleButton


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.swing;
21
22 import java.awt.Insets JavaDoc;
23 import java.awt.event.MouseAdapter JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27
28 import javax.swing.Action JavaDoc;
29 import javax.swing.Icon JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JToggleButton JavaDoc;
32 import javax.swing.KeyStroke JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34
35 public class ToolBarToggleButton extends JToggleButton JavaDoc {
36     // Private statics
37
private final static Insets JavaDoc INSETS = new Insets JavaDoc(0, 0, 0, 0);
38     // Private instance variables
39
private boolean hideText;
40     private boolean enablePlasticWorkaround;
41
42     public ToolBarToggleButton(AbstractToggleableAction action) {
43         this(action, true);
44     }
45
46     public ToolBarToggleButton(AbstractToggleableAction action, boolean useLargeIcon) {
47         this(action, useLargeIcon, true);
48     }
49
50     public ToolBarToggleButton(AbstractToggleableAction action, boolean useLargeIcon, boolean showSelectiveText) {
51         super();
52         init(action, useLargeIcon ? AppAction.LARGE_ICON : AppAction.SMALL_ICON, showSelectiveText);
53     }
54
55     public ToolBarToggleButton(AbstractToggleableAction action, String JavaDoc iconKey, boolean showSelectiveText) {
56         super();
57         init(action, iconKey, showSelectiveText);
58     }
59
60     public void setSelected(boolean b) {
61         System.out.println("Setting " + getAction().getValue(Action.NAME) + " to " + b);
62         super.setSelected(b);
63         if (!isSelected()) {
64             setBorderPainted(false);
65             setContentAreaFilled(enablePlasticWorkaround);
66         }
67     }
68
69     private void init(AbstractToggleableAction a, String JavaDoc iconKey, boolean showText) {
70         enablePlasticWorkaround = UIManager.getLookAndFeel().getClass().getName().startsWith("com.jgoodies.looks.plastic.");
71         setAction(a);
72         addMouseListener(new MouseAdapter JavaDoc() {
73             public void mouseEntered(MouseEvent JavaDoc e) {
74                 if (isEnabled() || isSelected()) {
75                     setBorderPainted(true);
76                     if (!enablePlasticWorkaround) {
77                         setContentAreaFilled(true);
78                     }
79                 }
80             }
81
82             public void mouseExited(MouseEvent JavaDoc e) {
83                 if (!isSelected()) {
84                     setBorderPainted(false);
85                     setContentAreaFilled(enablePlasticWorkaround);
86                 }
87             }
88         });
89         setBorderPainted(false);
90         setContentAreaFilled(enablePlasticWorkaround);
91         if (a != null && a.getValue(Action.ACCELERATOR_KEY) != null) {
92             setMnemonic(0);
93             registerKeyboardAction(a, (KeyStroke JavaDoc) a.getValue(Action.ACCELERATOR_KEY), JButton.WHEN_IN_FOCUSED_WINDOW);
94         }
95         setIcon((Icon JavaDoc) a.getValue(iconKey));
96         if (Boolean.FALSE.equals(a.getValue(AppAction.TEXT_ON_TOOLBAR)) || !showText) {
97             setHideText(true);
98         } else {
99             setHideText(false);
100         }
101         setVerticalTextPosition(JButton.BOTTOM);
102         setHorizontalTextPosition(JButton.CENTER);
103         a.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
104             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
105                 if (evt.getPropertyName().equals("selected")) {
106                     boolean sel = ((Boolean JavaDoc) evt.getNewValue()).booleanValue();
107                     setSelected(sel);
108                 }
109             }
110         });
111         setSelected(a.isSelected());
112     }
113
114     public Insets JavaDoc getMargin() {
115         return INSETS;
116     }
117
118     public boolean isRequestFocusEnabled() {
119         return false;
120     }
121
122     public boolean isFocusTraversable() {
123         return false;
124     }
125
126     public void setHideText(boolean hideText) {
127         if (this.hideText != hideText) {
128             firePropertyChange("hideText", this.hideText, hideText);
129         }
130         this.hideText = hideText;
131         this.setHorizontalTextPosition(ToolBarToggleButton.RIGHT);
132         repaint();
133     }
134
135     public String JavaDoc getText() {
136         return hideText ? null : super.getText();
137     }
138 }
Popular Tags