KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > ui > swing > AJButtonMenuCombo


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

25
26 package org.aspectj.ajde.ui.swing;
27
28 import java.awt.*;
29 import java.awt.event.*;
30 import javax.swing.*;
31 import javax.swing.event.*;
32 import java.util.*;
33 import org.aspectj.ajde.*;
34 import org.aspectj.asm.*;
35 import org.aspectj.asm.views.*;
36 import org.aspectj.ajde.ui.*;
37 import javax.swing.border.*;
38
39 public class AJButtonMenuCombo extends JPanel {
40     
41     private JButton mainButton;
42     private JButton popupButton;
43     private JPopupMenu menu;
44     private boolean depressable = false;
45     private boolean isPressed = false;
46     
47     public AJButtonMenuCombo(String JavaDoc name,
48         String JavaDoc toolTipText,
49         Icon icon,
50         JPopupMenu menu,
51         boolean depressable) {
52             
53         this.menu = menu;
54         this.depressable = depressable;
55         mainButton = new JButton();
56         mainButton.setIcon(icon);
57         mainButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
58         mainButton.setToolTipText(toolTipText);
59         mainButton.setPreferredSize(new Dimension(22, 20));
60         mainButton.setMinimumSize(new Dimension(22, 20));
61         mainButton.setMaximumSize(new Dimension(22, 20));
62         
63         popupButton = new JButton();
64         popupButton.setIcon(AjdeUIManager.getDefault().getIconRegistry().getPopupIcon());
65         popupButton.setBorder(BorderFactory.createEmptyBorder());
66         popupButton.setToolTipText(toolTipText);
67         popupButton.setPreferredSize(new Dimension(13, 20));
68         popupButton.setMinimumSize(new Dimension(13, 20));
69         popupButton.setMaximumSize(new Dimension(13, 20));
70     
71         PopupListener popupListener = new PopupListener(mainButton);
72         
73         if (depressable) {
74             mainButton.addActionListener(new ButtonActionListener());
75         } else {
76             mainButton.addMouseListener(popupListener);
77         }
78     
79         popupButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
80             public void actionPerformed(ActionEvent e) {
81                 popupButton.setBorder(null);
82             }
83         });
84
85         BorderUpdateListener borderUpdateListner = new BorderUpdateListener();
86         mainButton.addMouseListener(borderUpdateListner);
87         popupButton.addMouseListener(borderUpdateListner);
88         
89         popupButton.addMouseListener(popupListener);
90         
91         this.setLayout(new BorderLayout());
92         this.add(mainButton, BorderLayout.CENTER);
93         this.add(popupButton, BorderLayout.EAST);
94         
95         this.setMinimumSize(new Dimension(35, 20));
96         this.setMaximumSize(new Dimension(35, 20));
97     }
98
99     class ButtonActionListener implements ActionListener {
100         public void actionPerformed(ActionEvent e) {
101             if (isPressed) {
102                 mainButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
103                 isPressed = false;
104             } else {
105                 mainButton.setBorder(AjdeWidgetStyles.LOWERED_BEVEL_BORDER);
106                 isPressed = true;
107             }
108         }
109     }
110     
111
112     class BorderUpdateListener extends MouseAdapter {
113         public void mouseEntered(MouseEvent e) {
114             popupButton.setBorder(AjdeWidgetStyles.RAISED_BEVEL_BORDER);
115             mainButton.setBorder(AjdeWidgetStyles.RAISED_BEVEL_BORDER);
116         }
117         
118         public void mouseExited(MouseEvent e) {
119             popupButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
120             if (isPressed) {
121                 mainButton.setBorder(AjdeWidgetStyles.LOWERED_BEVEL_BORDER);
122             } else {
123                 mainButton.setBorder(AjdeWidgetStyles.DEFAULT_BORDER);
124             }
125         }
126     }
127
128     class PopupListener extends MouseAdapter {
129         private JButton button;
130
131         public PopupListener(JButton button) {
132             this.button = button;
133         }
134
135         public void mousePressed(MouseEvent e) {
136             maybeShowPopup(e);
137         }
138
139         public void mouseReleased(MouseEvent e) {
140             maybeShowPopup(e);
141         }
142
143         private void maybeShowPopup(MouseEvent e) {
144             menu.show(e.getComponent(), button.getX(), button.getY() + popupButton.getHeight());
145         }
146     }
147     
148     public void setEnabled(boolean enabled) {
149         mainButton.setEnabled(enabled);
150         popupButton.setEnabled(enabled);
151     }
152
153     public void setMenu(JPopupMenu menu) {
154         this.menu = menu;
155         this.repaint();
156     }
157
158 }
Popular Tags