1 19 package org.netbeans.api.retouche.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.retouche.source.Phase; 28 import org.netbeans.api.retouche.source.Source.Priority; 29 import org.netbeans.api.retouche.source.SourceTaskFactory; 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 53 public abstract class LookupBasedSourceTaskFactory extends SourceTaskFactory { 54 55 private Result<FileObject> fileObjectResult; 56 private Result<DataObject> dataObjectResult; 57 private Result<Node> nodeResult; 58 59 private List <FileObject> currentFiles; 60 private LookupListener listener; 61 62 67 public LookupBasedSourceTaskFactory(Phase phase, Priority priority) { 68 super(phase, priority); 69 currentFiles = Collections.emptyList(); 70 listener = new LookupListenerImpl(); 71 } 72 73 77 protected synchronized final void setLookup(Lookup lookup) { 78 if (fileObjectResult != null) { 79 fileObjectResult.removeLookupListener(listener); 80 } 81 if (dataObjectResult != null) { 82 dataObjectResult.removeLookupListener(listener); 83 } 84 if (nodeResult != null) { 85 nodeResult.removeLookupListener(listener); 86 } 87 fileObjectResult = lookup.lookupResult(FileObject.class); 88 dataObjectResult = lookup.lookupResult(DataObject.class); 89 nodeResult = lookup.lookupResult(Node.class); 90 91 fileObjectResult.addLookupListener(listener); 92 dataObjectResult.addLookupListener(listener); 93 nodeResult.addLookupListener(listener); 94 95 updateCurrentFiles(); 96 fileObjectsChanged(); 97 } 98 99 private synchronized void updateCurrentFiles() { 100 Set <FileObject> newCurrentFiles = new HashSet (); 101 102 newCurrentFiles.addAll(fileObjectResult.allInstances()); 103 104 for (DataObject d : dataObjectResult.allInstances()) { 105 newCurrentFiles.add(d.getPrimaryFile()); 106 } 107 108 for (Node n : nodeResult.allInstances()) { 109 newCurrentFiles.addAll(n.getLookup().lookupAll(FileObject.class)); 110 111 for (DataObject d : n.getLookup().lookupAll(DataObject.class)) { 112 newCurrentFiles.add(d.getPrimaryFile()); 113 } 114 } 115 116 currentFiles = new ArrayList <FileObject>(newCurrentFiles); 117 118 lookupContentChanged(); 119 } 120 121 122 public synchronized List <FileObject> getFileObjects() { 123 return currentFiles; 124 } 125 126 129 protected void lookupContentChanged() { 130 } 131 132 private class LookupListenerImpl implements LookupListener { 133 public void resultChanged(LookupEvent ev) { 134 updateCurrentFiles(); 135 fileObjectsChanged(); 136 } 137 } 138 139 } 140 | Popular Tags |