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