KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.event.MouseAdapter JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28
29 import javax.swing.Action JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.KeyStroke JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34
35 public class ActionButton extends JButton 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     private Color JavaDoc hoverForeground, oldForeground;
42
43     public ActionButton(AppAction action) {
44         this(action, true);
45     }
46
47     public ActionButton(AppAction action, boolean useLargeIcon) {
48         this(action, useLargeIcon ? AppAction.LARGE_ICON : AppAction.SMALL_ICON, true);
49     }
50
51     public ActionButton(AppAction action, boolean useLargeIcon, boolean showSelectiveText) {
52         this(action, useLargeIcon ? AppAction.LARGE_ICON : AppAction.SMALL_ICON, showSelectiveText);
53         
54     }
55
56     public ActionButton(AppAction action, String JavaDoc iconKey, boolean showSelectiveText) {
57         super();
58         init(action, iconKey, showSelectiveText);
59     }
60
61     private void init(AppAction a, final String JavaDoc iconKey, boolean showText) {
62         enablePlasticWorkaround = UIManager.getLookAndFeel().getClass().getName().startsWith("com.jgoodies.looks.plastic.");
63         setAction(a);
64         addMouseListener(new MouseAdapter JavaDoc() {
65             public void mouseEntered(MouseEvent JavaDoc e) {
66                 if (isEnabled()) {
67                     if(hoverForeground != null) {
68                         oldForeground = getForeground();
69                         setForeground(hoverForeground);
70                     }
71                     setBorderPainted(true);
72                     if (!enablePlasticWorkaround) {
73                         setContentAreaFilled(true);
74                     }
75                 }
76             }
77
78             public void mouseExited(MouseEvent JavaDoc e) {
79                 setBorderPainted(false);
80                 setContentAreaFilled(enablePlasticWorkaround);
81                 if(oldForeground != null) {
82                     setForeground(oldForeground);
83                     oldForeground = null;
84                 }
85             }
86         });
87         a.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
88             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
89                 if (evt.getPropertyName().equals(iconKey)) {
90                     Icon JavaDoc icon = (Icon JavaDoc) evt.getNewValue();
91                     ActionButton.this.setIcon(icon);
92                     ActionButton.this.invalidate();
93                     ActionButton.this.repaint();
94                 }
95             }
96         });
97         setBorderPainted(false);
98         setContentAreaFilled(enablePlasticWorkaround);
99         if (a != null && a.getValue(Action.ACCELERATOR_KEY) != null) {
100             setMnemonic(0);
101             registerKeyboardAction(a, (KeyStroke JavaDoc) a.getValue(Action.ACCELERATOR_KEY), JButton.WHEN_IN_FOCUSED_WINDOW);
102         }
103         setIcon((Icon JavaDoc) a.getValue(iconKey));
104         if (Boolean.FALSE.equals(a.getValue(AppAction.TEXT_ON_TOOLBAR)) || !showText) {
105             setHideText(true);
106         } else {
107             setHideText(false);
108         }
109         setVerticalTextPosition(JButton.BOTTOM);
110         setHorizontalTextPosition(JButton.CENTER);
111     }
112
113     public Insets JavaDoc getMargin() {
114         return INSETS;
115     }
116
117     public boolean isRequestFocusEnabled() {
118         return false;
119     }
120
121     public boolean isFocusTraversable() {
122         return false;
123     }
124
125     public void setHideText(boolean hideText) {
126         if (this.hideText != hideText) {
127             firePropertyChange("hideText", this.hideText, hideText);
128         }
129         this.hideText = hideText;
130         this.setHorizontalTextPosition(ActionButton.RIGHT);
131         repaint();
132     }
133
134     public String JavaDoc getText() {
135         return hideText ? null : super.getText();
136     }
137
138     public void setHoverForeground(Color JavaDoc hoverForeground) {
139         this.hoverForeground = hoverForeground;
140     }
141 }
Popular Tags