View Javadoc
1   package net.logAnalyzer.utils.gui.quickbar;
2   
3   import java.awt.Cursor;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import java.awt.event.MouseEvent;
7   import java.awt.event.MouseListener;
8   
9   import javax.swing.Icon;
10  import javax.swing.JComponent;
11  import javax.swing.JLabel;
12  import javax.swing.SwingConstants;
13  import javax.swing.event.EventListenerList;
14  
15  import net.logAnalyzer.utils.gui.GraphicsAttributes;
16  
17  public class QuickActionButton extends JLabel implements QuickAction,
18          MouseListener {
19      private static final long serialVersionUID = 1L;
20  
21      private String plainText;
22  
23      public QuickActionButton(String name, String text, Icon icon) {
24          super(text, icon, SwingConstants.LEADING);
25          setName(name);
26          plainText = text;
27          setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
28          setForeground(GraphicsAttributes.QUICKACTION_FOREGROUND_COLOR);
29          addMouseListener(this);
30      }
31  
32      /***
33       * Each QuickAction must be a {@link JComponent} or have an internal
34       * {@link JComponent} for GUI. Useful to place the QuickAction in the
35       * {@link QuickBar}.
36       * 
37       * @return The QuickAction GUI.
38       * @see QuickAction#getGUI()
39       */
40      public JComponent getGUI() {
41          return (JComponent) this;
42      }
43  
44      /***
45       * Adds an <code>ActionListener</code> to the button.
46       * 
47       * @param l
48       *            the <code>ActionListener</code> to be added
49       * @see QuickAction#addActionListener(ActionListener)
50       */
51      public void addActionListener(ActionListener l) {
52          listenerList.add(ActionListener.class, l);
53      }
54  
55      /***
56       * Notifies all listeners that have registered interest for notification on
57       * this event type. The event instance is lazily created using the
58       * <code>event</code> parameter.
59       * 
60       * @param event
61       *            the <code>ActionEvent</code> object
62       * @see EventListenerList
63       */
64      protected void fireActionPerformed(ActionEvent event) {
65          // Guaranteed to return a non-null array
66          Object[] listeners = listenerList.getListenerList();
67          ActionEvent e = null;
68          // Process the listeners last to first, notifying
69          // those that are interested in this event
70          for (int i = listeners.length - 2; i >= 0; i -= 2) {
71              if (listeners[i] == ActionListener.class) {
72                  // Lazily create the event:
73                  if (e == null) {
74                      String actionCommand = event.getActionCommand();
75                      if (actionCommand == null) {
76                          actionCommand = getName();
77                      }
78                      e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
79                              actionCommand, event.getWhen(), event
80                                      .getModifiers());
81                  }
82                  ((ActionListener) listeners[i + 1]).actionPerformed(e);
83              }
84          }
85      }
86  
87      /***
88       * Do nothing.
89       * 
90       * @see MouseListener#mouseClicked(java.awt.event.MouseEvent)
91       */
92      public void mouseClicked(MouseEvent e) {
93          fireActionPerformed(new ActionEvent(this, e.getID(), getName()));
94      }
95  
96      /***
97       * Do nothing.
98       * 
99       * @see MouseListener#mouseEntered(java.awt.event.MouseEvent)
100      */
101     public void mouseEntered(MouseEvent e) {
102         setText("<html><u>" + plainText + "</u></html>");
103     }
104 
105     /***
106      * Remove underlining.
107      * 
108      * @see MouseListener#mouseExited(java.awt.event.MouseEvent)
109      */
110     public void mouseExited(MouseEvent e) {
111         setText(plainText);
112     }
113 
114     /***
115      * Fire an action event.
116      * 
117      * @see MouseListener#mousePressed(java.awt.event.MouseEvent)
118      */
119     public void mousePressed(MouseEvent e) {
120     }
121 
122     /***
123      * Do nothing.
124      * 
125      * @see MouseListener#mouseReleased(java.awt.event.MouseEvent)
126      */
127     public void mouseReleased(MouseEvent e) {
128     }
129     /***
130      * Do nothing.
131      * 
132      * @see java.awt.Component#toString()
133      */
134     public String toString() {
135         return getText();
136     }
137     
138 }