KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ui > CheckListener


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.palette.ui;
21 import java.awt.Point JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.awt.event.KeyListener JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.awt.event.MouseListener JavaDoc;
27 import javax.swing.JTree JavaDoc;
28 import javax.swing.tree.DefaultTreeModel JavaDoc;
29 import javax.swing.tree.TreePath JavaDoc;
30 import org.netbeans.modules.palette.DefaultSettings;
31 import org.openide.explorer.view.Visualizer;
32 import org.openide.nodes.Node;
33
34 /**
35  * This listener controls click and double click on the CheckNodes. In addition
36  * to it provides support for keyboard node checking/unchecking and opening
37  * document.
38  *
39  * todo (#pf): Improve behaviour and comments.
40  *
41  * @author Pavel Flaska, S. Aubrecht
42  */

43 class CheckListener implements MouseListener JavaDoc, KeyListener JavaDoc {
44
45     DefaultSettings settings;
46     
47     public CheckListener( DefaultSettings settings ) {
48         this.settings = settings;
49     }
50     
51     public void mouseClicked(MouseEvent JavaDoc e) {
52         // todo (#pf): we need to solve problem between click and double
53
// click - click should be possible only on the check box area
54
// and double click should be bordered by title text.
55
// we need a test how to detect where the mouse pointer is
56
JTree JavaDoc tree = (JTree JavaDoc) e.getSource();
57         Point JavaDoc p = e.getPoint();
58         int x = e.getX();
59         int y = e.getY();
60         int row = tree.getRowForLocation(x, y);
61         TreePath JavaDoc path = tree.getPathForRow(row);
62
63         // if path exists and mouse is clicked exactly once
64
if( null == path )
65             return;
66         
67         Node node = Visualizer.findNode( path.getLastPathComponent() );
68         if( null == node )
69             return;
70         
71         Rectangle JavaDoc chRect = CheckRenderer.getCheckBoxRectangle();
72         Rectangle JavaDoc rowRect = tree.getPathBounds(path);
73         chRect.setLocation(chRect.x + rowRect.x, chRect.y + rowRect.y);
74         if (e.getClickCount() == 1 && chRect.contains(p)) {
75             boolean isSelected = settings.isNodeVisible( node );
76             settings.setNodeVisible( node, !isSelected );
77             tree.repaint();
78         }
79     }
80     
81     public void keyTyped(KeyEvent JavaDoc e) {
82     }
83     
84     public void keyReleased(KeyEvent JavaDoc e) {
85     }
86     
87     public void mouseEntered(MouseEvent JavaDoc e) {
88     }
89     
90     public void mouseExited(MouseEvent JavaDoc e) {
91     }
92     
93     public void mousePressed(MouseEvent JavaDoc e) {
94     }
95     
96     public void mouseReleased(MouseEvent JavaDoc e) {
97     }
98     
99     public void keyPressed(KeyEvent JavaDoc e) {
100         if (e.getKeyChar() == ' ') {
101             JTree JavaDoc tree = (JTree JavaDoc) e.getSource();
102             TreePath JavaDoc path = tree.getSelectionPath();
103             if( null == path )
104                 return;
105
106             Node node = Visualizer.findNode( path.getLastPathComponent() );
107             if( null == node )
108                 return;
109             
110             boolean isSelected = settings.isNodeVisible( node );
111             settings.setNodeVisible( node, !isSelected );
112             tree.repaint();
113             
114             e.consume();
115         }
116     }
117 } // end CheckNodeListener
118
Popular Tags