1 19 package org.netbeans.api.java.source.support; 20 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Set ; 26 import javax.swing.event.ChangeEvent ; 27 import org.netbeans.api.java.source.JavaSource.Phase; 28 import org.netbeans.api.java.source.JavaSource.Priority; 29 import org.netbeans.api.java.source.JavaSourceTaskFactory; 30 import org.openide.filesystems.FileObject; 31 import org.openide.loaders.DataObject; 32 import org.openide.nodes.Node; 33 import org.openide.util.Lookup; 34 import org.openide.util.Lookup.Result; 35 import org.openide.util.LookupEvent; 36 import org.openide.util.LookupListener; 37 38 47 public abstract class LookupBasedJavaSourceTaskFactory extends JavaSourceTaskFactory { 48 49 private Result<FileObject> fileObjectResult; 50 private Result<DataObject> dataObjectResult; 51 private Result<Node> nodeResult; 52 53 private List <FileObject> currentFiles; 54 private LookupListener listener; 55 56 61 public LookupBasedJavaSourceTaskFactory(Phase phase, Priority priority) { 62 super(phase, priority); 63 currentFiles = Collections.emptyList(); 64 listener = new LookupListenerImpl(); 65 } 66 67 71 protected synchronized final void setLookup(Lookup lookup) { 72 if (fileObjectResult != null) { 73 fileObjectResult.removeLookupListener(listener); 74 } 75 if (dataObjectResult != null) { 76 dataObjectResult.removeLookupListener(listener); 77 } 78 if (nodeResult != null) { 79 nodeResult.removeLookupListener(listener); 80 } 81 fileObjectResult = lookup.lookupResult(FileObject.class); 82 dataObjectResult = lookup.lookupResult(DataObject.class); 83 nodeResult = lookup.lookupResult(Node.class); 84 85 fileObjectResult.addLookupListener(listener); 86 dataObjectResult.addLookupListener(listener); 87 nodeResult.addLookupListener(listener); 88 89 updateCurrentFiles(); 90 fileObjectsChanged(); 91 } 92 93 private synchronized void updateCurrentFiles() { 94 Set <FileObject> newCurrentFiles = new HashSet (); 95 96 newCurrentFiles.addAll(fileObjectResult.allInstances()); 97 98 for (DataObject d : dataObjectResult.allInstances()) { 99 newCurrentFiles.add(d.getPrimaryFile()); 100 } 101 102 for (Node n : nodeResult.allInstances()) { 103 newCurrentFiles.addAll(n.getLookup().lookupAll(FileObject.class)); 104 105 for (DataObject d : n.getLookup().lookupAll(DataObject.class)) { 106 newCurrentFiles.add(d.getPrimaryFile()); 107 } 108 } 109 110 currentFiles = new ArrayList <FileObject>(newCurrentFiles); 111 112 lookupContentChanged(); 113 } 114 115 116 public synchronized List <FileObject> getFileObjects() { 117 return currentFiles; 118 } 119 120 123 protected void lookupContentChanged() { 124 } 125 126 private class LookupListenerImpl implements LookupListener { 127 public void resultChanged(LookupEvent ev) { 128 updateCurrentFiles(); 129 fileObjectsChanged(); 130 } 131 } 132 133 } 134 | Popular Tags |