KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > logger > listeners > TCRPropertyListener


1 /*
2  * TCRPropertyListener.java
3  *
4  * Created on April 5, 2005, 4:42 PM
5  */

6
7 package org.netbeans.modules.logger.listeners;
8 import java.beans.PropertyChangeListener JavaDoc;
9 import java.beans.PropertyChangeEvent JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import org.openide.windows.*;
13 import org.openide.nodes.Node;
14 import org.openide.loaders.DataObject;
15 import java.awt.event.WindowFocusListener JavaDoc;
16 import java.awt.event.WindowEvent JavaDoc;
17 import java.awt.Frame JavaDoc;
18
19 /**
20  * Logs events fired by TopComponent.Registry
21  */

22 public class TCRPropertyListener extends ListenerTools implements PropertyChangeListener JavaDoc{
23     String JavaDoc mostRecentWindow;
24     DocManager docManager;
25     
26     /**
27      * Creates a new instance of TCRPropertyListener
28      */

29     public TCRPropertyListener() {
30         super("TCR");
31         mostRecentWindow = "";
32         docManager = new DocManager();
33         listenToMainWindow();
34     }
35     
36     /**
37      * Listens to the main IDE window: when NB loses or gains focus
38      */

39     private void listenToMainWindow(){
40         Runnable JavaDoc findWindow = new Runnable JavaDoc() {
41             public void run() {
42                 Frame JavaDoc f = WindowManager.getDefault().getMainWindow();
43         f.addWindowFocusListener(new WindowFocusListener JavaDoc(){
44             public void windowGainedFocus(WindowEvent JavaDoc e){
45                 gainedFocus();
46             }
47             public void windowLostFocus(WindowEvent JavaDoc e){
48                 lostFocus();
49             }
50         });
51             }
52         };
53         findWindow.run();
54     }
55     
56     
57     /**
58      * IDE focused
59      */

60     private synchronized void gainedFocus(){
61         newBuffer();
62         append(System.currentTimeMillis()+"\t"+"NBGAINEDFOCUS");
63         flush();
64     }
65     
66     
67     /**
68      * IDE lost focus
69      */

70     private synchronized void lostFocus(){
71         newBuffer();
72         append(System.currentTimeMillis()+"\t"+"NBLOSTFOCUS");
73         flush();
74     }
75     
76     /**
77      * Some event has occured
78      * @param evt event
79      */

80     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
81         
82         String JavaDoc evtPpty = evt.getPropertyName();
83         newBuffer();
84         append(System.currentTimeMillis()+"\t"+evtPpty);
85         
86         // opening / closing windows
87
if(evtPpty.equalsIgnoreCase("opened")){
88             caseOpened(evt);
89             // activated (focus) window
90
} else if(evtPpty.equalsIgnoreCase("activated")){
91             caseActivated(evt);
92         } else if(evtPpty.equalsIgnoreCase("activatedNodes")){
93             caseNodes();
94         } else if(evtPpty.equalsIgnoreCase("currentNodes")){
95             return;
96         }else{
97             append("unexpected:"+evtPpty);
98         }
99         flush();
100     }
101     
102     /**
103      * Some TopComponent is activated.
104      * @param evt event
105      */

106     private void caseActivated(PropertyChangeEvent JavaDoc evt){
107         Object JavaDoc o = evt.getNewValue();
108         if(o==null){
109             erase();
110             return;
111         }
112         String JavaDoc s = ((TopComponent)o).getName();
113         if(s==null) {
114             erase();
115             return;
116         }
117         s = trim(s);
118         if(getClassName(o).lastIndexOf("Editor")>0){
119             append("editor");
120             mostRecentWindow = s;
121             s = listenToFile();
122         }
123         
124         if(s.length()>0)append(s);
125         else erase();
126     }
127     
128     /**
129      * removes some unwanted characters
130      * @param s the original String
131      * @return the string without funny characters
132      */

