 |
Gridarta Editor
|
Go to the documentation of this file.
20 package net.sf.gridarta.utils;
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.Component;
26 import java.awt.Frame;
27 import java.awt.Window;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Queue;
32 import java.util.concurrent.ConcurrentLinkedQueue;
33 import javax.swing.Action;
34 import javax.swing.JDialog;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.JTextArea;
38 import javax.swing.JToolBar;
39 import javax.swing.SwingUtilities;
40 import net.
sf.japi.swing.action.ActionBuilder;
41 import net.
sf.japi.swing.action.ActionBuilderFactory;
42 import net.
sf.japi.swing.action.ActionMethod;
43 import org.apache.log4j.Category;
44 import org.apache.log4j.Logger;
45 import org.jetbrains.annotations.NotNull;
46 import org.jetbrains.annotations.Nullable;
69 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
83 private final String
key;
97 private final File
dir;
110 private final JTextArea
stdtxt =
new JTextArea(25, 80);
144 private final Object
lock =
new Object();
155 this.command =
command.clone();
156 this.dir =
dir ==
null ? null :
new File(
dir);
157 setLayout(
new BorderLayout());
158 stdtxt.setFont(
new Font(
"monospaced", Font.PLAIN,
stdtxt.getFont().getSize()));
159 stdtxt.setEditable(
false);
160 stdtxt.setFocusable(
false);
162 stdtxt.setBackground(Color.BLACK);
163 stdtxt.setForeground(Color.WHITE);
164 final Component scrollPane =
new JScrollPane(
stdtxt);
165 scrollPane.setFocusable(
true);
166 add(scrollPane, BorderLayout.CENTER);
167 final JToolBar toolBar =
new JToolBar();
170 toolBar.add(
ACTION_BUILDER.createAction(
false,
"controlClear",
this));
173 add(toolBar, BorderLayout.SOUTH);
209 dialog.setLocationRelativeTo(parent);
217 this.command =
command.clone();
225 synchronized (
lock) {
229 process.getInputStream().close();
230 }
catch (
final IOException ignored) {
234 process.getErrorStream().close();
235 }
catch (
final IOException ignored) {
239 process.getOutputStream().close();
240 }
catch (
final IOException ignored) {
244 }
catch (
final IllegalThreadStateException ignored) {
245 LOG.error(
"Still running!");
252 process =
new ProcessBuilder(
command).directory(
dir).redirectErrorStream(
true).start();
253 final InputStream out =
process.getInputStream();
254 final InputStream err =
process.getErrorStream();
261 }
catch (
final IOException e) {
296 private InputStream
in;
324 final byte[] buf =
new byte[4096];
327 final int bytesRead =
in.read(buf);
328 if (bytesRead == -1) {
340 }
catch (
final IOException e) {
351 private void start(@NotNull
final InputStream stream) {
353 if (
LOG.isInfoEnabled()) {
354 LOG.info(
"Trying to stop previous stream.");
359 }
catch (
final IOException ignored) {
362 if (
LOG.isInfoEnabled()) {
363 LOG.info(
"Stopped previous stream.");
368 new Thread(
this).start();
379 private static class Appender implements Runnable {
385 private final Queue<String>
texts =
new ConcurrentLinkedQueue<>();
406 for (
final String text :
texts) {
407 this.texts.offer(text);
409 SwingUtilities.invokeLater(
this);
414 while (!
texts.isEmpty()) {
final JTextArea textArea
JTextArea to append to.
final Action controlStop
Action for stop.
InputStream in
BufferedReader to read from.
final String key
The i18n key.
final Action controlStart
Action for start.
CopyOutput(@NotNull final String title, @NotNull final JTextArea textArea)
Creates a new instance.
Class to run an external process.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
void controlClear()
Action method for clearing the log.
static final long serialVersionUID
Serial Version.
Class for reading data from a stream and appending it to a JTextArea.
final JTextArea stdtxt
JTextArea with log.
void createDialog(@NotNull final Frame parent)
Creates the dialog.
ProcessRunner(@NotNull final String key, @NotNull final String @NotNull[] command)
Creates a ProcessRunner for running the given command in its directory.
String[] command
The command and arguments.
final CopyOutput stdout
CopyOutput for stdout.
void controlStop()
Action method for stopping.
final Object lock
The lock object for thread synchronization.
void setCommand(@NotNull final String @NotNull[] command)
Sets the command to be executed by this ProcessRunner.
static final ActionBuilder ACTION_BUILDER
Action Builder.
void append(@NotNull final String... texts)
Appends text to the text area.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Class for SwingUtilities to append text.
final CopyOutput stderr
CopyOutput for stderr.
ProcessRunner(@NotNull final String key, @NotNull final String @NotNull[] command, @Nullable final String dir)
Creates a ProcessRunner for running the given command in the given directory.
Appender(@NotNull final JTextArea textArea)
Creates a new instance.
static final Category LOG
The Logger for printing log messages.
Utility class for ActionBuilder related functions.
transient Process process
The Process.
void showDialog(@NotNull final Frame parent)
Show a dialog if not already visible.
final Queue< String > texts
Strings to append.
final File dir
The working directory for the command.
void start(@NotNull final InputStream stream)
Starts running.
final Appender appender
JTextArea to write to.
void controlStart()
Action method for starting.