XJMenuItem.java
01 /*  XJMenuItem.java
02  *
03  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
04  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
05  *
06  *  This file is part of GATE (see http://gate.ac.uk/), and is free
07  *  software, licenced under the GNU Library General Public License,
08  *  Version 2, June 1991 (in the distribution as file licence.html,
09  *  and also available at http://gate.ac.uk/gate/licence.html).
10  *
11  *  Valentin Tablan 02/04/2001
12  *
13  *  $Id: XJMenuItem.java 12006 2009-12-01 17:24:28Z thomas_heitz $
14  *
15  */
16 
17 package gate.swing;
18 
19 import java.awt.event.*;
20 
21 import javax.swing.*;
22 import javax.swing.event.ChangeListener;
23 import javax.swing.event.ChangeEvent;
24 
25 import gate.event.StatusListener;
26 
27 /**
28  * Extension of a JMenuItem that adds a description and a StatusListener
29  * as parameters. The description is used in the statusListener.
30  */
31 public class XJMenuItem extends JMenuItem {
32 
33   public XJMenuItem(Icon icon, String description, StatusListener listener){
34     super(icon);
35     this.description = description;
36     this.listener = listener;
37     initListeners();
38   }
39 
40   public XJMenuItem(String text, String description, StatusListener listener){
41     super(text);
42     this.description = description;
43     this.listener = listener;
44     initListeners();
45   }
46 
47   public XJMenuItem(Action a, StatusListener listener){
48     super(a);
49     this.description = (Stringa.getValue(Action.SHORT_DESCRIPTION);
50     this.listener = listener;
51     // stop showing tooltip in the menu, status bar is enough
52     setToolTipText(null);
53     initListeners();
54   }
55 
56   public XJMenuItem(String text, Icon icon,
57                     String description, StatusListener listener){
58     super(text, icon);
59     this.description = description;
60     this.listener = listener;
61     initListeners();
62   }
63 
64   public XJMenuItem(String text, int mnemonic,
65                     String description, StatusListener listener){
66     super(text, mnemonic);
67     this.description = description;
68     this.listener = listener;
69     initListeners();
70   }
71 
72   protected void initListeners(){
73     this.addMouseListener(new MouseAdapter() {
74       public void mouseExited(MouseEvent e) {
75         // clear the status
76         listener.statusChanged("");
77       }
78     });
79     this.addChangeListener(new ChangeListener() {
80       public void stateChanged(ChangeEvent e) {
81         // display the menu item description in the status
82         listener.statusChanged(description);
83       }
84     });
85   }
86 
87   private StatusListener listener;
88   String description;
89 }