1/*
2 * Copyright (c) 1997, 2004, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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 {@literal L&F} implementation of MenuUI.
40 *
41 * @author Georges Saab
42 * @author Rich Schiavi
43 */
44public class MotifMenuUI extends BasicMenuUI
45{
46
47    public static ComponentUI createUI( JComponent x ) {
48        return new MotifMenuUI();
49    }
50
51// These should not be necessary because BasicMenuUI does this,
52// and this class overrides createChangeListener.
53//    protected void installListeners() {
54//      super.installListeners();
55//        changeListener = createChangeListener(menuItem);
56//        menuItem.addChangeListener(changeListener);
57//    }
58//
59//    protected void uninstallListeners() {
60//      super.uninstallListeners();
61//      menuItem.removeChangeListener(changeListener);
62//    }
63
64    protected ChangeListener createChangeListener(JComponent c) {
65        return new MotifChangeHandler((JMenu)c, this);
66    }
67
68    private boolean popupIsOpen(JMenu m,MenuElement me[]) {
69        int i;
70        JPopupMenu pm = m.getPopupMenu();
71
72        for(i=me.length-1;i>=0;i--) {
73            if(me[i].getComponent() == pm)
74                return true;
75        }
76        return false;
77    }
78
79    protected MouseInputListener createMouseInputListener(JComponent c) {
80        return new MouseInputHandler();
81    }
82
83    public class MotifChangeHandler extends ChangeHandler {
84        public MotifChangeHandler(JMenu m, MotifMenuUI ui) {
85            super(m, ui);
86        }
87
88
89        public void stateChanged(ChangeEvent e) {
90            JMenuItem c = (JMenuItem)e.getSource();
91            if (c.isArmed() || c.isSelected()) {
92                c.setBorderPainted(true);
93                // c.repaint();
94            } else {
95                c.setBorderPainted(false);
96            }
97
98            super.stateChanged(e);
99        }
100    }
101
102    protected class MouseInputHandler implements MouseInputListener {
103        public void mouseClicked(MouseEvent e) {}
104        public void mousePressed(MouseEvent e) {
105            MenuSelectionManager manager = MenuSelectionManager.defaultManager();
106            JMenu menu = (JMenu)e.getComponent();
107            if(menu.isEnabled()) {
108                if(menu.isTopLevelMenu()) {
109                    if(menu.isSelected()) {
110                        manager.clearSelectedPath();
111                    } else {
112                        Container cnt = menu.getParent();
113                        if(cnt != null && cnt instanceof JMenuBar) {
114                            MenuElement me[] = new MenuElement[2];
115                            me[0]=(MenuElement)cnt;
116                            me[1]=menu;
117                            manager.setSelectedPath(me);
118                        }
119                    }
120                }
121
122                MenuElement path[] = getPath();
123                if (path.length > 0) {
124                    MenuElement newPath[] = new MenuElement[path.length+1];
125                    System.arraycopy(path,0,newPath,0,path.length);
126                    newPath[path.length] = menu.getPopupMenu();
127                    manager.setSelectedPath(newPath);
128                }
129            }
130        }
131
132        public void mouseReleased(MouseEvent e) {
133            MenuSelectionManager manager =
134                MenuSelectionManager.defaultManager();
135            JMenuItem menuItem = (JMenuItem)e.getComponent();
136            Point p = e.getPoint();
137            if(!(p.x >= 0 && p.x < menuItem.getWidth() &&
138                 p.y >= 0 && p.y < menuItem.getHeight())) {
139                manager.processMouseEvent(e);
140            }
141        }
142        public void mouseEntered(MouseEvent e) {}
143        public void mouseExited(MouseEvent e) {}
144        public void mouseDragged(MouseEvent e) {
145            MenuSelectionManager.defaultManager().processMouseEvent(e);
146        }
147        public void mouseMoved(MouseEvent e) { }
148    }
149
150}
151