00001 package org.hfbk.vis.visnode;
00002
00003 import java.io.File;
00004 import java.util.ArrayList;
00005 import java.util.HashMap;
00006 import java.util.List;
00007
00008 import org.dronus.graph.Node;
00009 import org.hfbk.vis.Allesfresser;
00010 import org.hfbk.vis.Prefs;
00011 import org.lwjgl.util.vector.Vector3f;
00012
00020 public class VisDir extends VisImagefield {
00021
00022 HashMap<File, VisNode> visnodesByFiles = new HashMap<File, VisNode>();
00023
00024 HashMap<File, Long> tstamps = new HashMap<File, Long>();
00025
00026 Allesfresser af;
00027
00028 public VisDir(Node n, Vector3f position) {
00029 super(n, position);
00030 }
00031
00032 void renderSelf() {
00033 String dir = node.text;
00034 if (dir.equals(""))
00035 dir = Prefs.current.watchdir;
00036 if (dir.equals(".") || !new File(dir).exists()) {
00037 children.clear();
00038 return;
00039 }
00040
00041
00042 if (af == null)
00043 af = new Allesfresser(getRoot().client);
00044
00045 int count = children.size();
00046
00047 List<VisNode> active = new ArrayList<VisNode>();
00048
00049
00050 for (File f : new File(dir).listFiles()) {
00051
00052 long tstamp = f.lastModified();
00053 VisNode currentv = visnodesByFiles.get(f);
00054
00055
00056
00057 if ((currentv == null || tstamps.get(f) < tstamp) && !f.isHidden()
00058 && !f.getPath().endsWith("~")) {
00059
00060
00061
00062 VisNode v = null;
00063 try {
00064 v = af.frissFile("file://" + f.getAbsolutePath());
00065 } catch (Exception e) {
00066 System.out.println("Unable to read " + f.getAbsolutePath());
00067 }
00068
00069 if (v != null) {
00070
00071
00072
00073 if (v instanceof VisDir)
00074 v = new OpenButton(v, f);
00075
00076 if (currentv != null) {
00077
00078
00079 children.set(children.indexOf(currentv), v);
00080 v.position = currentv.position;
00081 v.parent = this;
00082 } else {
00083
00084 add(v);
00085 }
00086 currentv = v;
00087 }
00088
00089 visnodesByFiles.put(f, v);
00090 tstamps.put(f, tstamp);
00091
00092
00093
00094 }
00095
00096 if (currentv != null)
00097 active.add(currentv);
00098 }
00099
00100 children.retainAll(active);
00101
00102 if (count != children.size())
00103 setLayout(layoutMode);
00104
00105 super.renderSelf();
00106 }
00107
00111 class OpenButton extends MoreButton {
00112 VisNode n;
00113 File dir;
00114
00115 public OpenButton(VisNode n) {
00116 super(new Vector3f());
00117 this.n = n;
00118
00119 }
00120
00121 public OpenButton(VisNode n, File dir) {
00122 super(new Vector3f());
00123 this.n = n;
00124 this.help = dir.getName();
00125 this.dir = dir;
00126 }
00127
00129 public void handleClick() {
00130
00131
00132 int i = parent.children.indexOf(this);
00133 parent.children.remove(i);
00134 parent.children.add(i, n);
00135 n.parent = parent;
00136 n.position.x = parent.getExtends();
00137 visnodesByFiles.put(dir, n);
00138 }
00139 }
00140 }