KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.spi.impl;
20 import java.awt.Point JavaDoc;
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.awt.event.KeyListener JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import java.awt.event.MouseListener JavaDoc;
26 import javax.swing.JTree JavaDoc;
27 import javax.swing.tree.DefaultTreeModel JavaDoc;
28 import javax.swing.tree.TreePath JavaDoc;
29 import org.netbeans.modules.refactoring.api.RefactoringElement;
30 import org.netbeans.modules.refactoring.api.impl.APIAccessor;
31 import org.netbeans.modules.refactoring.spi.ui.TreeElement;
32
33 /**
34  * This listener controls click and double click on the CheckNodes. In addition
35  * to it provides support for keyboard node checking/unchecking and opening
36  * document.
37  *
38  * todo (#pf): Improve behaviour and comments.
39  *
40  * @author Pavel Flaska
41  */

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