blob: e831346d95f1fef2580fa6154ee83def6b938ed1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package com.sun.java.swing.plaf.motif;
26
27import java.awt.*;
28import java.awt.event.*;
29import javax.swing.*;
30import javax.swing.event.*;
31import javax.swing.plaf.*;
32import javax.swing.border.*;
33import javax.swing.plaf.basic.*;
34
35
36import javax.swing.plaf.basic.BasicMenuUI;
37
38/**
39 * A Motif L&F implementation of MenuUI.
40 * <p>
41 *
42 * @author Georges Saab
43 * @author Rich Schiavi
44 */
45public class MotifMenuUI extends BasicMenuUI
46{
47
48 public static ComponentUI createUI( JComponent x ) {
49 return new MotifMenuUI();
50 }
51
52// These should not be necessary because BasicMenuUI does this,
53// and this class overrides createChangeListener.
54// protected void installListeners() {
55// super.installListeners();
56// changeListener = createChangeListener(menuItem);
57// menuItem.addChangeListener(changeListener);
58// }
59//
60// protected void uninstallListeners() {
61// super.uninstallListeners();
62// menuItem.removeChangeListener(changeListener);
63// }
64
65 protected ChangeListener createChangeListener(JComponent c) {
66 return new MotifChangeHandler((JMenu)c, this);
67 }
68
69 private boolean popupIsOpen(JMenu m,MenuElement me[]) {
70 int i;
71 JPopupMenu pm = m.getPopupMenu();
72
73 for(i=me.length-1;i>=0;i--) {
74 if(me[i].getComponent() == pm)
75 return true;
76 }
77 return false;
78 }
79
80 protected MouseInputListener createMouseInputListener(JComponent c) {
81 return new MouseInputHandler();
82 }
83
84 public class MotifChangeHandler extends ChangeHandler {
85 public MotifChangeHandler(JMenu m, MotifMenuUI ui) {
86 super(m, ui);
87 }
88
89
90 public void stateChanged(ChangeEvent e) {
91 JMenuItem c = (JMenuItem)e.getSource();
92 if (c.isArmed() || c.isSelected()) {
93 c.setBorderPainted(true);
94 // c.repaint();
95 } else {
96 c.setBorderPainted(false);
97 }
98
99 super.stateChanged(e);
100 }
101 }
102
103 protected class MouseInputHandler implements MouseInputListener {
104 public void mouseClicked(MouseEvent e) {}
105 public void mousePressed(MouseEvent e) {
106 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
107 JMenu menu = (JMenu)e.getComponent();
108 if(menu.isEnabled()) {
109 if(menu.isTopLevelMenu()) {
110 if(menu.isSelected()) {
111 manager.clearSelectedPath();
112 } else {
113 Container cnt = menu.getParent();
114 if(cnt != null && cnt instanceof JMenuBar) {
115 MenuElement me[] = new MenuElement[2];
116 me[0]=(MenuElement)cnt;
117 me[1]=menu;
118 manager.setSelectedPath(me);
119 }
120 }
121 }
122
123 MenuElement path[] = getPath();
124 if (path.length > 0) {
125 MenuElement newPath[] = new MenuElement[path.length+1];
126 System.arraycopy(path,0,newPath,0,path.length);
127 newPath[path.length] = menu.getPopupMenu();
128 manager.setSelectedPath(newPath);
129 }
130 }
131 }
132
133 public void mouseReleased(MouseEvent e) {
134 MenuSelectionManager manager =
135 MenuSelectionManager.defaultManager();
136 JMenuItem menuItem = (JMenuItem)e.getComponent();
137 Point p = e.getPoint();
138 if(!(p.x >= 0 && p.x < menuItem.getWidth() &&
139 p.y >= 0 && p.y < menuItem.getHeight())) {
140 manager.processMouseEvent(e);
141 }
142 }
143 public void mouseEntered(MouseEvent e) {}
144 public void mouseExited(MouseEvent e) {}
145 public void mouseDragged(MouseEvent e) {
146 MenuSelectionManager.defaultManager().processMouseEvent(e);
147 }
148 public void mouseMoved(MouseEvent e) { }
149 }
150
151}