KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import org.exoplatform.faces.application.ExoFacesMessage;
10 import org.exoplatform.faces.core.component.InformationProvider;
11 import org.exoplatform.faces.core.event.ExoActionEvent;
12 import org.exoplatform.faces.core.event.ExoActionListener;
13 import org.exoplatform.portlets.content.explorer.component.model.NodeDescriptor;
14 /**
15  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
16  * @since Jan 20, 2005
17  * @version $Id$
18  */

19 public class UIFileUpload extends org.exoplatform.faces.core.component.UIFileUpload
20   implements ExplorerListener {
21   
22   public UIFileUpload() {
23     setShowCancel(false) ;
24     setShowHeader(false) ;
25   }
26   
27   public void addFileSystemActionListener() {
28     addActionListener(FileSystemUploadActionListener.class, UPLOAD_ACTION) ;
29   }
30   
31   public void onChange(UIExplorer uiExplorer, NodeDescriptor node) {
32     if(node.isLeafNode()) setRendered(false) ;
33     else setRendered(true) ;
34   }
35   
36   public void onModify(UIExplorer uiExplorer, NodeDescriptor node) {}
37   
38   public void onAddChild(UIExplorer uiExplorer, NodeDescriptor node) { }
39   
40   public void onRemove(UIExplorer uiExplorer, NodeDescriptor node) { }
41   
42   
43   static public class FileSystemUploadActionListener extends ExoActionListener {
44     public void execute(ExoActionEvent event) throws Exception JavaDoc {
45       UIFileUpload uiFileUpload = (UIFileUpload) event.getComponent() ;
46       UIFileExplorer uiExplorer =
47         (UIFileExplorer) uiFileUpload.getAncestorOfType(UIFileExplorer.class) ;
48       NodeDescriptor node = uiExplorer.getSelectNode() ;
49       InformationProvider iprovider = findInformationProvider(uiFileUpload) ;
50       String JavaDoc dir = uiExplorer.getRealPathBaseDir() + node.getUri() ;
51       Iterator JavaDoc i = uiFileUpload.getUserInputs().iterator() ;
52       while(i.hasNext()) {
53         UIFileUpload.UserInput input = (UIFileUpload.UserInput) i.next() ;
54         String JavaDoc fileName = input.getName() ;
55         if (fileName == null || fileName.length() == 0) fileName = input.getFileName() ;
56         String JavaDoc realPath = dir + "/" + fileName ;
57         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(realPath) ;
58         os.write(input.getStream()) ;
59         os.close() ;
60       }
61       uiExplorer.broadcastOnAddChild(node) ;
62       iprovider.addMessage(new ExoFacesMessage("#{UIFilePortlet.msg.file-upload-success}")) ;
63     }
64   }
65 }
66
Popular Tags