6 import netscape.javascript.JSObject;
8 public class TreeChooser extends Applet
9 implements CbButtonCallback, HierarchyCallback
11 CbButton add_b, remove_b, close_b;
17 Vector added = new Vector();
22 String rpath = getParameter("root");
23 root = new BaculaNode(this, rpath, true, null);
24 volume = getParameter("volume");
25 session = getParameter("session");
26 job = getParameter("job");
29 setLayout(new BorderLayout());
30 BorderPanel top = new BorderPanel(2);
31 top.setLayout(new FlowLayout(FlowLayout.LEFT));
32 top.add(add_b = new CbButton("Add", this));
33 top.add(remove_b = new CbButton("Remove", this));
34 top.add(close_b = new CbButton("Close", this));
36 add("Center", tree = new Hierarchy(root, this));
39 Image get_image(String img)
41 return getImage(getDocumentBase(), "images/"+img);
44 String[] get_text(String url)
46 Cursor orig = getCursor();
48 Cursor busy = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
50 long now = System.currentTimeMillis();
51 if (url.indexOf('?') > 0) url += "&rand="+now;
52 else url += "?rand="+now;
53 URL u = new URL(getDocumentBase(), url);
54 URLConnection uc = u.openConnection();
56 String charset = get_charset(uc.getContentType());
57 BufferedReader is = new BufferedReader(
59 new InputStreamReader(uc.getInputStream()) :
60 new InputStreamReader(uc.getInputStream(), charset));
61 Vector lv = new Vector();
63 String l = is.readLine();
64 if (l == null) { break; }
68 String rv[] = new String[lv.size()];
75 String err[] = { e.getMessage() };
83 void set_cookie(URLConnection conn)
86 conn.setRequestProperty("Cookie", session);
89 // Gets charset parameter from Content-Type: header
90 String get_charset(String ct)
94 StringTokenizer st = new StringTokenizer(ct, ";");
95 while (st.hasMoreTokens()) {
96 String l = st.nextToken().trim().toLowerCase();
97 if (l.startsWith("charset=")) {
98 // get the value of charset= param.
99 return l.substring(8);
105 public void openNode(Hierarchy h, HierarchyNode n)
107 // Get the files under this directory, and expand the tree
108 BaculaNode bn = (BaculaNode)n;
112 public void closeNode(Hierarchy h, HierarchyNode n)
114 // No need to do anything
117 public void clickNode(Hierarchy h, HierarchyNode n)
119 // Also no need to do anything
122 public void doubleNode(Hierarchy h, HierarchyNode n)
124 // add or remove a file
125 BaculaNode sel = (BaculaNode)n;
126 if (sel.added) remove_node(sel);
130 public void click(CbButton b)
132 BaculaNode sel = (BaculaNode)tree.selected();
134 // Close the window, and update the text box
136 JSObject win = JSObject.getWindow(this);
137 String params1[] = { "" };
138 win.call("clear_files", params1);
139 for(int i=0; i<added.size(); i++) {
140 BaculaNode n = (BaculaNode)added.elementAt(i);
141 String params2[] = { n.path };
142 if (n.isdir && !n.path.equals("/"))
143 params2[0] = n.path+"/";
144 win.call("add_file", params2);
146 String params3[] = { "" };
147 win.call("finished", params3);
151 new ErrorWindow("Failed to set files : "+
155 else if (b == add_b) {
156 // Flag the selected file as added
161 else if (b == remove_b) {
162 // Un-flag the selected file
169 void add_node(BaculaNode n)
179 void remove_node(BaculaNode n)
185 added.removeElement(n);
189 static String urlize(String s)
191 StringBuffer rv = new StringBuffer();
192 for(int i=0; i<s.length(); i++) {
193 char c = s.charAt(i);
195 rv.append("%0"+Integer.toString(c, 16));
196 else if (!Character.isLetterOrDigit(c) && c != '/' &&
197 c != '.' && c != '_' && c != '-')
198 rv.append("%"+Integer.toString(c, 16));
202 return rv.toString();
206 class BaculaNode extends HierarchyNode
211 boolean known = false;
212 boolean added = false;
215 BaculaNode(TreeChooser parent, String path, boolean isdir, BaculaNode dir)
217 this.parent = parent;
223 ch = isdir ? new Vector() : null;
224 if (path.equals("/"))
227 String ns = path.endsWith("/") ?
228 path.substring(0, path.length() - 1) : path;
229 int slash = ns.lastIndexOf("/");
230 text = path.substring(slash+1);
236 String imname = isdir ? "dir.gif" : "rfile.gif";
237 if (selected()) imname = "s"+imname;
238 im = parent.get_image(imname);
245 for(int i=0; i<ch.size(); i++) {
246 BaculaNode c = (BaculaNode)ch.elementAt(i);
254 if (!known && isdir) {
255 ch.removeAllElements();
256 String l[] = parent.get_text("list.cgi?dir="+
259 parent.urlize(parent.volume)+
261 parent.urlize(parent.job));
262 if (l[0].length() > 0) {
263 new ErrorWindow("Failed to get files under "+path+
267 for(int i=1; i<l.length; i++) {
268 if (l[i].endsWith("/")) {
271 parent, l[i].substring(0, l[i].length()-1),
277 parent, l[i], false, this));
280 parent.tree.redraw();
289 if (n.added) return true;