001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.uiapi;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Dimension;
046: import java.awt.Image;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.beans.PropertyVetoException;
050: import javax.swing.JPanel;
051: import javax.swing.tree.TreeSelectionModel;
052: import org.netbeans.spi.project.ui.support.ProjectCustomizer;
053: import org.openide.explorer.ExplorerManager;
054: import org.openide.explorer.view.BeanTreeView;
055: import org.openide.nodes.AbstractNode;
056: import org.openide.nodes.Children;
057: import org.openide.nodes.Node;
058: import org.openide.util.NbBundle;
059: import org.openide.util.lookup.Lookups;
060:
061: /**
062: * @author Petr Hrebejk
063: */
064: public class CategoryView extends JPanel implements
065: ExplorerManager.Provider, PropertyChangeListener {
066:
067: private ExplorerManager manager;
068: private BeanTreeView btv;
069: private CategoryModel categoryModel;
070:
071: public CategoryView(CategoryModel categoryModel) {
072:
073: this .categoryModel = categoryModel;
074:
075: // See #36315
076: manager = new ExplorerManager();
077:
078: setLayout(new BorderLayout());
079:
080: Dimension size = new Dimension(220, 4);
081: btv = new BeanTreeView(); // Add the BeanTreeView
082: btv.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
083: btv.setPopupAllowed(false);
084: btv.setRootVisible(false);
085: btv.setDefaultActionAllowed(false);
086: btv.setMinimumSize(size);
087: btv.setPreferredSize(size);
088: btv.setMaximumSize(size);
089: btv.setDragSource(false);
090: this .add(btv, BorderLayout.CENTER);
091: manager.setRootContext(createRootNode(categoryModel));
092: manager.addPropertyChangeListener(this );
093: categoryModel.addPropertyChangeListener(this );
094: btv.expandAll();
095: selectNode(categoryModel.getCurrentCategory());
096:
097: btv.getAccessibleContext().setAccessibleName(
098: NbBundle.getMessage(CategoryView.class,
099: "AN_CatgoryView"));
100: btv.getAccessibleContext().setAccessibleDescription(
101: NbBundle.getMessage(CategoryView.class,
102: "AD_CategoryView"));
103:
104: }
105:
106: public ExplorerManager getExplorerManager() {
107: return manager;
108: }
109:
110: public void addNotify() {
111: super .addNotify();
112: btv.expandAll();
113: btv.requestFocusInWindow();
114: }
115:
116: // Private methods -----------------------------------------------------
117:
118: private void selectNode(ProjectCustomizer.Category category) {
119:
120: Node node = findNode(category, manager.getRootContext());
121:
122: if (node != null) {
123: try {
124: manager.setSelectedNodes(new Node[] { node });
125: } catch (PropertyVetoException e) {
126: // No node will be selected
127: }
128: }
129:
130: }
131:
132: private Node findNode(ProjectCustomizer.Category category, Node node) {
133:
134: Children ch = node.getChildren();
135:
136: if (ch != null && ch != Children.LEAF) {
137: Node nodes[] = ch.getNodes(true);
138:
139: if (nodes != null) {
140: for (Node child : nodes) {
141: ProjectCustomizer.Category cc = child.getLookup()
142: .lookup(ProjectCustomizer.Category.class);
143:
144: if (cc == category) {
145: return child;
146: } else {
147: Node n = findNode(category, child);
148: if (n != null) {
149: return n;
150: }
151: }
152: }
153: }
154: }
155:
156: return null;
157: }
158:
159: private Node createRootNode(CategoryModel categoryModel) {
160: ProjectCustomizer.Category rootCategory = ProjectCustomizer.Category
161: .create("root", "root", null, categoryModel
162: .getCategories()); // NOI18N
163: return new CategoryNode(rootCategory);
164: }
165:
166: // Implementation of property change listener --------------------------
167:
168: public void propertyChange(PropertyChangeEvent evt) {
169:
170: Object source = evt.getSource();
171: String propertyName = evt.getPropertyName();
172:
173: if (source == manager
174: && ExplorerManager.PROP_SELECTED_NODES
175: .equals(propertyName)) {
176: Node nodes[] = manager.getSelectedNodes();
177: if (nodes == null || nodes.length <= 0) {
178: return;
179: }
180: Node node = nodes[0];
181:
182: ProjectCustomizer.Category category = node.getLookup()
183: .lookup(ProjectCustomizer.Category.class);
184: if (category != categoryModel.getCurrentCategory()) {
185: categoryModel.setCurrentCategory(category);
186: }
187: }
188:
189: if (source == categoryModel
190: && CategoryModel.PROP_CURRENT_CATEGORY
191: .equals(propertyName)) {
192: selectNode((ProjectCustomizer.Category) evt.getNewValue());
193: }
194:
195: }
196:
197: // Private Inner classes -----------------------------------------------
198:
199: /** Node to be used for configuration
200: */
201: private static class CategoryNode extends AbstractNode implements
202: PropertyChangeListener {
203:
204: private Image icon = org.openide.util.Utilities
205: .loadImage("org/netbeans/modules/project/uiapi/defaultCategory.gif"); // NOI18N
206:
207: private ProjectCustomizer.Category category;
208:
209: public CategoryNode(ProjectCustomizer.Category category) {
210: super (
211: (category.getSubcategories() == null || category
212: .getSubcategories().length == 0) ? Children.LEAF
213: : new CategoryChildren(category
214: .getSubcategories()), Lookups
215: .fixed(category));
216: setName(category.getName());
217: this .category = category;
218: setDisplayName(category.getDisplayName());
219:
220: if (category.getIcon() != null) {
221: this .icon = category.getIcon();
222: }
223: Utilities.getCategoryChangeSupport(category)
224: .addPropertyChangeListener(this );
225: }
226:
227: public String getHtmlDisplayName() {
228: return category.isValid() ? null
229: : "<html><font color=\"!nb.errorForeground\">"
230: + // NOI18N
231: category.getDisplayName()
232: + "</font></html>"; // NOI18N
233: }
234:
235: public Image getIcon(int type) {
236: return this .icon;
237: }
238:
239: public Image getOpenedIcon(int type) {
240: return getIcon(type);
241: }
242:
243: public void propertyChange(PropertyChangeEvent evt) {
244: if (evt.getPropertyName() == CategoryChangeSupport.VALID_PROPERTY) {
245: fireDisplayNameChange(null, null);
246: }
247: }
248:
249: }
250:
251: /** Children used for configuration
252: */
253: private static class CategoryChildren extends
254: Children.Keys<ProjectCustomizer.Category> {
255:
256: private ProjectCustomizer.Category[] descriptions;
257:
258: public CategoryChildren(
259: ProjectCustomizer.Category[] descriptions) {
260: this .descriptions = descriptions;
261: }
262:
263: // Children.Keys impl --------------------------------------------------
264:
265: public void addNotify() {
266: setKeys(descriptions);
267: }
268:
269: public void removeNotify() {
270: setKeys(new ProjectCustomizer.Category[0]);
271: }
272:
273: protected Node[] createNodes(ProjectCustomizer.Category c) {
274: return new Node[] { new CategoryNode(c) };
275: }
276: }
277:
278: }
|