BrightSide Workbench Full Report + Source Code
FileEditor.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.zkoss.text;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.amic.util.file.FileUtil;
25 import org.turro.command.Command;
26 import org.turro.command.Context;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.i18n.I_;
29 import org.turro.menu.ElephantMenu;
30 import org.turro.zkoss.dialog.InputDialog;
31 import org.turro.zkoss.dialog.InputField;
32 import org.zkoss.lang.Strings;
33 import org.zkoss.zk.ui.ext.AfterCompose;
34 import org.zkoss.zul.Textbox;
35 
40 public class FileEditor extends Textbox implements AfterCompose {
41 
42  public static void editFile(File file) {
44  file, "fileToEdit",
45  "@Editor: " + file.getName(),
46  "/www/editor.zul");
47  }
48 
49  protected File file;
50  protected String encoding = null;
51 
52  public String getEncoding() {
53  return encoding;
54  }
55 
56  public void setEncoding(String encoding) {
57  this.encoding = encoding;
58  }
59 
60  public File getFile() {
61  return file;
62  }
63 
64  public void setFile(File file) {
65  this.file = file;
66  }
67 
68  public void save() {
69  try {
70  FileUtil.setContent(file, getText(), Strings.isBlank(encoding) ? ElephantContext.getEncoding() : encoding);
71  } catch (IOException ex) {
72  Logger.getLogger(FileEditor.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
73  }
74  }
75 
76  public void saveAs() throws IOException {
78  getPage(),
79  I_.get("Save as"),
80  new InputField[] {
81  new InputField("Name", getFile().getName(), null, 0)
82  }, new Command() {
83  @Override
84  public Object execute(Context context) {
85  InputField[] fields = (InputField[]) context.get("fields");
86  if(fields.length > 0) {
87  for(InputField f : fields) {
88  if("Name".equals(f.getLabel())) {
89  String path = FileUtil.getParentPath(file.getAbsolutePath());
90  if(path != null) {
91  file = new File(path + "/" + f.getValue());
92  save();
93  }
94  }
95  }
96  }
97  return null;
98  }
99  });
100  }
101 
102  @Override
103  public void afterCompose() {
104  try {
105  if(file != null) {
106  setValue(FileUtil.getContent(file, Strings.isBlank(encoding) ? ElephantContext.getEncoding() : encoding));
107  }
108  } catch (IOException ex) {
109  Logger.getLogger(FileEditor.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
110  }
111  }
112 
113 }
static void setContent(File file, String value)
Definition: FileUtil.java:274
static String getContent(File file)
Definition: FileUtil.java:245
static String get(String msg)
Definition: I_.java:41
static void showEntity(Object id, String attribute, String label, String include)
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static void editFile(File file)
Definition: FileEditor.java:42
void setEncoding(String encoding)
Definition: FileEditor.java:56