001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.demo.rbbi;
008:
009: import java.awt.*;
010: import java.awt.event.ItemListener;
011: import java.awt.event.ItemEvent;
012: import java.awt.event.KeyAdapter;
013: import java.awt.event.KeyEvent;
014: import java.awt.event.WindowEvent;
015: import javax.swing.JTextArea;
016: import javax.swing.JScrollPane;
017: import javax.swing.BorderFactory;
018: import com.ibm.icu.dev.demo.impl.*;
019: import com.ibm.icu.text.BreakIterator;
020:
021: public class TextBoundDemo extends DemoApplet {
022: public static void main(String argv[]) {
023: new TextBoundDemo().showDemo();
024: }
025:
026: public Frame createDemoFrame(DemoApplet applet) {
027: return new TextBoundFrame(applet);
028: }
029: }
030:
031: class TextBoundFrame extends Frame implements ItemListener {
032: private static final String creditString = "v1.1a9, Demo";
033:
034: private static final int FIELD_COLUMNS = 45;
035: private static final Font choiceFont = null;
036: private static final boolean DEBUG = false;
037: private DemoApplet applet;
038:
039: final String right = "-->";
040: final String left = "<--";
041:
042: private BreakIterator iter;
043: private boolean isctrldown_ = false;
044:
045: JTextArea text;
046: // TextArea text;
047: Choice bound;
048:
049: public TextBoundFrame(DemoApplet applet) {
050: this .applet = applet;
051: init();
052: start();
053: }
054:
055: public void run() {
056: /*
057: while (true) {
058: try {
059: checkChange();
060: Thread.sleep(250);
061: }
062: catch (InterruptedException e) {
063: }
064: catch (Exception e) {
065: }
066: catch (Throwable e) {
067: }
068: }
069: */
070: }
071:
072: int s, e;
073: int ts, te;
074:
075: public void checkChange() {
076: // System.out.println("checkChange...");
077: if ((text.getSelectionStart() & 0x7FFF) != ts
078: || (text.getSelectionEnd() & 0x7FFF) != te) {
079:
080: // not used int tempS = text.getSelectionStart() & 0x7FFF;
081: // not used int tempE = text.getSelectionEnd() & 0x7FFF;
082:
083: // System.out.println(">");
084: // select(0, 0);
085: // select(tempS, tempE);
086: //select(tempS - (ts - s), tempE - (te - e));
087: // System.out.println("<");
088:
089: // if (s != ts || e != te) System.out.println(" s("+s+") ts("+ts+") e("+e+") te("+te+")");
090: // if (tempS != ts || tempE != te) System.out.println(">s("+s+") tempS("+tempS+") e("+e+") tempE("+tempE+")");
091: // select(s - (ts - s), e - (te - e));
092: // if (tempS != ts || tempE != te) System.out.println("s("+s+") tempS("+tempS+") e("+e+") tempE("+tempE+")");
093:
094: // System.out.println("lkdslksj");
095: }
096: }
097:
098: public void select(int sIn, int eIn) {
099: s = sIn;
100: e = eIn;
101: text.select(s, e);
102: ts = text.getSelectionStart() & 0x7FFF;
103: te = text.getSelectionEnd() & 0x7FFF;
104: // if (s != ts || e != te) {
105: // System.out.println(">s("+s+") ts("+ts+") e("+e+") te("+te+")");
106: // System.out.println(" "+(ts-s)+","+(te-e));
107: // }
108: }
109:
110: public int getSelectionStart() {
111: checkChange();
112: // return s;
113: return text.getSelectionStart() & 0x7FFF;
114: }
115:
116: public int getSelectionEnd() {
117: checkChange();
118: // return e;
119: return text.getSelectionEnd() & 0x7FFF;
120: }
121:
122: public final synchronized void selectRange(int s, int e) {
123: try {
124: //if (getSelectionStart() != s || getSelectionEnd() != e) {
125: //text.select(s, e);
126: select(s, e);
127: //}
128: // if (getSelectionStart() != s || getSelectionEnd() != e) {
129: // System.out.println("AGH! select("+s+","+e+") -> ("+
130: // getSelectionStart()+","+getSelectionEnd()+")");
131: // text.select(s - (getSelectionStart() - s), e - (getSelectionEnd() - e));
132: // }
133: } catch (Exception exp) {
134: errorText(exp.toString());
135: }
136: }
137:
138: public void init() {
139: buildGUI();
140: }
141:
142: public void start() {
143: }
144:
145: void addWithFont(Container container, Component foo, Font font) {
146: if (font != null)
147: foo.setFont(font);
148: container.add(foo);
149: }
150:
151: public void buildGUI() {
152: setBackground(DemoUtility.bgColor);
153: setLayout(new BorderLayout());
154:
155: Panel topPanel = new Panel();
156:
157: Label titleLabel = new Label("Text Boundary Demo", Label.CENTER);
158: titleLabel.setFont(DemoUtility.titleFont);
159: topPanel.add(titleLabel);
160:
161: //Label demo=new Label(creditString, Label.CENTER);
162: //demo.setFont(DemoUtility.creditFont);
163: //topPanel.add(demo);
164:
165: Panel choicePanel = new Panel();
166:
167: Label demo1 = new Label("Boundaries", Label.LEFT);
168: demo1.setFont(DemoUtility.labelFont);
169: choicePanel.add(demo1);
170:
171: bound = new Choice();
172: bound.setBackground(DemoUtility.choiceColor);
173: bound.addItem("Sentence");
174: bound.addItem("Line Break");
175: bound.addItem("Word");
176: bound.addItem("Char");
177: bound.addItemListener(this );
178: if (choiceFont != null)
179: bound.setFont(choiceFont);
180:
181: choicePanel.add(bound);
182: topPanel.add(choicePanel);
183:
184: DemoUtility.fixGrid(topPanel, 1);
185:
186: add("North", topPanel);
187:
188: int ROWS = 15;
189: int COLUMNS = 50;
190: // text = new TextArea(getInitialText(), ROWS, COLUMNS);
191: text = new JTextArea(getInitialText(), ROWS, COLUMNS);
192: text.setLineWrap(true);
193: text.setWrapStyleWord(true);
194: text.setEditable(true);
195: text.selectAll();
196: text.setFont(DemoUtility.editFont);
197: text.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
198:
199: add("Center", new JScrollPane(text,
200: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
201: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
202:
203: Panel copyrightPanel = new Panel();
204: addWithFont(copyrightPanel, new Label(DemoUtility.copyright1,
205: Label.LEFT), DemoUtility.creditFont);
206: DemoUtility.fixGrid(copyrightPanel, 1);
207: add("South", copyrightPanel);
208:
209: //layout();
210: handleEnumChanged();
211:
212: enableEvents(WindowEvent.WINDOW_CLOSING);
213: enableEvents(KeyEvent.KEY_PRESSED);
214: enableEvents(KeyEvent.KEY_RELEASED);
215:
216: text.addKeyListener(new KeyAdapter() {
217: public void keyPressed(KeyEvent e) {
218: if (e.isControlDown()) {
219: int kc = e.getKeyCode();
220: switch (e.getKeyCode()) {
221: case KeyEvent.VK_N:
222: case KeyEvent.VK_RIGHT:
223: handleForward();
224: break;
225: case KeyEvent.VK_P:
226: case KeyEvent.VK_LEFT:
227: handleBackward();
228: break;
229: default:
230: break;
231: }
232: e.consume();
233: }
234: }
235:
236: public void keyReleased(KeyEvent e) {
237: if (e.isControlDown()) {
238: e.consume();
239: }
240: }
241:
242: public void keyTyped(KeyEvent e) {
243: if (e.isControlDown()) {
244: e.consume();
245: }
246: }
247: });
248:
249: // (new Thread(this)).start();
250: }
251:
252: public String getInitialText() {
253: return
254: /*
255: "\"This is a sentence.\" This is not.\" \"because. And go. " +
256: "This is a simple 012.566,5 sample sentence. \n"+
257: "It does not have to make any sense as you can see. \n"+
258: "Nel mezzo del cammin di nostra vita, mi ritrovai in "+
259: "una selva oscura. \n"+
260: "Che la dritta via aveo smarrita. \n"+
261: "He said, that I said, that you said!! \n"+
262: "Don't rock the boat.\n\n"+
263: "Because I am the daddy, that is why. \n"+
264: "Not on my time (el timo.)! \n"+
265: "Tab\tTab\rTab\tWow."+
266: "So what!!\n\n"+
267: "Is this a question??? " +
268: "I wonder...Hmm.\n" +
269: "Harris thumbed down several, including \"Away We Go\" "+
270: "(which became the huge success Oklahoma!). \n"+
271: "One species, B. anthracis, is highly virulent.\n"+
272: "Wolf said about Sounder: \"Beautifully thought-out and "+
273: "directed.\"\n"+
274: "Have you ever said, \"This is where I shall live\"? \n"+
275: "He 1000,233,456.000 answered, \"You may not!\" \n"+
276: "Another popular saying is: \"How do you do?\". \n"+
277: "What is the proper use of the abbreviation pp.? \n"+
278: "Yes, I am 1,23.322% definatelly 12\" tall!!";
279: */
280: "(\"This is a complete sentence.\") This is (\"not.\") also. "
281: + "An abbreviation in the middle, etc. and one at the end, etc. "
282: + "This "
283: + "is a simple sample 012.566,5 sentence. It doesn't "
284: + "have to make any sense, as you can see. Nel mezzo del c"
285: + "ammin di nostra vita, mi ritrovai in una selva oscura. Che "
286: + "la dritta via aveo smarrita. Not on my time (el timo.)! And "
287: + "tabulated columns: \tCol1\tCol2\t3,456%.\t "
288: + "Is this a question??? I wonder... Hmm. Harris thumbed "
289: + "down several, including \"Away We Go\" (which became the "
290: + "huge success Oklahoma!). One species, B. anthracis, is "
291: + "highly virulent. Wolf said about Sounder: \"Beautifully "
292: + "thought-out and directed.\" Have you ever said, \"This is "
293: + "where I "
294: + "shall live\"? He said 1000,233,456.000 and answered, \"You "
295: + "may not!\" "
296: + "Another popular saying is: \"How do you do?\". What is the "
297: + "proper use of the abbreviation pp.? Yes, I am 12\' 3\" tall!!";
298: }
299:
300: public void handleEnumChanged() {
301: String s = bound.getSelectedItem();
302: if (s.equals("Char")) {
303: errorText("getCharacterInstance");
304: iter = BreakIterator.getCharacterInstance();
305: } else if (s.equals("Word")) {
306: errorText("tWordBreak");
307: iter = BreakIterator.getWordInstance();
308: } else if (s.equals("Line Break")) {
309: errorText("getLineInstance");
310: iter = BreakIterator.getLineInstance();
311: } else /* if (s.equals("Sentence")) */{
312: errorText("getSentenceInstance");
313: iter = BreakIterator.getSentenceInstance();
314: }
315: iter.setText(text.getText());
316: selectRange(0, 0);
317: //text.select(0,0);
318: }
319:
320: public void handleForward() {
321: try {
322: // System.out.println("entering handleForward");
323: iter.setText(text.getText());
324: int oldStart = getSelectionStart();
325: int oldEnd = getSelectionEnd();
326:
327: // System.out.println("handleForward: oldStart=" + oldStart + ", oldEnd=" + oldEnd);
328:
329: if (oldEnd < 1) {
330: selectRange(0, iter.following(0));
331: } else {
332: int s = iter.following(oldEnd - 1);
333: int e = iter.next();
334: if (e == -1) {
335: e = s;
336: }
337: selectRange(s, e);
338: }
339: //text.select(s, e);
340: errorText("<" + oldStart + "," + oldEnd + "> -> <" + s
341: + "," + e + ">");
342: } catch (Exception exp) {
343: errorText(exp.toString());
344: }
345: }
346:
347: public void handleBackward() {
348: try {
349: iter.setText(text.getText());
350: int oldStart = getSelectionStart();
351: int oldEnd = getSelectionEnd();
352: if (oldStart < 1) {
353: selectRange(0, 0);
354: } else {
355: int e = iter.following(oldStart - 1);
356: int s = iter.previous();
357: selectRange(s, e);
358: }
359: //text.select(s, e);
360: errorText("<" + oldStart + "," + oldEnd + "> -> <" + s
361: + "," + e + ">");
362: } catch (Exception exp) {
363: errorText(exp.toString());
364: }
365: }
366:
367: public void itemStateChanged(ItemEvent evt) {
368: if (evt.getSource() instanceof Choice) {
369: handleEnumChanged();
370: }
371: }
372:
373: public void errorText(String s) {
374: if (DEBUG)
375: System.out.println(s);
376: }
377:
378: protected void processWindowEvent(WindowEvent evt) {
379: if (evt.getID() == WindowEvent.WINDOW_CLOSING
380: && evt.getWindow() == this ) {
381: hide();
382: dispose();
383: if (applet != null) {
384: applet.demoClosed();
385: } else
386: System.exit(0);
387: }
388: }
389:
390: protected void processKeyEvent(KeyEvent evt) {
391: switch (evt.getID()) {
392: case KeyEvent.KEY_PRESSED:
393: if (evt.getKeyCode() == KeyEvent.VK_CONTROL) {
394: isctrldown_ = true;
395: }
396: break;
397: case KeyEvent.KEY_RELEASED:
398: // key detection for left and right buttons are removed
399: // to emulate the old release behaviour
400: int key = evt.getKeyCode();
401: if (key == KeyEvent.VK_N && isctrldown_) {
402: handleForward();
403: } else if (key == KeyEvent.VK_P && isctrldown_) {
404: handleBackward();
405: } else if (key == KeyEvent.VK_CONTROL) {
406: isctrldown_ = false;
407: }
408: break;
409: }
410: }
411: }
|