001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: * Created on 11.11.2004
021:
022: */package javax.swing.text;
023:
024: import java.awt.Color;
025: import java.awt.Graphics;
026: import java.awt.Rectangle;
027: import java.awt.Shape;
028: import javax.swing.JFrame;
029: import javax.swing.JTextArea;
030: import javax.swing.SwingWaitTestCase;
031: import junit.framework.AssertionFailedError;
032: import junit.framework.TestCase;
033:
034: public class DefaultHighlighter_DefaultHighlightPainterTest extends
035: TestCase {
036: JTextArea jta;
037:
038: JFrame jf;
039:
040: DefaultHighlighter dh;
041:
042: AssertionFailedError[] afe;
043:
044: DefaultHighlighter.DefaultHighlightPainter lp;
045:
046: boolean bWasException;
047:
048: String s = null;
049:
050: int i = 0;
051:
052: DefPainter dp;
053:
054: String str;
055:
056: Rectangle rectangle;
057:
058: int pos0;
059:
060: int pos1;
061:
062: String sRTL = new String("\u05DC");
063:
064: String sLTR = new String("\u0061");
065:
066: class DefPainter extends DefaultHighlighter.DefaultHighlightPainter {
067: Graphics graphics;
068:
069: int i0;
070:
071: int i1;
072:
073: Shape shape;
074:
075: JTextComponent jtc;
076:
077: View view;
078:
079: DefPainter(final Color c) {
080: super (c);
081: }
082:
083: @Override
084: public Shape paintLayer(final Graphics g, final int p0,
085: final int p1, final Shape sh, final JTextComponent jt,
086: final View v) {
087: graphics = g;
088: i0 = p0;
089: i1 = p1;
090: shape = sh;
091: jtc = jt;
092: view = v;
093: return super .paintLayer(g, p0, p1, sh, jt, v);
094: }
095:
096: public Shape paintAgain() {
097: return paintLayer(graphics, i0, i1, shape, jtc, view);
098: }
099: }
100:
101: /*
102: * @see TestCase#setUp()
103: */
104: @Override
105: protected void setUp() throws Exception {
106: jta = new JTextArea();
107: jf = new JFrame();
108: dh = new DefaultHighlighter();
109: dp = new DefPainter(null);
110: afe = new AssertionFailedError[50];
111: lp = new DefaultHighlighter.DefaultHighlightPainter(Color.red);
112: bWasException = false;
113: s = null;
114: jta.setHighlighter(dh);
115: jf.getContentPane().add(jta);
116: jf.setLocation(200, 300);
117: jf.setSize(200, 300);
118: jf.setVisible(true);
119: SwingWaitTestCase.isRealized(jf);
120: super .setUp();
121: }
122:
123: /*
124: * @see TestCase#tearDown()
125: */
126: @Override
127: protected void tearDown() throws Exception {
128: jf.dispose();
129: super .tearDown();
130: }
131:
132: void throwEx(final AssertionFailedError[] e) {
133: for (int i = 0; i < e.length; i++) {
134: if (e[i] != null) {
135: throw e[i];
136: }
137: }
138: }
139:
140: public void testDefaultHighlightPainter() {
141: assertNotNull(lp);
142: assertEquals(Color.red, lp.getColor());
143: lp = new DefaultHighlighter.DefaultHighlightPainter(null);
144: assertNotNull(lp);
145: assertNull(lp.getColor());
146: }
147:
148: public Shape getShape(final int p0, final int p1,
149: final Shape shape, final View view) {
150: Rectangle r = null;
151: Rectangle shapeBounds = shape.getBounds();
152: try {
153: r = (Rectangle) view.modelToView(Math.min(p0, p1),
154: Position.Bias.Forward, Math.max(p0, p1),
155: Position.Bias.Backward, shapeBounds);
156: } catch (BadLocationException e) {
157: }
158: if (r == null) {
159: return null;
160: }
161: return r;
162: }
163: /* hard link to coordinates
164: public void testPaintLayer() throws Exception{
165: String s1;
166: String s2;
167: paintCase("JTextArea",5,7,new Rectangle(28,0,12,16));
168: paintCase(incString(sLTR,4) + incString(sRTL,4),3,4,
169: new Rectangle(86,0,8,15));
170: paintCase(incString(sLTR,4) + "\n" + incString(sRTL,4),3,4,
171: new Rectangle(21,0,8,15));
172: paintCase(sLTR + sRTL + "\n" + "\n" + sLTR + sRTL + sLTR,2,4,
173: new Rectangle(0,15,1,15));
174: paintCase("JTex\nArea for D\n\n\nefaultHighlighter testing",15,20,
175: new Rectangle(0,60,11,15));
176: s1 = incString(sLTR,3);
177: s2 = incString(sRTL,1);
178: s2 = s2 + "\n" + s2;
179: paintCase(s1 + s2 + s1 + s2,15,16,new Rectangle(52,15,1,15));
180: paintCase(s1 + s2 + s1 + s2,4,7,new Rectangle(40,0,1,15));
181: paintCase(s1 + s2 + s1 + incString(sLTR,1) + "\n" + incString(sLTR,1),
182: 15,16,new Rectangle(54,15,1,15));
183: paintCase(s1 + s2 + s1 + incString(sLTR,1) + "\n" + incString(sLTR,1),
184: 2,5,new Rectangle(46,0,7,15));
185:
186: throwEx(afe);
187: }
188: */
189: }
|