Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

175
LINES

< > BotCompany Repo | #1000818 // Console

JavaX fragment (include)

1  
!614 // actionListener {
2  
!1000805 // awt {
3  
4  
!include #1000943 // DelayedUpdate
5  
6  
import java.awt.event.*;
7  
8  
static class Console extends WindowAdapter implements WindowListener, ActionListener, Runnable {
9  
	private JFrame frame;
10  
	private JTextArea textArea;
11  
	new StringBuffer buf;
12  
	private Thread reader;
13  
	private Thread reader2;
14  
	private boolean quit;
15  
					
16  
	private final PipedInputStream pin=new PipedInputStream(); 
17  
	private final PipedInputStream pin2=new PipedInputStream(); 
18  
	
19  
	final DelayedUpdate du = new DelayedUpdate(runnable {
20  
    textArea.append(buf.substring(textArea.getText().length()));
21  
  });
22  
23  
	public Console()
24  
	{
25  
		// create all components and add them
26  
		frame=new JFrame("JavaX Output");
27  
		
28  
		/*Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
29  
		Dimension frameSize=new Dimension((int)(screenSize.width/2),(int)(screenSize.height/2));
30  
		int x=(int)(frameSize.width/2);
31  
		int y=(int)(frameSize.height/2);
32  
		frame.setBounds(x,y,frameSize.width,frameSize.height);*/
33  
		
34  
		// put in right-bottom corner
35  
		Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
36  
		int w = 550, h = 200;
37  
		frame.setBounds(r.x+r.width-w, r.y+r.height-h, w, h);
38  
		
39  
		textArea=new JTextArea();
40  
		textArea.setEditable(false);
41  
		JButton button=new JButton("clear");
42  
		JButton buttonkill=new JButton("kill");
43  
		
44  
		JPanel buttons = new JPanel(new GridLayout(1, 2));
45  
		buttons.add(button);
46  
		buttons.add(buttonkill);
47  
		
48  
		frame.getContentPane().setLayout(new BorderLayout());
49  
		frame.getContentPane().add(new JScrollPane(textArea),BorderLayout.CENTER);
50  
		frame.getContentPane().add(buttons,BorderLayout.SOUTH);
51  
		frame.setVisible(true);		
52  
		
53  
		//frame.addWindowListener(this); // disabled for now
54  
		button.addActionListener(this);
55  
		buttonkill.addActionListener(actionListener {
56  
		  print("Console: Kill button pressed!");
57  
		  System.exit(0);
58  
		});
59  
		
60  
		try
61  
		{
62  
			PipedOutputStream pout=new PipedOutputStream(this.pin);
63  
			System.setOut(new PrintStream(pout,true)); 
64  
		} 
65  
		catch (java.io.IOException io)
66  
		{
67  
			appendText("Couldn't redirect STDOUT to this console\n"+io.getMessage());
68  
		}
69  
		catch (SecurityException se)
70  
		{
71  
			appendText("Couldn't redirect STDOUT to this console\n"+se.getMessage());
72  
	    } 
73  
		
74  
		try 
75  
		{
76  
			PipedOutputStream pout2=new PipedOutputStream(this.pin2);
77  
			System.setErr(new PrintStream(pout2,true));
78  
		} 
79  
		catch (java.io.IOException io)
80  
		{
81  
			appendText("Couldn't redirect STDERR to this console\n"+io.getMessage());
82  
		}
83  
		catch (SecurityException se)
84  
		{
85  
			appendText("Couldn't redirect STDERR to this console\n"+se.getMessage());
86  
	    } 		
87  
			
88  
		quit=false; // signals the Threads that they should exit
89  
				
90  
		// Starting two seperate threads to read from the PipedInputStreams				
91  
		//
92  
		reader=new Thread(this);
93  
		reader.setDaemon(true);	
94  
		reader.start();	
95  
		//
96  
		reader2=new Thread(this);	
97  
		reader2.setDaemon(true);	
98  
		reader2.start();
99  
	}
100  
	
101  
	public synchronized void windowClosed(WindowEvent evt)
102  
	{
103  
		quit=true;
104  
		this.notifyAll(); // stop all threads
105  
		try { reader.join(1000);pin.close();   } catch (Exception e){}		
106  
		try { reader2.join(1000);pin2.close(); } catch (Exception e){}
107  
		System.exit(0);
108  
	}		
109  
		
110  
	public synchronized void windowClosing(WindowEvent evt)
111  
	{
112  
		frame.setVisible(false); // default behaviour of JFrame	
113  
		frame.dispose();
114  
	}
115  
	
116  
	public synchronized void actionPerformed(ActionEvent evt)
117  
	{
118  
		textArea.setText("");
119  
		buf = new StringBuffer;
120  
	}
121  
122  
	public synchronized void run()
123  
	{
124  
		try
125  
		{			
126  
			while (Thread.currentThread()==reader)
127  
			{
128  
				try { this.wait(100);}catch(InterruptedException ie) {}
129  
				if (pin.available()!=0)
130  
				{
131  
					String input=this.readLine(pin);
132  
					appendText(input);
133  
				}
134  
				if (quit) return;
135  
			}
136  
		
137  
			while (Thread.currentThread()==reader2)
138  
			{
139  
				try { this.wait(100);}catch(InterruptedException ie) {}
140  
				if (pin2.available()!=0)
141  
				{
142  
					String input=this.readLine(pin2);
143  
					appendText(input);
144  
				}
145  
				if (quit) return;
146  
			}			
147  
		} catch (Exception e)
148  
		{
149  
			appendText("\nConsole reports an Internal error.");
150  
			appendText("The error is: "+e);			
151  
		}
152  
	}
153  
	
154  
	public synchronized String readLine(PipedInputStream in) throws IOException
155  
	{
156  
		String input="";
157  
		do
158  
		{
159  
			int available=in.available();
160  
			if (available==0) break;
161  
			byte b[]=new byte[available];
162  
			in.read(b);
163  
			input=input+new String(b,0,b.length);														
164  
		}while( !input.endsWith("\n") &&  !input.endsWith("\r\n") && !quit);
165  
		return input;
166  
	}	
167  
	
168  
	public void appendText(String s) {
169  
	  buf.append(s);
170  
	  /*textArea.append(s);
171  
	  textArea.setCaretPosition(textArea.getText().length()); // Hopefully not too slow...
172  
	  */
173  
	  du.trigger();
174  
	}
175  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1070 stefan Thanks to http://www.comweb.nl/java/Console/Console.html 2015-08-28 20:21:50

add comment

Snippet ID: #1000818
Snippet name: Console
Eternal ID of this version: #1000818/1
Text MD5: 5940e76abac8d5dfc4c8c5e126a808d0
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-11 13:54:20
Source code size: 4841 bytes / 175 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 959 / 1351
Referenced in: [show references]