1 5 package org.exoplatform.portlets.content.explorer.component; 6 7 import java.io.File ; 8 import org.exoplatform.faces.application.ExoFacesMessage; 9 import org.exoplatform.faces.core.component.InformationProvider; 10 import org.exoplatform.faces.core.component.UISimpleForm; 11 import org.exoplatform.faces.core.component.UIStringInput; 12 import org.exoplatform.faces.core.component.model.*; 13 import org.exoplatform.faces.core.event.ExoActionEvent; 14 import org.exoplatform.faces.core.event.ExoActionListener; 15 import org.exoplatform.portlets.content.explorer.component.model.NodeDescriptor; 16 20 public class UIFileNodeEditor extends UISimpleForm implements ExplorerListener { 21 final static public String CREATE_DIR_ACTION = "createDir" ; 22 final static public String CREATE_FILE_ACTION = "createFile" ; 23 24 protected UIStringInput dirInput ; 25 protected UIStringInput fileInput ; 26 27 public UIFileNodeEditor() { 28 super("contentForm", "post", null) ; 29 setId("UIContentEditor") ; 30 setClazz("UIContentEditor") ; 31 32 dirInput = new UIStringInput("dir", "") ; 33 fileInput = new UIStringInput("file", "") ; 34 add(new Row(). 35 add(new LabelCell("#{UIFileNodeEditor.label.create-dir}")). 36 add(new ComponentCell(this, dirInput)). 37 add(new ListComponentCell(). 38 add(new FormButton("#{UIFileNodeEditor.button.create-dir}", CREATE_DIR_ACTION)))); 39 add(new Row(). 40 add(new LabelCell("#{UIFileNodeEditor.label.create-file}")). 41 add(new ComponentCell(this, fileInput)). 42 add(new ListComponentCell(). 43 add(new FormButton("#{UIFileNodeEditor.button.create-file}", CREATE_FILE_ACTION)))); 44 addActionListener(CreateDirectoryActionListener.class, CREATE_DIR_ACTION) ; 45 addActionListener(CreateFileActionListener.class, CREATE_FILE_ACTION) ; 46 } 47 48 public void onChange(UIExplorer uiExplorer, NodeDescriptor node) { 49 if(node.isLeafNode()) setRendered(false) ; 50 else setRendered(true) ; 51 } 52 53 public void onModify(UIExplorer uiExplorer, NodeDescriptor node) {} 54 55 public void onAddChild(UIExplorer uiExplorer, NodeDescriptor node) { } 56 57 public void onRemove(UIExplorer uiExplorer, NodeDescriptor node) { } 58 59 static public class CreateDirectoryActionListener extends ExoActionListener { 60 public void execute(ExoActionEvent event) throws Exception { 61 UIFileNodeEditor uicomp = (UIFileNodeEditor) event.getComponent() ; 62 UIFileExplorer uiExplorer = 63 (UIFileExplorer) uicomp.getAncestorOfType(UIFileExplorer.class) ; 64 NodeDescriptor node = uiExplorer.getSelectNode() ; 65 String realPath = uiExplorer.getRealPathBaseDir() + node.getUri() + "/" + uicomp.dirInput.getValue() ; 66 File file = new File (realPath) ; 67 InformationProvider iprovider = findInformationProvider(uiExplorer) ; 68 if(file.exists()) { 69 iprovider.addMessage(new ExoFacesMessage("#{UIFileNodeEditor.msg.name-exist}")) ; 70 } else { 71 file.mkdir() ; 72 uiExplorer.broadcastOnAddChild(node) ; 73 iprovider.addMessage(new ExoFacesMessage("#{UIFileNodeEditor.msg.create-dir-ok}")) ; 74 } 75 } 76 } 77 78 static public class CreateFileActionListener extends ExoActionListener { 79 public void execute(ExoActionEvent event) throws Exception { 80 UIFileNodeEditor uicomp = (UIFileNodeEditor) event.getComponent() ; 81 UIFileExplorer uiExplorer = 82 (UIFileExplorer) uicomp.getAncestorOfType(UIFileExplorer.class) ; 83 NodeDescriptor node = uiExplorer.getSelectNode() ; 84 String realPath = uiExplorer.getRealPathBaseDir() + node.getUri() + "/" + uicomp.fileInput.getValue() ; 85 File file = new File (realPath) ; 86 InformationProvider iprovider = findInformationProvider(uiExplorer) ; 87 if(file.exists()) { 88 iprovider.addMessage(new ExoFacesMessage("#{UIFileNodeEditor.msg.name-exist}")) ; 89 } else { 90 file.createNewFile() ; 91 uiExplorer.broadcastOnAddChild(node) ; 92 iprovider.addMessage(new ExoFacesMessage("#{UIFileNodeEditor.msg.create-file-ok}")) ; 93 } 94 } 95 } 96 } | Popular Tags |