BrightSide Workbench Full Report + Source Code
RepositoryContent.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 java.io.FileFilter;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.command.Command;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.file.Folder;
28 import org.turro.upload.Medias;
29 import org.zkoss.util.media.Media;
30 import org.zkoss.zk.ui.HtmlBasedComponent;
31 import org.zkoss.zk.ui.event.Event;
32 import org.zkoss.zk.ui.event.EventListener;
33 import org.zkoss.zk.ui.event.Events;
34 import org.zkoss.zk.ui.event.UploadEvent;
35 import org.zkoss.zul.Div;
36 import org.zkoss.zul.Fileupload;
37 import org.zkoss.zul.Image;
38 import org.zkoss.zul.Label;
39 
44 public class RepositoryContent extends Div {
45 
46  public static final String
47  IMAGE_FILTER = ".*(\\.jpg|\\.jpeg|\\.png|\\.gif)",
48  FILE_FILTER = ".*(\\.pdf|\\.doc|\\.xls|\\.ppt|\\.od[a-z]|\\.txt|\\.json)";
49 
50  private File folder;
51  private String matchFilter = ".*";
52 
53  public File getFolder() {
54  return folder;
55  }
56 
57  public void setFolder(File folder) {
58  this.folder = folder;
59  Folder.from(folder).ensure();
60  }
61 
62  public String getMatchFilter() {
63  return matchFilter;
64  }
65 
66  public void setMatchFilter(String matchFilter) {
67  this.matchFilter = matchFilter;
68  }
69 
70  public void showContent() {
71  if(folder != null && folder.exists()) {
72  getChildren().clear();
73  for(File file : folder.listFiles(new FileFilter() {
74  @Override
75  public boolean accept(File pathname) {
76  return pathname.isFile() && !pathname.isHidden() && pathname.getName().matches(matchFilter);
77  }
78  })) {
79  appendChild(createBox(file));
80  }
81  }
82  }
83 
84  public void upload(final Command command) {
85  if(!folder.exists()) {
86  folder.mkdirs();
87  }
88  Fileupload.get(100, new EventListener<UploadEvent>() {
89  @Override
90  public void onEvent(UploadEvent event) {
91  addMedias(event.getMedias());
92  if(command != null) {
93  try {
94  command.execute(null);
95  } catch (Exception ex) {
96  Logger.getLogger(RepositoryContent.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
97  }
98  }
99  }
100  });
101  }
102 
103 
104  public void addMedias(Media[] medias) {
105  if(medias == null || medias.length == 0) return;
106  for(Media media : medias) {
107  Medias.toFolder(media, folder);
108  }
109  }
110 
111  private HtmlBasedComponent createBox(final File file) {
112  Div box = new Div();
113  if(file.getName().matches(IMAGE_FILTER)) {
114  Image img = new Image(ElephantContext.getRelativePath(file.getAbsolutePath()));
115  box.setSclass("rc_image");
116  box.appendChild(img);
117  } else {
118  Label label = new Label(file.getName());
119  box.setSclass("rc_file");
120  box.appendChild(label);
121  }
122  box.addEventListener(Events.ON_DOUBLE_CLICK, new EventListener<Event>() {
123  @Override
124  public void onEvent(Event event) throws Exception {
125  Events.postEvent(new Event(Events.ON_SELECT, RepositoryContent.this, file));
126  }
127  });
128  return box;
129  }
130 
131 }
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50