Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / plaf / basic / BasicMenuBarUI.java
blobf258ebe3069fc6dc1d1cb9a4ed1ab3c337ee578a
1 /* BasicMenuBarUI.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.basic;
41 import gnu.classpath.NotImplementedException;
43 import java.awt.Dimension;
44 import java.awt.event.ContainerEvent;
45 import java.awt.event.ContainerListener;
46 import java.awt.event.MouseEvent;
47 import java.beans.PropertyChangeEvent;
48 import java.beans.PropertyChangeListener;
50 import javax.swing.BoxLayout;
51 import javax.swing.JComponent;
52 import javax.swing.JMenu;
53 import javax.swing.JMenuBar;
54 import javax.swing.LookAndFeel;
55 import javax.swing.MenuElement;
56 import javax.swing.event.ChangeEvent;
57 import javax.swing.event.ChangeListener;
58 import javax.swing.event.MouseInputListener;
59 import javax.swing.plaf.ComponentUI;
60 import javax.swing.plaf.MenuBarUI;
62 /**
63 * UI Delegate for JMenuBar.
65 public class BasicMenuBarUI extends MenuBarUI
67 protected ChangeListener changeListener;
69 /*ContainerListener that listens to the ContainerEvents fired from menu bar*/
70 protected ContainerListener containerListener;
72 /*Property change listeners that listener to PropertyChangeEvent from menu bar*/
73 private PropertyChangeListener propertyChangeListener;
75 /* menu bar for which this UI delegate is for*/
76 protected JMenuBar menuBar;
78 /* MouseListener that listens to the mouseEvents fired from menu bar*/
79 private MouseInputListener mouseListener;
81 /**
82 * Creates a new BasicMenuBarUI object.
84 public BasicMenuBarUI()
86 changeListener = createChangeListener();
87 containerListener = createContainerListener();
88 propertyChangeListener = new PropertyChangeHandler();
89 mouseListener = new MouseInputHandler();
92 /**
93 * Creates ChangeListener
95 * @return The ChangeListener
97 protected ChangeListener createChangeListener()
99 return new ChangeHandler();
103 * Creates ContainerListener() to listen for ContainerEvents
104 * fired by JMenuBar.
106 * @return The ContainerListener
108 protected ContainerListener createContainerListener()
110 return new ContainerHandler();
114 * Factory method to create a BasicMenuBarUI for the given {@link
115 * JComponent}, which should be a {@link JMenuBar}.
117 * @param x The {@link JComponent} a UI is being created for.
119 * @return A BasicMenuBarUI for the {@link JComponent}.
121 public static ComponentUI createUI(JComponent x)
123 return new BasicMenuBarUI();
127 * Returns maximum size for the specified menu bar
129 * @param c component for which to get maximum size
131 * @return Maximum size for the specified menu bar
133 public Dimension getMaximumSize(JComponent c)
135 // let layout manager calculate its size
136 return null;
140 * Returns maximum allowed size of JMenuBar.
142 * @param c menuBar for which to return maximum size
144 * @return Maximum size of the give menu bar.
146 public Dimension getMinimumSize(JComponent c)
148 // let layout manager calculate its size
149 return null;
153 * Returns preferred size of JMenuBar.
155 * @param c menuBar for which to return preferred size
157 * @return Preferred size of the give menu bar.
159 public Dimension getPreferredSize(JComponent c)
161 // let layout manager calculate its size
162 return null;
166 * Initializes any default properties that this UI has from the defaults for
167 * the Basic look and feel.
169 protected void installDefaults()
171 LookAndFeel.installBorder(menuBar, "MenuBar.border");
172 LookAndFeel.installColorsAndFont(menuBar, "MenuBar.background",
173 "MenuBar.foreground", "MenuBar.font");
174 menuBar.setOpaque(true);
178 * This method installs the keyboard actions for the JMenuBar.
180 protected void installKeyboardActions()
181 throws NotImplementedException
183 // FIXME: implement
187 * This method installs the listeners needed for this UI to function.
189 protected void installListeners()
191 menuBar.addContainerListener(containerListener);
192 menuBar.addPropertyChangeListener(propertyChangeListener);
193 menuBar.addMouseListener(mouseListener);
197 * Installs and initializes all fields for this UI delegate. Any properties
198 * of the UI that need to be initialized and/or set to defaults will be
199 * done now. It will also install any listeners necessary.
201 * @param c The {@link JComponent} that is having this UI installed.
203 public void installUI(JComponent c)
205 super.installUI(c);
206 menuBar = (JMenuBar) c;
207 menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.X_AXIS));
208 installDefaults();
209 installListeners();
210 installKeyboardActions();
214 * This method uninstalls the defaults and nulls any objects created during
215 * install.
217 protected void uninstallDefaults()
219 menuBar.setBackground(null);
220 menuBar.setBorder(null);
221 menuBar.setFont(null);
222 menuBar.setForeground(null);
226 * This method reverses the work done in installKeyboardActions.
228 protected void uninstallKeyboardActions()
229 throws NotImplementedException
231 // FIXME: implement.
235 * Unregisters all the listeners that this UI delegate was using.
237 protected void uninstallListeners()
239 menuBar.removeContainerListener(containerListener);
240 menuBar.removePropertyChangeListener(propertyChangeListener);
241 menuBar.removeMouseListener(mouseListener);
245 * Performs the opposite of installUI. Any properties or resources that need
246 * to be cleaned up will be done now. It will also uninstall any listeners
247 * it has. In addition, any properties of this UI will be nulled.
249 * @param c The {@link JComponent} that is having this UI uninstalled.
251 public void uninstallUI(JComponent c)
253 uninstallDefaults();
254 uninstallListeners();
255 uninstallKeyboardActions();
256 menuBar = null;
259 private class ChangeHandler implements ChangeListener
261 public void stateChanged(ChangeEvent event)
263 // TODO: What should be done here, if anything?
268 * This class handles ContainerEvents fired by JMenuBar. It revalidates
269 * and repaints menu bar whenever menu is added or removed from it.
271 private class ContainerHandler implements ContainerListener
274 * This method is called whenever menu is added to the menu bar
276 * @param e The ContainerEvent.
278 public void componentAdded(ContainerEvent e)
280 menuBar.revalidate();
281 menuBar.repaint();
285 * This method is called whenever menu is removed from the menu bar.
287 * @param e The ContainerEvent.
289 public void componentRemoved(ContainerEvent e)
291 menuBar.revalidate();
292 menuBar.repaint();
297 * This class handles PropertyChangeEvents fired from the JMenuBar
299 private class PropertyChangeHandler implements PropertyChangeListener
302 * This method is called whenever one of the properties of the MenuBar
303 * changes.
305 * @param e The PropertyChangeEvent.
307 public void propertyChange(PropertyChangeEvent e)
309 if (e.getPropertyName().equals("borderPainted"))
310 menuBar.repaint();
311 if (e.getPropertyName().equals("margin"))
312 menuBar.repaint();
316 private class MouseInputHandler implements MouseInputListener
319 * Handles mouse clicked event
321 * @param e Mouse event
323 public void mouseClicked(MouseEvent e)
325 MenuElement[] me = menuBar.getSubElements();
327 for (int i = 0; i < me.length; i++)
329 JMenu menu = menuBar.getMenu(i);
330 if (menu != null)
331 menu.setSelected(false);
336 * Handles mouse pressed event
338 * @param e Mouse event
340 public void mousePressed(MouseEvent e)
342 // TODO: What should be done here, if anything?
346 * Handles mouse released event
348 * @param e Mouse event
350 public void mouseReleased(MouseEvent e)
352 // TODO: What should be done here, if anything?
356 * Handles mouse exited event
358 * @param e Mouse event
360 public void mouseExited(MouseEvent e)
362 // TODO: What should be done here, if anything?
366 * Handles mouse dragged event
368 * @param e Mouse event
370 public void mouseDragged(MouseEvent e)
372 // TODO: What should be done here, if anything?
376 * Handles mouse moved event
378 * @param e Mouse event
380 public void mouseMoved(MouseEvent e)
382 // TODO: What should be done here, if anything?
386 * Handles mouse entered event
388 * @param e Mouse event
390 public void mouseEntered(MouseEvent e)
392 // TODO: What should be done here, if anything?