KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > ui > j > ui > CheckNodeListener


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.refactoring.ui.j.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 java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import javax.swing.JTree JavaDoc;
33 import javax.swing.tree.DefaultTreeModel JavaDoc;
34 import javax.swing.tree.TreePath JavaDoc;
35 import org.netbeans.modules.xml.refactoring.ui.j.api.RefactoringElement;
36 import org.openide.nodes.Node;
37
38 /**
39  * This listener controls click and double click on the CheckNodes. In addition
40  * to it provides support for keyboard node checking/unchecking and opening
41  * document.
42  *
43  * todo (#pf): Improve behaviour and comments.
44  *
45  * @author Pavel Flaska
46  */

47 class CheckNodeListener implements MouseListener JavaDoc, KeyListener JavaDoc {
48     private final boolean isQuery;
49     private PropertyChangeSupport JavaDoc propertyChangeSupport;
50     
51     public CheckNodeListener(boolean isQuery) {
52         this.isQuery = isQuery;
53         propertyChangeSupport =
54             new PropertyChangeSupport JavaDoc(this);
55         
56     }
57
58     public void mouseClicked(MouseEvent JavaDoc e) {
59         // todo (#pf): we need to solve problem between click and double
60
// click - click should be possible only on the check box area
61
// and double click should be bordered by title text.
62
// we need a test how to detect where the mouse pointer is
63
JTree JavaDoc tree = (JTree JavaDoc) e.getSource();
64         Point JavaDoc p = e.getPoint();
65         int x = e.getX();
66         int y = e.getY();
67         int row = tree.getRowForLocation(x, y);
68         TreePath JavaDoc path = tree.getPathForRow(row);
69
70         // if path exists and mouse is clicked exactly once
71
if (path != null) {
72             CheckNode node = (CheckNode) path.getLastPathComponent();
73             if (isQuery) {
74                 if (e.getClickCount() == 2) {
75                     if (node.getChildCount() == 0){
76                         doPreferredAction(node);
77                     } else {
78                         if (tree.isCollapsed(row))
79                             tree.expandRow(row);
80                         else
81                             tree.collapseRow(row);
82                     }
83                 }
84             } else {
85                 Rectangle JavaDoc chRect = CheckRenderer.getCheckBoxRectangle();
86                 Rectangle JavaDoc rowRect = tree.getPathBounds(path);
87                 chRect.setLocation(chRect.x + rowRect.x, chRect.y + rowRect.y);
88                 if (e.getClickCount() == 1 && chRect.contains(p)) {
89                     boolean isSelected = !(node.isSelected());
90                     node.setSelected(isSelected);
91                     if (node.getSelectionMode() == CheckNode.DIG_IN_SELECTION) {
92                         if (isSelected)
93                             tree.expandPath(path);
94                         else
95                             tree.collapsePath(path);
96                     }
97                     ((DefaultTreeModel JavaDoc) tree.getModel()).nodeChanged(node);
98                     if (row == 0) {
99                         tree.revalidate();
100                         tree.repaint();
101                     }
102                 }
103                 // double click, open the document
104
else if (e.getClickCount() == 2 && chRect.contains(p) == false) {
105                     if (node.getChildCount() == 0){
106                         doPreferredAction(node);
107                     }
108                     else {
109                         if (tree.isCollapsed(row))
110                             tree.expandRow(row);
111                         else
112                             tree.collapseRow(row);
113                     }
114                 }
115             }
116         }
117     }
118
119     private void doPreferredAction(CheckNode node) {
120     Node originalNode = node.getOrigNode();
121     // originalNode could be null if the node was added as part of the
122
// refactoring infrastructure such as the find usage node
123
if (originalNode != null) {
124         Action JavaDoc preferredAction = originalNode.getPreferredAction();
125         if (preferredAction != null) {
126         String JavaDoc command = (String JavaDoc)
127         preferredAction.getValue(Action.ACTION_COMMAND_KEY);
128         ActionEvent JavaDoc ae = new ActionEvent JavaDoc(originalNode, 0, command);
129         preferredAction.actionPerformed(ae);
130         }
131     }
132     }
133     
134     public void keyTyped(KeyEvent JavaDoc e) {
135     }
136     
137     public void keyReleased(KeyEvent JavaDoc e) {
138         // Enter key was pressed, find the reference in document
139
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
140             JTree JavaDoc tree = (JTree JavaDoc) e.getSource();
141             TreePath JavaDoc path = tree.getSelectionPath();
142             if (path != null) {
143                 CheckNode node = (CheckNode) path.getLastPathComponent();
144                 findInSource(node);
145             }
146         }
147     }
148     
149     public void mouseEntered(MouseEvent JavaDoc e) {
150     }
151     
152     public void mouseExited(MouseEvent JavaDoc e) {
153     }
154     
155     public void mousePressed(MouseEvent JavaDoc e) {
156     }
157     
158     public void mouseReleased(MouseEvent JavaDoc e) {
159     }
160     
161     public void keyPressed(KeyEvent JavaDoc e) {
162         if (e.getKeyChar() == ' ') {
163             JTree JavaDoc tree = (JTree JavaDoc) e.getSource();
164             TreePath JavaDoc path = tree.getSelectionPath();
165             if (path != null) {
166                 CheckNode node = (CheckNode) path.getLastPathComponent();
167                 node.setSelected(!node.isSelected());
168                 e.consume();
169             }
170         }
171     }
172     
173     static void findInSource(CheckNode node) {
174         Object JavaDoc o = node.getUserObject();
175         if (o instanceof RefactoringElement) {
176             ((RefactoringElement) o).openInEditor();
177         }
178     }
179     
180 // public void openSchemaView(CheckNode node){
181
// Object o = node.getUserObject();
182
// if (o instanceof Usage){
183
// Usage usage = Usage.class.cast(o);
184
// Component component = usage.getComponent();
185
// if (component instanceof SchemaComponent){
186
// SchemaComponent sc = SchemaComponent.class.cast(component);
187
// AnalysisUtilities.openSchemaView(sc);
188
// }
189
// }
190
// }
191

192     
193     public void addFindInSchemaViewListener(PropertyChangeListener JavaDoc l){
194         propertyChangeSupport.addPropertyChangeListener(l);
195     }
196     
197     public void removeFindInSchemaViewListener(PropertyChangeListener JavaDoc l){
198         propertyChangeSupport.removePropertyChangeListener(l);
199     }
200
201 } // end CheckNodeListener
202
Popular Tags