KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > networkplaces > forms > FileSystemForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.networkplaces.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.action.ActionErrors;
31 import org.apache.struts.action.ActionMapping;
32
33 import com.sslexplorer.networkplaces.NetworkPlace;
34 import com.sslexplorer.networkplaces.model.FileSystemItem;
35 import com.sslexplorer.networkplaces.model.FileSystemItemModel;
36 import com.sslexplorer.policyframework.LaunchSession;
37 import com.sslexplorer.policyframework.forms.AbstractResourcesForm;
38 import com.sslexplorer.security.SessionInfo;
39 import com.sslexplorer.vfs.VFSPath;
40 import com.sslexplorer.vfs.VFSResource;
41
42 /**
43  * <p>
44  * The form that provides the data for file system view.
45  *
46  * @author James D Robinson
47  *
48  * @mail <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
49  */

50 public class FileSystemForm extends AbstractResourcesForm {
51
52     private static final long serialVersionUID = -8944288890287463553L;
53     static Log log = LogFactory.getLog(FileSystemForm.class);
54
55     private String JavaDoc path;
56     private boolean confirmDeletion;
57     private String JavaDoc newFolder;
58     private String JavaDoc newName;
59     private String JavaDoc fileName;
60     private List JavaDoc paths;
61     private boolean viewOnly;
62     private LaunchSession launchSession;
63     private String JavaDoc launchId;
64     private VFSResource vfsResource;
65
66     /**
67      * Constructor that sets up the form.
68      */

69     public FileSystemForm() {
70         super(new FileSystemItemModel("fileSystem"));
71         this.path = null;
72         this.confirmDeletion = false;
73         this.newFolder = null;
74         this.newName = null;
75         this.fileName = null;
76         this.launchSession = null;
77         this.paths = new ArrayList JavaDoc();
78     }
79     
80     /**
81      * Set the launch ID. This is used to retrieve the resource session after
82      * the network place has been launched
83      *
84      * @param launchId launch ID
85      */

86     public void setLaunchId(String JavaDoc launchId) {
87         this.launchId = launchId;
88     }
89
90     /**
91      * Set the {@link VFSResource} for the current path.
92      *
93      * @param vfsResource vfs resource
94      */

95     public void setVFSResource(VFSResource vfsResource) {
96         this.vfsResource = vfsResource;
97         
98     }
99
100     /**
101      * Get the {@link VFSResource} for the current path.
102      *
103      * @return vfs resource
104      */

105     public VFSResource getVFSResource() {
106         return vfsResource;
107         
108     }
109     
110     /**
111      * Get the launch ID. This is used to retrieve the resource session after
112      * the network place has been launched
113      *
114      * @return launch ID
115      */

116     public String JavaDoc getLaunchId() {
117         return launchId;
118     }
119     
120     /**
121      * Set the launch session.
122      *
123      * @param launchSession launch session
124      */

125     public void setLaunchSession(LaunchSession launchSession) {
126         this.launchSession = launchSession;
127         launchId = launchSession.getId();
128     }
129     
130     /**
131      * Get the launch session
132      *
133      * @return launch session
134      */

135     public LaunchSession getLaunchSession() {
136         return launchSession;
137     }
138
139     /**
140      * @return Object[] of the selected resources.
141      */

142     public String JavaDoc[] getSelectedFileNames() {
143         List JavaDoc selected = new ArrayList JavaDoc();
144         for (Iterator JavaDoc i = getModel().getItems().iterator(); i.hasNext();) {
145             FileSystemItem ti = (FileSystemItem) i.next();
146             if (ti.getChecked())
147                 selected.add(ti.getFileName());
148         }
149         return (String JavaDoc[]) selected.toArray(new String JavaDoc[selected.size()]);
150     }
151     
152     /**
153      * Set the policy object this network place was launched under. Access
154      * to this resource should be
155      * @return weather the source resource is deleted after an operation, used
156      * on cut.
157      */

158     public boolean isConfirmDeletion() {
159         return confirmDeletion;
160     }
161
162     /**
163      * @param confirmDeletion weather the resource should be deleted.
164      */

165     public void setConfirmDeletion(boolean confirmDeletion) {
166         this.confirmDeletion = confirmDeletion;
167     }
168
169     /**
170      * @return The path to the current location.
171      */

172     public String JavaDoc getPath() {
173         return path;
174     }
175
176     /**
177      * @param String path Sets the path to the current location.
178      */

179     public void setPath(String JavaDoc path) {
180         this.path = path;
181     }
182
183     /**
184      * @return String the path to the home of the network place.
185      */

186     public String JavaDoc getHome() {
187         VFSPath tmp = (VFSPath) this.paths.get(0);
188         this.paths.clear();
189         this.paths.add(0, tmp);
190         return tmp.getPath();
191     }
192
193     /**
194      * @param String home sets the path to the home for this network place.
195      */

196     public void setHome(String JavaDoc home) {
197         this.paths.add(new VFSPath(0, home));
198     }
199
200     public List JavaDoc getPaths() {
201         return this.paths;
202     }
203
204     /**
205      * @return int the id of the resource.
206      */

207     public int getResourceId() {
208         return getNetworkPlace() == null ? -1 : getNetworkPlace().getResourceId();
209     }
210
211     /**
212      * @return String the name of the new folder.
213      */

214     public String JavaDoc getNewFolder() {
215         return newFolder;
216     }
217
218     /**
219      * @param newFolderString String to set the name of the new folder.
220      */

221     public void setNewFolder(String JavaDoc newFolderString) {
222         this.newFolder = newFolderString;
223     }
224
225     /**
226      * @return String the name of the new name.
227      */

228     public String JavaDoc getNewName() {
229         return newName;
230     }
231
232     /**
233      * @param newName String to set the name of the file.
234      */

235     public void setNewName(String JavaDoc newName) {
236         this.newName = newName;
237     }
238
239     /**
240      * @return String of the file name selected.
241      */

242     public String JavaDoc getFileName() {
243         return fileName;
244     }
245
246     /**
247      * @param fileName String of the file selected.
248      */

249     public void setFileName(String JavaDoc fileName) {
250         this.fileName = fileName;
251     }
252
253     /**
254      * <p>
255      * Initialise the form.
256      *
257      * @param request The operation request.
258      * @param allFileSystemItems List of files to be viewed.
259      * @throws Exception
260      */

261     public void initialize(HttpServletRequest JavaDoc request, List JavaDoc allFileSystemItems, SessionInfo session) throws Exception JavaDoc {
262         super.initialize(session.getHttpSession(), "name");
263
264         try {
265             Iterator JavaDoc i = allFileSystemItems.iterator();
266
267             while (i.hasNext()) {
268                 FileSystemItem it = (FileSystemItem) i.next();
269                 it.setSortFoldersFirst(true);
270                 getModel().addItem(it);
271             }
272             getPager().setSortReverse(getSortReverse());
273             getPager().rebuild(getFilterText());
274         } catch (Throwable JavaDoc t) {
275             log.error("Failed to initialise resources form.", t);
276         }
277     }
278
279     /**
280      * Set the folder to be read / write. By default a form is view only, this
281      * method must be called before any write functions are allowed.
282      *
283      */

284     public void setReadWrite() {
285         viewOnly = false;
286     }
287
288     /**
289      * Set this folder to be a read only
290      */

291     public void setViewOnly() {
292         viewOnly = true;
293     }
294     
295     /**
296      * Get if this should be <i>view only</i>. For a folder to not be view
297      * only, {@link #setReadWrite()} must have been called
298      *
299      * @return view only
300      */

301     public boolean isViewOnly() {
302         return viewOnly;
303     }
304
305     /**
306      * Get the network place that points to the mount currently being
307      * viewed. <code>null</code> will be returned if the mount does
308      * not point to a network place.
309      *
310      * @return The network place acossiated with the file system view.
311      */

312     public NetworkPlace getNetworkPlace() {
313         return launchSession == null ? null : (NetworkPlace)launchSession.getResource();
314     }
315
316     /**
317      * @param path The path to add or go to.
318      */

319     public void addPath(String JavaDoc path) {
320         VFSPath newPath = new VFSPath(this.paths.size(), path);
321         VFSPath oldPath = null;
322         boolean add = true;
323         List JavaDoc newPaths = new ArrayList JavaDoc();
324         Iterator JavaDoc iter = this.paths.iterator();
325         while (iter.hasNext()) {
326             VFSPath element = (VFSPath) iter.next();
327             if (element.getPath().equals(newPath.getPath())) {
328                 add = false;
329                 newPaths.add(element);
330                 break;
331             } else {
332                 newPaths.add(element);
333             }
334         }
335         this.paths = newPaths;
336         if (add)
337             this.paths.add(newPath);
338     }
339
340     /**
341      * @param _id The position to move to...
342      */

343     public void clearPathsTo(String JavaDoc _id) {
344         int id = Integer.parseInt(_id);
345         Iterator JavaDoc iter = this.paths.iterator();
346         List JavaDoc newPaths = new ArrayList JavaDoc();
347         while (iter.hasNext()) {
348             VFSPath element = (VFSPath) iter.next();
349             if (element.getPosition() <= id)
350                 newPaths.add(element);
351         }
352         this.paths = newPaths;
353     }
354
355     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
356         /*
357          * NOTE This done to avoid double slashes between folder names.
358          */

359         if ("list".equals(getActionTarget())) {
360             if (this.getPath()!=null && this.getPath().endsWith("/")) {
361                 this.setPath(this.getPath().substring(0, this.getPath().length() - 1));
362             }
363         }
364         return super.validate(mapping, request);
365     }
366
367     public String JavaDoc getFullURI() {
368         if (vfsResource == null ) {
369             return null;
370         } else {
371             return vfsResource.getFullPath();
372         }
373     }
374
375     /* (non-Javadoc)
376      * @see com.sslexplorer.table.forms.AbstractPagerForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
377      */

378     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
379         super.reset(mapping, request);
380         viewOnly = true;
381     }
382 }
383
Popular Tags