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 Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Component;
023: import java.awt.Graphics;
024: import java.awt.Rectangle;
025: import java.awt.Shape;
026: import java.awt.geom.Ellipse2D;
027: import java.awt.geom.Rectangle2D;
028: import javax.swing.Icon;
029: import javax.swing.JTextPane;
030: import javax.swing.SwingTestCase;
031: import javax.swing.plaf.metal.MetalIconFactory;
032:
033: public class IconViewTest extends SwingTestCase {
034: StyledDocument document;
035:
036: JTextPane textPane;
037:
038: Element iconElement;
039:
040: Icon icon;
041:
042: IconView view;
043:
044: @Override
045: protected void setUp() throws Exception {
046: super .setUp();
047: textPane = new JTextPane();
048: document = textPane.getStyledDocument();
049: icon = MetalIconFactory.getCheckBoxMenuItemIcon();
050: document.insertString(0, "Hello\n word!!!",
051: new SimpleAttributeSet());
052: textPane.setCaretPosition(3);
053: textPane.insertIcon(icon);
054: iconElement = document.getDefaultRootElement().getElement(0)
055: .getElement(1);
056: view = new IconView(iconElement);
057: }
058:
059: public void testIconView() {
060: assertNotNull(StyleConstants.getIcon(iconElement
061: .getAttributes()));
062: iconElement = document.getDefaultRootElement();
063: view = new IconView(iconElement);
064: assertNull(StyleConstants.getIcon(iconElement.getAttributes()));
065: assertNull(view.getParent());
066: testExceptionalCase(new NullPointerCase() {
067: @Override
068: public void exceptionalAction() throws Exception {
069: view.getPreferredSpan(View.X_AXIS);
070: }
071: });
072: testExceptionalCase(new NullPointerCase() {
073: @Override
074: public void exceptionalAction() throws Exception {
075: view.getPreferredSpan(View.Y_AXIS);
076: }
077: });
078: testExceptionalCase(new NullPointerCase() {
079: @Override
080: public void exceptionalAction() throws Exception {
081: view.paint(createTestGraphics(), new Rectangle(10, 10));
082: }
083: });
084: }
085:
086: public void testGetPreferredSpan() {
087: if (isHarmony()) {
088: assertEquals(icon.getIconWidth() + 2, view
089: .getPreferredSpan(View.X_AXIS), 1);
090: } else {
091: assertEquals(icon.getIconWidth(), view
092: .getPreferredSpan(View.X_AXIS), 1);
093: }
094: assertEquals(icon.getIconHeight(), view
095: .getPreferredSpan(View.Y_AXIS), 1);
096: testExceptionalCase(new IllegalArgumentCase() {
097: @Override
098: public void exceptionalAction() throws Exception {
099: view.getPreferredSpan(2);
100: }
101: });
102: testExceptionalCase(new IllegalArgumentCase() {
103: @Override
104: public void exceptionalAction() throws Exception {
105: view.getPreferredSpan(-1);
106: }
107: });
108: MutableAttributeSet attrs = new SimpleAttributeSet();
109: icon = MetalIconFactory.getRadioButtonIcon();
110: StyleConstants.setIcon(attrs, icon);
111: document.setCharacterAttributes(3, 1, attrs, true);
112: view = new IconView(iconElement);
113: if (isHarmony()) {
114: assertEquals(icon.getIconWidth() + 2, view
115: .getPreferredSpan(View.X_AXIS), 0.1);
116: } else {
117: assertEquals(icon.getIconWidth(), view
118: .getPreferredSpan(View.X_AXIS), 0.1);
119: }
120: assertEquals(icon.getIconHeight(), view
121: .getPreferredSpan(View.Y_AXIS), 0.1);
122: }
123:
124: public void testGetAlignment() {
125: assertEquals(View.ALIGN_CENTER, view.getAlignment(View.X_AXIS),
126: 0.1);
127: assertEquals(View.ALIGN_RIGHT, view.getAlignment(View.Y_AXIS),
128: 0.1);
129: assertEquals(View.ALIGN_CENTER, view.getAlignment(2), 0.1);
130: assertEquals(View.ALIGN_CENTER, view.getAlignment(-1), 0.1);
131: }
132:
133: public void testModelToView() throws BadLocationException {
134: testExceptionalCase(new BadLocationCase() {
135: @Override
136: public void exceptionalAction() throws Exception {
137: view.modelToView(1, new Rectangle(),
138: Position.Bias.Backward);
139: }
140: });
141: testExceptionalCase(new BadLocationCase() {
142: @Override
143: public void exceptionalAction() throws Exception {
144: view.modelToView(5, new Rectangle(),
145: Position.Bias.Forward);
146: }
147: });
148: assertEquals(3, view.getStartOffset());
149: view.modelToView(3, new Rectangle(), Position.Bias.Backward);
150: view.modelToView(3, new Rectangle(), Position.Bias.Forward);
151: assertEquals(4, view.getEndOffset());
152: view.modelToView(4, new Rectangle(), Position.Bias.Backward);
153: view.modelToView(4, new Rectangle(), Position.Bias.Forward);
154: testExceptionalCase(new BadLocationCase() {
155: @Override
156: public void exceptionalAction() throws Exception {
157: view.modelToView(500, new Rectangle(),
158: Position.Bias.Forward);
159: }
160: });
161: Shape box;
162: box = view.modelToView(3, new Rectangle(2, 5, 20, 30),
163: Position.Bias.Backward);
164: assertNotNull(box);
165: checkBounds(2, 5, 30, box);
166: box = view.modelToView(3, new Rectangle(2, 3, icon
167: .getIconWidth(), icon.getIconHeight()),
168: Position.Bias.Backward);
169: assertNotNull(box);
170: checkBounds(2, 3, icon.getIconHeight(), box);
171: box = view.modelToView(4, new Rectangle(1, 1, 16, 7),
172: Position.Bias.Forward);
173: assertNotNull(box);
174: checkBounds(17, 1, 7, box);
175: box = view.modelToView(4, new Rectangle(0, 0, 1, 1),
176: Position.Bias.Forward);
177: assertNotNull(box);
178: checkBounds(1, 0, 1, box);
179: Ellipse2D ellipse = new Ellipse2D.Float(25, 3, 30, 40);
180: box = view.modelToView(4, ellipse, Position.Bias.Forward);
181: checkBounds(55, 3, 40, box);
182: box = view.modelToView(4, ellipse, Position.Bias.Backward);
183: assertNotNull(box);
184: checkBounds(55, 3, 40, box);
185: Rectangle2D rect = new Rectangle2D() {
186: @Override
187: public void setRect(double x, double y, double width,
188: double height) {
189: }
190:
191: @Override
192: public int outcode(double x, double y) {
193: return 0;
194: }
195:
196: @Override
197: public Rectangle2D createIntersection(Rectangle2D r) {
198: return null;
199: }
200:
201: @Override
202: public Rectangle2D createUnion(Rectangle2D r) {
203: return null;
204: }
205:
206: @Override
207: public double getX() {
208: return 1;
209: }
210:
211: @Override
212: public double getY() {
213: return 2;
214: }
215:
216: @Override
217: public double getWidth() {
218: return 50;
219: }
220:
221: @Override
222: public double getHeight() {
223: return 60;
224: }
225:
226: @Override
227: public boolean isEmpty() {
228: return false;
229: }
230: };
231: box = view.modelToView(4, rect, Position.Bias.Backward);
232: assertNotNull(box);
233: checkBounds(51, 2, 60, box);
234: }
235:
236: public void testPaint() {
237: Shape shape;
238: Graphics g = createTestGraphics();
239: shape = new Rectangle(4, 3, 2, 3);
240: checkPaintIcon(g, shape, 10, 15);
241: shape = new Rectangle(4, 3, 30, 40);
242: checkPaintIcon(g, shape, 10, 15);
243: shape = new Ellipse2D() {
244: @Override
245: public double getX() {
246: return 12;
247: }
248:
249: @Override
250: public double getY() {
251: return 4;
252: }
253:
254: @Override
255: public double getWidth() {
256: return 30;
257: }
258:
259: @Override
260: public double getHeight() {
261: return 5;
262: }
263:
264: @Override
265: public boolean isEmpty() {
266: return false;
267: }
268:
269: @Override
270: public void setFrame(double x, double y, double w, double h) {
271: return;
272: }
273:
274: public Rectangle2D getBounds2D() {
275: return null;
276: }
277: };
278: checkPaintIcon(g, shape, 2, 4);
279: }
280:
281: public void testViewToModel() {
282: Shape shape;
283: shape = new Rectangle(4, 3, 20, 30);
284: checkViewToModel(shape, 1, 1);
285: checkViewToModel(shape, 1, 5);
286: checkViewToModel(shape, 1, 15);
287: checkViewToModel(shape, 1, -5);
288: checkViewToModel(shape, 12, 5);
289: checkViewToModel(shape, 15, 5);
290: checkViewToModel(shape, 16, 5);
291: checkViewToModel(shape, 30, 5);
292: checkViewToModel(shape, 46, 5);
293: final Ellipse2D floatEllipse = new Ellipse2D.Float(25, 3, 3, 4);
294: checkViewToModelWithEllipse(floatEllipse);
295: Ellipse2D intEllipse = new Ellipse2D() {
296: @Override
297: public double getX() {
298: return 25;
299: }
300:
301: @Override
302: public double getY() {
303: return 3;
304: }
305:
306: @Override
307: public double getWidth() {
308: return 3;
309: }
310:
311: @Override
312: public double getHeight() {
313: return 4;
314: }
315:
316: @Override
317: public boolean isEmpty() {
318: return false;
319: }
320:
321: @Override
322: public void setFrame(double x, double y, double w, double h) {
323: return;
324: }
325:
326: public Rectangle2D getBounds2D() {
327: return null;
328: }
329: };
330: checkViewToModelWithEllipse(intEllipse);
331: }
332:
333: private void checkViewToModelWithEllipse(final Ellipse2D ellipse) {
334: if (isHarmony()) {
335: checkViewToModel(ellipse, 25, 15);
336: checkViewToModel(ellipse, 26, 5);
337: } else {
338: testExceptionalCase(new ClassCastCase() {
339: @Override
340: public void exceptionalAction() throws Exception {
341: checkViewToModel(ellipse, 25, 15);
342: }
343: });
344: testExceptionalCase(new ClassCastCase() {
345: @Override
346: public void exceptionalAction() throws Exception {
347: checkViewToModel(ellipse, 26, 5);
348: }
349: });
350: }
351: }
352:
353: private void checkBounds(int x, int y, int height, Shape box) {
354: Rectangle bounds = box.getBounds();
355: assertEquals(x, bounds.x);
356: assertEquals(y, bounds.y);
357: assertEquals(0, bounds.width);
358: assertEquals(height, bounds.height);
359: }
360:
361: private void checkViewToModel(Shape shape, int x, int y) {
362: Position.Bias[] bias = new Position.Bias[1];
363: int position = view.viewToModel(x, y, shape, bias);
364: if (x > shape.getBounds().width / 2 + shape.getBounds().x - 1) {
365: assertEquals(Position.Bias.Backward, bias[0]);
366: assertEquals(position, iconElement.getEndOffset());
367: } else {
368: assertEquals(Position.Bias.Forward, bias[0]);
369: assertEquals(position, iconElement.getStartOffset());
370: }
371: }
372:
373: private void checkPaintIcon(Graphics g, Shape shape, int iconWidth,
374: int iconHeight) {
375: textPane.setCaretPosition(3);
376: icon = createIcon(shape, iconWidth, iconHeight);
377: textPane.insertIcon(icon);
378: iconElement = document.getDefaultRootElement().getElement(0)
379: .getElement(1);
380: view = new IconView(iconElement);
381: assertEquals(StyleConstants.getIcon(view.getElement()
382: .getAttributes()), icon);
383: view.paint(g, shape);
384: }
385:
386: private Icon createIcon(Shape shape, final int width,
387: final int height) {
388: final Rectangle bounds = shape.getBounds();
389: return new Icon() {
390: public void paintIcon(Component c, Graphics g, int x, int y) {
391: if (isHarmony()) {
392: assertEquals(x, bounds.x + 1);
393: } else {
394: assertEquals(x, bounds.x);
395: }
396: assertEquals(y, bounds.y);
397: }
398:
399: public int getIconWidth() {
400: return width;
401: }
402:
403: public int getIconHeight() {
404: return height;
405: }
406: };
407: }
408: }
|