19 package org.turro.file.zul.repository;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.file.FileUtil;
27 import org.apache.commons.io.FileUtils;
28 import org.turro.command.Command;
29 import org.turro.command.Context;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.util.Messages;
33 import org.turro.file.FileWrapper;
34 import org.turro.file.action.FileAction;
35 import org.turro.file.zul.FileClipboard;
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.Component;
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.zul.A;
44 import org.zkoss.zul.Detail;
45 import org.zkoss.zul.Hlayout;
46 import org.zkoss.zul.Label;
47 import org.zkoss.zul.Row;
48 import org.zkoss.zul.Space;
49 import org.zkoss.zul.Toolbarbutton;
50 import org.zkoss.zul.Vbox;
63 setStyle(
"cursor:pointer");
65 A a =
new A(folder.getLabel(),
"/_zul/images/folder.png");
66 a.addEventListener(Events.ON_CLICK,
new EventListener<Event>() {
68 public void onEvent(Event event) throws Exception {
69 folder.getTree().setSelectedItem(folder);
70 Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
74 appendChild(getSizeComponent());
75 appendChild(
new Label(
""));
78 private boolean _listenerAttached =
false;
79 private boolean _onOpenTriggered =
false;
81 private void addDetail() {
82 final Detail detail =
new Detail();
85 detail.addEventListener(Events.ON_OPEN,
new EventListener() {
87 public void onEvent(Event event)
throws Exception {
88 _onOpenTriggered =
true;
93 if(!_listenerAttached) {
94 addEventListener(Events.ON_CLICK,
new EventListener<Event>() {
96 public void onEvent(Event event) throws Exception {
97 if(_onOpenTriggered) {
98 _onOpenTriggered = false;
101 folder.getTree().setSelectedItem(folder);
102 Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
105 _listenerAttached =
true;
109 private void fillDetail(Detail detail) {
110 if(detail.getChildren().isEmpty()) {
111 Application app = Application.getApplication();
113 Vbox vbox =
new Vbox();
114 vbox.setSclass(
"detailBox");
115 vbox.setStyle(
"background-image:url('" +
116 ElephantContext.getCurrent().getContextPath() +
117 "/_zul/images/bg/sunken.png')");
118 detail.appendChild(vbox);
120 Hlayout buttons =
new Hlayout();
121 vbox.appendChild(buttons);
122 if(app.isInRole(
"file-attach:edit")) {
123 Toolbarbutton edit =
new Toolbarbutton();
124 edit.setImage(
"/_zul/images/properties.png");
125 edit.setTooltiptext(I_.get(
"Properties"));
126 edit.addEventListener(Events.ON_CLICK,
new EventListener() {
128 public void onEvent(Event event)
throws Exception {
129 InputDialog.getInput(
133 new InputField(
"Name", folder.getRealFolder().getName(), null, 0)
136 public Object execute(Context context) {
137 InputField[] fields = (InputField[]) context.get(
"fields");
138 if(fields.length > 0) {
139 for(InputField f : fields) {
140 if(
"Name".equals(f.getLabel())) {
142 Events.postEvent(
new Event(Events.ON_CHANGE, folder.
getTree()));
151 buttons.appendChild(edit);
152 buttons.appendChild(
new Space());
154 Toolbarbutton copy =
new Toolbarbutton(I_.get(
"Copy"),
"/_zul/images/edit-copy.png");
155 copy.addEventListener(Events.ON_CLICK,
new EventListener() {
157 public void onEvent(Event event)
throws Exception {
161 buttons.appendChild(copy);
162 Toolbarbutton cut =
new Toolbarbutton(I_.get(
"Cut"),
"/_zul/images/edit-cut.png");
163 cut.addEventListener(Events.ON_CLICK,
new EventListener() {
165 public void onEvent(Event event)
throws Exception {
166 FileClipboard.cutFolder(folder.getLabel(), folder.
getRealFolder());
169 buttons.appendChild(cut);
170 final Toolbarbutton paste =
new Toolbarbutton(I_.get(
"Paste"),
"/_zul/images/edit-paste.png");
171 paste.setDisabled(!FileClipboard.hasClipboard());
172 paste.addEventListener(Events.ON_CLICK,
new EventListener() {
174 public void onEvent(Event event)
throws Exception {
176 paste.setDisabled(
true);
179 buttons.appendChild(paste);
180 if(app.isInRole(
"file-attach:delete")) {
181 Toolbarbutton
delete =
new Toolbarbutton(I_.get(
"Delete"),
"/_zul/images/edit-delete.png");
182 delete.addEventListener(Events.ON_CLICK,
new EventListener() {
184 public void onEvent(Event event)
throws Exception {
185 Messages.confirmDeletion().show(() -> {
188 FolderRow.this.detach();
189 Events.postEvent(
new Event(Events.ON_CHANGE, folder.
getTree()));
190 }
catch (IOException ex) {
191 Logger.getLogger(FolderRow.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
196 buttons.appendChild(
delete);
198 buttons.appendChild(
new Space());
199 FileWrapper wrapper = FileWrapper.getFileByType(folder.
getRealFolder());
200 Toolbarbutton gallery = doButton(wrapper.getAction(I_.get(
"Create a gallery")));
201 if(gallery !=
null) buttons.appendChild(gallery);
202 Toolbarbutton zip = doButton(wrapper.getAction(I_.get(
"Zip")));
203 if(zip !=
null) buttons.appendChild(zip);
207 private Toolbarbutton doButton(
final FileAction action) {
209 Toolbarbutton tb =
new Toolbarbutton(action.getLabel(), action.getImage());
210 tb.addEventListener(Events.ON_CLICK,
new EventListener<Event>() {
212 public void onEvent(Event event) throws Exception {
221 private Component getSizeComponent() {
222 String size =
new org.turro.formatter.BytesFormatter(sizeOfDirectory(folder.
getRealFolder())).toString();
223 return new Label(size);
226 private long sizeOfDirectory(File directory) {
227 final Collection<File> files = folder.
getFilesFrom(directory);
229 for(
final File file : files) {
230 if (!FileUtils.isSymlink(file)) {
231 size += sizeOf(file);
240 private long sizeOf(File file) {
241 if(file.isDirectory()) {
242 return sizeOfDirectory(file);
244 return file.length();
FolderRow(final RepositoryItem folder)
Collection< File > getFilesFrom(File folder)