1 /* This file is part of the KDE project 2 * 3 * Copyright (C) 2002 Till 4 * Copyright (C) 2005 Koos Vriezen <koos ! vriezen () xs4all ! nl> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public License 17 * along with this library; see the file COPYING.LIB. If not, write to 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 */ 21 22 package org.kde.kjas.server; 23 24 import java.awt.Toolkit; 25 import java.awt.Image; 26 import java.awt.BorderLayout; 27 import java.awt.event.ActionListener; 28 import java.awt.event.ActionEvent; 29 import java.awt.event.KeyAdapter; 30 import java.awt.event.KeyEvent; 31 import java.awt.event.WindowAdapter; 32 import java.awt.event.WindowEvent; 33 import java.io.PrintStream; 34 import java.util.Enumeration; 35 import java.util.Properties; 36 import javax.swing.JFrame; 37 import javax.swing.JPanel; 38 import javax.swing.JScrollPane; 39 import javax.swing.JButton; 40 import javax.swing.JTextArea; 41 import javax.swing.border.EmptyBorder; 42 43 44 public class KJASSwingConsole implements Console { 45 private JFrame frame = null; 46 private JPanel jPanel1; 47 private JScrollPane jScrollPane1; 48 private JButton clearButton; 49 private JTextArea textField; 50 private JButton closeButton; 51 private JButton copyButton; 52 final static int NR_BUFFERS = 3; 53 final static int MAX_BUF_LENGTH = 3000; 54 private int queue_pos = 0; 55 private StringBuffer [] output_buffer = new StringBuffer[NR_BUFFERS]; 56 57 private PrintStream real_stderr = new PrintStream(System.err); 58 59 /** Creates new form KJASSwingConsole */ KJASSwingConsole()60 public KJASSwingConsole() { 61 PrintStream st = new PrintStream( new KJASConsoleStream(this) ); 62 System.setOut(st); 63 System.setErr(st); 64 } 65 initComponents()66 private void initComponents() { 67 frame = new JFrame("Konqueror Java Console"); 68 jPanel1 = new JPanel(); 69 clearButton = new JButton(); 70 closeButton = new JButton(); 71 copyButton = new JButton(); 72 jScrollPane1 = new JScrollPane(); 73 textField = new JTextArea(); 74 75 frame.setFont(new java.awt.Font("Monospaced", 0, 10)); 76 frame.setName("KJAS Console"); 77 frame.addWindowListener(new WindowAdapter() { 78 public void windowClosing(WindowEvent evt) { 79 exitForm(evt); 80 } 81 }); 82 83 jPanel1.setLayout(new BorderLayout()); 84 jPanel1.setBorder(new EmptyBorder(new java.awt.Insets(1, 1, 1, 1))); 85 clearButton.setText("clear"); 86 clearButton.addActionListener(new ActionListener() { 87 public void actionPerformed(ActionEvent evt) { 88 clearButtonActionPerformed(evt); 89 } 90 }); 91 92 jPanel1.add(clearButton, BorderLayout.WEST); 93 94 closeButton.setText("close"); 95 closeButton.addActionListener(new ActionListener() { 96 public void actionPerformed(ActionEvent evt) { 97 closeButtonActionPerformed(evt); 98 } 99 }); 100 101 jPanel1.add(closeButton, BorderLayout.EAST); 102 103 copyButton.setText("copy"); 104 copyButton.addActionListener(new ActionListener() { 105 public void actionPerformed(ActionEvent evt) { 106 copyButtonActionPerformed(evt); 107 } 108 }); 109 110 jPanel1.add(copyButton, BorderLayout.CENTER); 111 112 frame.getContentPane().add(jPanel1, BorderLayout.SOUTH); 113 114 textField.setColumns(40); 115 textField.setEditable(false); 116 textField.setRows(10); 117 textField.addKeyListener(new KeyAdapter() { 118 public void keyPressed(KeyEvent evt) { 119 textFieldKeyPressed(evt); 120 } 121 }); 122 123 jScrollPane1.setViewportView(textField); 124 125 frame.getContentPane().add(jScrollPane1, BorderLayout.CENTER); 126 127 try { 128 java.net.URL iconUrl = getClass().getClassLoader().getResource("images/beanicon.png"); 129 if (iconUrl != null) { 130 Toolkit tk = Toolkit.getDefaultToolkit(); 131 Image icon = tk.createImage(iconUrl); 132 frame.setIconImage(icon); 133 } 134 } catch (Throwable e) { 135 } 136 frame.pack(); 137 frame.setSize(500, 300); 138 } 139 textFieldKeyPressed(java.awt.event.KeyEvent evt)140 private void textFieldKeyPressed(java.awt.event.KeyEvent evt) { 141 // Add your handling code here: 142 char key = evt.getKeyChar(); 143 switch (key) { 144 case 'h': 145 showHelp(); 146 break; 147 case 'g': 148 append("Running Garbage Collection ...\n", true); 149 System.gc(); 150 case 'm': 151 append("Total Memory: " + Runtime.getRuntime().totalMemory() + " bytes\n", true); 152 append("Free Memory : " + Runtime.getRuntime().freeMemory() + " bytes\n", true); 153 break; 154 case 'c': 155 clear(); 156 break; 157 case 's': 158 showSystemProperties(); 159 break; 160 case 't': 161 showThreads(); 162 break; 163 case 'x': 164 KJASAppletClassLoader.removeLoaders(); 165 append("Emptied Classloader Cache\n", true); 166 break; 167 } 168 } 169 showHelp()170 private void showHelp() { 171 append("Java VM: " + System.getProperty("java.vendor") + " " + System.getProperty("java.version") + "\n", true); 172 String ph = System.getProperty("http.proxyHost"); 173 if (ph != null) { 174 append("Proxy: " + ph + ":" + System.getProperty("java.proxyPort") + "\n", true); 175 } 176 SecurityManager sec = System.getSecurityManager(); 177 if (sec == null) { 178 append("WARNING: Security Manager disabled!\n", true); 179 } else { 180 append("SecurityManager=" + sec + "\n", true); 181 } 182 appendSeparator(); 183 append("Konqueror Java Console Help\n", true); 184 append(" c: clear console\n", true); 185 append(" g: run garbage collection\n", true); 186 append(" h: show help\n", true); 187 append(" m: show memory info\n", true); 188 append(" s: print system properties\n", true); 189 append(" t: list threads\n", true); 190 append(" x: empty classloader cache\n", true); 191 appendSeparator(); 192 } 193 showSystemProperties()194 private void showSystemProperties() { 195 append("Printing System Properties ...\n", true); 196 appendSeparator(); 197 Properties p = System.getProperties(); 198 for (Enumeration e = p.keys(); e.hasMoreElements();) { 199 Object key = e.nextElement(); 200 if ("line.separator".equals(key)) { 201 String value = (String) p.get(key); 202 StringBuffer unescaped = new StringBuffer(10); 203 for (int i = 0; i < value.length(); i++) { 204 char c = value.charAt(i); 205 if (c == '\n') unescaped.append("\\n"); 206 else if (c == '\r') unescaped.append("\\n"); 207 else unescaped.append(c); 208 } 209 append(key + " = " + unescaped + "\n", true); 210 } else append(key + " = " + p.get(key) + "\n", true); 211 } 212 appendSeparator(); 213 } 214 showThreads()215 private void showThreads() { 216 Thread t = Thread.currentThread(); 217 ThreadGroup g = t.getThreadGroup(); 218 ThreadGroup parent; 219 while ((parent = g.getParent()) != null) { 220 g = parent; 221 } 222 g.list(); 223 } 224 copyButtonActionPerformed(java.awt.event.ActionEvent evt)225 private void copyButtonActionPerformed(java.awt.event.ActionEvent evt) { 226 textField.selectAll(); 227 textField.copy(); 228 } 229 closeButtonActionPerformed(java.awt.event.ActionEvent evt)230 private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) { 231 frame.setVisible(false); 232 } 233 clearButtonActionPerformed(java.awt.event.ActionEvent evt)234 private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) { 235 clear(); 236 } 237 238 /** Exit the Application */ exitForm(java.awt.event.WindowEvent evt)239 private void exitForm(java.awt.event.WindowEvent evt) { 240 frame.setVisible(false); 241 } 242 setVisible(boolean visible)243 public void setVisible(boolean visible) { 244 if (frame == null && visible) { 245 initComponents(); 246 frame.setVisible(visible); 247 System.out.println( "Java VM version: " + 248 System.getProperty("java.version") ); 249 System.out.println( "Java VM vendor: " + 250 System.getProperty("java.vendor") ); 251 String ph = System.getProperty("http.proxyHost"); 252 String pp = System.getProperty("http.proxyPort"); 253 if (ph != null) { 254 System.out.println("Proxy: " + ph + ":" + pp); 255 } 256 SecurityManager sec = System.getSecurityManager(); 257 Main.debug("SecurityManager=" + sec); 258 if (sec == null) { 259 System.out.println( "WARNING: Security Manager disabled!" ); 260 textField.setForeground(java.awt.Color.red); 261 } 262 showHelp(); 263 } else if (frame != null) 264 frame.setVisible(visible); 265 266 if (visible) { 267 for (int i = 0; i < NR_BUFFERS; i++) 268 if (output_buffer[(queue_pos + i + 1) % 3] != null) { 269 textField.append(output_buffer[(queue_pos + i + 1) % 3].toString()); 270 output_buffer[(queue_pos + i + 1) % 3] = null; 271 } 272 } 273 } 274 275 /** 276 * @param args the command line arguments 277 */ main(String args[])278 public static void main(String args[]) { 279 new KJASSwingConsole().setVisible(true); 280 } 281 clear()282 public void clear() { 283 textField.setText(""); 284 } 285 appendSeparator()286 private void appendSeparator() { 287 append("----------------------------------------------------\n", true); 288 } 289 append(String txt)290 public void append(String txt) { 291 append(txt, false); 292 } 293 append(String txt, boolean force)294 public void append(String txt, boolean force) { 295 if (txt == null) 296 return; 297 if (frame == null || !frame.isVisible()) { 298 if (Main.Debug) 299 real_stderr.print(txt); 300 if (output_buffer[queue_pos] != null && 301 output_buffer[queue_pos].length() > MAX_BUF_LENGTH) { 302 queue_pos = (++queue_pos) % NR_BUFFERS; 303 if (output_buffer[queue_pos] != null) { 304 // starting overwriting old log, clear textField if exists 305 if (frame != null) 306 textField.setText(""); 307 output_buffer[queue_pos] = null; 308 } 309 } 310 if (output_buffer[queue_pos] == null) 311 output_buffer[queue_pos] = new StringBuffer(txt); 312 else 313 output_buffer[queue_pos].append(txt); 314 return; 315 } 316 int length = txt.length(); 317 synchronized(textField) { 318 //get the caret position, and then get the new position 319 int old_pos = textField.getCaretPosition(); 320 textField.append(txt); 321 textField.setCaretPosition( old_pos + length ); 322 } 323 } 324 } 325 326