BrightSide Workbench Full Report + Source Code
FilesItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.elephant.configuration;
20 
21 import java.io.File;
22 import java.util.Arrays;
23 import java.util.Comparator;
24 import org.amic.util.file.FileUtil;
25 import org.turro.string.Strings;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.util.CompareUtil;
28 import org.zkoss.zul.Treecell;
29 import org.zkoss.zul.Treechildren;
30 import org.zkoss.zul.Treeitem;
31 import org.zkoss.zul.Treerow;
32 
37 public class FilesItem extends Treeitem {
38 
39  private final String label, pattern;
40  private boolean dynamicLoad;
41 
42  public FilesItem(File file, String label, String pattern) {
43  this(file, label, pattern, false);
44  }
45 
46  public FilesItem(File file, String label, String pattern, boolean dynamicLoad) {
47  this.label = Strings.isBlank(label) ? file.getName() : label;
48  this.pattern = pattern;
49  this.dynamicLoad = dynamicLoad;
50  setValue(file);
51  initiate();
52  }
53 
54  public boolean isDynamicLoad() {
55  return dynamicLoad;
56  }
57 
58  public void addFile(FilesItem file) {
59  if(!isContainer()) {
60  appendChild(new Treechildren());
61  }
62  getTreechildren().appendChild(file);
63  }
64 
65  @Override
66  public FilesTree getTree() {
67  return (FilesTree) super.getTree();
68  }
69 
70  public File getCurrentFile() {
71  return getValue();
72  }
73 
74  public void refresh() {
75  getTreechildren().getChildren().clear();
76  fillChildren();
77  }
78 
79  private void initiate() {
80  if(getCurrentFile() == null) {
81  addCells();
82  setOpen(false);
83  } else if(!getCurrentFile().exists() || getCurrentFile().isFile()) {
84  addCells();
85  } else if(getCurrentFile().isDirectory()) {
86  addCells();
87  fillChildren();
88  setOpen(false);
89  }
90  }
91 
92  private void fillChildren() {
93  File file = getCurrentFile();
94  File[] sortedFiles = file.listFiles();
95  Arrays.sort(sortedFiles, new Comparator<File>() {
96  @Override
97  public int compare(File o1, File o2) {
98  return CompareUtil.compare(o1.getName(), o2.getName());
99  }
100  });
101  if(getTreechildren() == null) {
102  Treechildren children = new Treechildren();
103  appendChild(children);
104  }
105  for(File f : sortedFiles) {
106  if(pattern == null || (f.isFile() && f.getName().matches(pattern))) {
107  getTreechildren().appendChild(new FilesItem(f, null, pattern, f.getAbsolutePath().contains("templates")));
108  }
109  }
110  }
111 
112  private void addCells() {
113  Treerow row = new Treerow();
114  appendChild(row);
115  Treecell cell = new Treecell(label);
116  row.appendChild(cell);
117  setImage(getMimeImage());
118  }
119 
120  private String getMimeImage() {
121  if(getValue() == null || !new File(ElephantContext.getRealPath("/_internal/system/mime/" + FileUtil.getExtension((File) getValue()) + ".png")).exists()) {
122  return "/_internal/system/mime/empty.png";
123  } else {
124  return "/_internal/system/mime/" + FileUtil.getExtension((File) getValue()) + ".png";
125  }
126  }
127 
128 }
FilesItem(File file, String label, String pattern)
Definition: FilesItem.java:42
FilesItem(File file, String label, String pattern, boolean dynamicLoad)
Definition: FilesItem.java:46