KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ui > CategoryButton


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 package org.netbeans.modules.palette.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.dnd.Autoscroll JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25 import java.awt.event.FocusListener JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import javax.swing.*;
28 import javax.swing.border.CompoundBorder JavaDoc;
29 import java.awt.*;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import org.netbeans.modules.palette.Category;
33 import org.netbeans.modules.palette.Utils;
34 import org.openide.awt.Mnemonics;
35 import org.openide.nodes.Node;
36 import org.openide.util.Utilities;
37
38
39 /**
40  * @author David Kaspar, Jan Stola
41  */

42 class CategoryButton extends JCheckBox implements Autoscroll JavaDoc {
43
44     private static final Color AQUA_BK_COLOR = new Color(225, 235, 240);
45     
46     static final boolean isGTK = "GTK".equals( UIManager.getLookAndFeel().getID() );
47     private static final boolean isAqua = "Aqua".equals( UIManager.getLookAndFeel().getID() );
48
49     private CategoryDescriptor descriptor;
50     private Category category;
51     
52     private AutoscrollSupport support;
53     
54     // Workaround for JDK bug in GTK #6527149 - use Metal UI class
55
static {
56         if (isGTK) {
57             UIManager.put("MetalCheckBoxUI_4_GTK", "javax.swing.plaf.metal.MetalCheckBoxUI");
58         }
59     }
60
61     @Override JavaDoc
62     public String JavaDoc getUIClassID() {
63         String JavaDoc classID = super.getUIClassID();
64         if (isGTK) {
65             classID = "MetalCheckBoxUI_4_GTK";
66         }
67         return classID;
68     }
69
70     
71
72     CategoryButton( CategoryDescriptor descriptor, Category category ) {
73         this.descriptor = descriptor;
74         this.category = category;
75
76         //force initialization of PropSheet look'n'feel values
77
UIManager.get( "nb.propertysheet" );
78             
79         setFont( getFont().deriveFont( Font.BOLD ) );
80         setMargin(new Insets(0, 3, 0, 3));
81         setFocusPainted( false );
82
83         setSelected( false );
84
85         setHorizontalAlignment( SwingConstants.LEFT );
86         setHorizontalTextPosition( SwingConstants.RIGHT );
87         setVerticalTextPosition( SwingConstants.CENTER );
88
89         updateProperties();
90         
91         if( getBorder() instanceof CompoundBorder JavaDoc ) { // from BasicLookAndFeel
92
Dimension pref = getPreferredSize();
93             pref.height -= 3;
94             setPreferredSize( pref );
95         }
96
97         addActionListener( new ActionListener JavaDoc () {
98             public void actionPerformed( ActionEvent JavaDoc e ) {
99                 boolean opened = !CategoryButton.this.descriptor.isOpened();
100                 setExpanded( opened );
101             }
102         });
103         
104         addFocusListener( new FocusListener JavaDoc() {
105             public void focusGained(FocusEvent JavaDoc e) {
106                 scrollRectToVisible( getBounds() );
107             }
108             public void focusLost(FocusEvent JavaDoc e) {
109             }
110         });
111         
112         initActions();
113     }
114     
115     private void initActions() {
116         InputMap inputMap = getInputMap( WHEN_FOCUSED );
117         inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, 0, false ), "moveFocusDown" ); //NOI18N
118
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_UP, 0, false ), "moveFocusUp" ); //NOI18N
119
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, 0, false ), "collapse" ); //NOI18N
120
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, 0, false ), "expand" ); //NOI18N
121
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_F10, KeyEvent.SHIFT_DOWN_MASK, false ), "popup" ); //NOI18N
122
inputMap.put( KeyStroke.getKeyStroke( "ctrl V" ), "paste" ); //NOI18N //NOI18N
123
inputMap.put( KeyStroke.getKeyStroke( "PASTE" ), "paste" ); //NOI18N //NOI18N
124

