Initial commit of newLISP.
[newlisp.git] / guiserver / java / aTextWidget.java
blobe5d353977b59b0546ea73f9de9dd4e092f54dffe
1 //
2 // aTextWidget.java
3 // guiserver
4 //
5 // Created by Lutz Mueller on 6/14/07.
6 //
7 //
8 // Copyright (C) 2007 Lutz Mueller
9 //
10 // This program is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.util.*;
28 import javax.swing.*;
29 import javax.swing.text.*;
30 import java.io.UnsupportedEncodingException;
32 public class aTextWidget extends gsObject {
34 JTextComponent textcomp;
35 JScrollPane areaScrollPane;
36 boolean isScrollable;
37 String findTextAction;
39 public void setText(StringTokenizer tokens)
41 String text = Base64Coder.decodeString(tokens.nextToken());
42 textcomp.setText(text);
43 //if(isScrollable) scrollUp();
47 public void appendText(StringTokenizer tokens)
49 String text = Base64Coder.decodeString(tokens.nextToken());
50 String oldtext = textcomp.getText();
51 int len = text.length() + oldtext.length();
52 if(len > 100000)
53 textcomp.setText((oldtext + text).substring(len - 100000));
54 else
56 textcomp.setText(oldtext + text);
57 if(isScrollable) scrollUp();
58 textcomp.repaint();
61 public void scrollUp()
63 // work around for Java bug #4201999
64 try {Thread.sleep(100); } catch (InterruptedException ie) {}
65 //textArea.setCaretPosition(textArea.getText().length() - 1);
66 JScrollBar vbar = areaScrollPane.getVerticalScrollBar();
67 vbar.setValue(vbar.getMaximum());
70 public void gotoText(StringTokenizer params)
72 int line = Integer.parseInt(params.nextToken());
73 int column = Integer.parseInt(params.nextToken());
75 if(line < 1) line = 1;
76 line--;
78 String text = textcomp.getText();
79 int len = text.length();
80 int pos = 0;
81 int col;
84 for(int idx = 0; idx < line; idx++)
86 pos = text.indexOf("\n", pos) + 1;
87 if(pos >= len) break;
90 for(col = 0; col < column; col++)
92 if(pos + col >= len) break;
93 if(text.charAt(pos + col) == '\n') break;
96 textcomp.setCaretPosition(pos + col);
99 public void clearText(StringTokenizer params)
101 textcomp.setText("");
104 public void getText(StringTokenizer params)
106 String string = textcomp.getText();
107 String text;
109 if(guiserver.UTF8)
111 try {
112 byte[] utf8 = string.getBytes("UTF-8");
113 text = new String(utf8);
115 catch (UnsupportedEncodingException e)
117 System.out.println("errorconverting to UTF8");
118 text = string;
121 else
122 text = string;
124 if(params.hasMoreTokens())
126 String action = params.nextToken();
127 if(text.length() == 0)
128 guiserver.out.println("(" + action + " \"" + id + "\")");
129 else
130 guiserver.out.println("(" + action + " \"" + id + "\" [text]" + Base64Coder.encodeString(text) + "[/text])");
132 else
134 if(text.length() == 0)
135 guiserver.out.println("(set 'gs:text nil)");
136 else
137 guiserver.out.println("(set 'gs:text (base64-dec [text]" + Base64Coder.encodeString(text) + "[/text]))");
140 guiserver.out.flush();
143 public void getSelection(StringTokenizer params)
145 String action = params.nextToken();
146 String text = textcomp.getSelectedText();
147 if(text == null) text = "";
148 if(text.length() == 0)
149 guiserver.out.println("(" + action + " \"" + id + "\")");
150 else
151 guiserver.out.println("(" + action + " \"" + id + "\" [text]" + Base64Coder.encodeString(text) + "[/text])");
152 guiserver.out.flush();
155 public void getTextPosition(StringTokenizer tokens)
157 String text = textcomp.getText();
158 int caret = textcomp.getCaretPosition();
159 int line = 1;
160 int col = 1;
162 for(int i= 0; i < caret; i++)
164 if(text.charAt(i) == '\n')
166 col = 0;
167 ++line;
169 ++col;
172 guiserver.out.println("(set 'gs:text-position '(" + line + " " + col + "))");
173 guiserver.out.flush();
177 public void setEditable(StringTokenizer tokens)
179 if(tokens.nextToken().equals("true"))
180 textcomp.setEditable(true);
181 else
182 textcomp.setEditable(false);
185 public void setCaret(StringTokenizer tokens)
187 int pos = Integer.parseInt(tokens.nextToken());
189 if(pos > textcomp.getText().length())
190 pos = textcomp.getText().length();
192 textcomp.setCaretPosition(pos);
193 textcomp.moveCaretPosition(pos);
196 public void selectText(StringTokenizer tokens)
198 int toPos;
200 textcomp.setCaretPosition(Integer.parseInt(tokens.nextToken()));
201 if(tokens.hasMoreTokens())
202 toPos = Integer.parseInt(tokens.nextToken());
203 else
204 toPos = textcomp.getText().length();
206 textcomp.moveCaretPosition(toPos);
209 public void copyText(StringTokenizer tokens)
211 textcomp.copy();
214 public void cutText(StringTokenizer tokens)
216 textcomp.cut();
219 public void pasteText(StringTokenizer tokens)
221 if(tokens.hasMoreTokens())
222 textcomp.replaceSelection(Base64Coder.decodeString(tokens.nextToken()));
223 else
224 textcomp.paste();
227 public void insertText(StringTokenizer tokens)
229 String text = Base64Coder.decodeString(tokens.nextToken());
230 String oldtext = textcomp.getText();
231 int newpos = Integer.parseInt(tokens.nextToken());
232 if(newpos > textcomp.getText().length()) newpos = textcomp.getText().length();
233 textcomp.setText(oldtext.substring(0, newpos) + text + oldtext.substring(newpos));
234 textcomp.update(textcomp.getGraphics());
237 public void requestFocus(StringTokenizer tokens)
239 textcomp.requestFocus();
242 public void setFont(StringTokenizer tokens)
244 String name = Base64Coder.decodeString(tokens.nextToken());
245 int style = 0;
246 int size = 12;
248 if(tokens.hasMoreTokens())
249 size = Integer.parseInt(tokens.nextToken());
251 while(tokens.hasMoreTokens())
253 String sstyle = tokens.nextToken();
254 if(sstyle.equals("plain")) style = style | Font.PLAIN;
255 if(sstyle.equals("bold")) style = style | Font.BOLD;
256 if(sstyle.equals("italic")) style = style | Font.ITALIC;
259 Font font = new Font(name, style, size);
260 textcomp.setFont(font);
264 public void registerMouseEvent(StringTokenizer tokens)
266 mouseEvent = tokens.nextToken();
267 jcomponent.addMouseListener(new MouseAdapter () {
268 public void mouseClicked(MouseEvent e) { mouseEventOut(e, "clicked"); }
269 public void mousePressed(MouseEvent e) { mouseEventOut(e, "pressed"); }
270 public void mouseReleased(MouseEvent e) { mouseEventOut(e, "released"); }
274 private void mouseEventOut(MouseEvent e, String type)
276 guiserver.out.println("(" + mouseEvent + " \"" + id + "\" \"" + type + "\" " + e.getX() + " " + e.getY() + " " +
277 e.getButton() + " " + e.getClickCount() + " " + e.getModifiers() + ")");
278 guiserver.out.flush();
281 public static String removeChar(String str, char ch)
283 String result = "";
284 for (int i = 0; i < str.length(); i ++)
286 if (str.charAt(i) != ch)
287 result += str.charAt(i);
290 return result;
293 public void setCaretColor(StringTokenizer tokens)
295 Float red = Float.parseFloat(tokens.nextToken());
296 Float green = Float.parseFloat(tokens.nextToken());
297 Float blue = Float.parseFloat(tokens.nextToken());
299 textcomp.setCaretColor(new Color(red, green, blue));
303 public void setSelectionColor(StringTokenizer tokens)
305 Float red = Float.parseFloat(tokens.nextToken());
306 Float green = Float.parseFloat(tokens.nextToken());
307 Float blue = Float.parseFloat(tokens.nextToken());
308 Float alpha = (float)1.0;
310 if(tokens.hasMoreTokens())
311 alpha = Float.parseFloat(tokens.nextToken());
313 textcomp.setSelectionColor(new Color(red, green, blue, alpha));
319 // eof //