BrightSide Workbench Full Report + Source Code
elephant-zkoss/src/main/java/org/turro/zkoss/text/RepositoryItem.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.turro.elephant.context.ElephantContext;
23 import org.turro.elephant.impl.util.StringParser;
24 import org.turro.entities.Entities;
25 import org.turro.file.Document;
26 import org.turro.file.Folder;
27 import org.turro.path.Path;
28 import org.zkoss.lang.Strings;
29 import org.zkoss.zul.Treechildren;
30 import org.zkoss.zul.Treeitem;
31 
36 public class RepositoryItem extends Treeitem {
37 
38  private final File file;
39  private final String path;
40 
41  public RepositoryItem(File file, String name, String path) {
42  super(name);
43  this.file = file;
44  this.path = path;
45  if(doProcessors(file)) {
46  setOpen(false);
47  fillFolder();
48  }
49  }
50 
51  public File getRepositoryFile() {
52  return new File(file, path);
53  }
54 
55  public boolean isFolder(String folder) {
56  return ElephantContext.getRelativePath(file.getAbsolutePath()).equals(folder);
57  }
58 
59  private void fillFolder() {
60  if(!Strings.isBlank(path)) { // Add also repository subfolders
61  for(File child : getRepositoryFile().listFiles(new RepositoryFilter(""))) {
62  Treechildren children = getTreechildren();
63  if(children == null) {
64  children = new Treechildren();
65  appendChild(children);
66  }
67  children.appendChild(new RepositoryItem(child, child.getName(), ""));
68  }
69  }
70  for(File child : file.listFiles(new RepositoryFilter(path))) {
71  Treechildren children = getTreechildren();
72  if(children == null) {
73  children = new Treechildren();
74  appendChild(children);
75  }
76  children.appendChild(new RepositoryItem(child, child.getName(), path));
77  }
78  }
79 
80  private boolean doProcessors(File file) {
81  if(RepositoriesTree.isPublishable(file)) {
82  if(Folder.from(file).removeEmpties()) {
83  RepositoryItem.this.detach();
84  return false;
85  } else {
86  Path p = new Path(RepositoriesTree.getEntityPath(file));
87  if(p.getSize() > 0 && p.getSize() < 3) {
88  String str = Entities.getController(p.getPath()).getTreeLabel();
89  if(str != null) {
90  setLabel(StringParser.cutString(str, 50));
91  setTooltiptext(str);
92  } else if(p.getSize() == 2) {
93  Document.from(file).delete();
94  RepositoryItem.this.detach();
95  return false;
96  }
97  }
98  }
99  }
100  return true;
101  }
102 
103 }