001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tasklist.ui;
043:
044: import java.awt.Component;
045: import java.awt.Graphics;
046: import java.awt.Point;
047: import java.awt.event.MouseAdapter;
048: import java.awt.event.MouseEvent;
049: import java.awt.event.MouseMotionAdapter;
050: import javax.swing.Icon;
051: import javax.swing.JPopupMenu;
052: import javax.swing.JToggleButton;
053: import javax.swing.UIManager;
054:
055: /**
056: *
057: * @author S. Aubrecht
058: */
059: class MenuToggleButton extends JToggleButton {
060:
061: private boolean mouseInArrowArea = false;
062:
063: /** Creates a new instance of MenuToggleButton */
064: public MenuToggleButton(final Icon regIcon, Icon rollOverIcon,
065: int arrowWidth) {
066: assert null != regIcon;
067: assert null != rollOverIcon;
068: final Icon lineIcon = new LineIcon(rollOverIcon, arrowWidth);
069: setIcon(regIcon);
070: setRolloverIcon(lineIcon);
071: setRolloverSelectedIcon(lineIcon);
072: setFocusable(false);
073:
074: addMouseMotionListener(new MouseMotionAdapter() {
075: public void mouseMoved(MouseEvent e) {
076: mouseInArrowArea = isInArrowArea(e.getPoint());
077: setRolloverIcon(mouseInArrowArea ? regIcon : lineIcon);
078: setRolloverSelectedIcon(mouseInArrowArea ? regIcon
079: : lineIcon);
080: }
081: });
082:
083: addMouseListener(new MouseAdapter() {
084: public void mousePressed(MouseEvent e) {
085: if (isInArrowArea(e.getPoint())) {
086: JPopupMenu popup = getPopupMenu();
087: if (null != popup)
088: popup.show(MenuToggleButton.this , 0,
089: getHeight());
090: }
091: }
092:
093: public void mouseEntered(MouseEvent e) {
094: mouseInArrowArea = isInArrowArea(e.getPoint());
095: setRolloverIcon(mouseInArrowArea ? regIcon : lineIcon);
096: setRolloverSelectedIcon(mouseInArrowArea ? regIcon
097: : lineIcon);
098: }
099:
100: public void mouseExited(MouseEvent e) {
101: mouseInArrowArea = false;
102: setRolloverIcon(regIcon);
103: setRolloverSelectedIcon(regIcon);
104: }
105: });
106:
107: setModel(new Model());
108: }
109:
110: protected JPopupMenu getPopupMenu() {
111: return null;
112: }
113:
114: private boolean isInArrowArea(Point p) {
115: return p.getLocation().x >= getWidth() - 3 - 2
116: - getInsets().right;
117: }
118:
119: private static class LineIcon implements Icon {
120: private Icon origIcon;
121: private int arrowWidth;
122:
123: public LineIcon(Icon origIcon, int arrowWidth) {
124: this .origIcon = origIcon;
125: this .arrowWidth = arrowWidth;
126: }
127:
128: public void paintIcon(Component c, Graphics g, int x, int y) {
129: origIcon.paintIcon(c, g, x, y);
130:
131: g.setColor(UIManager.getColor("controlHighlight")); //NOI18N
132: g.drawLine(x + origIcon.getIconWidth() - arrowWidth - 2, y,
133: x + origIcon.getIconWidth() - arrowWidth - 2, y
134: + getIconHeight());
135: g.setColor(UIManager.getColor("controlShadow")); //NOI18N
136: g.drawLine(x + origIcon.getIconWidth() - arrowWidth - 3, y,
137: x + origIcon.getIconWidth() - arrowWidth - 3, y
138: + getIconHeight());
139: }
140:
141: public int getIconWidth() {
142: return origIcon.getIconWidth();
143: }
144:
145: public int getIconHeight() {
146: return origIcon.getIconHeight();
147: }
148: }
149:
150: private class Model extends JToggleButton.ToggleButtonModel {
151: public void setPressed(boolean b) {
152: if (mouseInArrowArea)
153: return;
154: super.setPressed(b);
155: }
156: }
157: }
|