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()) {