KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > ui > TabbedPanel


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.options.ui;
21
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Cursor JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.GridBagConstraints JavaDoc;
27 import java.awt.GridBagLayout JavaDoc;
28 import java.awt.SystemColor JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.awt.event.KeyListener JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.awt.event.MouseListener JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Set JavaDoc;
39 import java.util.Vector JavaDoc;
40 import javax.swing.AbstractButton JavaDoc;
41 import javax.swing.ImageIcon JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JComponent JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.SwingUtilities JavaDoc;
47 import javax.swing.border.EmptyBorder JavaDoc;
48 import javax.swing.border.LineBorder JavaDoc;
49 import org.openide.util.Utilities;
50
51
52 /**
53  * @author Jan Jancura
54  */

55 public class TabbedPanel extends JPanel JavaDoc {
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 JavaDoc bColor = new Color JavaDoc (
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 JavaDoc<Integer JavaDoc> selectedIndexes = new HashSet JavaDoc<Integer JavaDoc> ();
71     private int expansionPolicy;
72     private boolean fill;
73     private JComponent JavaDoc selectedComponent;
74     private JComponent JavaDoc[] titles;
75     private Vector JavaDoc<ActionListener JavaDoc> listeners = new Vector JavaDoc<ActionListener JavaDoc> ();
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 JavaDoc (new Color JavaDoc (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 JavaDoc (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 JavaDoc i = new Integer JavaDoc (index);
108             if (!selectedIndexes.remove (i))
109                 selectedIndexes.add (i);
110         } else
111             selectedIndex = index;
112         refreshPanels ();
113         SwingUtilities.invokeLater (new Runnable JavaDoc () {
114             public void run () {
115                 if (ii >= 0)
116                     titles [ii].requestFocus ();
117             }
118         });
119     }
120     
121     public JComponent JavaDoc getSelectedComponent () {
122         return selectedComponent;
123     }
124     
125     public String JavaDoc getSelectedCategory () {
126         int i = getSelectedIndex ();
127         if (i < 0) return null;
128         return (String JavaDoc) model.getCategories ().get (i);
129     }
130     
131     public void addActionListener (ActionListener JavaDoc listener) {
132         listeners.add (listener);
133     }
134     
135     public void removeActionListener (ActionListener JavaDoc listener) {
136         listeners.remove (listener);
137     }
138     
139     protected void fireActionPerformed (ActionEvent JavaDoc event) {
140         Vector JavaDoc l = (Vector JavaDoc) listeners.clone ();
141         Iterator JavaDoc it = l.iterator ();
142         while (it.hasNext ())
143             ((ActionListener JavaDoc) 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 JavaDoc ());
155         GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc ();
156         constraints.gridwidth = GridBagConstraints.REMAINDER;
157         constraints.weightx = 1.0;
158         constraints.fill = GridBagConstraints.BOTH;
159         List JavaDoc categories = model.getCategories ();
160         int i, k = categories.size (), j = 1;
161         titles = new JComponent JavaDoc [k];
162         for (i = 0; i < k; i++) {
163             String JavaDoc category = (String JavaDoc) categories.get (i);
164             JComponent JavaDoc l = createTitleComponent (
165                 category,
166                 model.getToolTip (category),
167                 i
168             );
169 // layout.appendRow (new RowSpec ("p"));
170
add (l, constraints); //cc.xy (1, j++, "l,c"));
171
titles [i] = l;
172     
173             if ( (expansionPolicy == EXPAND_ALL &&
174                   selectedIndexes.contains (new Integer JavaDoc (i))
175                  ) ||
176                  i == selectedIndex
177             ) {
178                 selectedComponent = model.getPanel ((String JavaDoc) categories.get (i));
179                 //layout.appendRow (new RowSpec (fill ? "f:p:g" : "f:p"));
180
add (selectedComponent, constraints); //cc.xy (1, j++));
181
}
182         }
183         constraints.weighty = 1.0;
184         JPanel JavaDoc fooPanel = new JPanel JavaDoc ();
185         fooPanel.setOpaque (false);
186         add (fooPanel, constraints);
187         revalidate ();
188         repaint ();
189         fireActionPerformed (new ActionEvent JavaDoc (this, 0, "selectedIndex"));
190     }
191
192     protected JComponent JavaDoc createTitleComponent (
193         String JavaDoc name,
194         String JavaDoc toolTip,
195         int index
196     ) {
197         JLabel JavaDoc l = new JLabel JavaDoc (
198             name,
199             new ImageIcon JavaDoc (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 JavaDoc (index));
210         l.addMouseListener (listener);
211         l.setBorder (new EmptyBorder JavaDoc (1, 1, 1, 1));
212 // l.setBorder (new BevelBorder (
213
// BevelBorder.RAISED,
214
// SystemColor.control.brighter (),
215
// SystemColor.control,
216
// SystemColor.control.darker (),
217
// SystemColor.control
218
// ));
219
l.setCursor (Cursor.getPredefinedCursor (Cursor.HAND_CURSOR));
220         return l;
221     }
222
223
224     private Listener JavaDoc listener = new Listener JavaDoc ();
225     
226     private class Listener implements ActionListener JavaDoc, KeyListener JavaDoc,
227     MouseListener JavaDoc {
228         public void actionPerformed (ActionEvent JavaDoc e) {
229             AbstractButton JavaDoc b = (AbstractButton JavaDoc) e.getSource ();
230             int i = ((Integer JavaDoc) b.getClientProperty ("index")).intValue ();
231             if (i == selectedIndex)
232                 setSelectedIndex (-1);
233             else
234                 setSelectedIndex (i);
235         }
236
237         public void keyTyped (KeyEvent JavaDoc e) {
238             
239         }
240
241         public void keyPressed (KeyEvent JavaDoc e) {}
242         public void keyReleased (KeyEvent JavaDoc e) {}
243         
244         public void mouseClicked (MouseEvent JavaDoc e) {
245             if (!(e.getSource () instanceof JLabel JavaDoc)) return;
246             JLabel JavaDoc l = (JLabel JavaDoc) e.getSource ();
247             int i = ((Integer JavaDoc) 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 JavaDoc e) {}
256         public void mouseReleased (MouseEvent JavaDoc e) {}
257         
258         public void mouseEntered (MouseEvent JavaDoc e) {
259             if (!(e.getSource () instanceof JLabel JavaDoc)) return;
260             JLabel JavaDoc l = (JLabel JavaDoc) e.getSource ();
261             l.setBackground (SystemColor.control);
262             revalidate ();
263             repaint ();
264         }
265         
266         public void mouseExited (MouseEvent JavaDoc e) {
267             if (!(e.getSource () instanceof JLabel JavaDoc)) return;
268             JLabel JavaDoc l = (JLabel JavaDoc) e.getSource ();
269             l.setBackground (bColor);
270             revalidate ();
271             repaint ();
272         }
273     };
274 }
275
276
277
278
279
280
Popular Tags