BrightSide Workbench Full Report + Source Code
FilesComposer.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.io.IOException;
23 import java.nio.charset.StandardCharsets;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.command.Command;
27 import org.turro.command.Context;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.util.Messages;
30 import org.turro.file.Document;
31 import org.turro.file.Folder;
32 import org.turro.upload.Medias;
33 import org.turro.zip.Compress;
34 import org.turro.zkoss.text.CodemirrorEditor;
35 import org.zkoss.util.media.Media;
36 import org.zkoss.zk.ui.Component;
37 import org.zkoss.zk.ui.event.Event;
38 import org.zkoss.zk.ui.event.UploadEvent;
39 import org.zkoss.zk.ui.select.SelectorComposer;
40 import org.zkoss.zk.ui.select.annotation.Listen;
41 import org.zkoss.zk.ui.select.annotation.Wire;
42 import org.zkoss.zul.Label;
43 import org.zkoss.zul.Panel;
44 import org.zkoss.zul.Toolbarbutton;
45 
50 public class FilesComposer extends SelectorComposer<Component> {
51 
52  @Wire("#fileName")
53  private Label fileName;
54 
55  @Wire("#save")
56  private Toolbarbutton save;
57 
58  @Wire("#saveAs")
59  private Toolbarbutton saveAs;
60 
61  @Wire("#filesTree")
62  private FilesTree filesTree;
63 
64  @Wire("#fileContent")
65  private Panel fileContent;
66 
67  @Wire("#editor")
68  private CodemirrorEditor editor;
69 
70  @Listen("onSelect = #filesTree")
71  public void onSelectFile(Event event) {
72  save.setDisabled(true);
73  saveAs.setDisabled(true);
74  File file = filesTree.getSelectedFile().getCurrentFile();
75  if(file != null && (file.isFile() || !file.exists())) {
76  fileName.setValue(file.getName());
77  editor.setEncoding(guessEncoding(file));
78  editor.setFile(file);
79  editor.afterCompose();
80  editor.setDisabled(false);
81  } else {
82  fileName.setValue(null);
83  editor.setText("");
84  editor.setEncoding(null);
85  editor.setDisabled(true);
86  filesTree.getSelectedItem().setOpen(true);
87  }
88  }
89 
90  @Listen("onClick = #save")
91  public void onSave(Event event) {
92  editor.save();
93  }
94 
95  @Listen("onClick = #saveAs")
96  public void onSaveAs(Event event) {
97  try {
98  editor.saveAs(new Command() {
99  @Override
100  public Object execute(Context context) {
101  save.setDisabled(false);
102  saveAs.setDisabled(false);
103  FilesItem selectedParent = filesTree.getSelectedFileParent();
104  selectedParent.refresh();
105  filesTree.selectFile((File) context.get("file"), selectedParent);
106  return null;
107  }
108  });
109  } catch (IOException ex) {
110  Logger.getLogger(FilesComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
111  }
112  }
113 
114  @Listen("onClick = #newFile")
115  public void onNewFile(Event event) {
116  try {
117  final FilesItem folder = filesTree.getSelectedFolder();
118  if(folder != null) {
119  editor.newFile(folder.getCurrentFile(), new Command() {
120  @Override
121  public Object execute(Context context) {
122  save.setDisabled(false);
123  saveAs.setDisabled(false);
124  folder.refresh();
125  filesTree.selectFile((File) context.get("file"), folder);
126  return null;
127  }
128  });
129  }
130  } catch (IOException ex) {
131  Logger.getLogger(FilesComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
132  }
133  }
134 
135  @Listen("onClick = #delFile")
136  public void onDelFile(Event event) {
137  final FilesItem folder = filesTree.getSelectedFileParent();
138  final FilesItem file = filesTree.getSelectedFile();
139  Messages.confirmDeletion().add(file.getLabel()).show(() -> {
140  if(Document.from(file.getCurrentFile()).delete()) {
141  folder.refresh();
142  filesTree.setSelectedItem(folder);
143  }
144  });
145  }
146 
147  @Listen("onUpload = #fomantic")
148  public void onFomantic(UploadEvent event) throws Exception {
149  Folder fomantic = Folder.from(ElephantContext.getRealPath("/_internal/js/semantic-ui"));
150  fomantic.ensure();
151  for(Media media : event.getMedias()) {
152  Medias.toFolder(media, fomantic.folder());
153  Compress.unzip(fomantic.path().resolve(media.getName()), fomantic.path());
154  Document.from(fomantic.path().resolve(media.getName())).delete();
155  }
156  }
157 
158  @Listen("onChange = *; onChanging = *")
159  public void onChange(Event event) {
160  save.setDisabled(false);
161  if(filesTree.getSelectedFile() != null &&
162  filesTree.getSelectedFile().isDynamicLoad()) {
163  saveAs.setDisabled(false);
164  }
165  }
166 
167  @Override
168  public void doFinally() throws Exception {
169  save.setDisabled(true);
170  save.setAutodisable("+self,+saveAs");
171  saveAs.setDisabled(true);
172  saveAs.setAutodisable("+self,+save");
173  }
174 
175  private String guessEncoding(File file) {
176  if(file.getAbsolutePath().contains("/elephant/conf/mail/")) {
177  return StandardCharsets.UTF_8.name();
178  }
179  return null;
180  }
181 
182 }
void selectFile(File file, Treeitem parent)
Definition: FilesTree.java:51
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50
void saveAs(final Command command)
void newFile(final File folder, final Command command)