KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > codegen > ui > CheckTreeView


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.java.editor.codegen.ui;
21
22 import java.awt.Point JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.awt.event.KeyListener JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28 import java.awt.event.MouseListener JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import javax.swing.JTree JavaDoc;
32 import javax.swing.tree.TreePath JavaDoc;
33 import javax.swing.tree.TreeSelectionModel JavaDoc;
34 import org.openide.explorer.view.BeanTreeView;
35 import org.openide.explorer.view.Visualizer;
36 import org.openide.nodes.Node;
37
38 /**
39  *
40  * @author Petr Hrebejk
41  */

42 public class CheckTreeView extends BeanTreeView {
43     
44     private static final JScrollPane JavaDoc FOR_BORDER = new JScrollPane JavaDoc();
45     
46     /** Creates a new instance of CheckTreeView */
47     public CheckTreeView() {
48         
49         setFocusable( false );
50         
51         setBorder(FOR_BORDER.getBorder());
52         setViewportBorder(FOR_BORDER.getViewportBorder());
53         
54         CheckListener l = new CheckListener();
55         tree.addMouseListener( l );
56         tree.addKeyListener( l );
57
58         CheckRenderer check = new CheckRenderer();
59         tree.setCellRenderer( check );
60         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
61         
62         tree.setShowsRootHandles(false);
63     }
64     
65     class CheckListener implements MouseListener JavaDoc, KeyListener JavaDoc {
66
67         // MouseListener -------------------------------------------------------
68

69         public void mouseClicked(MouseEvent JavaDoc e) {
70             Point JavaDoc p = e.getPoint();
71             TreePath JavaDoc path = tree.getPathForLocation(e.getPoint().x, e.getPoint().y);
72             toggle( path );
73         }
74
75         public void keyTyped(KeyEvent JavaDoc e) {}
76
77         public void keyReleased(KeyEvent JavaDoc e) {}
78
79         public void mouseEntered(MouseEvent JavaDoc e) {}
80
81         public void mouseExited(MouseEvent JavaDoc e) {}
82
83         public void mousePressed(MouseEvent JavaDoc e) {}
84
85         public void mouseReleased(MouseEvent JavaDoc e) {}
86
87         // Key Listener --------------------------------------------------------
88

89         public void keyPressed(KeyEvent JavaDoc e) {
90             if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER ) {
91                 
92                 JTree JavaDoc tree = (JTree JavaDoc) e.getSource();
93                 TreePath JavaDoc path = tree.getSelectionPath();
94                 
95                 if ( toggle( path )) {
96                     e.consume();
97                 }
98             }
99         }
100         
101         // Private methods -----------------------------------------------------
102

103         private boolean toggle( TreePath JavaDoc treePath ) {
104             
105             if( treePath == null )
106                 return false;
107             
108             Node node = Visualizer.findNode( treePath.getLastPathComponent() );
109             if( node == null )
110                 return false ;
111
112             ElementNode.Description description = node.getLookup().lookup(ElementNode.Description.class);
113             if (description != null && description.isSelectable() ) {
114                 description.setSelected( !description.isSelected() );
115                 return true;
116             }
117             
118             return false;
119         }
120         
121     }
122     
123 }
124
Popular Tags