00001 package org.hfbk.vis.visnode;
00002
00003 import java.awt.AWTEvent;
00004 import java.io.BufferedReader;
00005 import java.io.File;
00006 import java.io.FileFilter;
00007 import java.io.FileReader;
00008 import java.io.IOException;
00009 import java.util.HashMap;
00010 import java.util.HashSet;
00011 import java.util.Iterator;
00012 import java.util.List;
00013 import java.util.Set;
00014
00015 import org.dronus.graph.Node;
00016 import org.hfbk.util.Sleeper;
00017 import org.hfbk.vis.Prefs;
00018 import org.lwjgl.util.vector.Vector3f;
00019
00027 public class VisScanDir extends VisNodeDraggable {
00028
00029 private String path;
00030 private File filesArray [];
00031 private HashMap<File,Long> dir = new HashMap<File,Long>();
00032 private DirFilterWatcher dfw;
00033
00034
00036 final int DELAY = 1000;
00037
00038
00039 boolean dirty = true;
00040
00042 public VisScanDir(Node n, Vector3f position) {
00043 super(null, position);
00044
00045 if (n.text.length()==0) path=Prefs.current.watchdir;
00046
00047
00048 String filter=Prefs.current.suffixesImage;
00049
00050
00051
00052 System.out.println("watching "+path);
00053
00054 dfw = new DirFilterWatcher(filter);
00055
00056 filesArray = new File(path).listFiles(dfw);
00057
00058
00059
00060 if (filesArray!=null)
00061 {for(int i = 0; i < filesArray.length; i++) {
00062 dir.put(filesArray[i], new Long(filesArray[i].lastModified()));
00063 System.out.println(filesArray[i].getName());
00064 }
00065
00066 }
00067
00068 final String start = n.text;
00069 Thread scanner = new Thread() {
00070
00071 public void run() {
00072 setName("VisScanner");
00073 while (true) {
00074 scanfile(start, null);
00075 Sleeper.sleep(DELAY);
00076 }
00077 }
00078 };
00079 scanner.start();
00080
00081 }
00082
00088 @SuppressWarnings("unchecked")
00089 void scanfile(String startatdir, Node parentnode) {
00090 HashSet<File> checkedFiles = new HashSet<File>();
00091 filesArray = new File(path).listFiles(dfw);
00092 if (filesArray!=null)
00093 {
00094
00095 for(int i = 0; i < filesArray.length; i++) {
00096 Long current = (Long)dir.get(filesArray[i]);
00097 checkedFiles.add(filesArray[i]);
00098 if (current == null) {
00099
00100 dir.put(filesArray[i], new Long(filesArray[i].lastModified()));
00101 if (Prefs.current.verbose) System.out.println(filesArray[i].getName()+ " added");
00102 dirty=true;
00103 }
00104 else if (current.longValue() != filesArray[i].lastModified()){
00105
00106 dir.put(filesArray[i], new Long(filesArray[i].lastModified()));
00107 if (Prefs.current.verbose) System.out.println(filesArray[i].getName()+ " modified");
00108 dirty=true;
00109 }
00110 }
00111
00112
00113 Set<Object> ref = ((HashMap)dir.clone()).keySet();
00114 ref.removeAll((Set)checkedFiles);
00115 Iterator it = ref.iterator();
00116 while (it.hasNext()) {
00117 File deletedFile = (File)it.next();
00118 dir.remove(deletedFile);
00119 if (Prefs.current.verbose) System.out.println(deletedFile.getName()+ " deleted");
00120 dirty=true;
00121 }
00122 }
00123 }
00124
00125
00126 void transform() {
00127 super.transform();
00128
00129 }
00130
00131 void renderSelf() {
00132 if (dirty && (filesArray!=null))
00133 {
00134 System.out.println("updating contents from "+path);
00135 this.children.clear();
00136
00137 if (true){
00138 VisNode struct=parent.findNode(VisStructure.class);
00139 struct.remove(this);
00140 if (struct!=null){
00141 parent.remove(this);
00142
00143 VisNode finddir=struct.findNode(VisScanDir.class);
00144 if (finddir!=null)parent.remove(finddir);
00145
00146 struct.add(this);
00147
00148 for (int i=0;i<filesArray.length;i++)
00149 {
00150 String content="";
00151 try {
00152 content=load(path+"/"+filesArray[i].getName());
00153 } catch (IOException e) {
00154
00155 e.printStackTrace();
00156 }
00157 Vector3f pe=new Vector3f();
00158 pe.x=(float)Math.random()*30;
00159 pe.y=(float)Math.random()*10;
00160 pe.z=(float)Math.random()*30;
00161 VisNode um=new VisText(new Node(content), pe);
00162 this.add(um);
00163 }
00164
00165 }}
00166 dirty=false;}
00167
00168 }
00169
00170 String load(String filename) throws IOException{
00171 BufferedReader r=new BufferedReader(new FileReader(new File(filename)));
00172 String line,content;
00173 content="";
00174
00175 while ((line=r.readLine())!=null)
00176 {content+="\n"+line;}
00177
00178 return content;
00179
00180 }
00181
00182
00183 public void render(List<AWTEvent> events) {
00184
00185 super.render(events);
00186
00187 }
00188 class DirFilterWatcher implements FileFilter {
00189 private String filter;
00190
00191 public DirFilterWatcher() {
00192 this.filter = "";
00193 }
00194
00195 public DirFilterWatcher(String filter) {
00196 this.filter = filter;
00197 }
00198
00199 public boolean accept(File file) {
00200 if ("".equals(filter)) {
00201 return true;
00202 }
00203 return (file.getName().endsWith(filter));
00204 }
00205 }
00206
00207 }