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  package org.apache.log4j.chainsaw;
18  
19  import javax.swing.*;
20  import javax.swing.border.BevelBorder;
21  import javax.swing.border.Border;
22  import javax.swing.border.EmptyBorder;
23  import javax.swing.border.SoftBevelBorder;
24  import java.awt.*;
25  import java.awt.event.MouseEvent;
26  import java.awt.event.MouseListener;
27  
28  
29  /**
30   * A better button class that has nice roll over effects.
31   * <p>
32   * This class is borrowed (quite heavily, but with modifications)
33   * from the "Swing: Second Edition"
34   * book by Matthew Robinson and Pavel Vorobeiv. An excellent book on Swing.
35   *
36   * @author Matthew Robinson
37   * @author Pavel Vorobeiv
38   * @author Paul Smith &lt;psmith@apache.org&gt;
39   */
40  public class SmallButton extends JButton implements MouseListener {
41      protected Border m_inactive = new EmptyBorder(3, 3, 3, 3);
42      protected Border m_border = m_inactive;
43      protected Border m_lowered = new SoftBevelBorder(BevelBorder.LOWERED);
44      protected Border m_raised = new SoftBevelBorder(BevelBorder.RAISED);
45      protected Insets m_ins = new Insets(4, 4, 4, 4);
46  
47      public SmallButton() {
48          super();
49          setBorder(m_inactive);
50          setMargin(m_ins);
51          setRequestFocusEnabled(false);
52          addMouseListener(this);
53      }
54  
55      public SmallButton(Action act) {
56          this();
57          setAction(act);
58          setRequestFocusEnabled(false);
59  
60          //      addActionListener(act);
61          addMouseListener(this);
62      }
63  
64      /**
65       * DOCUMENT ME!
66       *
67       * @return DOCUMENT ME!
68       */
69      public float getAlignmentY() {
70          return 0.5f;
71      }
72  
73      /**
74       * DOCUMENT ME!
75       *
76       * @return DOCUMENT ME!
77       */
78      public Border getBorder() {
79          return m_border;
80      }
81  
82      /**
83       * DOCUMENT ME!
84       *
85       * @return DOCUMENT ME!
86       */
87      public Insets getInsets() {
88          return m_ins;
89      }
90  
91      /**
92       * DOCUMENT ME!
93       *
94       * @param e DOCUMENT ME!
95       */
96      public void mouseClicked(MouseEvent e) {
97      }
98  
99      /**
100      * DOCUMENT ME!
101      *
102      * @param e DOCUMENT ME!
103      */
104     public void mouseEntered(MouseEvent e) {
105         if (isEnabled()) {
106             m_border = m_raised;
107             setBorder(m_raised);
108         }
109     }
110 
111     /**
112      * DOCUMENT ME!
113      *
114      * @param e DOCUMENT ME!
115      */
116     public void mouseExited(MouseEvent e) {
117         m_border = m_inactive;
118         setBorder(m_inactive);
119     }
120 
121     /**
122      * DOCUMENT ME!
123      *
124      * @param e DOCUMENT ME!
125      */
126     public void mousePressed(MouseEvent e) {
127 
128         if (isEnabled()) {
129             m_border = m_lowered;
130             setBorder(m_lowered);
131         }
132     }
133 
134     /**
135      * DOCUMENT ME!
136      *
137      * @param e DOCUMENT ME!
138      */
139     public void mouseReleased(MouseEvent e) {
140         m_border = m_inactive;
141         setBorder(m_inactive);
142     }
143 }