125         ActionMap actionMap = getActionMap();
126         actionMap.put( "moveFocusDown", new MoveFocusAction( true ) ); //NOI18N
127
actionMap.put( "moveFocusUp", new MoveFocusAction( false ) ); //NOI18N
128
actionMap.put( "collapse", new ExpandAction( false ) ); //NOI18N
129
actionMap.put( "expand", new ExpandAction( true ) ); //NOI18N
130
actionMap.put( "popup", new PopupAction() ); //NOI18N
131
Node categoryNode = (Node)category.getLookup().lookup( Node.class );
132         if( null != categoryNode )
133             actionMap.put( "paste", new Utils.PasteItemAction( categoryNode ) ); //NOI18N
134
}
135     
136     void updateProperties() {
137         setIcon( (Icon)UIManager.get("Tree.collapsedIcon") );
138         setSelectedIcon( (Icon)UIManager.get("Tree.expandedIcon") );
139         Mnemonics.setLocalizedText( this, category.getDisplayName() );
140         setToolTipText( category.getShortDescription() );
141         getAccessibleContext().setAccessibleName( category.getDisplayName() );
142         getAccessibleContext().setAccessibleDescription( category.getShortDescription() );
143     }
144     
145     Category getCategory() {
146         return category;
147     }
148
149     
150     /** notify the Component to autoscroll */
151     public void autoscroll( Point cursorLoc ) {
152         Point p = SwingUtilities.convertPoint( this, cursorLoc, getParent().getParent() );
153         getSupport().autoscroll( p );
154     }
155
156     /** @return the Insets describing the autoscrolling
157      * region or border relative to the geometry of the
158      * implementing Component.
159      */

160     public Insets getAutoscrollInsets() {
161         return getSupport().getAutoscrollInsets();
162     }
163     
164     boolean isExpanded() {
165         return isSelected();
166     }
167     
168     void setExpanded( boolean expand ) {
169         setSelected( expand );
170         descriptor.setOpened( expand );
171         descriptor.getPalettePanel().computeHeights( expand ? CategoryButton.this.category : null );
172         requestFocus ();
173     }
174
175     /** Safe getter for autoscroll support. */
176     AutoscrollSupport getSupport() {
177         if( null == support ) {
178             support = new AutoscrollSupport( getParent().getParent() );
179         }
180
181         return support;
182     }
183
184     public Color getBackground() {
185         if( isFocusOwner() ) {
186             if( isAqua )
187                 return UIManager.getColor("Table.selectionBackground"); //NOI18N
188
return UIManager.getColor( "PropSheet.selectedSetBackground" ); //NOI18N
189
} else {
190             if( isAqua ) {
191                 return AQUA_BK_COLOR;
192             } else {
193                 return UIManager.getColor( "PropSheet.setBackground" ); //NOI18N
194
}
195         }
196     }
197
198     public Color getForeground() {
199         if( isFocusOwner() ) {
200             if( isAqua )
201                 return UIManager.getColor( "Table.selectionForeground" ); //NOI18N
202
return UIManager.getColor( "PropSheet.selectedSetForeground" ); //NOI18N
203
} else {
204             return super.getForeground();
205         }
206     }
207     
208     private class MoveFocusAction extends AbstractAction {
209         private boolean moveDown;
210         
211         public MoveFocusAction( boolean moveDown ) {
212             this.moveDown = moveDown;
213         }
214         
215         public void actionPerformed(ActionEvent JavaDoc e) {
216             KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
217             Container container = kfm.getCurrentFocusCycleRoot();
218             FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
219             if( null == policy )
220                 policy = kfm.getDefaultFocusTraversalPolicy();
221             Component JavaDoc next = moveDown ? policy.getComponentAfter( container, CategoryButton.this )
222                                       : policy.getComponentBefore( container, CategoryButton.this );
223             if( null != next && next instanceof CategoryList ) {
224                 if( ((CategoryList)next).getModel().getSize() != 0 ) {
225                     ((CategoryList)next).takeFocusFrom( CategoryButton.this );
226                     return;
227                 } else {
228                     next = moveDown ? policy.getComponentAfter( container, next )
229                                     : policy.getComponentBefore( container, next );
230                 }
231             }
232             if( null != next && next instanceof CategoryButton ) {
233                 next.requestFocus();
234             }
235         }
236     }
237     
238     private class ExpandAction extends AbstractAction {
239         private boolean expand;
240         
241         public ExpandAction( boolean expand ) {
242             this.expand = expand;
243         }
244         
245         public void actionPerformed(ActionEvent JavaDoc e) {
246             if( expand == isExpanded() )
247                 return;
248             setExpanded( expand );
249         }
250     }
251
252     private class PopupAction extends AbstractAction {
253
254         public void actionPerformed(ActionEvent JavaDoc e) {
255             Action[] actions = category.getActions();
256             JPopupMenu popup = Utilities.actionsToPopup( actions, CategoryButton.this );
257             Utils.addCustomizationMenuItems( popup, descriptor.getPalettePanel().getController(), descriptor.getPalettePanel().getSettings() );
258             popup.show( getParent(), 0, getHeight() );
259         }
260     }
261 }
262
Popular Tags