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.WindowEvent;
011: import java.awt.event.KeyAdapter;
012: import java.awt.event.KeyEvent;
013: import java.awt.event.ItemListener;
014: import java.awt.event.ItemEvent;
015: import javax.swing.JTextArea;
016: import javax.swing.JScrollPane;
017: import javax.swing.BorderFactory;
018: import java.util.*;
019: import com.ibm.icu.dev.demo.impl.*;
020: import com.ibm.icu.text.BreakIterator;
021:
022: public class RBBIDemo extends DemoApplet {
023: public static void main(String argv[]) {
024: Locale.setDefault(new Locale("en", "US"));
025: new RBBIDemo().showDemo();
026: }
027:
028: public Frame createDemoFrame(DemoApplet applet) {
029: return new RBBIFrame(applet);
030: }
031: }
032:
033: class RBBIFrame extends Frame implements ItemListener {
034: private static final String creditString = "v1.1a9, Demo";
035:
036: private static final int FIELD_COLUMNS = 45;
037: private static final Font choiceFont = null;
038: private static final boolean DEBUG = false;
039: private DemoApplet applet;
040:
041: final String right = "-->";
042: final String left = "<--";
043:
044: private BreakIterator iter;
045: private boolean isctrldown_ = false;
046:
047: JTextArea text;
048: // TextArea text;
049: Choice bound;
050:
051: public RBBIFrame(DemoApplet applet) {
052: this .applet = applet;
053: init();
054: start();
055: }
056:
057: public void run() {
058: /*
059: while (true) {
060: try {
061: checkChange();
062: Thread.sleep(250);
063: }
064: catch (InterruptedException e) {
065: }
066: catch (Exception e) {
067: }
068: catch (Throwable e) {
069: }
070: }
071: */
072: }
073:
074: int s, e;
075: int ts, te;
076:
077: public void checkChange() {
078: // System.out.println("checkChange...");
079: if ((text.getSelectionStart() & 0x7FFF) != ts
080: || (text.getSelectionEnd() & 0x7FFF) != te) {
081:
082: // not used int tempS = text.getSelectionStart() & 0x7FFF;
083: // not used int tempE = text.getSelectionEnd() & 0x7FFF;
084:
085: // System.out.println(">");
086: // select(0, 0);
087: // select(tempS, tempE);
088: //select(tempS - (ts - s), tempE - (te - e));
089: // System.out.println("<");
090:
091: // if (s != ts || e != te) System.out.println(" s("+s+") ts("+ts+") e("+e+") te("+te+")");
092: // if (tempS != ts || tempE != te) System.out.println(">s("+s+") tempS("+tempS+") e("+e+") tempE("+tempE+")");
093: // select(s - (ts - s), e - (te - e));
094: // if (tempS != ts || tempE != te) System.out.println("s("+s+") tempS("+tempS+") e("+e+") tempE("+tempE+")");
095:
096: // System.out.println("lkdslksj");
097: }
098: }
099:
100: public void select(int sIn, int eIn) {
101: s = sIn;
102: e = eIn;
103: text.select(s, e);
104: ts = text.getSelectionStart() & 0x7FFF;
105: te = text.getSelectionEnd() & 0x7FFF;
106: // if (s != ts || e != te) {
107: // System.out.println(">s("+s+") ts("+ts+") e("+e+") te("+te+")");
108: // System.out.println(" "+(ts-s)+","+(te-e));
109: // }
110: }
111:
112: public int getSelectionStart() {
113: checkChange();
114: // return s;
115: return text.getSelectionStart() & 0x7FFF;
116: }
117:
118: public int getSelectionEnd() {
119: checkChange();
120: // return e;
121: return text.getSelectionEnd() & 0x7FFF;
122: }
123:
124: public final synchronized void selectRange(int s, int e) {
125: try {
126: //if (getSelectionStart() != s || getSelectionEnd() != e) {
127: //text.select(s, e);
128: select(s, e);
129: //}
130: // if (getSelectionStart() != s || getSelectionEnd() != e) {
131: // System.out.println("AGH! select("+s+","+e+") -> ("+
132: // getSelectionStart()+","+getSelectionEnd()+")");
133: // text.select(s - (getSelectionStart() - s), e - (getSelectionEnd() - e));
134: // }
135: } catch (Exception exp) {
136: errorText(exp.toString());
137: }
138: }
139:
140: public void init() {
141: buildGUI();
142: }
143:
144: public void start() {
145: }
146:
147: void addWithFont(Container container, Component foo, Font font) {
148: if (font != null)
149: foo.setFont(font);
150: container.add(foo);
151: }
152:
153: public void buildGUI() {
154: setBackground(DemoUtility.bgColor);
155: setLayout(new BorderLayout());
156:
157: Panel topPanel = new Panel();
158:
159: Label titleLabel = new Label("Deva Text Boundary Demo",
160: Label.CENTER);
161: titleLabel.setFont(DemoUtility.titleFont);
162: topPanel.add(titleLabel);
163:
164: //Label demo=new Label(creditString, Label.CENTER);
165: //demo.setFont(DemoUtility.creditFont);
166: //topPanel.add(demo);
167:
168: Panel choicePanel = new Panel();
169:
170: Label demo1 = new Label("Boundaries", Label.LEFT);
171: demo1.setFont(DemoUtility.labelFont);
172: choicePanel.add(demo1);
173:
174: bound = new Choice();
175: bound.setBackground(DemoUtility.choiceColor);
176: bound.addItem("Sentence");
177: bound.addItem("Line Break");
178: bound.addItem("Word");
179: bound.addItem("Char");
180: bound.addItemListener(this );
181: if (choiceFont != null)
182: bound.setFont(choiceFont);
183:
184: choicePanel.add(bound);
185: topPanel.add(choicePanel);
186:
187: DemoUtility.fixGrid(topPanel, 1);
188:
189: add("North", topPanel);
190:
191: int ROWS = 15;
192: int COLUMNS = 50;
193: // text = new TextArea(getInitialText(), ROWS, COLUMNS);
194: text = new JTextArea(getInitialText(), ROWS, COLUMNS);
195: text.setLineWrap(true);
196: text.setWrapStyleWord(true);
197: text.setEditable(true);
198: text.selectAll();
199: text.setFont(new Font("Devanagari MT for IBM", Font.PLAIN, 48));
200: text.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
201:
202: add("Center", new JScrollPane(text,
203: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
204: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
205:
206: Panel copyrightPanel = new Panel();
207: addWithFont(copyrightPanel, new Label(DemoUtility.copyright1,
208: Label.LEFT), DemoUtility.creditFont);
209: DemoUtility.fixGrid(copyrightPanel, 1);
210: add("South", copyrightPanel);
211:
212: //layout();
213: handleEnumChanged();
214:
215: enableEvents(WindowEvent.WINDOW_CLOSING);
216: enableEvents(KeyEvent.KEY_PRESSED);
217: enableEvents(KeyEvent.KEY_RELEASED);
218:
219: text.addKeyListener(new KeyAdapter() {
220: public void keyPressed(KeyEvent e) {
221: if (e.isControlDown()) {
222: int kc = e.getKeyCode();
223: switch (e.getKeyCode()) {
224: case KeyEvent.VK_N:
225: case KeyEvent.VK_RIGHT:
226: handleForward();
227: break;
228: case KeyEvent.VK_P:
229: case KeyEvent.VK_LEFT:
230: handleBackward();
231: break;
232: default:
233: break;
234: }
235: e.consume();
236: }
237: }
238:
239: public void keyReleased(KeyEvent e) {
240: if (e.isControlDown()) {
241: e.consume();
242: }
243: }
244:
245: public void keyTyped(KeyEvent e) {
246: if (e.isControlDown()) {
247: e.consume();
248: }
249: }
250: });
251:
252: // (new Thread(this)).start();
253: }
254:
255: public String getInitialText() {
256: return "\u0936\u094d\u0930\u0940\u092e\u0926\u094d "
257: + "\u092d\u0917\u0935\u0926\u094d\u0917\u0940\u0924\u093e "
258: + "\u0905\u0927\u094d\u092f\u093e\u092f "
259: + "\u0905\u0930\u094d\u091c\u0941\u0928 "
260: + "\u0935\u093f\u0937\u093e\u0926 "
261: + "\u092f\u094b\u0917 "
262: + "\u0927\u0943\u0924\u0930\u093e\u0937\u094d\u091f\u094d\u0930 "
263: + "\u0909\u0935\u093E\u091A\u0964 "
264: + "\u0927\u0930\u094d\u092e\u0915\u094d\u0937\u0947\u0924\u094d\u0930\u0947 "
265: + "\u0915\u0941\u0930\u0941\u0915\u094d\u0937\u0947\u0924\u094d\u0930\u0947 "
266: + "\u0938\u092e\u0935\u0947\u0924\u093e "
267: + "\u092f\u0941\u092f\u0941\u0924\u094d\u0938\u0935\u0903 "
268: + "\u092e\u093e\u092e\u0915\u093e\u0903 "
269: + "\u092a\u093e\u0923\u094d\u0921\u0935\u093e\u0936\u094d\u091a\u0948\u0935 "
270: + "\u0915\u093f\u092e\u0915\u0941\u0930\u094d\u0935\u0924 "
271: + "\u0938\u0902\u091c\u092F";
272: }
273:
274: public void handleEnumChanged() {
275: String s = bound.getSelectedItem();
276: if (s.equals("Char")) {
277: errorText("getCharacterInstance");
278: iter = BreakIterator.getCharacterInstance();
279: } else if (s.equals("Word")) {
280: errorText("tWordBreak");
281: iter = BreakIterator.getWordInstance();
282: } else if (s.equals("Line Break")) {
283: errorText("getLineInstance");
284: iter = BreakIterator.getLineInstance();
285: } else /* if (s.equals("Sentence")) */{
286: errorText("getSentenceInstance");
287: iter = BreakIterator.getSentenceInstance();
288: }
289: iter.setText(text.getText());
290: selectRange(0, 0);
291: //text.select(0,0);
292: }
293:
294: public void handleForward() {
295: try {
296: // System.out.println("entering handleForward");
297: iter.setText(text.getText());
298: int oldStart = getSelectionStart();
299: int oldEnd = getSelectionEnd();
300:
301: // System.out.println("handleForward: oldStart=" + oldStart + ", oldEnd=" + oldEnd);
302:
303: if (oldEnd < 1) {
304: selectRange(0, iter.following(0));
305: } else {
306: int s = iter.following(oldEnd - 1);
307: int e = iter.next();
308: if (e == -1) {
309: e = s;
310: }
311: selectRange(s, e);
312: }
313: //text.select(s, e);
314: errorText("<" + oldStart + "," + oldEnd + "> -> <" + s
315: + "," + e + ">");
316: } catch (Exception exp) {
317: errorText(exp.toString());
318: }
319: }
320:
321: public void handleBackward() {
322: try {
323: iter.setText(text.getText());
324: int oldStart = getSelectionStart();
325: int oldEnd = getSelectionEnd();
326: if (oldStart < 1) {
327: selectRange(0, 0);
328: } else {
329: int e = iter.following(oldStart - 1);
330: int s = iter.previous();
331: selectRange(s, e);
332: }
333: //text.select(s, e);
334: errorText("<" + oldStart + "," + oldEnd + "> -> <" + s
335: + "," + e + ">");
336: } catch (Exception exp) {
337: errorText(exp.toString());
338: }
339: }
340:
341: /*
342: public boolean action(Event evt, Object obj)
343: {
344:
345: if(evt.target instanceof Button && left.equals(obj))
346: {
347: handleBackward();
348: return true;
349: }
350: else if(evt.target instanceof Button && right.equals(obj))
351: {
352: handleForward();
353: return true;
354: }
355: else if(evt.target instanceof Choice)
356: {
357: handleEnumChanged();
358: return true;
359: }
360: return false;
361: }
362:
363: public boolean handleEvent(Event evt)
364: {
365: if (evt.id == Event.KEY_PRESS || evt.id == Event.KEY_ACTION) {
366: if (evt.key == Event.RIGHT || (evt.key == 0x0E && evt.controlDown())) {
367: handleForward();
368: return true;
369: }
370: else if (evt.key == Event.LEFT || (evt.key == 0x10 && evt.controlDown())) {
371: handleBackward();
372: return true;
373: }
374: }
375: else
376: if (evt.id == Event.WINDOW_DESTROY && evt.target == this) {
377: this.hide();
378: this.dispose();
379: if (applet != null) {
380: applet.demoClosed();
381: } else System.exit(0);
382: return true;
383: }
384: return super.handleEvent(evt);
385: }*/
386:
387: public void itemStateChanged(ItemEvent evt) {
388: if (evt.getSource() instanceof Choice) {
389: handleEnumChanged();
390: }
391: }
392:
393: public void errorText(String s) {
394: if (DEBUG)
395: System.out.println(s);
396: }
397:
398: protected void processWindowEvent(WindowEvent evt) {
399: if (evt.getID() == WindowEvent.WINDOW_CLOSING
400: && evt.getWindow() == this ) {
401: hide();
402: dispose();
403: if (applet != null) {
404: applet.demoClosed();
405: } else
406: System.exit(0);
407: }
408: }
409:
410: protected void processKeyEvent(KeyEvent evt) {
411: switch (evt.getID()) {
412: case KeyEvent.KEY_PRESSED:
413: if (evt.getKeyCode() == KeyEvent.VK_CONTROL) {
414: isctrldown_ = true;
415: }
416: break;
417: case KeyEvent.KEY_RELEASED:
418: // key detection for left and right buttons are removed
419: // to emulate the old release behaviour
420: int key = evt.getKeyCode();
421: if (key == KeyEvent.VK_N && isctrldown_) {
422: handleForward();
423: } else if (key == KeyEvent.VK_P && isctrldown_) {
424: handleBackward();
425: } else if (key == KeyEvent.VK_CONTROL) {
426: isctrldown_ = false;
427: }
428: break;
429: }
430: }
431: }
|