KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > base > TapPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * TapPanel.java
21  *
22  * Created on 22. srpen 2003, 22:20
23  */

24
25 package org.netbeans.modules.java.navigation.base;
26
27 import javax.swing.*;
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.lang.ref.*;
31
32 /**
33  * Panel that can collapse to a small size and be reexpanded.
34  *
35  * @author Tim Boudreau
36  */

37 public final class TapPanel extends javax.swing.JPanel JavaDoc {
38     public static final int UP = 0;
39     public static final int DOWN = 2;
40
41     public static final String JavaDoc PROP_ORIENTATION = "orientation"; //NOI18N
42
private int orientation = UP;
43     private boolean armed = false;
44     private boolean expanded = true;
45     private int minimumHeight = 8;
46
47     /**
48      * Creates a new instance of TapPanel
49      */

50     public TapPanel () {
51         setLayout ( new TrivialLayout () );
52     }
53
54     private static WeakReference<Adap> adapRef = null;
55
56     static class Adap extends MouseAdapter implements MouseMotionListener {
57         MouseListener other = null;
58
59         public void mouseEntered (MouseEvent e) {
60             ( (TapPanel) e.getSource () ).setArmed ( true );
61         }
62
63         public void mouseExited (MouseEvent e) {
64             ( (TapPanel) e.getSource () ).setArmed ( false );
65         }
66
67         public void mouseMoved (MouseEvent e) {
68             ( (TapPanel) e.getSource () ).setArmed ( ( (TapPanel) e.getSource () ).isArmPoint ( e.getPoint () ) );
69         }
70
71         public void mousePressed (MouseEvent e) {
72             if ( ( (TapPanel) e.getSource () ).isArmPoint ( e.getPoint () ) ) {
73                 ( (TapPanel) e.getSource () ).setExpanded ( !( (TapPanel) e.getSource () ).isExpanded () );
74                 e.consume ();
75             } else if ( other != null ) {
76                 other.mousePressed ( e );
77             }
78         }
79
80         public void mouseDragged (MouseEvent e) {
81             //do nothing
82
}
83     }
84
85     private static Adap getAdapter () {
86         Adap result = null;
87         if ( adapRef != null ) {
88             result = (Adap) adapRef.get ();
89         }
90         if ( result == null ) {
91             result = new Adap ();
92             adapRef = new WeakReference<Adap>( result );
93         }
94         return result;
95     }
96
97     /**
98      * Allows mouse clicks *not* in the expansion bar to cause the navigator component to become activated, but the user
99      * can click to expand/collapse without activating the component.
100      */

101     void setSecondaryMouseHandler (MouseListener lis) {
102         getAdapter ().other = lis;
103     }
104
105     public void addNotify () {
106         addMouseMotionListener ( getAdapter () );
107         addMouseListener ( getAdapter () );
108         super.addNotify ();
109     }
110
111     public void removeNotify () {
112         super.removeNotify ();
113         removeMouseMotionListener ( getAdapter () );
114         removeMouseListener ( getAdapter () );
115     }
116
117     public int getOrientation () {
118         return orientation;
119     }
120
121     public void setOrientation (int i) {
122         if ( i != orientation ) {
123             int oldOr = i;
124             orientation = i;
125             firePropertyChange ( PROP_ORIENTATION, oldOr, i );
126         }
127     }
128
129     private void setArmed (boolean val) {
130         if ( val != armed ) {
131             armed = val;
132             repaint ();
133         }
134     }
135
136     public boolean isExpanded () {
137         return expanded;
138     }
139
140     public Dimension getPreferredSize () {
141         return getLayout ().preferredLayoutSize ( this );
142     }
143
144     public Dimension getMinimumSize () {
145         Dimension d = getPreferredSize ();
146         d.width = 20;
147         return d;
148     }
149
150     public Dimension getMaximumSize () {
151         return getPreferredSize ();
152     }
153
154     public void setExpanded (boolean b) {
155         if ( expanded != b ) {
156             Dimension d = getPreferredSize ();
157             expanded = b;
158             Dimension d1 = getPreferredSize ();
159             if ( isDisplayable () ) {
160                 revalidate();
161             }
162         }
163     }
164
165     private boolean isArmPoint (Point p) {
166         if ( !expanded ) {
167             return p.y > 0 && p.y < getHeight ();
168         } else {
169             if ( orientation == UP ) {
170                 return p.y > getHeight () - minimumHeight;
171             } else {
172                 return p.y < minimumHeight;
173             }
174         }
175     }
176
177     public void updateBorder () {
178         if ( orientation == UP ) {
179             super.setBorder ( BorderFactory.createEmptyBorder ( 0, 0, minimumHeight, 0 ) );
180         } else {
181             super.setBorder ( BorderFactory.createEmptyBorder ( minimumHeight, 0, 0, 0 ) );
182         }
183     }
184
185     public int getMinimumHeight () {
186         return minimumHeight;
187     }
188
189     public void setBorder () {
190         //do nothing
191
}
192
193     public void paintBorder (Graphics g) {
194         Color c = armed ? UIManager.getColor ( "List.selectionBackground" ) : getBackground (); //NOI18N
195
int x = 0;
196         int y = orientation == UP ? 1 + ( getHeight () - minimumHeight ) : 0;
197         int w = getWidth ();
198         int h = minimumHeight - 1;
199         g.setColor ( c );
200         g.fillRect ( x, y, w, h );
201
202         int pos = orientation == UP ? getHeight () - 1 : 0;
203         int dir = orientation == UP ? -1 : 1;
204         g.setColor ( armed ? c.darker () : UIManager.getColor ( "controlShadow" ) ); //NOI18N
205
g.drawLine ( 0, pos, w, pos );
206         pos += dir;
207
208         if ( ( orientation == UP ) == expanded ) {
209             up.paintIcon ( this, g, ( getWidth () / 2 ) - ( up.getIconWidth () / 2 ),
210                     getHeight () - ( minimumHeight + ( expanded ? 0 : -1 ) ) );
211         } else {
212             down.paintIcon ( this, g, ( getWidth () / 2 ) - ( up.getIconWidth () / 2 ), expanded ? 2 : 1 );
213         }
214     }
215
216     public void paintChildren (Graphics g) {
217         if ( !expanded ) return;
218         super.paintChildren(g);
219     }
220
221     private Icon up = new UpIcon ();
222     private Icon down = new DownIcon ();
223
224     private int ICON_SIZE = 8;
225
226     private class UpIcon implements Icon {
227         public int getIconHeight () {
228             return ICON_SIZE - 2;
229         }
230
231         public int getIconWidth () {
232             return ICON_SIZE + 2;
233         }
234
235         public void paintIcon (java.awt.Component JavaDoc c, Graphics g, int x, int y) {
236
237             g.setColor ( armed ?
238                     UIManager.getColor ( "List.selectionForeground" ) : //NOI18N
239
UIManager.getColor ( "controlShadow" ) ); //NOI18N
240
/* int[] xPoints = new int[] {x+getIconWidth()/2, x+getIconWidth(), x};
241             int[] yPoints = new int[] {y, y+getIconHeight()-1, y+getIconHeight()-1};
242  */

243             int[] xPoints = new int[]{x, x + 8, x + 4};
244             int[] yPoints = new int[]{y + 5, y + 5, y};
245
246             g.fillPolygon ( xPoints, yPoints, 3 );
247         }
248     }
249
250     private class DownIcon implements Icon {
251
252         public int getIconHeight () {
253             return ICON_SIZE - 3;
254         }
255
256         public int getIconWidth () {
257             return ICON_SIZE + 2;
258         }
259
260         public void paintIcon (java.awt.Component JavaDoc c, Graphics g, int x, int y) {
261             x++;
262             g.setColor ( armed ?
263                     UIManager.getColor ( "List.selectionForeground" ) : //NOI18N
264
UIManager.getColor ( "controlShadow" ) ); //NOI18N
265
/*
266             int[] xPoints = new int[] {(x+getIconWidth()/2), x+getIconWidth()-1, x};
267             int[] yPoints = new int[] {y+getIconHeight()-2, y, y};
268              */

269             int[] xPoints = new int[]{x, x + 8, x + 4};
270             int[] yPoints = new int[]{y, y, y + 4};
271
272             g.fillPolygon ( xPoints, yPoints, 3 );
273         }
274
275     }
276
277 }
278
Popular Tags