KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > LargeMenu


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.client;
21
22 import java.awt.Container JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JMenu JavaDoc;
29 import javax.swing.JPopupMenu JavaDoc;
30
31 /**
32  * The default JMenu class does not work correctly when the popup menu contains
33  * large numbers of elements.
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
37  * @since 4.1
38  */

39 public class LargeMenu
40     extends JMenu JavaDoc
41 {
42     /*---------------------------------------------------------------
43      * Constructors
44      *-------------------------------------------------------------*/

45     /**
46      * Constructs a new <code>JMenu</code> with no text.
47      */

48     public LargeMenu()
49     {
50         super();
51     }
52     
53     /**
54      * Constructs a new <code>JMenu</code> with the supplied string
55      * as its text.
56      *
57      * @param s the text for the menu label
58      */

59     public LargeMenu( String JavaDoc s )
60     {
61         super( s );
62     }
63     
64     /**
65      * Constructs a menu whose properties are taken from the
66      * <code>Action</code> supplied.
67      * @param a an <code>Action</code>
68      */

69     public LargeMenu( Action JavaDoc a )
70     {
71         super( a );
72     }
73     
74     /**
75      * Constructs a new <code>JMenu</code> with the supplied string as
76      * its text and specified as a tear-off menu or not.
77      *
78      * @param s the text for the menu label
79      * @param b can the menu be torn off (not yet implemented)
80      */

81     public LargeMenu( String JavaDoc s, boolean b )
82     {
83         super( s, b );
84     }
85
86     /*---------------------------------------------------------------
87      * JMenu Methods
88      *-------------------------------------------------------------*/

89     /**
90      * Computes the origin for the <code>JMenu</code>'s popup menu.
91      * <p>
92      * Code is copied from JDK1.3 source, but has been patched.
93      *
94      * @return a <code>Point</code> in the coordinate space of the
95      * menu which should be used as the origin
96      * of the <code>JMenu</code>'s popup menu
97      */

98     protected Point JavaDoc getPopupMenuOrigin()
99     {
100         int x = 0;
101         int y = 0;
102         JPopupMenu JavaDoc pm = getPopupMenu();
103         // Figure out the sizes needed to caclulate the menu position
104
Dimension JavaDoc screenSize =Toolkit.getDefaultToolkit().getScreenSize();
105         Dimension JavaDoc s = getSize();
106         Dimension JavaDoc pmSize = pm.getSize();
107         // For the first time the menu is popped up,
108
// the size has not yet been initiated
109
if (pmSize.width==0)
110         {
111             pmSize = pm.getPreferredSize();
112         }
113         Point JavaDoc position = getLocationOnScreen();
114         
115         Container JavaDoc parent = getParent();
116         if (parent instanceof JPopupMenu JavaDoc)
117         {
118             // We are a submenu (pull-right)
119

120             // Can not call SwingUtilities.isLeftToRight(this) from here, so assume true.
121
// First determine x:
122
if (position.x+s.width + pmSize.width < screenSize.width)
123             {
124                 x = s.width; // Prefer placement to the right
125
}
126             else
127             {
128                 x = 0-pmSize.width; // Otherwise place to the left
129
}
130             
131             // Then the y:
132
if (position.y+pmSize.height < screenSize.height)
133             {
134                 y = 0; // Prefer dropping down
135
}
136             else
137             {
138                 // ****************
139
// This code was patched.
140
// ****************
141
// Old Code:
142
// y = s.height-pmSize.height; // Otherwise drop 'up'
143

144                 // New Code:
145
if ( position.y + s.height - pmSize.height >= 0 )
146                 {
147                     // Fits in the screen when dripped up.
148
y = s.height - pmSize.height;
149                 }
150                 else
151                 {
152                     // Does not fit, so show it starting at the top of the screen.
153
// This is an offset.
154
y = 0 - position.y;
155                 }
156             }
157         } else {
158             // We are a toplevel menu (pull-down)
159

160             // Can not call SwingUtilities.isLeftToRight(this) from here, so assume true.
161
// First determine the x:
162
if (position.x+pmSize.width < screenSize.width) {
163                 x = 0; // Prefer extending to right
164
} else {
165                 x = s.width-pmSize.width; // Otherwise extend to left
166
}
167             
168             // Then the y:
169
if (position.y+s.height+pmSize.height < screenSize.height) {
170                 y = s.height; // Prefer dropping down
171
} else {
172                 // ****************
173
// This code was patched.
174
// ****************
175
// Old Code:
176
//y = 0-pmSize.height; // Otherwise drop 'up'
177

178                 // New Code:
179
if ( position.y - pmSize.height >= 0 )
180                 {
181                     // Fits in the screen when dripped up.
182
y = 0 - pmSize.height;
183                 }
184                 else
185                 {
186                     // Does not fit, so show it starting at the top of the screen.
187
// This is an offset.
188
y = 0 - position.y;
189                 }
190             }
191         }
192         return new Point JavaDoc(x,y);
193     }
194 }
195
Popular Tags