1 19 20 package org.netbeans.modules.palette.ui; 21 22 import java.awt.Component ; 23 import java.awt.dnd.Autoscroll ; 24 import java.awt.event.FocusEvent ; 25 import java.awt.event.FocusListener ; 26 import java.awt.event.KeyEvent ; 27 import javax.swing.*; 28 import javax.swing.border.CompoundBorder ; 29 import java.awt.*; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 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 42 class CategoryButton extends JCheckBox implements Autoscroll { 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 static { 56 if (isGTK) { 57 UIManager.put("MetalCheckBoxUI_4_GTK", "javax.swing.plaf.metal.MetalCheckBoxUI"); 58 } 59 } 60 61 @Override 62 public String getUIClassID() { 63 String 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 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 ) { Dimension pref = getPreferredSize(); 93 pref.height -= 3; 94 setPreferredSize( pref ); 95 } 96 97 addActionListener( new ActionListener () { 98 public void actionPerformed( ActionEvent e ) { 99 boolean opened = !CategoryButton.this.descriptor.isOpened(); 100 setExpanded( opened ); 101 } 102 }); 103 104 addFocusListener( new FocusListener () { 105 public void focusGained(FocusEvent e) { 106 scrollRectToVisible( getBounds() ); 107 } 108 public void focusLost(FocusEvent 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" ); inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_UP, 0, false ), "moveFocusUp" ); inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, 0, false ), "collapse" ); inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, 0, false ), "expand" ); inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_F10, KeyEvent.SHIFT_DOWN_MASK, false ), "popup" ); inputMap.put( KeyStroke.getKeyStroke( "ctrl V" ), "paste" ); inputMap.put( KeyStroke.getKeyStroke( "PASTE" ), "paste" ); 125 ActionMap actionMap = getActionMap(); 126 actionMap.put( "moveFocusDown", new MoveFocusAction( true ) ); actionMap.put( "moveFocusUp", new MoveFocusAction( false ) ); actionMap.put( "collapse", new ExpandAction( false ) ); actionMap.put( "expand", new ExpandAction( true ) ); actionMap.put( "popup", new PopupAction() ); Node categoryNode = (Node)category.getLookup().lookup( Node.class ); 132 if( null != categoryNode ) 133 actionMap.put( "paste", new Utils.PasteItemAction( categoryNode ) ); } 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 151 public void autoscroll( Point cursorLoc ) { 152 Point p = SwingUtilities.convertPoint( this, cursorLoc, getParent().getParent() ); 153 getSupport().autoscroll( p ); 154 } 155 156 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 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"); return UIManager.getColor( "PropSheet.selectedSetBackground" ); } else { 190 if( isAqua ) { 191 return AQUA_BK_COLOR; 192 } else { 193 return UIManager.getColor( "PropSheet.setBackground" ); } 195 } 196 } 197 198 public Color getForeground() { 199 if( isFocusOwner() ) { 200 if( isAqua ) 201 return UIManager.getColor( "Table.selectionForeground" ); return UIManager.getColor( "PropSheet.selectedSetForeground" ); } 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 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 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 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 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 |