View Javadoc

1   /***
2    * $Id: FilesView.java,v 1.1 2004/11/08 23:15:38 mch Exp $
3    */
4   package org.astrogrid.store.browser;
5   
6   import java.io.IOException;
7   import java.io.PrintWriter;
8   import java.io.Writer;
9   import java.net.URISyntaxException;
10  import java.net.URL;
11  import java.text.DateFormat;
12  import java.text.SimpleDateFormat;
13  import java.util.Date;
14  import java.util.Vector;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import org.astrogrid.community.Account;
18  import org.astrogrid.community.User;
19  import org.astrogrid.store.Agsl;
20  import org.astrogrid.store.Ivorn;
21  import org.astrogrid.store.Msrl;
22  import org.astrogrid.store.VoSpaceClient;
23  import org.astrogrid.store.delegate.StoreClient;
24  import org.astrogrid.store.delegate.StoreDelegateFactory;
25  import org.astrogrid.store.delegate.StoreFile;
26  
27  /***
28   * An object representing a view onto a store, such as myspace, suitable for
29   * display via HTML
30   */
31  public class FilesView  {
32     /*** Holds the file tree on the server */
33     private StoreFile root = null;
34     
35     private DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
36     
37     public static final String hdrClr = "#BBBBBB";
38    // public static final String hilitedClr = "#0000BB";
39     
40     Browser parent = null;
41     
42     public FilesView(Browser aBrowser) {
43        this.parent = aBrowser;
44     }
45     
46     /***
47      * Returns the currently selected path
48      */
49     public String getSelectedPath(HttpServletRequest request) {
50        return parent.getParameter(request, Browser.SELECTED_PATH_KEY);
51     }
52     
53     /***
54      * Returns the currently selected StoreFile
55      *
56      private String getSelectedFile(HttpServletRequest request) {
57      String path = getSelectedPath(request);
58      //rather poor search, anyway...
59      
60      }
61      */
62     
63     /*** Refresh file tree. @todo get it to preserve open nodes */
64     public void refreshTree(HttpServletRequest request) throws IOException {
65        StoreClient client = parent.getStoreClient(request);
66  
67        if (client == null) {
68           //throw new IOException("No Store Given");
69           return;
70        }
71           
72        root = client.getFiles("*");
73     }
74  
75     /*** Open the given path */
76     public void openPath(String path, HttpServletRequest request) {
77        String[] openPaths = (String[]) request.getSession().getValue("openPaths");
78        if (openPaths == null) { openPaths = new String[] { }; }
79        
80         //add path to open paths
81        String[] newPaths = new String[openPaths.length+1];
82        for (int i = 0; i < openPaths.length; i++) {
83           newPaths[i] = openPaths[i];
84        }
85        newPaths[openPaths.length] = path;
86  
87        request.getSession().putValue("openPaths", newPaths);
88     }
89     
90     /*** Close the given path */
91     public void closePath(String closePath, HttpServletRequest request) {
92        String[] openPaths = (String[]) request.getSession().getValue("openPaths");
93        if (openPaths == null) { openPaths = new String[] { }; }
94        
95        //remove path from open paths
96        Vector newPaths = new Vector();
97        for (int i = 0; i < openPaths.length; i++) {
98           if (!openPaths[i].startsWith(closePath)) {
99              newPaths.add(openPaths[i]);
100          }
101       }
102       openPaths = (String[]) newPaths.toArray(new String[] {} );
103 
104       request.getSession().putValue("openPaths", openPaths);
105    }
106 
107    /*** Writes the panel that contains the list of files */
108    public void writeView(HttpServletRequest request, HttpServletResponse response) throws IOException {
109  
110       response.getWriter().print("\n<div id='files'>\n"+
111                                     "<table align='full'>\n"+
112                                     "<tr bgcolor='#AAAAAA'>"+
113                                     "  <th align='left'>Name</th>"+
114                                     "  <th>Size</th>"+
115                                     "  <th>Type</th>"+
116                                     "  <th>Created</th>"+
117                                     "  <th>Modified</th>"+
118                                     "</tr>\n");
119       
120       response.getWriter().flush();
121 
122       if (root == null) { refreshTree(request); }
123    
124       
125       String[] openPaths = (String[]) request.getSession().getValue("openPaths");
126       if (openPaths == null) { openPaths = new String[] { }; }
127       String selectedPath = getSelectedPath(request);
128       
129       if ((root == null) || (root.listFiles() == null)) {
130          response.getWriter().println("</table>No entries to list from "+parent.getStoreURI(request)+"</div>");
131          return;
132       }
133       else {
134          //write root's children
135          for (int i=0;i<root.listFiles().length;i++) {
136             writeFile(root.listFiles()[i], openPaths, selectedPath, response.getWriter());
137          }
138       }
139       
140       response.getWriter().print("</table></div>\n");
141       
142    }
143    
144    /*** creates an indent string for the given file */
145    private String getIndent(StoreFile file) {
146       StringBuffer indent = new StringBuffer();
147       while ((file.getParent() != null) && (file.getParent().getParent() != null)) {
148          indent.append("&nbsp;&nbsp;&nbsp;");
149          //indent.append("___");
150          file=file.getParent();
151       }
152       return indent.toString();
153    }
154    
155    
156    //prints out a single StoreFile
157    private void writeFile(StoreFile file, String[] openPaths, String selectedPath, Writer out) throws IOException {
158       
159       String cellColour = "#FFFFFF";  //white
160       String inkColour = "#000000";  //black
161       //if ((selectedPath != null) && (selectedPath.startsWith(file.getPath()))) {
162       if ((selectedPath != null) && (selectedPath.equals(file.getPath()))) {
163          cellColour = "#6666FF";
164 //         inkColour = "#FFFFFF";  //white
165       }
166       
167       if (file.isFolder()) {
168          
169          //is it open?
170          boolean isOpen = false;
171          if (file.getPath().length()==0) {
172             //root
173             isOpen = true;
174          }
175          else {
176             for (int i = 0; i < openPaths.length; i++) {
177                if (openPaths[i].startsWith(file.getPath())) {
178                   isOpen = true;
179                }
180             }
181          }
182          if (isOpen) {
183             if (file.getPath().length()>0) {
184                out.write("<tr bgcolor='"+cellColour+"' fgcolor='"+inkColour+"'>"+
185                             "<td><pre>"+
186                             getIndent(file)+
187                             "<a href=\""+parent.refRoot+"close="+file.getPath()+"\">"+
188                             "<img ref='images/OpenFolder.png' alt='(-)' border='0'/>"+
189                             "</a>"+
190                             " <a href=\""+parent.refRoot+"path="+file.getPath()+"\">"+
191                             file.getName()+
192                             "</a></pre></td>"+
193                             "<td></td>"+
194                             "<td>Folder</td>"+
195                             //"<td/>"+
196                             "<td></td>"+
197                             "<td></td>"+
198                             "</tr>\n");
199             }
200             for (int i=0;i<file.listFiles().length;i++) {
201                writeFile(file.listFiles()[i], openPaths, selectedPath, out);
202                out.write("\n");
203             }
204          }
205          else {
206             //folder is closed
207             out.write("<tr bgcolor='"+cellColour+"' fgcolor='"+inkColour+"'>"+
208                          "<td><pre>"+
209                          getIndent(file)+
210                          "<a href=\""+parent.refRoot+"open="+file.getPath()+"\">"+
211                          "<img ref='images/ClosedFolder.png' alt='(+)' border='0'/>"+
212                          "</a>"+
213                          " <a href=\""+parent.refRoot+"path="+file.getPath()+"\">"+
214                          "<font color='"+inkColour+"'>"+
215                          file.getName()+
216                          "</font>"+
217                          "</a>"+
218                          "</pre></td>"+
219                          "<td></td>"+
220                          "<td>Folder</td>"+
221                          //"<td/>"+
222                          "<td></td>"+
223                          "<td></td>"+
224                          "</tr>\n");
225             
226          }
227       }
228       else {
229          //it's a file
230          out.write("<tr bgcolor='"+cellColour+"' fgcolor='"+inkColour+"'>"+
231                       "<td>"+"<pre> "+
232                       getIndent(file)+
233                       getFileIconHtml(file)+ //"&nbsp;&nbsp;&nbsp;"+//could add here some check on mime type and suitable icon instead of spaces
234                       "<a href=\""+parent.refRoot+"path="+file.getPath()+"\">"+
235                       file.getName() +
236                       "</a>"+
237                       "</pre>"+
238                       "</td>"+
239                       "<td align='right'>"+ file.getSize() + "</td>"+
240                       "<td><tiny>"+ emptyNull(file.getMimeType())+"</tiny></td>"+
241                       "<td>"+ date(file.getCreated()) + "</td>"+
242                       "<td>"+ date(file.getModified())+"</td>"+
243                       "</tr>\n");
244       }
245    }
246    
247    
248    public String getFileIconHtml(StoreFile file) {
249       String mimeType = file.getMimeType();
250       String html = "&nbsp;&nbsp;&nbsp;";
251 
252       if ((mimeType==null) || (mimeType.equals("null"))) { //strange botch...
253          return html;
254       }
255       mimeType = mimeType.trim().toLowerCase();
256       if (mimeType.startsWith("text/xml")) {
257          html="<img ref='./images/Document.png' alt='&nbsp;&nbsp;&nbsp' border='0'/>";
258          if (mimeType.endsWith("org.astrogrid.mime.workflow")) {
259             html="<img ref='./images/Workflow.gif' alt='&nbsp;&nbsp;&nbsp' border='0'/>";
260          }
261       }
262       else {
263          //default to what?  Could do with some tool tippy thing
264       }
265       return html;
266    }
267    
268    public String emptyNull(String s) {
269       if (s==null) {
270          return "";
271       }
272       else {
273          return s;
274       }
275    }
276    
277    public String date(Date d) {
278       if (d==null) {
279          return "";
280       }
281       else {
282          return dateFormat.format(d);
283       }
284    }
285 }