View Javadoc

1   package fr.ove.openmath.mfd2;
2   
3   // MenuPanel:  The Menu Container class.
4   
5   // Uses text Labels and PopupMenus to simulate a Menu Bar. Standard MenuBars
6   // can only be used in Frame windows, but this can also be used in Applets, as
7   // well as any other Container class.
8   
9   // DISCLAIMER:
10  // Written by Bruce Stone.  (c)1997-1998 Stonerware Enterprises.
11  // You are free to modify this code provided you include this disclaimer.
12  
13  // USAGE:
14  // Set up your MenuBar object as usual, then call setMenuBar(menuBar).
15  // You can use MenuPanel.makeMenu(...) to help create MenuBar items quickly.
16  //
17  // Modified by Stephane Vinter.
18  // Modified by Laurent Dirat.
19  
20  import java.awt.*;
21  import java.awt.event.*;
22  
23  public class MenuPanel extends Panel implements MouseListener, MouseMotionListener
24  {
25    PopupMenu popList[];		// A PopupMenu for each item in your "menu bar".
26    Label     lblList[];		// Labels to hold the names of "menu bar" items.
27    //Color     clrDefault;		// Default color to use for this control
28    int       idxHighlight;	// The menu item index that has the focus. (-1=none)
29    int       idxPressed;		// Menu item idx that user clicked on
30    boolean   bMenuUp;
31    // String    szInfo = "";
32  
33  
34    // Default constructor for the MenuPanel class.
35    public MenuPanel() {
36      setLayout(new FlowLayout(FlowLayout.LEFT, 4, 3));
37      //clrDefault = SystemColor.menu;
38      //setBackground(clrDefault);
39      addMouseMotionListener(this);
40      addMouseListener(this);
41      idxHighlight = -1;    
42      idxPressed = -1;    
43      bMenuUp = false;    
44      
45      lblList = new Label[1];
46      lblList[0] = new Label("Testing...", Label.CENTER);
47      lblList[0].addMouseMotionListener(this);
48      lblList[0].addMouseListener(this);
49      
50      add(lblList[0]);
51      
52      validate();
53    }
54  
55  
56    // Converts a MenuBar to a set of Label/PopupMenu pairs used by MenuPanel.
57    public void setMenuBar(MenuBar mBar) {
58  
59      clearMouseListeners();
60      int menuCount = mBar.getMenuCount();
61      lblList = new Label[menuCount];
62      popList = new PopupMenu[menuCount];
63  
64      // For each Menu in the MenuBar...
65      for (int i = 0;  i < menuCount;  i++) {
66        Menu m = mBar.getMenu(i);
67        String szName = m.getLabel();
68        int itemCount = m.getItemCount();
69  
70        // Generate the corresponding Label/PopupMenu object pairs...
71        lblList[i] = new Label(szName, Label.CENTER);
72        lblList[i].addMouseMotionListener(this);
73        lblList[i].addMouseListener(this);
74        Font fnt = lblList[i].getFont();
75        add(lblList[i]);
76  
77        // Build this PopupMenu item by item (assignment won't work).
78        popList[i] = new PopupMenu(szName);
79        for (int j = 0;  j < itemCount;  j++) {
80  	MenuItem mi = m.getItem(0);
81  	mi.setFont(fnt);
82  	popList[i].add(mi);
83        }
84        lblList[i].add(popList[i]);
85      }
86    }
87  
88  
89    // Paints any 3D rectangles around the Menu labels.
90    public void paint(Graphics g) {
91  
92      Rectangle rect = getBounds();
93  
94      // g.setColor(clrDefault);			// Prepares screen wash
95      g.setColor(getBackground());
96      g.fillRect(0, 0, rect.width, rect.height);	// fill with bkg color
97  
98      // g.setColor(Color.black);
99      // g.drawString(szInfo, 150, 10);
100     // g.setColor(clrDefault);
101 
102     // Paint 3D line underneath menus...
103     //g.draw3DRect(rect.x+1,rect.y+rect.height-3, rect.width-4,1, false);
104 
105     // Paint the Labels and PopupMenus as appropriate...
106     for (int i = 0;  i < lblList.length;  i++) {
107       rect = lblList[i].getBounds();
108       if (i == idxHighlight)
109 	g.draw3DRect(rect.x-1,rect.y-1, rect.width+2,rect.height+2, true);
110       if (i == idxPressed) {
111 	g.draw3DRect(rect.x-1,rect.y-1, rect.width+2,rect.height+2, false);
112 	if (bMenuUp == false) {
113 	  bMenuUp = true;
114 	  popList[i].show(this, rect.x, rect.y+rect.height+1);
115 	}
116       }
117     }
118   }
119 
120 
121   // Used to clear any Listeners before re-creating them with setMenuBar()
122   protected void clearMouseListeners() {
123     for (int i = 0;  i < lblList.length;  i++) {
124       remove(lblList[i]);
125       lblList[i].removeMouseListener(this);
126       lblList[i].removeMouseMotionListener(this);
127     }
128   }
129 
130 
131   // A simplified interface to install Menus and register them for events.
132   // parent - String for Menu name, or other Menu object (i.e. PopupMenus) 
133   // items  - Object array with Strings, MenuItems or "null" for Separators.
134   // target - The ActionListener who receives the Menu events.
135   /* Example 1:
136      mbar.add(makeMenu("Help", new Object[] { "Index","About" }, this) );
137      Example 2:
138      mbar.add(makeMenu("Edit", new Object[] { "Undo", null,
139      new MenuItem("Cut", new MenuShortcut(KeyEvent.VK_LEFT) ),
140      makeMenu("Options", new Object[] { new CheckboxMenuItem("Insert Mode"), }
141      , this) }, this) );
142      */
143   static Menu makeMenu(Object parent, Object[] items, Object target) {
144     Menu m = null;
145 
146     if (parent instanceof Menu)
147       m = (Menu)parent;
148     else if (parent instanceof String)
149       m = new Menu((String)parent);
150     else
151       return null;
152 
153     for (int i = 0;  i < items.length;  i++) {
154       if (items[i] instanceof String) {
155 	MenuItem mi = new MenuItem((String)items[i]);
156 	if (target instanceof ActionListener)
157 	  mi.addActionListener((ActionListener)target);
158 	m.add(mi);
159       }
160       else if (items[i] instanceof CheckboxMenuItem  &&
161 	       target instanceof ItemListener)	{
162 	CheckboxMenuItem cmi = (CheckboxMenuItem)items[i];
163 	cmi.addItemListener((ItemListener)target);
164 	m.add(cmi);
165       }
166       else if (items[i] instanceof MenuItem) {
167 	MenuItem mi = (MenuItem)items[i];
168 	if (target instanceof ActionListener)
169 	  mi.addActionListener((ActionListener)target);
170 	m.add(mi);
171       }
172       else if (items[i] == null)
173 	m.addSeparator();
174     }
175     return m;
176   }
177 
178 
179   //// Event Handlers for MouseMotionEvents and MouseEvents...
180   // Handle the MouseListener.mouseClicked event.
181   public void mousePressed(MouseEvent evt) {
182     int oldIdx = idxPressed;
183 
184     if (idxPressed >= 0) {
185       idxHighlight = idxPressed;
186       idxPressed = -1;
187       repaint();    
188       return;
189     }
190 
191     // Which Label[] index is the mouse hovering over?
192     idxPressed = -1;    //lblState = 0;
193     if (evt.getSource() instanceof Label) {
194       Label lbl = (Label)evt.getSource();
195       for (int i = 0;  i < lblList.length;  i++) {
196 	if (lblList[i] == lbl) {
197 	  idxPressed = i;    
198 	  idxHighlight = -1;    
199 	  bMenuUp = false;
200 	}
201       }
202     }
203 
204     // Point evtPoint = evt.getPoint();
205     // szInfo = "("+evtPoint.x+","+evtPoint.y+")";
206     if (idxPressed != oldIdx)    
207       repaint();
208   }
209 
210   // Handle the MouseListener.mouseExited event.
211   public void mouseExited(MouseEvent evt) {
212     idxHighlight = -1;   
213     idxPressed = -1;
214     repaint();
215   }
216 
217   // Handle the MouseListener.mouseReleased event.
218   public void mouseReleased(MouseEvent evt) {
219     if (evt.getSource() instanceof Label)  
220       mouseClicked(evt);
221   }
222 
223   // Handle the MouseMotionListener.mouseMoved event.
224   public void mouseMoved(MouseEvent evt) {
225     int oldIdx = idxHighlight;
226 
227     // Which Label[] index is the mouse hovering over?
228     idxHighlight = -1;    //lblState = 0;
229     if (evt.getSource() instanceof Label) {
230       Label lbl = (Label)evt.getSource();
231       for (int i = 0;  i < lblList.length;  i++) {
232 	if (lblList[i] == lbl  &&  i != idxPressed) {
233 	  idxHighlight = i;
234 	  if (idxPressed >= 0)    
235 	    idxPressed = i;
236 	}
237       }
238     }
239 
240     // Point evtPoint = evt.getPoint();
241     // szInfo = "("+evtPoint.x+","+evtPoint.y+")";
242     if (idxHighlight != oldIdx)    repaint();
243   }
244 
245   // Handle the MouseMotionListener.mouseDragged event.
246   public void mouseDragged(MouseEvent evt) {
247     mouseMoved(evt);
248   }
249 
250   // Unused MouseEvents.
251   // Not using MouseAdapter & MouseMotionAdapter yields 2 less .class files.
252   public void mouseClicked(MouseEvent evt)    { }
253   public void mouseEntered(MouseEvent evt)    { }
254 }