KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > MenuSelectionManager


1 /*
2  * @(#)MenuSelectionManager.java 1.38 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.*;
10 import java.util.*;
11 import java.awt.event.*;
12 import javax.swing.event.*;
13
14 /**
15  * A MenuSelectionManager owns the selection in menu hierarchy.
16  *
17  * @version 1.38 12/19/03
18  * @author Arnaud Weber
19  */

20 public class MenuSelectionManager {
21     private static final MenuSelectionManager JavaDoc instance =
22         new MenuSelectionManager JavaDoc();
23
24     private Vector selection = new Vector();
25
26     /* diagnostic aids -- should be false for production builds. */
27     private static final boolean TRACE = false; // trace creates and disposes
28
private static final boolean VERBOSE = false; // show reuse hits/misses
29
private static final boolean DEBUG = false; // show bad params, misc.
30

31     /**
32      * Returns the default menu selection manager.
33      *
34      * @return a MenuSelectionManager object
35      */

36     public static MenuSelectionManager JavaDoc defaultManager() {
37         return instance;
38     }
39     
40     /**
41      * Only one ChangeEvent is needed per button model instance since the
42      * event's only state is the source property. The source of events
43      * generated is always "this".
44      */

45     protected transient ChangeEvent changeEvent = null;
46     protected EventListenerList listenerList = new EventListenerList();
47
48     /**
49      * Changes the selection in the menu hierarchy. The elements
50      * in the array are sorted in order from the root menu
51      * element to the currently selected menu element.
52      * <p>
53      * Note that this method is public but is used by the look and
54      * feel engine and should not be called by client applications.
55      *
56      * @param path an array of <code>MenuElement</code> objects specifying
57      * the selected path
58      */

59     public void setSelectedPath(MenuElement JavaDoc[] path) {
60         int i,c;
61         int currentSelectionCount = selection.size();
62         int firstDifference = 0;
63
64         if(path == null) {
65             path = new MenuElement JavaDoc[0];
66         }
67
68     if (DEBUG) {
69         System.out.print("Previous: "); printMenuElementArray(getSelectedPath());
70         System.out.print("New: "); printMenuElementArray(path);
71     }
72
73         for(i=0,c=path.length;i<c;i++) {
74             if(i < currentSelectionCount && (MenuElement JavaDoc)selection.elementAt(i) == path[i])
75                 firstDifference++;
76             else
77                 break;
78         }
79
80         for(i=currentSelectionCount - 1 ; i >= firstDifference ; i--) {
81             MenuElement JavaDoc me = (MenuElement JavaDoc)selection.elementAt(i);
82             selection.removeElementAt(i);
83             me.menuSelectionChanged(false);
84         }
85
86         for(i = firstDifference, c = path.length ; i < c ; i++) {
87         if (path[i] != null) {
88         selection.addElement(path[i]);
89         path[i].menuSelectionChanged(true);
90         }
91     }
92
93     fireStateChanged();
94     }
95
96     /**
97      * Returns the path to the currently selected menu item
98      *
99      * @return an array of MenuElement objects representing the selected path
100      */

101     public MenuElement JavaDoc[] getSelectedPath() {
102         MenuElement JavaDoc res[] = new MenuElement JavaDoc[selection.size()];
103         int i,c;
104         for(i=0,c=selection.size();i<c;i++)
105             res[i] = (MenuElement JavaDoc) selection.elementAt(i);
106         return res;
107     }
108
109     /**
110      * Tell the menu selection to close and unselect all the menu components. Call this method
111      * when a choice has been made
112      */

113     public void clearSelectedPath() {
114         if (selection.size() > 0) {
115             setSelectedPath(null);
116         }
117     }
118
119     /**
120      * Adds a ChangeListener to the button.
121      *
122      * @param l the listener to add
123      */

124     public void addChangeListener(ChangeListener l) {
125         listenerList.add(ChangeListener.class, l);
126     }
127     
128     /**
129      * Removes a ChangeListener from the button.
130      *
131      * @param l the listener to remove
132      */

133     public void removeChangeListener(ChangeListener l) {
134         listenerList.remove(ChangeListener.class, l);
135     }
136
137     /**
138      * Returns an array of all the <code>ChangeListener</code>s added
139      * to this MenuSelectionManager with addChangeListener().
140      *
141      * @return all of the <code>ChangeListener</code>s added or an empty
142      * array if no listeners have been added
143      * @since 1.4
144      */

145     public ChangeListener[] getChangeListeners() {
146         return (ChangeListener[])listenerList.getListeners(
147                 ChangeListener.class);
148     }
149
150     /**
151      * Notifies all listeners that have registered interest for
152      * notification on this event type. The event instance
153      * is created lazily.
154      *
155      * @see EventListenerList
156      */

157     protected void fireStateChanged() {
158         // Guaranteed to return a non-null array
159
Object JavaDoc[] listeners = listenerList.getListenerList();
160         // Process the listeners last to first, notifying
161
// those that are interested in this event
162
for (int i = listeners.length-2; i>=0; i-=2) {
163             if (listeners[i]==ChangeListener.class) {
164                 // Lazily create the event:
165
if (changeEvent == null)
166                     changeEvent = new ChangeEvent(this);
167                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
168             }
169         }
170     }
171
172     /**
173      * When a MenuElement receives an event from a MouseListener, it should never process the event
174      * directly. Instead all MenuElements should call this method with the event.
175      *
176      * @param event a MouseEvent object
177      */

178     public void processMouseEvent(MouseEvent event) {
179         int screenX,screenY;
180         Point p;
181         int i,c,j,d;
182         Component mc;
183         Rectangle r2;
184         int cWidth,cHeight;
185         MenuElement JavaDoc menuElement;
186         MenuElement JavaDoc subElements[];
187         MenuElement JavaDoc path[];
188         Vector tmp;
189         int selectionSize;
190         p = event.getPoint();
191     
192     Component source = (Component)event.getSource();
193
194     if (!source.isShowing()) {
195         // This can happen if a mouseReleased removes the
196
// containing component -- bug 4146684
197
return;
198     }
199
200     int type = event.getID();
201     int modifiers = event.getModifiers();
202     // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
203
if ((type==MouseEvent.MOUSE_ENTERED||
204          type==MouseEvent.MOUSE_EXITED)
205         && ((modifiers & (InputEvent.BUTTON1_MASK |
206                   InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 )) {
207         return;
208     }
209
210         SwingUtilities.convertPointToScreen(p,source);
211
212         screenX = p.x;
213         screenY = p.y;
214
215         tmp = (Vector)selection.clone();
216         selectionSize = tmp.size();
217     boolean success = false;
218     for (i=selectionSize - 1;i >= 0 && success == false; i--) {
219             menuElement = (MenuElement JavaDoc) tmp.elementAt(i);
220             subElements = menuElement.getSubElements();
221             
222             path = null;
223         for (j = 0, d = subElements.length;j < d && success == false; j++) {
224         if (subElements[j] == null)
225             continue;
226                 mc = subElements[j].getComponent();
227                 if(!mc.isShowing())
228                     continue;
229                 if(mc instanceof JComponent JavaDoc) {
230                     cWidth = ((JComponent JavaDoc)mc).getWidth();
231                     cHeight = ((JComponent JavaDoc)mc).getHeight();
232                 } else {
233                     r2 = mc.getBounds();
234                     cWidth = r2.width;
235                     cHeight = r2.height;
236                 }
237                 p.x = screenX;
238                 p.y = screenY;
239                 SwingUtilities.convertPointFromScreen(p,mc);
240
241                 /** Send the event to visible menu element if menu element currently in
242                  * the selected path or contains the event location
243                  */

244                 if(
245                    (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight)) {
246                     int k;
247                     if(path == null) {
248                         path = new MenuElement JavaDoc[i+2];
249                         for(k=0;k<=i;k++)
250                             path[k] = (MenuElement JavaDoc)tmp.elementAt(k);
251                     }
252                     path[i+1] = subElements[j];
253             MenuElement JavaDoc currentSelection[] = getSelectedPath();
254
255             // Enter/exit detection -- needs tuning...
256
if (currentSelection[currentSelection.length-1] !=
257             path[i+1] &&
258             (currentSelection.length < 2 ||
259              currentSelection[currentSelection.length-2] !=
260              path[i+1])) {
261             Component oldMC = currentSelection[currentSelection.length-1].getComponent();
262
263             MouseEvent exitEvent = new MouseEvent(oldMC, MouseEvent.MOUSE_EXITED,
264                                   event.getWhen(),
265                                   event.getModifiers(), p.x, p.y,
266                                   event.getClickCount(),
267                                   event.isPopupTrigger());
268             currentSelection[currentSelection.length-1].
269                 processMouseEvent(exitEvent, path, this);
270
271             MouseEvent enterEvent = new MouseEvent(mc,
272                                    MouseEvent.MOUSE_ENTERED,
273                                    event.getWhen(),
274                                    event.getModifiers(), p.x, p.y,
275                                    event.getClickCount(),
276                                    event.isPopupTrigger());
277             subElements[j].processMouseEvent(enterEvent, path, this);
278             }
279             MouseEvent mouseEvent = new MouseEvent(mc, event.getID(),event. getWhen(),
280                                event.getModifiers(), p.x, p.y,
281                                event.getClickCount(),
282                                event.isPopupTrigger());
283             subElements[j].processMouseEvent(mouseEvent, path, this);
284             success = true;
285             event.consume();
286         }
287             }
288         }
289     }
290
291     private void printMenuElementArray(MenuElement JavaDoc path[]) {
292     printMenuElementArray(path, false);
293     }
294
295     private void printMenuElementArray(MenuElement JavaDoc path[], boolean dumpStack) {
296     System.out.println("Path is(");
297     int i, j;
298     for(i=0,j=path.length; i<j ;i++){
299         for (int k=0; k<=i; k++)
300         System.out.print(" ");
301         MenuElement JavaDoc me = (MenuElement JavaDoc) path[i];
302         if(me instanceof JMenuItem JavaDoc) {
303         System.out.println(((JMenuItem JavaDoc)me).getText() + ", ");
304         } else if (me instanceof JMenuBar JavaDoc) {
305         System.out.println("JMenuBar, ");
306         } else if(me instanceof JPopupMenu JavaDoc) {
307         System.out.println("JPopupMenu, ");
308         } else if (me == null) {
309         System.out.println("NULL , ");
310         } else {
311         System.out.println("" + me + ", ");
312         }
313     }
314     System.out.println(")");
315
316     if (dumpStack == true)
317         Thread.dumpStack();
318     }
319
320     /**
321      * Returns the component in the currently selected path
322      * which contains sourcePoint.
323      *
324      * @param source The component in whose coordinate space sourcePoint
325      * is given
326      * @param sourcePoint The point which is being tested
327      * @return The component in the currently selected path which
328      * contains sourcePoint (relative to the source component's
329      * coordinate space. If sourcePoint is not inside a component
330      * on the currently selected path, null is returned.
331      */

332     public Component componentForPoint(Component source, Point sourcePoint) {
333         int screenX,screenY;
334         Point p = sourcePoint;
335         int i,c,j,d;
336         Component mc;
337         Rectangle r2;
338         int cWidth,cHeight;
339         MenuElement JavaDoc menuElement;
340         MenuElement JavaDoc subElements[];
341         Vector tmp;
342         int selectionSize;
343
344         SwingUtilities.convertPointToScreen(p,source);
345
346         screenX = p.x;
347         screenY = p.y;
348
349         tmp = (Vector)selection.clone();
350         selectionSize = tmp.size();
351         for(i=selectionSize - 1 ; i >= 0 ; i--) {
352             menuElement = (MenuElement JavaDoc) tmp.elementAt(i);
353             subElements = menuElement.getSubElements();
354             
355             for(j = 0, d = subElements.length ; j < d ; j++) {
356         if (subElements[j] == null)
357             continue;
358                 mc = subElements[j].getComponent();
359                 if(!mc.isShowing())
360                     continue;
361                 if(mc instanceof JComponent JavaDoc) {
362                     cWidth = ((JComponent JavaDoc)mc).getWidth();
363                     cHeight = ((JComponent JavaDoc)mc).getHeight();
364                 } else {
365                     r2 = mc.getBounds();
366                     cWidth = r2.width;
367                     cHeight = r2.height;
368                 }
369                 p.x = screenX;
370                 p.y = screenY;
371                 SwingUtilities.convertPointFromScreen(p,mc);
372         
373                 /** Return the deepest component on the selection
374          * path in whose bounds the event's point occurs
375                  */

376                 if (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight) {
377                     return mc;
378                 }
379             }
380         }
381     return null;
382     }
383
384     /**
385      * When a MenuElement receives an event from a KeyListener, it should never process the event
386      * directly. Instead all MenuElements should call this method with the event.
387      *
388      * @param e a KeyEvent object
389      */

390     public void processKeyEvent(KeyEvent e) {
391         MenuElement JavaDoc[] sel2 = new MenuElement JavaDoc[0];
392         sel2 = (MenuElement JavaDoc[])selection.toArray(sel2);
393         int selSize = sel2.length;
394         MenuElement JavaDoc[] path;
395
396         if (selSize < 1) {
397             return;
398     }
399
400         for (int i=selSize-1; i>=0; i--) {
401             MenuElement JavaDoc elem = sel2[i];
402             MenuElement JavaDoc[] subs = elem.getSubElements();
403             path = null;
404
405             for (int j=0; j<subs.length; j++) {
406         if (subs[j] == null || !subs[j].getComponent().isShowing()
407                     || !subs[j].getComponent().isEnabled()) {
408             continue;
409                 }
410
411                 if(path == null) {
412                     path = new MenuElement JavaDoc[i+2];
413                     System.arraycopy(sel2, 0, path, 0, i+1);
414                     }
415                 path[i+1] = subs[j];
416                 subs[j].processKeyEvent(e, path, this);
417                 if (e.isConsumed()) {
418                     return;
419             }
420         }
421     }
422
423         // finally dispatch event to the first component in path
424
path = new MenuElement JavaDoc[1];
425         path[0] = sel2[0];
426         path[0].processKeyEvent(e, path, this);
427         if (e.isConsumed()) {
428             return;
429         }
430     }
431     
432     /**
433      * Return true if c is part of the currently used menu
434      */

435     public boolean isComponentPartOfCurrentMenu(Component c) {
436         if(selection.size() > 0) {
437             MenuElement JavaDoc me = (MenuElement JavaDoc)selection.elementAt(0);
438             return isComponentPartOfCurrentMenu(me,c);
439         } else
440             return false;
441     }
442
443     private boolean isComponentPartOfCurrentMenu(MenuElement JavaDoc root,Component c) {
444         MenuElement JavaDoc children[];
445         int i,d;
446     
447     if (root == null)
448         return false;
449
450         if(root.getComponent() == c)
451             return true;
452         else {
453             children = root.getSubElements();
454             for(i=0,d=children.length;i<d;i++) {
455                 if(isComponentPartOfCurrentMenu(children[i],c))
456                     return true;
457             }
458         }
459         return false;
460     }
461 }
462
463
464
Popular Tags