1 5 package org.exoplatform.portlets.content.explorer.component; 6 7 import java.io.FileOutputStream ; 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 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 mimeType = node.getNodeType() ; 31 if (!mimeType.startsWith("text")) { 32 setRendered(false) ; 33 return ; 34 } 35 FileNodeDescriptor fnode = (FileNodeDescriptor) node ; 36 String content = null ; 37 if(fnode.getCacheContent() == null) { 38 UIFileExplorer uiFileExplorer = (UIFileExplorer)uiExplorer; 39 String path = uiFileExplorer.getRealPathBaseDir() + uiExplorer.getSelectNode().getUri() ; 40 try { 41 content = IOUtil.getFileContenntAsString(path) ; 42 fnode.setCacheContent(content) ; 43 } catch (Exception 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 { 57 UIFileContentEditor uiEditor = (UIFileContentEditor) event.getComponent() ; 58 UIFileExplorer uiExplorer = 59 (UIFileExplorer) uiEditor.getAncestorOfType(UIFileExplorer.class) ; 60 InformationProvider iprovider = findInformationProvider(uiEditor) ; 61 String content = uiEditor.content_.getValue() ; 62 FileNodeDescriptor fnode =(FileNodeDescriptor) uiExplorer.getSelectNode() ; 63 String uri = fnode.getUri() ; 64 String realPath = uiExplorer.getRealPathBaseDir() + uri ; 65 FileOutputStream os= new FileOutputStream (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 |