18 package org.turro.file.zul.tree;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.file.FileUtil;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.elephant.context.Application;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.impl.util.StringParser;
32 import org.turro.elephant.util.Messages;
33 import org.turro.entities.Entities;
34 import org.turro.file.FileComparator;
35 import org.turro.file.FileWrapper;
36 import org.turro.i18n.I_;
37 import org.turro.zkoss.dialog.InputDialog;
38 import org.turro.zkoss.dialog.InputField;
39 import org.zkoss.zk.ui.event.DropEvent;
40 import org.zkoss.zk.ui.event.Event;
41 import org.zkoss.zk.ui.event.EventListener;
42 import org.zkoss.zk.ui.event.Events;
43 import org.zkoss.zk.ui.ext.AfterCompose;
44 import org.zkoss.zul.Treechildren;
45 import org.zkoss.zul.Treeitem;
46 import org.zkoss.zul.Treerow;
52 public class FileItem extends Treeitem implements AfterCompose {
57 private Treechildren children;
58 private String fileLabel;
66 this.fileLabel = label;
69 addEventListener(Events.ON_OPEN,
new EventListener<Event>() {
71 public void onEvent(Event event) throws Exception {
72 processSelection(false);
79 throw new UnsupportedOperationException(
"Do not use");
83 throw new UnsupportedOperationException(
"Do not use");
104 new InputField(
"Name",
"", null, 0)
107 public Object execute(Context context) {
109 if(fields.length > 0) {
111 if(
"Name".equals(f.getLabel())) {
112 File newFolder =
new File(getFile().getAbsolutePath() +
"/" + f.getValue());
113 if(newFolder.mkdirs()) {
124 public void doRename() throws InterruptedException {
125 if(getParentItem() ==
null)
return;
130 new InputField(
"Name", getFile().getName(), null, 0)
133 public Object execute(Context context) {
135 if(fields.length > 0) {
137 if(
"Name".equals(f.getLabel())) {
138 String path = FileUtil.getParentPath(getFile().getAbsolutePath());
140 if(getFile().renameTo(
new File(path +
"/" + f.getValue()))) {
141 FileItem.this.setValue(
new File(path +
"/" + f.getValue()));
142 FileItem.this.setLabel(getFile().getName());
159 if(children !=
null) {
160 children.getChildren().clear();
166 return (File) getValue();
176 path =
"/" + fi.
getFile().getName() + path;
183 if(getFile().isDirectory()) {
198 getTree().getActionMenu().open(
this);
203 File file = getValue();
204 if(!file.isDirectory()) {
206 getTree().getParent().appendChild(fp);
211 private String getMimeImage() {
212 if(!
new File(
ElephantContext.
getRealPath(
"/_internal/system/mime/" + FileUtil.getExtension((File) getValue()) +
".png")).exists()) {
213 return "/_internal/system/mime/empty.png";
215 return "/_internal/system/mime/" + FileUtil.getExtension((File) getValue()) +
".png";
219 private void addCells() {
222 row.setDraggable(
"true");
223 row.setDroppable(getFile().isDirectory() ?
"true" :
"false");
224 row.addEventListener(Events.ON_DROP,
new EventListener() {
226 public void onEvent(Event event)
throws Exception {
227 DropEvent dp = (DropEvent) event;
228 if(getFile().isDirectory() && dp.getDragged().getParent() instanceof FileItem) {
229 final FileItem dragged = (FileItem) dp.getDragged().getParent();
230 Messages.confirmMove().show(() -> {
232 FileWrapper fw =
new FileWrapper(dragged.getFile());
233 fw.copyTo(getFile());
237 }
catch (IOException ex) {
238 Logger.getLogger(FileItem.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
246 row.getChildren().clear();
248 for(FileColumn fc : getTree().getColumns()) {
249 row.appendChild(fc.getCell(
this));
253 private void addChildrenSpace() {
254 children =
new Treechildren();
255 appendChild(children);
258 private void initLoadOnDemand() {
259 if(((File) getValue()).isDirectory()) {
261 addEventListener(Events.ON_OPEN,
new EventListener() {
263 public void onEvent(Event event)
throws Exception {
271 addEventListener(Events.ON_CLICK,
new EventListener() {
273 public void onEvent(Event event)
throws Exception {
274 if(((File) getValue()).isDirectory()) {
277 getTree().getActionMenu().open(FileItem.this);
283 private void fillFolder() {
284 if(children.getChildren().isEmpty()) {
285 File file = (File) getValue();
289 if(file.isDirectory()) {
290 File[] sortedFiles = file.listFiles(getTree().getFileFilter());
292 Arrays.sort(sortedFiles,
new FileComparator(
new FileWrapper(file).getProperties()));
293 }
catch (FileNotFoundException ex) {
294 Logger.getLogger(FileItem.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
295 }
catch (IOException ex) {
296 Logger.getLogger(FileItem.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
298 for(File f : sortedFiles) {
299 FileItem fi =
new FileItem(f);
300 children.appendChild(fi);
302 fi.doProcessors(getPath());
303 fi.setContext(getTree().getActionMenu());
304 if(fi instanceof AfterCompose) {
305 ((AfterCompose) fi).afterCompose();
315 File file = (File) getValue();
317 setImage(getMimeImage());
318 }
else if(!file.exists() || file.isDirectory()) {
319 setImage(
"/_zul/images/folder.png");
324 private void doProcessors(String rootPath) {
static Application getApplication()
static String getRealPath(String path)
static String cutString(String value, int maxChars)
static IElephantEntity getController(String path)
void processSelection(boolean openItem)
FileItem(String label, File file)
static String get(String msg)