KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.exoplatform.commons.utils.ExceptionUtil;
9 import org.exoplatform.commons.utils.IOUtil;
10 import org.exoplatform.faces.application.ExoFacesMessage;
11 import org.exoplatform.faces.core.component.InformationProvider;
12 import org.exoplatform.faces.core.event.ExoActionEvent;
13 import org.exoplatform.faces.core.event.ExoActionListener;
14 import org.exoplatform.portlets.content.explorer.component.model.FileNodeDescriptor;
15 import org.exoplatform.portlets.content.explorer.component.model.NodeDescriptor;
16
17 /**
18  * Jun 22, 2004
19  * @author: Tuan Nguyen
20  * @email: tuan08@users.sourceforge.net
21  * @version: $Id: UIFileContentEditor.java,v 1.3 2004/08/17 13:07:02 tuan08 Exp $
22  */

23 public class UIFileContentEditor extends UIContentEditor {
24   
25     public UIFileContentEditor() {
26         addActionListener(SaveContentActionListener.class, SAVE_CONTENT_ACTION) ;
27     }
28     
29     public void onChange(UIExplorer uiExplorer, NodeDescriptor node) {
30     String JavaDoc mimeType = node.getNodeType() ;
31     if (!mimeType.startsWith("text")) {
32       setRendered(false) ;
33       return ;
34     }
35     FileNodeDescriptor fnode = (FileNodeDescriptor) node ;
36     String JavaDoc content = null ;
37     if(fnode.getCacheContent() == null) {
38       UIFileExplorer uiFileExplorer = (UIFileExplorer)uiExplorer;
39       String JavaDoc path = uiFileExplorer.getRealPathBaseDir() + uiExplorer.getSelectNode().getUri() ;
40       try {
41         content = IOUtil.getFileContenntAsString(path) ;
42         fnode.setCacheContent(content) ;
43       } catch (Exception JavaDoc ex) {
44         content = ExceptionUtil.getExoStackTrace(ex) ;
45       }
46     } else {
47       content = fnode.getCacheContent();
48     }
49     setContent(content) ;
50     setRendered(true) ;
51     setContentType(mimeType) ;
52     setRendererType(CONTENT_FORM_RENDERER) ;
53   }
54     
55     static public class SaveContentActionListener extends ExoActionListener {
56       public void execute(ExoActionEvent event) throws Exception JavaDoc {
57         UIFileContentEditor uiEditor = (UIFileContentEditor) event.getComponent() ;
58       UIFileExplorer uiExplorer =
59         (UIFileExplorer) uiEditor.getAncestorOfType(UIFileExplorer.class) ;
60         InformationProvider iprovider = findInformationProvider(uiEditor) ;
61         String JavaDoc content = uiEditor.content_.getValue() ;
62       FileNodeDescriptor fnode =(FileNodeDescriptor) uiExplorer.getSelectNode() ;
63         String JavaDoc uri = fnode.getUri() ;
64         String JavaDoc realPath = uiExplorer.getRealPathBaseDir() + uri ;
65         FileOutputStream JavaDoc os= new FileOutputStream JavaDoc(realPath) ;
66         os.write(content.getBytes()) ;
67         os.close() ;
68       fnode.setCacheContent(content) ;
69         uiExplorer.broacastOnModify(fnode) ;
70         iprovider.addMessage(new ExoFacesMessage("#{UIFileContentEditor.msg.save-file-success}")) ;
71       }
72     }
73 }
Popular Tags