~davewalker/etherpad/ubuntu-unlimited-max-users-and-revisions

« back to all changes in this revision

Viewing changes to infrastructure/rhino1_7R1/toolsrc/org/mozilla/javascript/tools/shell/ConsoleTextArea.java

  • Committer: James Page
  • Date: 2011-04-13 08:00:43 UTC
  • Revision ID: james.page@canonical.com-20110413080043-eee2nq7y1v7cv2mp
* Refactoring to use native Ubuntu Java libraries. 
* debian/control:
  - use openjdk instead of sun's java
  - update maintainer
* debian/etherpad.init.orig, debian/etherpad.upstart:
  - move the init script out of the way
  - create a basic upstart script
  - note that the open office document conversion daemon was dropped
    from the upstart configuration; if this behavior is desired, please
    create a separate upstart job for it
* debian/rules:
  - just use basic dh_installinit, as it will pick up the new upstart job
* New release
* Changed maintainer to Packaging
* Fixed installation scripts
* Initial Release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * ***** BEGIN LICENSE BLOCK *****
 
4
 * Version: MPL 1.1/GPL 2.0
 
5
 *
 
6
 * The contents of this file are subject to the Mozilla Public License Version
 
7
 * 1.1 (the "License"); you may not use this file except in compliance with
 
8
 * the License. You may obtain a copy of the License at
 
9
 * http://www.mozilla.org/MPL/
 
10
 *
 
11
 * Software distributed under the License is distributed on an "AS IS" basis,
 
12
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
13
 * for the specific language governing rights and limitations under the
 
14
 * License.
 
15
 *
 
16
 * The Original Code is Rhino JavaScript Debugger code, released
 
17
 * November 21, 2000.
 
18
 *
 
19
 * The Initial Developer of the Original Code is
 
20
 * See Beyond Corporation.
 
21
 * Portions created by the Initial Developer are Copyright (C) 2000
 
22
 * the Initial Developer. All Rights Reserved.
 
23
 *
 
24
 * Contributor(s):
 
25
 *   Christopher Oliver
 
26
 *
 
27
 * Alternatively, the contents of this file may be used under the terms of
 
28
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 
29
 * case the provisions of the GPL are applicable instead of those above. If
 
30
 * you wish to allow use of your version of this file only under the terms of
 
31
 * the GPL and not to allow others to use your version of this file under the
 
32
 * MPL, indicate your decision by deleting the provisions above and replacing
 
33
 * them with the notice and other provisions required by the GPL. If you do
 
34
 * not delete the provisions above, a recipient may use your version of this
 
35
 * file under either the MPL or the GPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
package org.mozilla.javascript.tools.shell;
 
39
import java.io.*;
 
40
import java.awt.*;
 
41
import java.awt.event.*;
 
42
import javax.swing.*;
 
43
import javax.swing.event.*;
 
44
import javax.swing.text.Document;
 
45
import javax.swing.text.Segment;
 
46
 
 
47
class ConsoleWrite implements Runnable {
 
48
    private ConsoleTextArea textArea;
 
49
    private String str;
 
50
 
 
51
    public ConsoleWrite(ConsoleTextArea textArea, String str) {
 
52
        this.textArea = textArea;
 
53
        this.str = str;
 
54
    }
 
55
 
 
56
    public void run() {
 
57
        textArea.write(str);
 
58
    }
 
59
}
 
60
 
 
61
class ConsoleWriter extends java.io.OutputStream {
 
62
 
 
63
    private ConsoleTextArea textArea;
 
64
    private StringBuffer buffer;
 
65
 
 
66
    public ConsoleWriter(ConsoleTextArea textArea) {
 
67
        this.textArea = textArea;
 
68
        buffer = new StringBuffer();
 
69
    }
 
70
 
 
71
    public synchronized void write(int ch) {
 
72
        buffer.append((char)ch);
 
73
        if(ch == '\n') {
 
74
            flushBuffer();
 
75
        }
 
76
    }
 
77
 
 
78
    public synchronized void write (char[] data, int off, int len) {
 
79
        for(int i = off; i < len; i++) {
 
80
            buffer.append(data[i]);
 
81
            if(data[i] == '\n') {
 
82
                flushBuffer();
 
83
            }
 
84
        }
 
85
    }
 
86
 
 
87
    public synchronized void flush() {
 
88
        if (buffer.length() > 0) {
 
89
            flushBuffer();
 
90
        }
 
91
    }
 
92
 
 
93
    public void close () {
 
94
        flush();
 
95
    }
 
96
 
 
97
    private void flushBuffer() {
 
98
        String str = buffer.toString();
 
99
        buffer.setLength(0);
 
100
        SwingUtilities.invokeLater(new ConsoleWrite(textArea, str));
 
101
    }
 
102
}
 
