001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 20 Feb 2003
011 *
012 * $Id: XJMenu.java 12433 2010-04-04 00:05:47Z ian_roberts $
013 */
014
015 package gate.swing;
016
017 import gate.event.StatusListener;
018
019 import javax.swing.*;
020 import javax.swing.event.*;
021 import java.awt.event.MouseAdapter;
022 import java.awt.event.MouseEvent;
023 import java.awt.*;
024
025 /**
026 * A modified version of JMenu that uses {@link MenuLayout} as its layout.
027 * Adds also a description and a StatusListener as parameters.
028 * The description is used in the statusListener.
029 */
030 public class XJMenu extends JMenu {
031 public XJMenu(){
032 super();
033 getPopupMenu().setLayout(new MenuLayout());
034 }
035
036 public XJMenu(Action a){
037 super(a);
038 // stop showing tooltip in the menu, status bar is enough
039 setToolTipText(null);
040 getPopupMenu().setLayout(new MenuLayout());
041 }
042
043 public XJMenu(Action a, StatusListener listener){
044 super(a);
045 this.description = (String)a.getValue(Action.SHORT_DESCRIPTION);
046 this.listener = listener;
047 // stop showing tooltip in the menu, status bar is enough
048 setToolTipText(null);
049 initListeners();
050 getPopupMenu().setLayout(new MenuLayout());
051 }
052
053 public XJMenu(String s){
054 super(s);
055 getPopupMenu().setLayout(new MenuLayout());
056 }
057
058 public XJMenu(String s, String description, StatusListener listener){
059 super(s);
060 this.description = description;
061 this.listener = listener;
062 initListeners();
063 getPopupMenu().setLayout(new MenuLayout());
064 }
065
066 public XJMenu(String s, boolean b){
067 super(s, b);
068 getPopupMenu().setLayout(new MenuLayout());
069 }
070
071 /**
072 * Force separators to be the same width as the JPopupMenu.
073 * This is because the MenuLayout make separators invisible contrary
074 * to the default JMenu layout manager.
075 * @param aFlag true if the popupmenu is visible
076 */
077 public void setPopupMenuVisible(boolean aFlag) {
078 super.setPopupMenuVisible(aFlag);
079 if (!aFlag) { return; }
080 MenuLayout layout = (MenuLayout) getPopupMenu().getLayout();
081 for (int i = 0; i < getMenuComponents().length; i++) {
082 Component component = getMenuComponents()[i];
083 if (component instanceof JSeparator) {
084 JSeparator separator = (JSeparator) component;
085 int column = layout.getColumnForComponentIndex(i);
086 int preferredWidth = layout.getPreferredWidthForColumn(column);
087 // use the popupmenu width to set the separators width
088 separator.setPreferredSize(new Dimension(
089 preferredWidth, separator.getHeight()));
090 }
091 }
092 getPopupMenu().revalidate();
093 }
094
095 protected void initListeners(){
096 this.addMouseListener(new MouseAdapter() {
097 public void mouseExited(MouseEvent e) {
098 // clear the status
099 listener.statusChanged("");
100 }
101 });
102 this.addChangeListener(new ChangeListener() {
103 public void stateChanged(ChangeEvent e) {
104 // display the menu description in the status
105 listener.statusChanged(description);
106 }
107 });
108 this.addMenuListener(new MenuListener() {
109 public void menuCanceled(MenuEvent e) {
110 // do nothing
111 }
112 public void menuDeselected(MenuEvent e) {
113 // clear the status
114 listener.statusChanged("");
115 }
116 public void menuSelected(MenuEvent e) {
117 // do nothing
118 }
119 });
120 }
121
122 protected StatusListener listener;
123 private String description;
124 }
|