1 6 7 package org.netbeans.modules.logger.listeners; 8 import java.beans.PropertyChangeListener ; 9 import java.beans.PropertyChangeEvent ; 10 import java.util.Iterator ; 11 import java.util.HashSet ; 12 import org.openide.windows.*; 13 import org.openide.nodes.Node; 14 import org.openide.loaders.DataObject; 15 import java.awt.event.WindowFocusListener ; 16 import java.awt.event.WindowEvent ; 17 import java.awt.Frame ; 18 19 22 public class TCRPropertyListener extends ListenerTools implements PropertyChangeListener { 23 String mostRecentWindow; 24 DocManager docManager; 25 26 29 public TCRPropertyListener() { 30 super("TCR"); 31 mostRecentWindow = ""; 32 docManager = new DocManager(); 33 listenToMainWindow(); 34 } 35 36 39 private void listenToMainWindow(){ 40 Runnable findWindow = new Runnable () { 41 public void run() { 42 Frame f = WindowManager.getDefault().getMainWindow(); 43 f.addWindowFocusListener(new WindowFocusListener (){ 44 public void windowGainedFocus(WindowEvent e){ 45 gainedFocus(); 46 } 47 public void windowLostFocus(WindowEvent e){ 48 lostFocus(); 49 } 50 }); 51 } 52 }; 53 findWindow.run(); 54 } 55 56 57 60 private synchronized void gainedFocus(){ 61 newBuffer(); 62 append(System.currentTimeMillis()+"\t"+"NBGAINEDFOCUS"); 63 flush(); 64 } 65 66 67 70 private synchronized void lostFocus(){ 71 newBuffer(); 72 append(System.currentTimeMillis()+"\t"+"NBLOSTFOCUS"); 73 flush(); 74 } 75 76 80 public void propertyChange(PropertyChangeEvent evt) { 81 82 String evtPpty = evt.getPropertyName(); 83 newBuffer(); 84 append(System.currentTimeMillis()+"\t"+evtPpty); 85 86 if(evtPpty.equalsIgnoreCase("opened")){ 88 caseOpened(evt); 89 } 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 106 private void caseActivated(PropertyChangeEvent evt){ 107 Object o = evt.getNewValue(); 108 if(o==null){ 109 erase(); 110 return; 111 } 112 String 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 133 private String trim(String s){ 134 String out = s.replaceAll("\\s+",""); 135 out = out.replaceAll("\\$",""); 136 return out.replaceAll("\\*",""); 137 } 138 139 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 159 private String nodeName(Node n){ 160 if(n==null)return "null"; 161 String s = n.getName(); 162 s = trim(s); 163 if(s==null||s.length()==0)s = "null"; 164 return s; 165 } 166 167 171 private void caseOpened(PropertyChangeEvent evt){ 172 try{ 173 int i = 0; 174 HashSet newH = (HashSet )evt.getNewValue(); 175 HashSet oldH = (HashSet )evt.getOldValue(); 176 Iterator 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 e){ 185 e.printStackTrace(); 186 } 187 } 188 189 195 private void iterateWindows(HashSet big, HashSet small, boolean opening){ 196 int charCounter = 0; 197 String window; 198 HashSet bigHash = new HashSet (big); 199 if(!small.isEmpty())bigHash.removeAll(small); 200 Iterator 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 223 private String 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 s = docManager.getName(data); 230 return s; 231 }else{ 232 String 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 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 )nodeName))return true; 245 return false; 246 } 247 248 249 } 250 | Popular Tags |