BrightSide Workbench Full Report + Source Code
RepositoriesTree.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.zkoss.text;
20 
21 import java.io.File;
22 import org.amic.util.file.FileUtil;
23 import org.turro.string.Strings;
24 import org.turro.command.Command;
25 import org.turro.command.Context;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.util.Messages;
28 import org.turro.i18n.I_;
29 import org.turro.zkoss.dialog.InputDialog;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.Events;
32 import org.zkoss.zul.Tree;
33 import org.zkoss.zul.Treechildren;
34 import org.zkoss.zul.Treeitem;
35 
40 public class RepositoriesTree extends Tree {
41 
42  public static final String
43  REPOSITORY_DIR = "/_internal/repository",
44  REPOSITORY_PUBLICATION = "/_internal/UserFiles",
45  REPOSITORY_PUBLISHABLE = "/_internal/files";
46 
47  public RepositoriesTree() {
48  addFolder(I_.get("Root"), new File(ElephantContext.getRealPath("/")), REPOSITORY_DIR);
49  addFolder(I_.get("Publication"), new File(ElephantContext.getRealPath(REPOSITORY_PUBLICATION)), "");
50  addFolder(I_.get("Publishable"), new File(ElephantContext.getRealPath(REPOSITORY_PUBLISHABLE)), "");
51  }
52 
53  public File getSelectedRepository() {
54  return ((RepositoryItem) getSelectedItem()).getRepositoryFile();
55  }
56 
57  public void createFolder() {
58  InputDialog.getInput(getPage(), I_.get("Folder"), "Name", "",
59  null, 0, new Command() {
60  @Override
61  public Object execute(Context context) {
62  String name = (String) context.get("value");
63  if(!Strings.isBlank(name)) {
64  File folder = getSelectedRepository();
65  if(folder != null && folder.isDirectory()) {
66  folder = new File(folder, name);
67  folder.mkdir();
68  addFolderOnSelected(folder.getName(), folder, "");
69  }
70  }
71  return null;
72  }
73  });
74  }
75 
76  private void addFolder(String name, File file, String path) {
77  Treechildren children = getTreechildren();
78  if(children == null) {
79  children = new Treechildren();
80  appendChild(children);
81  }
82  children.appendChild(new RepositoryItem(file, name, path));
83  }
84 
85  private void addFolderOnSelected(String name, File file, String path) {
86  Treeitem ti = getSelectedItem();
87  if(ti != null) {
88  Treechildren children = ti.getTreechildren();
89  if(children == null) {
90  children = new Treechildren();
91  ti.appendChild(children);
92  }
93  children.appendChild(new RepositoryItem(file, name, path));
94  }
95  }
96 
97  public void selectFolder(String folder) {
98  for(Treeitem ti : getItems()) {
100  if(ri.isFolder(folder)) {
101  setSelectedItem(ti);
102  while(ti != null) {
103  ti.setOpen(true);
104  ti = ti.getParentItem();
105  }
106  Events.postEvent(new Event(Events.ON_SELECT, RepositoriesTree.this));
107  break;
108  }
109  }
110  }
111 
112  public void deleteFolder() {
113  final File folder = getSelectedRepository();
114  if(folder != null) {
115  Messages.confirmDeletion().add(folder.getName()).show(() -> {
116  FileUtil.deleteFile(folder);
117  });
118  }
119  }
120 
121  public static boolean isPublishable(File file) {
122  return file.getAbsolutePath().startsWith(ElephantContext.getRealPath(REPOSITORY_PUBLISHABLE));
123  }
124 
125  public static String getEntityPath(File file) {
126  String path = file.getAbsolutePath();
127  return path.substring(path.indexOf(REPOSITORY_PUBLISHABLE) + REPOSITORY_PUBLISHABLE.length());
128  }
129 
130 }
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
static String get(String msg)
Definition: I_.java:41
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static boolean isPublishable(File file)