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