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