summaryrefslogtreecommitdiffstats
path: root/khtml/java/org/kde/kjas/server/KJASSwingConsole.java
blob: fedda6c48aa4d4ca417232ba1b7dc70179443143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* This file is part of the KDE project
 *
 * Copyright (C) 2002 Till
 * Copyright (C) 2005 Koos Vriezen <koos ! vriezen () xs4all ! nl>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

package org.kde.kjas.server;

import java.awt.Toolkit;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;


public class KJASSwingConsole implements Console {
    private JFrame frame = null;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JButton clearButton;
    private JTextArea textField;
    private JButton closeButton;
    private JButton copyButton;
    final static int NR_BUFFERS = 3;
    final static int MAX_BUF_LENGTH = 3000;
    private int queue_pos = 0;
    private StringBuffer [] output_buffer = new StringBuffer[NR_BUFFERS];

    private PrintStream real_stderr = new PrintStream(System.err);
    
    /** Creates new form KJASSwingConsole */
    public KJASSwingConsole() {
        PrintStream st = new PrintStream( new KJASConsoleStream(this) );
        System.setOut(st);
        System.setErr(st);
    }
    
    private void initComponents() {
        frame = new JFrame("Konqueror Java Console");
        jPanel1 = new JPanel();
        clearButton = new JButton();
        closeButton = new JButton();
        copyButton = new JButton();
        jScrollPane1 = new JScrollPane();
        textField = new JTextArea();

        frame.setFont(new java.awt.Font("Monospaced", 0, 10));
        frame.setName("KJAS Console");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new BorderLayout());
        jPanel1.setBorder(new EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
        clearButton.setText("clear");
        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        jPanel1.add(clearButton, BorderLayout.WEST);

        closeButton.setText("close");
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        jPanel1.add(closeButton, BorderLayout.EAST);

        copyButton.setText("copy");
        copyButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                copyButtonActionPerformed(evt);
            }
        });

        jPanel1.add(copyButton, BorderLayout.CENTER);

        frame.getContentPane().add(jPanel1, BorderLayout.SOUTH);

        textField.setColumns(40);
        textField.setEditable(false);
        textField.setRows(10);
        textField.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                textFieldKeyPressed(evt);
            }
        });

        jScrollPane1.setViewportView(textField);

        frame.getContentPane().add(jScrollPane1, BorderLayout.CENTER);

        try {
            java.net.URL iconUrl = getClass().getClassLoader().getResource("images/beanicon.png");
            if (iconUrl != null) {
                Toolkit tk = Toolkit.getDefaultToolkit();
                Image icon = tk.createImage(iconUrl);
                frame.setIconImage(icon);
            }
        } catch (Throwable e) {
        }
        frame.pack();
        frame.setSize(500, 300);
    }

    private void textFieldKeyPressed(java.awt.event.KeyEvent evt) {
        // Add your handling code here:
        char key = evt.getKeyChar();
        switch (key) {
            case 'h':
                showHelp();
                break;
            case 'g':
                append("Running Garbage Collection ...\n", true);
                System.gc();
            case 'm': 
                append("Total Memory: " + Runtime.getRuntime().totalMemory() + " bytes\n", true); 
                append("Free Memory : " + Runtime.getRuntime().freeMemory() + " bytes\n", true);
                break;
            case 'c':
                clear();
                break;
            case 's':
                showSystemProperties();
                break;
            case 't':
                showThreads();
                break;
            case 'x':
                KJASAppletClassLoader.removeLoaders();
                append("Emptied Classloader Cache\n", true);
                break;
        }
    }

    private void showHelp() {
        append("Java VM: " + System.getProperty("java.vendor") + " " + System.getProperty("java.version") + "\n", true);
        String ph = System.getProperty("http.proxyHost");
        if (ph != null) {
            append("Proxy: " + ph + ":" + System.getProperty("java.proxyPort") + "\n", true);
        }
        SecurityManager sec = System.getSecurityManager();
        if (sec == null) {
            append("WARNING: Security Manager disabled!\n", true);
        } else {
            append("SecurityManager=" + sec + "\n", true);
        }
        appendSeparator();
        append("Konqueror Java Console Help\n", true);
        append("  c: clear console\n", true);
        append("  g: run garbage collection\n", true);
        append("  h: show help\n", true);
        append("  m: show memory info\n", true);
        append("  s: print system properties\n", true);
        append("  t: list threads\n", true);
        append("  x: empty classloader cache\n", true);
        appendSeparator();
    }

    private void showSystemProperties() {
        append("Printing System Properties ...\n", true);
        appendSeparator();
        Properties p = System.getProperties();
        for (Enumeration e = p.keys(); e.hasMoreElements();) {
            Object key = e.nextElement();
            if ("line.separator".equals(key)) {
                String value = (String) p.get(key);
                StringBuffer unescaped = new StringBuffer(10);
                for (int i = 0; i < value.length(); i++) {
                    char c = value.charAt(i);
                    if (c == '\n') unescaped.append("\\n");
                    else if (c == '\r') unescaped.append("\\n");
                    else unescaped.append(c);
                }
                append(key + " = " + unescaped + "\n", true);
            } else append(key + " = " + p.get(key) + "\n", true);
        }
        appendSeparator();
    }

    private void showThreads() {
        Thread t = Thread.currentThread();
        ThreadGroup g = t.getThreadGroup();
        ThreadGroup parent;
        while ((parent = g.getParent()) != null) {
            g = parent;
        }
        g.list();
    }

    private void copyButtonActionPerformed(java.awt.event.ActionEvent evt) {
        textField.selectAll();
        textField.copy();
    }

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
        frame.setVisible(false);
    }

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
        clear();
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        frame.setVisible(false);
    }

    public void setVisible(boolean visible) {
        if (frame == null && visible) {
            initComponents();
            frame.setVisible(visible);
            System.out.println( "Java VM version: " +
                    System.getProperty("java.version") );
            System.out.println( "Java VM vendor:  " +
                    System.getProperty("java.vendor") );
            String ph = System.getProperty("http.proxyHost");
            String pp = System.getProperty("http.proxyPort");
            if (ph != null) {
                System.out.println("Proxy: " + ph + ":" + pp);
            }
            SecurityManager sec = System.getSecurityManager();
            Main.debug("SecurityManager=" + sec);
            if (sec == null) {
                System.out.println( "WARNING: Security Manager disabled!" );
                textField.setForeground(java.awt.Color.red);
            }
            showHelp();
        } else if (frame != null)
            frame.setVisible(visible);

        if (visible) {
            for (int i = 0; i < NR_BUFFERS; i++)
                if (output_buffer[(queue_pos + i + 1) % 3] != null) {
                    textField.append(output_buffer[(queue_pos + i + 1) % 3].toString());
                    output_buffer[(queue_pos + i + 1) % 3] = null;
                }
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new KJASSwingConsole().setVisible(true);
    }
    
    public void clear() {
        textField.setText("");
    }

    private void appendSeparator() {
        append("----------------------------------------------------\n", true);
    }

    public void append(String txt) {
        append(txt, false);
    }

    public void append(String txt, boolean force) {
        if (txt == null)
            return;
        if (frame == null || !frame.isVisible()) {
            if (Main.Debug)
                real_stderr.print(txt);
            if (output_buffer[queue_pos] != null &&
                    output_buffer[queue_pos].length() > MAX_BUF_LENGTH) {
                queue_pos = (++queue_pos) % NR_BUFFERS;
                if (output_buffer[queue_pos] != null) {
                    // starting overwriting old log, clear textField if exists
                    if (frame != null)
                        textField.setText("");
                    output_buffer[queue_pos] = null;
                }
            }
            if (output_buffer[queue_pos] == null)
                output_buffer[queue_pos] = new StringBuffer(txt);
            else
                output_buffer[queue_pos].append(txt);
            return;
        }
        int length = txt.length();
        synchronized(textField) {
            //get the caret position, and then get the new position
            int old_pos = textField.getCaretPosition();
            textField.append(txt);
            textField.setCaretPosition( old_pos + length );
        }
    }
}