20 package net.sf.gridarta.textedit.scripteditor;
22 import java.awt.Frame;
23 import java.io.BufferedReader;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
30 import java.lang.ProcessBuilder.Redirect;
31 import java.nio.charset.Charset;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.prefs.Preferences;
35 import javax.swing.Action;
36 import javax.swing.JFileChooser;
37 import javax.swing.JOptionPane;
38 import javax.swing.filechooser.FileFilter;
45 import net.
sf.japi.swing.action.ActionBuilder;
46 import net.
sf.japi.swing.action.ActionBuilderFactory;
47 import net.
sf.japi.swing.action.ActionMethod;
48 import org.apache.log4j.Category;
49 import org.apache.log4j.Logger;
50 import org.jetbrains.annotations.NotNull;
51 import org.jetbrains.annotations.Nullable;
71 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
85 private final List<String>
tabs;
123 tabs =
new ArrayList<>();
156 final File absoluteFile = file.getAbsoluteFile();
157 if (!absoluteFile.exists() || !absoluteFile.isFile()) {
158 if (
LOG.isInfoEnabled()) {
159 LOG.info(
"Error in ScriptEditControl.openScriptFile():");
160 LOG.info(
" File '" + absoluteFile +
"' doesn't exist.");
167 tabs.add(absoluteFile.getPath());
172 command.add(file.toString());
173 final ProcessBuilder builder =
new ProcessBuilder(command);
174 builder.directory(absoluteFile.getParentFile());
175 builder.redirectError(Redirect.PIPE);
176 builder.redirectOutput(Redirect.PIPE);
177 builder.redirectInput(Redirect.PIPE);
178 final Process process = builder.start();
180 process.getOutputStream().close();
181 }
catch (
final IOException ignored) {
184 process.getInputStream().close();
185 }
catch (
final IOException ignored) {
187 final Runnable runnable = () -> {
188 final StringBuilder sb =
new StringBuilder();
190 try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(process.getErrorStream(), Charset.defaultCharset()))) {
191 final char[] tmp =
new char[1024];
193 if (reader.ready()) {
194 final int ch = reader.read();
195 if (ch == -1 || ch ==
'\n' || ch ==
'\r') {
199 sb.append((
char) ch);
203 }
catch (
final IOException ignored) {
207 if (process.waitFor() != 0) {
208 if (sb.length() == 0) {
209 sb.append(
"(no output)");
213 }
catch (
final InterruptedException ignored) {
214 Thread.currentThread().interrupt();
218 final Thread thread =
new Thread(runnable,
"failed editor checker");
219 thread.setDaemon(
true);
221 }
catch (
final IOException ex) {
235 final JFileChooser fileChooser =
new JFileChooser();
236 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
237 fileChooser.setMultiSelectionEnabled(
false);
241 if (defaultScriptDir.exists() && defaultScriptDir.isDirectory()) {
257 if (returnVal != JFileChooser.APPROVE_OPTION) {
262 if (file.exists() && !file.isDirectory()) {
283 if (activeTextArea !=
null && activeTextArea.
isModified() && !
ACTION_BUILDER.showQuestionDialog(
view,
"scriptEdit.confirmClose", title)) {
297 view.setVisible(
false);
324 if (activeTextArea ==
null) {
327 final String text = activeTextArea.
getText();
334 if (activePath !=
null) {
335 final File file =
new File(activePath);
337 if (file.getParentFile().exists() && file.getParentFile().isDirectory()) {
344 if (returnVal != JFileChooser.APPROVE_OPTION) {
350 final String fileName = file.getAbsolutePath();
355 if (!file.exists() || (activePath !=
null && file.getAbsolutePath().equals(activePath)) ||
view.
askConfirm(
"Overwrite?",
"A file named \"" + file.getName() +
"\" already exists.\n" +
"Are you sure you want to overwrite it?")) {
362 if (tabIndex >= 0 &&
tabs.size() > tabIndex) {
368 tabs.set(tabIndex, file.getAbsolutePath());
379 LOG.error(
"ScriptEditControl.saveActiveTab(): Cannot save file without name!");
398 private boolean saveTextToFile(@NotNull
final File file, @NotNull
final String text) {
400 try (FileOutputStream fos =
new FileOutputStream(file)) {
401 try (Writer osw =
new OutputStreamWriter(fos)) {
405 }
catch (
final IOException e) {
407 view.
showMessage(
"Write Error",
"The file \"" + file.getName() +
"\" could not be written.\nPlease use the 'Save As...' menu.", JOptionPane.ERROR_MESSAGE);
434 return path !=
null && !path.isEmpty() && !path.equals(
"<>") ? path :
null;
438 public void setAction(@NotNull
final Action action, @NotNull
final String
name) {