103
 
 
104
public class ConsoleTextArea
 
105
    extends JTextArea implements KeyListener, DocumentListener
 
106
{
 
107
    static final long serialVersionUID = 8557083244830872961L;
 
108
 
 
109
    private ConsoleWriter console1;
 
110
    private ConsoleWriter console2;
 
111
    private PrintStream out;
 
112
    private PrintStream err;
 
113
    private PrintWriter inPipe;
 
114
    private PipedInputStream in;
 
115
    private java.util.Vector history;
 
116
    private int historyIndex = -1;
 
117
    private int outputMark = 0;
 
118
 
 
119
    public void select(int start, int end) {
 
120
        requestFocus();
 
121
        super.select(start, end);
 
122
    }
 
123
 
 
124
    public ConsoleTextArea(String[] argv) {
 
125
        super();
 
126
        history = new java.util.Vector();
 
127
        console1 = new ConsoleWriter(this);
 
128
        console2 = new ConsoleWriter(this);
 
129
        out = new PrintStream(console1);
 
130
        err = new PrintStream(console2);
 
131
        PipedOutputStream outPipe = new PipedOutputStream();
 
132
        inPipe = new PrintWriter(outPipe);
 
133
        in = new PipedInputStream();
 
134
        try {
 
135
            outPipe.connect(in);
 
136
        } catch(IOException exc) {
 
137
            exc.printStackTrace();
 
138
        }
 
139
        getDocument().addDocumentListener(this);
 
140
        addKeyListener(this);
 
141
        setLineWrap(true);
 
142
        setFont(new Font("Monospaced", 0, 12));
 
143
    }
 
144
 
 
145
 
 
146
    synchronized void returnPressed() {
 
147
        Document doc = getDocument();
 
148
        int len = doc.getLength();
 
149
        Segment segment = new Segment();
 
150
        try {
 
151
            doc.getText(outputMark, len - outputMark, segment);
 
152
        } catch(javax.swing.text.BadLocationException ignored) {
 
153
            ignored.printStackTrace();
 
154
        }
 
155
        if(segment.count > 0) {
 
156
            history.addElement(segment.toString());
 
157
        }
 
158
        historyIndex = history.size();
 
159
        inPipe.write(segment.array, segment.offset, segment.count);
 
160
        append("\n");
 
161
        outputMark = doc.getLength();
 
162
        inPipe.write("\n");
 
163
        inPipe.flush();
 
164
        console1.flush();
 
165
    }
 
166
 
 
167
    public void eval(String str) {
 
168
        inPipe.write(str);
 
169
        inPipe.write("\n");
 
170
        inPipe.flush();
 
171
        console1.flush();
 
172
    }
 
173
 
 
174
    public void keyPressed(KeyEvent e) {
 
175
        int code = e.getKeyCode();
 
176
        if(code == KeyEvent.VK_BACK_SPACE || code == KeyEvent.VK_LEFT) {
 
177
            if(outputMark == getCaretPosition()) {
 
178
                e.consume();
 
179
            }
 
180
        } else if(code == KeyEvent.VK_HOME) {
 
181
           int caretPos = getCaretPosition();
 
182
           if(caretPos == outputMark) {
 
183
               e.consume();
 
184
           } else if(caretPos > outputMark) {
 
185
               if(!e.isControlDown()) {
 
186
                   if(e.isShiftDown()) {
 
187
                       moveCaretPosition(outputMark);
 
188
                   } else {
 
189
                       setCaretPosition(outputMark);
 
190
                   }
 
191
                   e.consume();
 
192
               }
 
193
           }
 
194
        } else if(code == KeyEvent.VK_ENTER) {
 
195
            returnPressed();
 
196
            e.consume();
 
197
        } else if(code == KeyEvent.VK_UP) {
 
198
            historyIndex--;
 
199
            if(historyIndex >= 0) {
 
200
                if(historyIndex >= history.size()) {
 
201
                    historyIndex = history.size() -1;
 
202
                }
 
203
                if(historyIndex >= 0) {
 
204
                    String str = (String)history.elementAt(historyIndex);
 
205
                    int len = getDocument().getLength();
 
206
                    replaceRange(str, outputMark, len);
 
207
                    int caretPos = outputMark + str.length();
 
208
                    select(caretPos, caretPos);
 
209
                } else {
 
210
                    historyIndex++;
 
211
                }
 
212
            } else {
 
213
                historyIndex++;
 
214
            }
 
215
            e.consume();
 
216
        } else if(code == KeyEvent.VK_DOWN) {
 
217
            int caretPos = outputMark;
 
218
            if(history.size() > 0) {
 
219
                historyIndex++;
 
220
                if(historyIndex < 0) {historyIndex = 0;}
 
221
                int len = getDocument().getLength();
 
222
                if(historyIndex < history.size()) {
 
223
                    String str = (String)history.elementAt(historyIndex);
 
224
                    replaceRange(str, outputMark, len);
 
225
                    caretPos = outputMark + str.length();
 
226
                } else {
 
227
                    historyIndex = history.size();
 
228
                    replaceRange("", outputMark, len);
 
229
                }
 
230
            }
 
231
            select(caretPos, caretPos);
 
232
            e.consume();
 
233
        }
 
234
    }
 
235
 
 
236
    public void keyTyped(KeyEvent e) {
 
237
        int keyChar = e.getKeyChar();
 
238
        if(keyChar == 0x8 /* KeyEvent.VK_BACK_SPACE */) {
 
239
            if(outputMark == getCaretPosition()) {
 
240
                e.consume();
 
241
            }
 
242
        } else if(getCaretPosition() < outputMark) {
 
243
            setCaretPosition(outputMark);
 
244
        }
 
245
    }
 
246
 
 
247
    public synchronized void keyReleased(KeyEvent e) {
 
248
    }
 
249
 
 
250
    public synchronized void write(String str) {
 
251
        insert(str, outputMark);
 
252
        int len = str.length();
 
253
        outputMark += len;
 
254
        select(outputMark, outputMark);
 
255
    }
 
256
 
 
257
    public synchronized void insertUpdate(DocumentEvent e) {
 
258
        int len = e.getLength();
 
259
        int off = e.getOffset();
 
260
        if(outputMark > off) {
 
261
            outputMark += len;
 
262
        }
 
263
    }
 
264
 
 
265
    public synchronized void removeUpdate(DocumentEvent e) {
 
266
        int len = e.getLength();
 
267
        int off = e.getOffset();
 
268
        if(outputMark > off) {
 
269
            if(outputMark >= off + len) {
 
270
                outputMark -= len;
 
271
            } else {
 
272
                outputMark = off;
 
273
            }
 
274
        }
 
275
    }
 
276
 
 
277
    public synchronized void postUpdateUI() {
 
278
        // this attempts to cleanup the damage done by updateComponentTreeUI
 
279
        requestFocus();
 
280
        setCaret(getCaret());
 
281
        select(outputMark, outputMark);
 
282
    }
 
283
 
 
284
    public synchronized void changedUpdate(DocumentEvent e) {
 
285
    }
 
286
 
 
287
 
 
288
    public InputStream getIn() {
 
289
        return in;
 
290
    }
 
291
 
 
292
    public PrintStream getOut() {
 
293
        return out;
 
294
    }
 
295
 
 
296
    public PrintStream getErr() {
 
297
        return err;
 
298
    }
 
299
 
 
300
}