1 19 20 package org.netbeans.modules.options.ui; 21 22 23 import java.awt.Color ; 24 import java.awt.Cursor ; 25 import java.awt.Font ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.awt.SystemColor ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.KeyEvent ; 32 import java.awt.event.KeyListener ; 33 import java.awt.event.MouseEvent ; 34 import java.awt.event.MouseListener ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Set ; 39 import java.util.Vector ; 40 import javax.swing.AbstractButton ; 41 import javax.swing.ImageIcon ; 42 import javax.swing.JButton ; 43 import javax.swing.JComponent ; 44 import javax.swing.JLabel ; 45 import javax.swing.JPanel ; 46 import javax.swing.SwingUtilities ; 47 import javax.swing.border.EmptyBorder ; 48 import javax.swing.border.LineBorder ; 49 import org.openide.util.Utilities; 50 51 52 55 public class TabbedPanel extends JPanel { 56 57 public static int EXPAND_ONE = 1; 58 public static int EXPAND_SOME = 2; 59 public static int EXPAND_ALL = 3; 60 61 62 private static Color bColor = new Color ( 63 Math.max ((int)(SystemColor.control.getRed () * 0.9), 0), 64 Math.max ((int)(SystemColor.control.getGreen () * 0.9), 0), 65 Math.max ((int)(SystemColor.control.getBlue () * 0.9), 0) 66 ); 67 68 private TabbedPanelModel model; 69 private int selectedIndex = -1; 70 private Set <Integer > selectedIndexes = new HashSet <Integer > (); 71 private int expansionPolicy; 72 private boolean fill; 73 private JComponent selectedComponent; 74 private JComponent [] titles; 75 private Vector <ActionListener > listeners = new Vector <ActionListener > (); 76 77 78 public TabbedPanel ( 79 TabbedPanelModel model, 80 int expansionPolicy, 81 boolean fill 82 ) { 83 this.model = model; 84 this.expansionPolicy = expansionPolicy; 85 this.fill = fill; 86 addKeyListener (listener); 87 refreshPanels (); 88 setBorder (new LineBorder (new Color (127, 157, 185))); 89 } 90 91 public int getExpansionPolicy () { 92 return expansionPolicy; 93 } 94 95 public int getSelectedIndex () { 96 return selectedIndex; 97 } 98 99 public boolean isExpanded (int index) { 100 if (index == selectedIndex) return true; 101 return selectedIndexes.contains (new Integer (index)); 102 } 103 104 public void setSelectedIndex (final int index) { 105 final int ii = index >= 0 ? index : selectedIndex; 106 if (expansionPolicy == EXPAND_ALL) { 107 Integer i = new Integer (index); 108 if (!selectedIndexes.remove (i)) 109 selectedIndexes.add (i); 110 } else 111 selectedIndex = index; 112 refreshPanels (); 113 SwingUtilities.invokeLater (new Runnable () { 114 public void run () { 115 if (ii >= 0) 116 titles [ii].requestFocus (); 117 } 118 }); 119 } 120 121 public JComponent getSelectedComponent () { 122 return selectedComponent; 123 } 124 125 public String getSelectedCategory () { 126 int i = getSelectedIndex (); 127 if (i < 0) return null; 128 return (String ) model.getCategories ().get (i); 129 } 130 131 public void addActionListener (ActionListener listener) { 132 listeners.add (listener); 133 } 134 135 public void removeActionListener (ActionListener listener) { 136 listeners.remove (listener); 137 } 138 139 protected void fireActionPerformed (ActionEvent event) { 140 Vector l = (Vector ) listeners.clone (); 141 Iterator it = l.iterator (); 142 while (it.hasNext ()) 143 ((ActionListener ) it.next ()).actionPerformed (event); 144 } 145 146 public void refreshPanels () { 147 removeAll (); 148 selectedComponent = null; 149 150 if (expansionPolicy == EXPAND_ONE && 151 selectedIndex < 0 152 ) selectedIndex = 0; 153 154 setLayout (new GridBagLayout ()); 155 GridBagConstraints constraints = new GridBagConstraints (); 156 constraints.gridwidth = GridBagConstraints.REMAINDER; 157 constraints.weightx = 1.0; 158 constraints.fill = GridBagConstraints.BOTH; 159 List categories = model.getCategories (); 160 int i, k = categories.size (), j = 1; 161 titles = new JComponent [k]; 162 for (i = 0; i < k; i++) { 163 String category = (String ) categories.get (i); 164 JComponent l = createTitleComponent ( 165 category, 166 model.getToolTip (category), 167 i 168 ); 169 add (l, constraints); titles [i] = l; 172 173 if ( (expansionPolicy == EXPAND_ALL && 174 selectedIndexes.contains (new Integer (i)) 175 ) || 176 i == selectedIndex 177 ) { 178 selectedComponent = model.getPanel ((String ) categories.get (i)); 179 add (selectedComponent, constraints); } 182 } 183 constraints.weighty = 1.0; 184 JPanel fooPanel = new JPanel (); 185 fooPanel.setOpaque (false); 186 add (fooPanel, constraints); 187 revalidate (); 188 repaint (); 189 fireActionPerformed (new ActionEvent (this, 0, "selectedIndex")); 190 } 191 192 protected JComponent createTitleComponent ( 193 String name, 194 String toolTip, 195 int index 196 ) { 197 JLabel l = new JLabel ( 198 name, 199 new ImageIcon (Utilities.loadImage ( 200 index == selectedIndex ? 201 "org/netbeans/modules/options/resources/expanded.gif": 202 "org/netbeans/modules/options/resources/collapsed.gif" 203 )), 204 JButton.LEFT 205 ); 206 l.setFont (l.getFont ().deriveFont (Font.BOLD)); 207 l.setBackground (bColor); 208 l.setOpaque (true); 209 l.putClientProperty ("index", new Integer (index)); 210 l.addMouseListener (listener); 211 l.setBorder (new EmptyBorder (1, 1, 1, 1)); 212 l.setCursor (Cursor.getPredefinedCursor (Cursor.HAND_CURSOR)); 220 return l; 221 } 222 223 224 private Listener listener = new Listener (); 225 226 private class Listener implements ActionListener , KeyListener , 227 MouseListener { 228 public void actionPerformed (ActionEvent e) { 229 AbstractButton b = (AbstractButton ) e.getSource (); 230 int i = ((Integer ) b.getClientProperty ("index")).intValue (); 231 if (i == selectedIndex) 232 setSelectedIndex (-1); 233 else 234 setSelectedIndex (i); 235 } 236 237 public void keyTyped (KeyEvent e) { 238 239 } 240 241 public void keyPressed (KeyEvent e) {} 242 public void keyReleased (KeyEvent e) {} 243 244 public void mouseClicked (MouseEvent e) { 245 if (!(e.getSource () instanceof JLabel )) return; 246 JLabel l = (JLabel ) e.getSource (); 247 int i = ((Integer ) l.getClientProperty ("index")).intValue (); 248 if (i == selectedIndex) { 249 if (expansionPolicy != EXPAND_ONE) 250 setSelectedIndex (-1); 251 } else 252 setSelectedIndex (i); 253 } 254 255 public void mousePressed (MouseEvent e) {} 256 public void mouseReleased (MouseEvent e) {} 257 258 public void mouseEntered (MouseEvent e) { 259 if (!(e.getSource () instanceof JLabel )) return; 260 JLabel l = (JLabel ) e.getSource (); 261 l.setBackground (SystemColor.control); 262 revalidate (); 263 repaint (); 264 } 265 266 public void mouseExited (MouseEvent e) { 267 if (!(e.getSource () instanceof JLabel )) return; 268 JLabel l = (JLabel ) e.getSource (); 269 l.setBackground (bColor); 270 revalidate (); 271 repaint (); 272 } 273 }; 274 } 275 276 277 278 279 280 | Popular Tags |