KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > category > DefaultCategoryPane


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.category;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.GridBagLayout JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.AbstractAction JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.ButtonGroup JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.JToggleButton JavaDoc;
34 import javax.swing.JToolBar JavaDoc;
35 import javax.swing.UIManager JavaDoc;
36 import javax.swing.border.Border JavaDoc;
37 import org.netbeans.modules.xml.xam.ui.search.DefaultSearchControlPanel;
38
39 /**
40  * The default implementation of CategoryPane.
41  *
42  * @author Nathan Fiedler
43  */

44 public class DefaultCategoryPane extends AbstractCategoryPane {
45     /** The top component of our interface. */
46     private JPanel JavaDoc visualComponent;
47     /** Component for search interface. */
48     private DefaultSearchControlPanel searchComponent;
49     /** Where the Category components reside. */
50     private JPanel JavaDoc categoryPanel;
51     /** List of actions to show the categories. */
52     private List JavaDoc<ShowCategoryAction> categoryActions;
53     /** Makes the buttons act like radio buttons. */
54     private ButtonGroup JavaDoc buttonGroup;
55
56     /**
57      * Creates a new instance of DefaultCategoryPane.
58      */

59     public DefaultCategoryPane() {
60         categoryActions = new ArrayList JavaDoc<ShowCategoryAction>();
61         initComponents();
62     }
63
64     public void addCategory(Category category) {
65         categoryActions.add(new ShowCategoryAction(category, this));
66     }
67
68     public Component JavaDoc getComponent() {
69         return visualComponent;
70     }
71
72     public SearchComponent getSearchComponent() {
73         return searchComponent;
74     }
75
76     /**
77      * Construct our user interface.
78      */

79     private void initComponents() {
80         // Button group for our category buttons.
81
buttonGroup = new ButtonGroup JavaDoc();
82
83         // Build our overall container.
84
visualComponent = new JPanel JavaDoc(new GridBagLayout JavaDoc()) {
85             /** silence compiler warnings */
86             private static final long serialVersionUID = 1L;
87
88             @Override JavaDoc
89             public void requestFocus() {
90                 super.requestFocus();
91                 // Pass focus to category component to ease keyboard navigation.
92
Category cat = getCategory();
93                 if (cat != null) {
94                     cat.getComponent().requestFocus();
95                 }
96             }
97
98             @Override JavaDoc
99             public boolean requestFocusInWindow() {
100                 boolean retVal = super.requestFocusInWindow();
101                 // Pass focus to category component to ease keyboard navigation.
102
Category cat = getCategory();
103                 if (cat != null) {
104                     return cat.getComponent().requestFocusInWindow();
105                 }
106                 return retVal;
107             }
108         };
109
110         // Build the primary view component.
111
GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
112         gbc.fill = GridBagConstraints.BOTH;
113         gbc.gridwidth = GridBagConstraints.REMAINDER;
114         gbc.weightx = 1.0d;
115         gbc.weighty = 1.0d;
116         categoryPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
117         visualComponent.add(categoryPanel, gbc);
118
119         // Build the search component.
120
searchComponent = new DefaultSearchControlPanel(this);
121         searchComponent.hideComponent();
122         gbc.anchor = GridBagConstraints.WEST;
123         gbc.fill = GridBagConstraints.HORIZONTAL;
124         gbc.weightx = 0.0d;
125         gbc.weighty = 0.0d;
126         visualComponent.add(searchComponent.getComponent(), gbc);
127     }
128
129     public void populateToolbar(JToolBar JavaDoc toolbar) {
130         Category selected = getCategory();
131         // Get the NetBeans button border, if available.
132
Border JavaDoc border = UIManager.getBorder("nb.tabbutton.border"); //NOI18N
133
for (ShowCategoryAction action : categoryActions) {
134             JToggleButton JavaDoc button = new JToggleButton JavaDoc(action);
135             // Action has a name for accessibility purposes, but we do
136
// not want that to appear in the button label.
137
button.setText(null);
138             button.getAccessibleContext().setAccessibleName(action.getCategory().getTitle());
139             button.setRolloverEnabled(true);
140             if (border != null) {
141                 button.setBorder(border);
142             }
143             buttonGroup.add(button);
144             if (selected == null) {
145                 if (buttonGroup.getButtonCount() == 1) {
146                     // Make the first button the chosen one.
147
button.setSelected(true);
148                     // Select the category so it is visible.
149
action.actionPerformed(null);
150                 }
151             } else {
152                 if (action.getCategory().equals(selected)) {
153                     button.setSelected(true);
154                 }
155             }
156             toolbar.add(button);
157         }
158     }
159
160     @Override JavaDoc
161     public void setCategory(Category category) {
162         Category oldcat = getCategory();
163         super.setCategory(category);
164         if (oldcat != null) {
165             Component JavaDoc oldcomp = oldcat.getComponent();
166             categoryPanel.remove(oldcomp);
167             oldcat.componentHidden();
168         }
169         Component JavaDoc newcomp = category.getComponent();
170         categoryPanel.add(newcomp, BorderLayout.CENTER);
171         category.componentShown();
172         categoryPanel.validate();
173         categoryPanel.repaint();
174         searchComponent.updateSearchProviders();
175     }
176
177     /**
178      * An action to show a category.
179      */

180     private static class ShowCategoryAction extends AbstractAction JavaDoc {
181         private static final long serialVersionUID = 1L;
182         /** The Category we show. */
183         private transient Category category;
184         /** The CategoryPane containing the Category. */
185         private transient CategoryPane pane;
186         
187         /**
188          * Creates a new instance of ShowCategoryAction.
189          *
190          * @param category the Category to be shown.
191          * @param pane the CategoryPane for category.
192          */

193         public ShowCategoryAction(Category category, CategoryPane pane) {
194             this.category = category;
195             this.pane = pane;
196             putValue(Action.NAME, category.getTitle());
197             putValue(Action.SHORT_DESCRIPTION, category.getDescription());
198             putValue(Action.SMALL_ICON, category.getIcon());
199         }
200
201         public void actionPerformed(ActionEvent JavaDoc e) {
202             pane.setCategory(category);
203         }
204
205         /**
206          * Return the Category associated with this action.
207          *
208          * @return Category this action displays.
209          */

210         public Category getCategory() {
211             return category;
212         }
213     }
214 }
215
Popular Tags