KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.navigation.base;
26
27 import javax.swing.*;
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.lang.ref.*;
31
32 /**
33  * This file is originally from Retouche, the Java Support
34  * infrastructure in NetBeans. I have modified the file as little
35  * as possible to make merging Retouche fixes back as simple as
36  * possible.
37  * <p>
38  * Panel that can collapse to a small size and be reexpanded.
39  *
40  * @author Tim Boudreau
41  */

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

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

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

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

274             int[] xPoints = new int[]{x, x + 8, x + 4};
275             int[] yPoints = new int[]{y, y, y + 4};
276
277             g.fillPolygon ( xPoints, yPoints, 3 );
278         }
279
280     }
281
282 }
283
Popular Tags