KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > content > explorer > component > UIFileExplorer


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.content.explorer.component;
6
7 import java.io.File JavaDoc;
8 import javax.faces.context.FacesContext;
9 import javax.portlet.PortletPreferences;
10 import org.exoplatform.faces.FacesUtil;
11 import org.exoplatform.faces.core.component.UIGrid;
12 import org.exoplatform.faces.core.component.UINode;
13 import org.exoplatform.faces.core.component.model.ComponentCell;
14 import org.exoplatform.faces.core.component.model.Row;
15 import org.exoplatform.faces.core.event.ExoActionEvent;
16 import org.exoplatform.faces.core.event.ExoActionListener;
17 import org.exoplatform.portlets.content.ACL;
18 import org.exoplatform.portlets.content.FileACL;
19 import org.exoplatform.portlets.content.explorer.component.model.FileNodeDescriptor;
20 import org.exoplatform.portlets.content.explorer.component.model.NodeDescriptor;
21 /**
22  * Jun 22, 2004
23  * @author: Tuan Nguyen
24  * @email: tuan08@users.sourceforge.net
25  * @version: $Id: UIFileExplorer.java,v 1.3 2004/08/17 13:07:02 tuan08 Exp $
26  */

27 public class UIFileExplorer extends UIExplorer {
28   private String JavaDoc reposistory_ ;
29     private String JavaDoc relativePathBaseDir_ ;
30   private String JavaDoc realPathBaseDir_ ;
31     private ACL acl_ ;
32     
33     public UIFileExplorer() throws Exception JavaDoc {
34     init() ;
35       UINode uiDetail = (UINode)addChild(UINode.class) ;
36     uiDetail.setRendererType("PyramidTabBarRenderer") ;
37     UINode uiNodeContent = (UINode)uiDetail.addChild(UINode.class) ;
38     
39     uiNodeContent.setRendererType("ChildrenRenderer") ;
40     uiNodeContent.setName("View") ;
41     uiNodeContent.setId("UINodeContent") ;
42     uiNodeContent.addChild(UIFileChildrenInfo.class) ;
43     uiNodeContent.addChild(UIFileContentViewer.class).setRendered(false) ;
44     
45     if(acl_.hasWriteRole(null)) {
46       UINode uiAdmin = (UINode)uiDetail.addChild(UINode.class) ;
47       uiAdmin.setRendered(false) ;
48       uiAdmin.setId("UIAdmin") ;
49       uiAdmin.setName("Admin") ;
50       uiAdmin.setRendererType("SimpleTabRenderer") ;
51       uiAdmin.addChild(UIFileNodeInfo.class) ;
52       UINode uiEditor = (UINode) uiAdmin.addChild(UINode.class) ;
53       uiEditor.setId("UINodeEditor") ;
54       uiEditor.setRendered(false) ;
55       uiEditor.setRendererType("ChildrenRenderer") ;
56       
57       UIGrid grid = (UIGrid) uiEditor.addChild(UIGrid.class);
58       grid.setId("UICreateOrUpload");
59       
60       UIFileUpload uiUpload = new UIFileUpload();
61       uiUpload.setRendered(true) ;
62       uiUpload.setClazz("UIUpload");
63       uiUpload.addFileSystemActionListener() ;
64       
65       grid.add(new Row().
66                add(new ComponentCell(grid, new UIFileNodeEditor())));
67       grid.add(new Row().
68                add(new ComponentCell(grid, new UIChoiceLabel())));
69       grid.add(new Row().
70                add(new ComponentCell(grid, uiUpload)));
71       uiEditor.addChild(UIFileContentEditor.class) ;
72     }
73     
74       addActionListener(ChangeNodeActionListener.class, CHANGE_NODE_ACTION) ;
75     changeNode("/") ;
76     }
77     
78     public void init() throws Exception JavaDoc {
79     PortletPreferences prefs = FacesUtil.getPortletPreferences() ;
80     String JavaDoc user = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser() ;
81     if(user == null) user = "" ;
82     reposistory_ = FacesUtil.getServletContext().getInitParameter("file.portlet.reposistory") ;
83     if(reposistory_ == null || reposistory_.length() ==0 || reposistory_.equals("default")) {
84       reposistory_= FacesUtil.getServletContext().getRealPath("/") ;
85     }
86     if(reposistory_.endsWith("/")) {
87       reposistory_ = reposistory_.substring(0, reposistory_.length() - 1) ;
88     }
89     relativePathBaseDir_ = prefs.getValue("base-dir", "default") ;
90     if(relativePathBaseDir_ == null ||
91        relativePathBaseDir_.length() ==0 ||
92        relativePathBaseDir_.equals("default")) {
93       relativePathBaseDir_ = "" ;
94     }
95     if(relativePathBaseDir_.endsWith("/")) {
96       relativePathBaseDir_ = relativePathBaseDir_.substring(0, relativePathBaseDir_.length() - 1) ;
97     }
98     realPathBaseDir_ = reposistory_ + relativePathBaseDir_ ;
99     String JavaDoc readRole = prefs.getValue("read-role", "any") ;
100     String JavaDoc writeRole = prefs.getValue("write-role", "admin") ;
101     acl_ = new FileACL(user, readRole, writeRole) ;
102     }
103     
104   public String JavaDoc getRealPathBaseDir() { return realPathBaseDir_ ; }
105   
106   public String JavaDoc getRelativePathBaseDir() { return relativePathBaseDir_ ; }
107   
108   final public void changeNode(String JavaDoc uri) throws Exception JavaDoc {
109     setSelectNode(createNodeDescriptor(uri)) ;
110   }
111     
112   public NodeDescriptor createNodeDescriptor(String JavaDoc uri) {
113     String JavaDoc parentUri = getParentUri(uri) ;
114     String JavaDoc realPath = realPathBaseDir_ + uri ;
115     File JavaDoc file = new File JavaDoc(realPath) ;
116     NodeDescriptor node = new FileNodeDescriptor(file, uri, parentUri) ;
117     return node ;
118   }
119   
120     static public class ChangeNodeActionListener extends ExoActionListener {
121         public void execute(ExoActionEvent event) throws Exception JavaDoc {
122             UIFileExplorer uiExplorer = (UIFileExplorer) event.getComponent() ;
123             String JavaDoc uri = event.getParameter("uri") ;
124           if("../".equals(uri)) uri = uiExplorer.getSelectNode().getParentUri() ;
125             uiExplorer.changeNode(uri);
126         }
127     }
128     
129 }
Popular Tags