BrightSide Workbench Full Report + Source Code
RepositoryWalker.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.util.HashMap;
23 import org.turro.string.Strings;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.elephant.util.Components;
26 import org.turro.i18n.I_;
27 import org.turro.zkoss.dialog.SelectionDialog;
28 import org.zkoss.zk.ui.Executions;
29 import org.zkoss.zk.ui.IdSpace;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.Events;
32 import org.zkoss.zk.ui.event.UploadEvent;
33 import org.zkoss.zk.ui.select.Selectors;
34 import org.zkoss.zk.ui.select.annotation.Listen;
35 import org.zkoss.zk.ui.select.annotation.Wire;
36 import org.zkoss.zkmax.zul.Dropupload;
37 import org.zkoss.zul.Div;
38 import org.zkoss.zul.Hlayout;
39 import org.zkoss.zul.Toolbarbutton;
40 import org.zkoss.zul.West;
41 
46 public class RepositoryWalker extends Div implements IdSpace {
47 
48  private final WikiElement wikiElement;
49  private File result;
50  private boolean readOnlyRepository = false, noTreeRepository = false;
51  private String initialFolder;
52 
53  @Wire("#repositoriesTree") private RepositoriesTree repositoriesTree;
54  @Wire("#repositoryContent") private RepositoryContent repositoryContent;
55  @Wire("#treePanel") private West treePanel;
56  @Wire("#upload") private Toolbarbutton upload;
57  @Wire("#dropUpload") private Dropupload dropUpload;
58  @Wire("#buttons") private Hlayout buttons;
59 
60  @Listen("onClick = #newFolder")
61  public void onNewFolder() {
62  if(!noTreeRepository && !readOnlyRepository) {
63  repositoriesTree.createFolder();
64  }
65  }
66 
67  @Listen("onClick = #delFolder")
68  public void onDelFolder() {
69  if(!noTreeRepository && !readOnlyRepository) {
70  repositoriesTree.deleteFolder();
71  }
72  }
73 
74  @Listen("onUpload = #upload")
75  public void onUpload(UploadEvent event) {
76  repositoryContent.addMedias(event.getMedias());
77  repositoryContent.showContent();
78 // repositoryContent.upload(new Command() {
79 // @Override
80 // public boolean execute(Context context) throws Exception {
81 // repositoryContent.showContent();
82 // return Command.PROCESSING_COMPLETE;
83 // }
84 // });
85  }
86 
87  @Listen("onUpload = #dropUpload")
88  public void onDropUpload(UploadEvent event) {
89  repositoryContent.addMedias(event.getMedias());
90  repositoryContent.showContent();
91  }
92 
93  @Listen("onSelect = #repositoriesTree")
94  public void onRepository() {
95  repositoryContent.setFolder(repositoriesTree.getSelectedRepository());
96  repositoryContent.showContent();
97  }
98 
99  @Listen("onSelect = #repositoryContent")
100  public void onSelectFile(Event event) {
101  result = (File) event.getData();
102  Events.postEvent(new Event(Events.ON_CLOSE, Components.from(this).parent(SelectionDialog.class)));
103  }
104 
105  public RepositoryWalker(WikiElement wikiElement) {
106  HashMap args = new HashMap();
107  args.put("el_label", I_.compatibilityMap());
108  args.put("i_", I_.api());
109  args.put("i", I_.map());
110  args.put("k", I_.byKeyMap());
111  Executions.createComponents("/WEB-INF/_zul/bs/comps/editor/repositoryWalker.zul", this, args);
112  Selectors.wireComponents(this, this, false);
113  Selectors.wireEventListeners(this, this);
114  this.wikiElement = wikiElement;
115  }
116 
117  public void selectFolder(String folder) {
118  if(Strings.isBlank(initialFolder)) {
119  initialFolder = folder;
120  }
121  repositoriesTree.selectFolder(folder);
122  repositoryContent.setFolder(new File(ElephantContext.getRealPath(folder)));
123  repositoryContent.showContent();
124  }
125 
126  public void setFilter(String filter) {
127  repositoryContent.setMatchFilter(filter);
128  }
129 
130  public File getResult() {
131  return result;
132  }
133 
134  public boolean renderResult() {
135  return wikiElement.renderImage(result);
136  }
137 
138  public boolean isReadOnlyRepository() {
139  return readOnlyRepository;
140  }
141 
142  public void setReadOnlyRepository(boolean readOnlyRepository) {
143  this.readOnlyRepository = readOnlyRepository;
144  upload.setDisabled(readOnlyRepository);
145  if(readOnlyRepository) {
146  dropUpload.setDetection("none");
147  dropUpload.setVisible(false);
148  }
149  }
150 
151  public boolean isNoTreeRepository() {
152  return noTreeRepository;
153  }
154 
155  public void setNoTreeRepository(boolean noTreeRepository) {
156  this.noTreeRepository = noTreeRepository;
157  treePanel.setVisible(!noTreeRepository);
158  }
159 
160  private boolean isRoot(File repository) {
161  return repository != null ? repository.getAbsolutePath().endsWith(initialFolder) : true;
162  }
163 
164 }
static Components from(Object component)
Definition: Components.java:46
static I18nByKey byKeyMap()
Definition: I_.java:91
static I18nApiWrapper api()
Definition: I_.java:65
static I18nMapWrapper map()
Definition: I_.java:69
static I18nCompatibilityMap compatibilityMap()
Definition: I_.java:117
void setNoTreeRepository(boolean noTreeRepository)
RepositoryWalker(WikiElement wikiElement)
void setReadOnlyRepository(boolean readOnlyRepository)
boolean renderImage(File image)