View Javadoc

1   /*
2    *  ginp - Java Web Application for Viewing Photo Collections
3    *  Copyright (C) 2004  Douglas John Culnane <doug@culnane.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sf.ginp.tags;
20  
21  import java.io.*;
22  import java.util.Vector;
23  import javax.servlet.http.*;
24  import javax.servlet.jsp.*;
25  import net.sf.ginp.*;
26  import net.sf.ginp.config.ModelUtil;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   *  Tag for building a tree menu based on the model state.
32   *
33   *@author     Doug Culnane
34   *@version    $Revision: 303 $
35   */
36  public class TreeMenu extends javax.servlet.jsp.tagext.TagSupport
37           implements javax.servlet.jsp.tagext.IterationTag {
38  
39      /**
40  	 * 
41  	 */
42  	private static final long serialVersionUID = 822134669247999069L;
43  	private  String            anchorextra     = "";
44      private  String            currentPath     = "/";
45      private  String            folderclosed    = "+ ";
46      private  String            folderopen      = "# ";
47      private  boolean           gotMoreNodes;
48      private  Vector            grid;
49      private  String            gridblank       = "  ";
50      private  String            gridcont        = "| ";
51      private  String            gridend         = "'-";
52      private  String            gridtee         = "|-";
53      private  String            lineend         = "";
54      private  String            linestart       = "\n";
55      private  GinpModel         model;
56      private  String            nodeicon        = "% ";
57      private  String            selectedFolder;
58      private  boolean           shownodes       = true;
59      private Log log = LogFactory.getLog(TreeMenu.class);
60  
61      /**
62       *  Sets the beforelabel attribute of the TreeMenu object
63       *
64       *@param  s  The new beforelabel value
65       */
66      public void setAnchorextra(String s) {
67          anchorextra = s;
68      }
69  
70  
71  
72      /**
73       *  Sets the folderclosed attribute of the TreeMenu object
74       *
75       *@param  s  The new folderclosed value
76       */
77      public void setFolderclosed(String s) {
78          folderclosed = s;
79      }
80  
81  
82      /**
83       *  Sets the folderopen attribute of the TreeMenu object
84       *
85       *@param  s  The new folderopen value
86       */
87      public void setFolderopen(String s) {
88          folderopen = s;
89      }
90  
91  
92      /**
93       *  Sets the gridblank attribute of the TreeMenu object
94       *
95       *@param  s  The new gridblank value
96       */
97      public void setGridblank(String s) {
98          gridblank = s;
99      }
100 
101 
102     /**
103      *  Sets the gridcont attribute of the TreeMenu object
104      *
105      *@param  s  The new gridcont value
106      */
107     public void setGridcont(String s) {
108         gridcont = s;
109     }
110 
111 
112     /**
113      *  Sets the gridend attribute of the TreeMenu object
114      *
115      *@param  s  The new gridend value
116      */
117     public void setGridend(String s) {
118         gridend = s;
119     }
120 
121 
122     /**
123      *  Sets the gridtee attribute of the TreeMenu object
124      *
125      *@param  s  The new gridtee value
126      */
127     public void setGridtee(String s) {
128         gridtee = s;
129     }
130 
131 
132     /**
133      *  Sets the lineend attribute of the TreeMenu object
134      *
135      *@param  s  The new lineend value
136      */
137     public void setLineend(String s) {
138         lineend = s;
139     }
140 
141 
142     /**
143      *  Sets the linestart attribute of the TreeMenu object
144      *
145      *@param  s  The new linestart value
146      */
147     public void setLinestart(String s) {
148         linestart = s;
149     }
150 
151 
152     /**
153      *  Sets the nodeicon attribute of the TreeMenu object
154      *
155      *@param  s  The new nodeicon value
156      */
157     public void setNodeicon(String s) {
158         nodeicon = s;
159     }
160 
161 
162     /**
163      *  Constructor for the setShownodes object
164      *
165      *@param  b  Description of the Parameter
166      */
167     public void setShownodes(boolean b) {
168         shownodes = b;
169     }
170 
171 
172     /**
173      *  Description of the Method
174      *
175      *@return                   Description of the Return Value
176      *@exception  JspException  Description of the Exception
177      */
178     public int doStartTag() throws JspException {
179 		try {
180 			model = ModelUtil.getModel((HttpServletRequest) pageContext.getRequest());
181 		} catch (Exception e) {
182             log.error(e);
183 			throw new JspException(e);
184 		}
185         // get Currest collection root
186         grid = new Vector();
187         if (model.getCollections().length > 0) {
188             selectedFolder = model.getCollection().getPath();
189             currentPath = selectedFolder;
190             gotMoreNodes = false;
191             if (shownodes && selectedFolder.equals("/") 
192             		&& ModelUtil.getFolderManager().getPicturesInDirectoryLength(model.getCollection(),selectedFolder) > 0) {
193                 gotMoreNodes = true;
194             }
195             doFolders(ModelUtil.getFolderManager().getFoldersInDirectory(model.getCollection(),"/"));
196             if (gotMoreNodes) {
197                 doNodes("/");
198             }
199         }
200         return SKIP_BODY;
201     }
202 
203 
204     /**
205      *  Description of the Method
206      *
207      *@param  folders           Description of the Parameter
208      *@exception  JspException  Description of the Exception
209      */
210     void doFolders(String[] folders) throws JspException {
211         if (grid.size() > 0) {
212             if (((GridElement) grid.get(grid.size() - 1)).getHtml().equals(gridend)) {
213                 ((GridElement) grid.get(grid.size() - 1)).setHtml(gridblank);
214             } else {
215                 ((GridElement) grid.get(grid.size() - 1)).setHtml(gridcont);
216             }
217         }
218         grid.add(new GridElement(gridtee));
219         try {
220             for (int i = 0; i < folders.length; i++) {
221                 if (shownodes) {
222                     if (currentPath.equals(folders[i] + "/") 
223                     		&& selectedFolder.equals(folders[i] + "/")
224                     		&&ModelUtil.getFolderManager().getPicturesInDirectoryLength(model.getCollection(),folders[i]) > 0) {
225                         gotMoreNodes = true;
226                     }
227                 }
228                 if (i == folders.length - 1) {
229                     if (shownodes) {
230                         if (gotMoreNodes && !selectedFolder.equals(folders[i] + "/")) {
231                             ((GridElement) grid.get(grid.size() - 1)).setHtml(gridtee);
232                         } else {
233                             ((GridElement) grid.get(grid.size() - 1)).setHtml(gridend);
234                         }
235                     } else {
236                         ((GridElement) grid.get(grid.size() - 1)).setHtml(gridend);
237                     }
238                 }
239                 // write out line
240                 pageContext.getOut().write(linestart);
241                 for (int j = 0; j < grid.size(); j++) {
242                     pageContext.getOut().write(((GridElement) grid.get(j)).getHtml());
243                 }
244                 if (currentPath.startsWith(folders[i])) {
245                     pageContext.getOut().write(folderopen);
246                 } else {
247                     pageContext.getOut().write(folderclosed);
248                 }
249 
250                 pageContext.getOut().write("<a href=\""
251                          + "ginpservlet?cmd=selectpath&path=" +  folders[i]
252                          + "&colid=" + model.getCurrCollectionId() + "\"" + anchorextra + ">"
253                          + folders[i].substring(folders[i].lastIndexOf("/") + 1)
254                          + "</a>" + lineend);
255 
256                 if (currentPath.startsWith(folders[i])) {
257                     doFolders(ModelUtil.getFolderManager().getFoldersInDirectory(model.getCollection(),folders[i] + "/"));
258                     if (shownodes) {
259                         if (selectedFolder.equals(folders[i] + "/")) {
260                             doNodes(folders[i]);
261                         }
262                     }
263                     // crop the current path
264                     if (currentPath.indexOf("/", 1) > 0) {
265                         currentPath = currentPath.substring(currentPath.indexOf("/", 1));
266                     }
267                 }
268             }
269         } catch (IOException ioe) {
270             log.error(ioe);
271             throw new JspException(ioe.getMessage());
272         }
273         grid.removeElementAt(grid.size() - 1);
274         if (grid.size() > 0) {
275             if (!((GridElement) grid.get(grid.size() - 1)).getHtml().equals(gridblank)) {
276                 ((GridElement) grid.get(grid.size() - 1)).setHtml(gridtee);
277             }
278         }
279     }
280 
281 
282     /**
283      *  Description of the Method
284      *
285      *@param  path              Description of the Parameter
286      *@exception  JspException  Description of the Exception
287      */
288     void doNodes(String path) throws JspException {
289 
290         String[]  nodes  = ModelUtil.getFolderManager().getPicturesInDirectory(model.getCollection(),path);
291 
292         if (grid.size() > 0) {
293             if (((GridElement) grid.get(grid.size() - 1)).getHtml().equals(gridtee)) {
294                 ((GridElement) grid.get(grid.size() - 1)).setHtml(gridcont);
295             } else {
296                 ((GridElement) grid.get(grid.size() - 1)).setHtml(gridblank);
297             }
298         }
299         grid.add(new GridElement(gridtee));
300         try {
301             for (int i = 0; i < nodes.length; i++) {
302                 if (i == nodes.length - 1) {
303                     if (grid.size() > 0) {
304                         ((GridElement) grid.get(grid.size() - 1)).setHtml(gridend);
305                     }
306                 }
307                 pageContext.getOut().write(linestart);
308                 for (int j = 0; j < grid.size(); j++) {
309                     pageContext.getOut().write(((GridElement) grid.get(j)).getHtml());
310                 }
311 
312                 String  filename  = nodes[i].substring(0, nodes[i].lastIndexOf("."));
313                 // if this is a featured picture remove the path.
314                 if (filename.indexOf("/") != -1) {
315                     filename = filename.substring(filename.lastIndexOf("/") + 1);
316                 }
317                 pageContext.getOut().write(nodeicon + "<a href=\""
318                          + "ginpservlet?cmd=showpicture&colid="
319                          + model.getCurrCollectionId() + "&path="
320                          + path + "&name=" +  nodes[i]
321                          + "\"" + anchorextra + ">"
322                          + filename + "</a>" + lineend);
323             }
324 
325         } catch (IOException ioe) {
326             log.error(ioe);
327             throw new JspException(ioe.getMessage());
328         }
329         if (grid.size() > 0) {
330             grid.removeElementAt(grid.size() - 1);
331         }
332         if (grid.size() > 0) {
333             ((GridElement) grid.get(grid.size() - 1)).setHtml(gridtee);
334         }
335         gotMoreNodes = false;
336     }
337 }
338 
339 /**
340  *  Description of the Class
341  *
342  *@author     $Author: dougculnane $
343  *@version    $Revision: 303 $
344  */
345 class GridElement {
346 
347 
348     String  html  = "#";
349 
350 
351     /**
352      *  Constructor for the gridElement object
353      *
354      *@param  setHtml  Description of the Parameter
355      */
356     GridElement(String setHtml) {
357         html = setHtml;
358     }
359 
360 
361     /**
362      *  Sets the html attribute of the GridElement object
363      *
364      *@param  s  The new html value
365      */
366     public void setHtml(String s) {
367         html = s;
368     }
369 
370 
371     /**
372      *  Description of the Method
373      *
374      *@return    Description of the Return Value
375      */
376     public String getHtml() {
377         return html;
378     }
379 }
380