BrightSide Workbench Full Report + Source Code
CodemirrorEditor.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.zkoss.text;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.amic.util.file.FileUtil;
26 import org.turro.command.Command;
27 import org.turro.command.Context;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.i18n.I_;
30 import org.turro.zkoss.dialog.InputDialog;
31 import org.turro.zkoss.dialog.InputField;
32 import org.zkoss.codemirror.Codemirror;
33 import org.zkoss.lang.Strings;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 
40 public class CodemirrorEditor extends Codemirror implements AfterCompose {
41 
42  protected File file;
43  protected String encoding = null, forcedSyntax = null;
44 
45  public String getEncoding() {
46  return encoding;
47  }
48 
49  public void setEncoding(String encoding) {
50  this.encoding = encoding;
51  }
52 
53  public File getFile() {
54  return file;
55  }
56 
57  public void setFile(File file) {
58  this.file = file;
59  }
60 
61  public void setForcedSyntax(String forcedSyntax) {
62  this.forcedSyntax = forcedSyntax;
63  }
64 
65  public void setDisabled(boolean value) {
66  setReadonly(value);
67  }
68 
69  public void setText(String value) {
70  setValue(value);
71  }
72 
73  public void save() {
74  try {
75  if(!file.getParentFile().exists()) {
76  file.getParentFile().mkdirs();
77  }
78  if(FileUtil.getExtension(file).toLowerCase().equals("properties")) {
79  FileUtil.setPropertiesContent(file, getValue(), Strings.isBlank(encoding) ? ElephantContext.getEncoding() : encoding);
80  } else {
81  FileUtil.setContent(file, getValue(), Strings.isBlank(encoding) ? ElephantContext.getEncoding() : encoding);
82  }
83  } catch (IOException ex) {
84  Logger.getLogger(CodemirrorEditor.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
85  }
86  }
87 
88  public void saveAs(final Command command) throws IOException {
90  getPage(),
91  I_.get("Save as"),
92  new InputField[] {
93  new InputField("Name", getFile().getName(), null, 0),
94  new InputField("Override", Boolean.valueOf(Boolean.TRUE), null, 0)
95  }, new Command() {
96  @Override
97  public Object execute(Context context) {
98  InputField[] fields = (InputField[]) context.get("fields");
99  if(fields.length > 0) {
100  String name = null;
101  boolean override = false;
102  for(InputField f : fields) {
103  if("Name".equals(f.getLabel())) {
104  name = (String) f.getValue();
105  } else if("Override".equals(f.getLabel())) {
106  override = (Boolean) f.getValue();
107  }
108  }
109  if(!Strings.isBlank(name)) {
110  String path = FileUtil.getParentPath(file.getAbsolutePath());
111  if(path != null) {
112  if(override && !path.contains("/site")) {
113  path += "/site";
114  }
115  file = new File(path + "/" + name);
116  save();
117  context.put("file", file);
118  if(command != null) command.execute(context);
119  }
120  }
121  }
122  return null;
123  }
124  });
125  }
126 
127  public void newFile(final File folder, final Command command) throws IOException {
129  getPage(),
130  I_.get("New"),
131  new InputField[] {
132  new InputField("Name", "", null, 0)
133  }, new Command() {
134  @Override
135  public Object execute(Context context) {
136  InputField[] fields = (InputField[]) context.get("fields");
137  if(fields.length > 0) {
138  for(InputField f : fields) {
139  if("Name".equals(f.getLabel())) {
140  String path = folder.getAbsolutePath();
141  if(path != null) {
142  file = new File(path + "/" + f.getValue());
143  save();
144  context.put("file", file);
145  if(command != null) command.execute(context);
146  }
147  }
148  }
149  }
150  return null;
151  }
152  });
153  }
154 
155  @Override
156  public void afterCompose() {
157  try {
158  if(file != null) {
159  setSyntax(!Strings.isBlank(forcedSyntax) ? forcedSyntax : FileUtil.getExtension(file).toLowerCase());
160  if(FileUtil.getExtension(file).toLowerCase().equals("properties")) {
161  setValue(FileUtil.getPropertiesContent(file, Strings.isBlank(encoding) ? ElephantContext.getEncoding() : encoding));
162  } else {
163  setValue(FileUtil.getContent(file, Strings.isBlank(encoding) ? ElephantContext.getEncoding() : encoding));
164  }
165  }
166  } catch (IOException ex) {
167  Logger.getLogger(CodemirrorEditor.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
168  }
169  }
170 
171 }
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 getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
void setForcedSyntax(String forcedSyntax)
void saveAs(final Command command)
void newFile(final File folder, final Command command)