KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
17  * @email: tuan08@users.sourceforge.net
18  * @version: $Id: UIContentEditor.java,v 1.1 2004/07/16 09:51:34 oranheim Exp $
19  */

20 public class UIFileNodeEditor extends UISimpleForm implements ExplorerListener {
21     final static public String JavaDoc CREATE_DIR_ACTION = "createDir" ;
22     final static public String JavaDoc 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 JavaDoc {
61       UIFileNodeEditor uicomp = (UIFileNodeEditor) event.getComponent() ;
62       UIFileExplorer uiExplorer =
63         (UIFileExplorer) uicomp.getAncestorOfType(UIFileExplorer.class) ;
64       NodeDescriptor node = uiExplorer.getSelectNode() ;
65       String JavaDoc realPath = uiExplorer.getRealPathBaseDir() + node.getUri() + "/" + uicomp.dirInput.getValue() ;
66       File JavaDoc file = new File JavaDoc(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 JavaDoc {
80       UIFileNodeEditor uicomp = (UIFileNodeEditor) event.getComponent() ;
81       UIFileExplorer uiExplorer =
82         (UIFileExplorer) uicomp.getAncestorOfType(UIFileExplorer.class) ;
83       NodeDescriptor node = uiExplorer.getSelectNode() ;
84       String JavaDoc realPath = uiExplorer.getRealPathBaseDir() + node.getUri() + "/" + uicomp.fileInput.getValue() ;
85       File JavaDoc file = new File JavaDoc(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