133     private String JavaDoc trim(String JavaDoc s){
134         String JavaDoc out = s.replaceAll("\\s+","");
135         out = out.replaceAll("\\$","");
136         return out.replaceAll("\\*","");
137     }
138     
139     /**
140      * Change in the node selection
141      */

142     private void caseNodes(){
143         Node[] n = WindowManager.getDefault().getRegistry().getActivatedNodes();
144         if(n.length==0){
145             erase();
146             return;
147         }
148         for(int i = 0; i<n.length; i++){
149             append("parent "+nodeName(n[i].getParentNode()));
150             append("node "+nodeName(n[i]));
151         }
152     }
153     
154     /**
155      * get the node name
156      * @param n the node
157      * @return the name
158      */

159     private String JavaDoc nodeName(Node n){
160         if(n==null)return "null";
161         String JavaDoc s = n.getName();
162         s = trim(s);
163         if(s==null||s.length()==0)s = "null";
164         return s;
165     }
166     
167     /**
168      * A TopComponent is opened
169      * @param evt event
170      */

171     private void caseOpened(PropertyChangeEvent JavaDoc evt){
172         try{
173             int i = 0;
174             HashSet JavaDoc newH = (HashSet JavaDoc)evt.getNewValue();
175             HashSet JavaDoc oldH = (HashSet JavaDoc)evt.getOldValue();
176             Iterator JavaDoc it;
177             if(newH.size()>oldH.size()){
178                 append("opening");
179                 iterateWindows(newH,oldH,true);
180             } else {
181                 append("closing");
182                 iterateWindows(oldH,newH,false);
183             }
184         }catch(Exception JavaDoc e){
185             e.printStackTrace();
186         }
187     }
188     
189     /**
190      * Finds out which window has been opened or close by comparing two sets
191      * @param big the bigger hashset
192      * @param small the smaller HashSet
193      * @param opening is the window opening or closing
194      */

195     private void iterateWindows(HashSet JavaDoc big, HashSet JavaDoc small, boolean opening){
196         int charCounter = 0;
197         String JavaDoc window;
198         HashSet JavaDoc bigHash = new HashSet JavaDoc(big);
199         if(!small.isEmpty())bigHash.removeAll(small);
200         Iterator JavaDoc windows = bigHash.iterator();
201         while(windows.hasNext()) {
202             TopComponent tc = (TopComponent)windows.next();
203             window = tc.getName();
204             window = window==null ? "" : trim(window);
205             if(getClassName(tc).lastIndexOf("Editor")>0) {
206                 append("editor");
207                 if(opening) mostRecentWindow = window;
208                 window = listenToFile();
209             }
210             charCounter += window.length();
211             append(window);
212         }
213         if(charCounter<1)erase();
214         return;
215     }
216     
217     
218     /**
219      * Adds a listener to a file in the Editor
220      * @return returns the fake name that should be logged instead of the real
221      * file name
222      */

223     private String JavaDoc listenToFile(){
224         Node[] nodes = TopComponent.getRegistry().getCurrentNodes();
225         for (int i = 0; i<nodes.length; i++) {
226             DataObject data = (DataObject)nodes[i].getCookie(DataObject.class);
227             if(data!=null){
228                 if(isRightNode(nodes[i])){
229                     String JavaDoc s = docManager.getName(data);
230                     return s;
231                 }else{
232                     String JavaDoc s = docManager.findName(data);
233                     if(s.length()>0)return s;
234                 }
235             }
236         }
237         return "";
238     }
239     
240     private boolean isRightNode(Node n){
241         String JavaDoc nodeName = nodeName(n);
242         int dotIndex = mostRecentWindow.lastIndexOf(".");
243         nodeName = nodeName.concat(dotIndex!=-1 ? mostRecentWindow.substring(dotIndex,mostRecentWindow.length()) : "");
244         if(nodeName != null && mostRecentWindow.equals((Object JavaDoc)nodeName))return true;
245         return false;
246     }
247     
248     
249 }
250
Popular Tags