]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/demos/src/com/ibm/icu/dev/demo/impl/DemoTextBox.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / demos / src / com / ibm / icu / dev / demo / impl / DemoTextBox.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1997-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.demo.impl;
8
9
10 import java.awt.FontMetrics;
11 import java.awt.Graphics;
12 import java.text.BreakIterator;
13
14 public class DemoTextBox {
15
16     public DemoTextBox(Graphics g, String text, int width)
17     {
18         this.text = text;
19         this.chars = new char[text.length()];
20         text.getChars(0, text.length(), chars, 0);
21
22         this.width = width;
23 //        this.port = g;
24         this.metrics = g.getFontMetrics();
25
26         breakText();
27     }
28
29     public  int getHeight() {
30         return (nbreaks + 1) * metrics.getHeight();
31     }
32
33     public  void draw(Graphics g, int x, int y)
34     {
35         int index = 0;
36
37         y += metrics.getAscent();
38
39         for (int i = 0; i < nbreaks; i++)
40         {
41             g.drawChars(chars, index, breakPos[i] - index, x, y);
42             index = breakPos[i];
43             y += metrics.getHeight();
44         }
45
46         g.drawChars(chars, index, chars.length - index, x, y);
47     }
48
49
50     private void breakText()
51     {
52         if (metrics.charsWidth(chars, 0, chars.length) > width)
53         {
54             BreakIterator iter = BreakIterator.getWordInstance();
55             iter.setText(text);
56
57             int start = iter.first();
58             int end = start;
59             int pos;
60
61             while ( (pos = iter.next()) != BreakIterator.DONE )
62             {
63                 int w = metrics.charsWidth(chars, start, pos - start);
64                 if (w > width)
65                 {
66                     // We've gone past the maximum width, so break the line
67                     if (end > start) {
68                         // There was at least one break position before this point
69                         breakPos[nbreaks++] = end;
70                         start = end;
71                         end = pos;
72                     } else {
73                         // There weren't any break positions before this one, so
74                         // let this word overflow the margin (yuck)
75                         breakPos[nbreaks++] = pos;
76                         start = end = pos;
77                     }
78                 } else {
79                     // the current position still fits on the line; it's the best
80                     // tentative break position we have so far.
81                     end = pos;
82                 }
83
84             }
85         }
86     }
87
88     private String          text;
89     private char[]          chars;
90 //    private Graphics        port;
91     private FontMetrics     metrics;
92     private int             width;
93
94     private int[]           breakPos = new int[10]; // TODO: get real
95     private int             nbreaks = 0;
96 }