KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > EditorForm


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import java.io.Serializable JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionErrors;
28 import org.apache.struts.action.ActionError;
29 import org.apache.struts.validator.DynaValidatorForm;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.io.File JavaDoc;
34 import org.apache.struts.util.LabelValueBean;
35 import com.methodhead.util.Comparators;
36 import com.methodhead.sitecontext.SiteContext;
37 import com.methodhead.auth.AuthUtil;
38
39 public final class EditorForm
40 extends
41   DynaValidatorForm
42 implements
43   Serializable JavaDoc {
44
45   /**
46    * Recursively adds path options for <tt>dir</tt> to <tt>options</tt>.
47    */

48   protected void getPathOptions(
49     List JavaDoc options,
50     String JavaDoc path,
51     File JavaDoc dir ) {
52
53     //
54
// handle the root directory specially
55
//
56
if ( path == null )
57       options.add( new LabelValueBean( "/", "" ) );
58     else if ( "".equals( path ) )
59       options.add( new LabelValueBean( "/" + dir.getName(), dir.getName() ) );
60     else
61       options.add( new LabelValueBean(
62         "/" + path + "/" + dir.getName(), path + "/" + dir.getName() ) );
63
64
65     File JavaDoc[] files = dir.listFiles();
66     if ( files == null )
67       throw new ShimException( "Couldn't list files for \"" + dir + "\"" );
68
69     Arrays.sort( files, Comparators.fileComparator() );
70
71     for ( int i = 0; i < files.length; i++ ) {
72       if ( files[ i ].isDirectory() ) {
73         if ( "thumbs".equals( files[ i ].getName() ) )
74           continue;
75
76         if ( path == null )
77           getPathOptions( options, "", files[ i ] );
78         else if ( "".equals( path ) )
79           getPathOptions( options, dir.getName(), files[ i ] );
80         else
81           getPathOptions( options, path + "/" + dir.getName(), files[ i ] );
82       }
83     }
84   }
85     
86   public void reset(
87     ActionMapping mapping,
88     HttpServletRequest JavaDoc request ) {
89
90     //
91
// don't do anything unless someone is logged in
92
//
93
if ( AuthUtil.getUser( request ) == null )
94       return;
95
96     //
97
// get the site context root directory
98
//
99
SiteContext siteContext = SiteContext.getContext( request );
100
101     File JavaDoc dir =
102       new File JavaDoc(
103         request.getSession().getServletContext().getRealPath(
104           "/" + siteContext.getInt( "id" ) ) );
105
106     //
107
// add the options
108
//
109
List JavaDoc options = new ArrayList JavaDoc();
110     getPathOptions( options, null, dir );
111     set( "paths", options );
112   }
113
114   public ActionErrors validate(
115     ActionMapping mapping,
116     HttpServletRequest JavaDoc request ) {
117
118     return super.validate( mapping, request );
119   }
120 }
121
Popular Tags