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;
021:
022: import java.awt.Component;
023: import java.awt.Graphics;
024: import java.util.Enumeration;
025: import javax.swing.JEditorPane.PlainEditorKit;
026: import javax.swing.plaf.metal.MetalIconFactory;
027: import javax.swing.text.AttributeSet;
028: import javax.swing.text.BadLocationException;
029: import javax.swing.text.DefaultStyledDocument;
030: import javax.swing.text.EditorKit;
031: import javax.swing.text.Element;
032: import javax.swing.text.MutableAttributeSet;
033: import javax.swing.text.PlainDocument;
034: import javax.swing.text.SimpleAttributeSet;
035: import javax.swing.text.Style;
036: import javax.swing.text.StyleConstants;
037: import javax.swing.text.StyleContext;
038: import javax.swing.text.StyledDocument;
039: import javax.swing.text.StyledEditorKit;
040:
041: //TODO: add multhithreaded tests for all thread-safe
042: public class JTextPaneTest extends SwingTestCase {
043: private JTextPane textPane;
044:
045: private MutableAttributeSet attrs;
046:
047: @Override
048: protected void setUp() throws Exception {
049: super .setUp();
050: textPane = new JTextPane();
051: // init character attributeSet
052: attrs = new SimpleAttributeSet();
053: StyleConstants.setStrikeThrough(attrs, true);
054: StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_CENTER);
055: StyleConstants.setUnderline(attrs, true);
056: textPane.getDocument().insertString(0, "Hello !", attrs);
057: StyleConstants.setUnderline(attrs, false);
058: textPane.getDocument().insertString(6, "world", attrs);
059: textPane.getDocument().insertString(12,
060: "\n World is beautifull!", attrs);
061: }
062:
063: public void testJTextPane() {
064: assertNotNull(textPane.getDocument());
065: assertNotNull(textPane.getEditorKit());
066: }
067:
068: public void testGetUIClassID() {
069: assertNull(null);
070: assertEquals("TextPaneUI", textPane.getUIClassID());
071: }
072:
073: public void testSetDocument() {
074: StyledDocument doc = new DefaultStyledDocument();
075: textPane.setDocument(doc);
076: assertSame(doc, textPane.getDocument());
077: testExceptionalCase(new IllegalArgumentCase() {
078: @Override
079: public void exceptionalAction() throws Exception {
080: textPane.setDocument(new PlainDocument());
081: }
082: });
083: testExceptionalCase(new IllegalArgumentCase() {
084: @Override
085: public void exceptionalAction() throws Exception {
086: textPane.setDocument(null);
087: }
088: });
089: }
090:
091: public void testSetStyledDocument() {
092: StyledDocument doc = new DefaultStyledDocument();
093: textPane.setDocument(doc);
094: assertSame(doc, textPane.getDocument());
095: testExceptionalCase(new IllegalArgumentCase() {
096: @Override
097: public void exceptionalAction() throws Exception {
098: textPane.setDocument(new PlainDocument());
099: }
100: });
101: }
102:
103: public void testGetStyledEditorKit() {
104: assertSame(textPane.getEditorKit(), textPane
105: .getStyledEditorKit());
106: assertSame(textPane.getStyledEditorKit(), textPane
107: .getStyledEditorKit());
108: }
109:
110: public void testCreateDefaultEditorKit() {
111: EditorKit editorKit1 = textPane.createDefaultEditorKit();
112: EditorKit editorKit2 = textPane.createDefaultEditorKit();
113: assertNotSame(editorKit1, editorKit2);
114: assertEquals("javax.swing.text.StyledEditorKit", editorKit1
115: .getClass().getName());
116: }
117:
118: public void testSetEditorKit() {
119: testExceptionalCase(new IllegalArgumentCase() {
120: @Override
121: public void exceptionalAction() throws Exception {
122: textPane.setEditorKit(new PlainEditorKit());
123: }
124: });
125: testExceptionalCase(new IllegalArgumentCase() {
126: @Override
127: public void exceptionalAction() throws Exception {
128: textPane.setEditorKit(null);
129: }
130: });
131: }
132:
133: public void testGetStyle() {
134: Style style;
135: style = textPane.getStyle(StyleContext.DEFAULT_STYLE);
136: assertEquals(StyleContext.DEFAULT_STYLE, style.getName());
137: assertSame(textPane.getStyledDocument().getStyle(
138: StyleContext.DEFAULT_STYLE), textPane
139: .getStyle(StyleContext.DEFAULT_STYLE));
140: textPane.addStyle("child", style);
141: style = textPane.getStyle("child");
142: assertEquals("child", style.getName());
143: assertEquals(StyleContext.DEFAULT_STYLE, ((Style) style
144: .getResolveParent()).getName());
145: assertSame(textPane.getStyledDocument().getStyle("child"),
146: textPane.getStyle("child"));
147: }
148:
149: public void testAddStyle() {
150: Style parent = textPane.addStyle("parent", null);
151: Style child = textPane.addStyle("child", parent);
152: assertEquals(1, parent.getAttributeCount());
153: assertNull(parent.getResolveParent());
154: assertEquals(2, child.getAttributeCount());
155: assertNotNull(child.getResolveParent());
156: parent.addAttribute(StyleConstants.Bold, Boolean.FALSE);
157: child.addAttribute(StyleConstants.Bold, Boolean.TRUE);
158: assertFalse(((Boolean) parent.getAttribute(StyleConstants.Bold))
159: .booleanValue());
160: assertTrue(((Boolean) child.getAttribute(StyleConstants.Bold))
161: .booleanValue());
162: // Add styles with diff parameters
163: Style parent1 = textPane.addStyle("p1", null);
164: Style parent2 = textPane.addStyle("p2", null);
165: Object[] styles = { null, null, "one", null, null, parent1,
166: "two", parent2 };
167: for (int i = 0; i < styles.length; i += 2) {
168: Style style = textPane.addStyle((String) styles[i],
169: (Style) styles[i + 1]);
170: assertEquals("Iteration: " + i, (String) styles[i], style
171: .getName());
172: assertSame("Iteration: " + i, styles[i + 1], style
173: .getResolveParent());
174: }
175: // unnamed style
176: Style anotherChild = textPane.addStyle(null, parent);
177: assertEquals(1, anotherChild.getAttributeCount());
178: assertNotNull(anotherChild.getResolveParent());
179: //not unique name of the style
180: Style anotherStyle;
181: anotherStyle = textPane.addStyle("child", null);
182: assertNotSame(child, anotherStyle);
183: assertNotNull(anotherStyle);
184: anotherStyle = textPane.addStyle("child", parent);
185: assertNotSame(child, anotherStyle);
186: assertNotNull(anotherStyle);
187: }
188:
189: public void testRemoveStyle() {
190: Style parent = textPane.addStyle("parent", null);
191: Style child = textPane.addStyle("child", parent);
192: textPane.removeStyle("parent");
193: assertNull(textPane.getStyle("parent"));
194: assertEquals(2, child.getAttributeCount());
195: assertNotNull(child.getResolveParent());
196: assertNull(child.getAttribute("resolver"));
197: }
198:
199: public void testGetLogicalStyle() throws BadLocationException {
200: textPane.getStyledDocument().insertString(11, "bold", attrs);
201: Style style = textPane.addStyle("bold", textPane
202: .getStyle(StyleContext.DEFAULT_STYLE));
203: textPane.setCaretPosition(1);
204: style.addAttribute(StyleConstants.Bold, Boolean.TRUE);
205: textPane.setLogicalStyle(style);
206: style = textPane.getLogicalStyle();
207: textPane.setCaretPosition(3);
208: assertSame(style, textPane.getLogicalStyle());
209: assertTrue(((Boolean) style.getAttribute(StyleConstants.Bold))
210: .booleanValue());
211: attrs = new SimpleAttributeSet();
212: StyleConstants.setBold(attrs, true);
213: textPane.setParagraphAttributes(attrs, true);
214: assertNull(textPane.getLogicalStyle());
215: }
216:
217: public void testSetLogicalStyle() throws BadLocationException {
218: // Set text
219: attrs = textPane.getInputAttributes();
220: attrs.removeAttributes(attrs.getAttributeNames());
221: textPane.setText("");
222: StyleConstants.setBold(attrs, true);
223: textPane.getStyledDocument().insertString(0, "bold", attrs);
224: StyleConstants.setBold(attrs, false);
225: StyleConstants.setItalic(attrs, true);
226: textPane.getStyledDocument().insertString(4, "italic\n", attrs);
227: StyleConstants.setItalic(attrs, false);
228: StyleConstants.setBold(attrs, true);
229: // Add style
230: Style style = textPane.addStyle("bold", textPane
231: .getStyle(StyleContext.DEFAULT_STYLE));
232: // Set style
233: textPane.setCaretPosition(1);
234: style.addAttribute(StyleConstants.Bold, Boolean.TRUE);
235: assertFalse(StyleConstants.isBold(textPane
236: .getParagraphAttributes()));
237: textPane.setLogicalStyle(style);
238: style = textPane.getLogicalStyle();
239: textPane.setCaretPosition(3);
240: assertSame(style, textPane.getLogicalStyle());
241: assertTrue(((Boolean) style.getAttribute(StyleConstants.Bold))
242: .booleanValue());
243: assertTrue(StyleConstants.isBold(textPane
244: .getParagraphAttributes()));
245: assertTrue(StyleConstants.isBold(getCharacterAttributes(1)));
246: // Set paragraph attributes
247: attrs = new SimpleAttributeSet();
248: StyleConstants.setBold(attrs, true);
249: textPane.setParagraphAttributes(attrs, true);
250: assertNull(textPane.getLogicalStyle());
251: // Set another style
252: textPane.getStyledDocument().setCharacterAttributes(1, 1,
253: attrs, true);
254: assertFalse(StyleConstants.isUnderline(textPane
255: .getParagraphAttributes()));
256: assertFalse(StyleConstants
257: .isUnderline(getCharacterAttributes(1)));
258: style = textPane.addStyle("underline", textPane
259: .getStyle(StyleContext.DEFAULT_STYLE));
260: style.addAttribute(StyleConstants.Underline, Boolean.TRUE);
261: textPane.setLogicalStyle(style);
262: assertNotNull(textPane.getLogicalStyle());
263: assertEquals("underline", textPane.getLogicalStyle()
264: .getAttribute(StyleConstants.NameAttribute));
265: assertTrue(StyleConstants.isUnderline(textPane
266: .getParagraphAttributes()));
267: assertTrue(StyleConstants
268: .isUnderline(getCharacterAttributes(1)));
269: }
270:
271: public void testGetParagraphAttributes() {
272: // init paragraph attributeSet
273: textPane.setCaretPosition(1);
274: Element paragraph = textPane.getStyledDocument()
275: .getParagraphElement(textPane.getCaretPosition());
276: attrs = new SimpleAttributeSet();
277: StyleConstants.setStrikeThrough(attrs, true);
278: StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_CENTER);
279: textPane.getStyledDocument().setParagraphAttributes(
280: paragraph.getStartOffset(),
281: paragraph.getEndOffset() - paragraph.getStartOffset(),
282: attrs, true);
283: // tests
284: AttributeSet textAttrs;
285: textPane.setCaretPosition(1);
286: textAttrs = textPane.getParagraphAttributes();
287: assertFalse(StyleConstants.isUnderline(textAttrs));
288: assertTrue(StyleConstants.isStrikeThrough(textAttrs));
289: assertEquals(StyleConstants.ALIGN_CENTER, StyleConstants
290: .getAlignment(textAttrs));
291: textPane.setCaretPosition(8);
292: textAttrs = textPane.getParagraphAttributes();
293: assertFalse(StyleConstants.isUnderline(textAttrs));
294: assertTrue(StyleConstants.isStrikeThrough(textAttrs));
295: assertEquals(StyleConstants.ALIGN_CENTER, StyleConstants
296: .getAlignment(textAttrs));
297: }
298:
299: public void testSetParagraphAttributes() {
300: StyledDocument doc = textPane.getStyledDocument();
301: AttributeSet textAttrs;
302: // The attributes are applied to the paragraph at the current caret
303: // position.
304: textPane.setCaretPosition(1);
305: StyleConstants.setSubscript(attrs, false);
306: doc.setParagraphAttributes(0, doc.getLength(), attrs, false);
307: textAttrs = textPane.getParagraphAttributes();
308: assertFalse(StyleConstants.isSubscript(textAttrs));
309: StyleConstants.setSubscript(attrs, true);
310: textPane.setParagraphAttributes(attrs, true);
311: textAttrs = textPane.getParagraphAttributes();
312: assertTrue(StyleConstants.isSubscript(textAttrs));
313: // The attributes are applied to the to the paragraphs that intersect
314: // the selection
315: textPane.select(1, 2);
316: clearAndSetParagraphSubscript(doc);
317: textAttrs = getParagraphAttributes(1);
318: assertTrue(StyleConstants.isSubscript(textAttrs));
319: textAttrs = getParagraphAttributes(18);
320: assertFalse(StyleConstants.isSubscript(textAttrs));
321: textPane.select(1, 13);
322: clearAndSetParagraphSubscript(doc);
323: textAttrs = getParagraphAttributes(1);
324: assertTrue(StyleConstants.isSubscript(textAttrs));
325: textAttrs = getParagraphAttributes(18);
326: assertFalse(StyleConstants.isSubscript(textAttrs));
327: textPane.select(1, 14);
328: clearAndSetParagraphSubscript(doc);
329: textAttrs = getParagraphAttributes(1);
330: assertTrue(StyleConstants.isSubscript(textAttrs));
331: textAttrs = getParagraphAttributes(18);
332: assertTrue(StyleConstants.isSubscript(textAttrs));
333: textPane.select(18, 19);
334: clearAndSetParagraphSubscript(doc);
335: textAttrs = getParagraphAttributes(1);
336: assertFalse(StyleConstants.isSubscript(textAttrs));
337: textAttrs = getParagraphAttributes(18);
338: assertTrue(StyleConstants.isSubscript(textAttrs));
339: textPane.select(13, 19);
340: textPane.getUI().getRootView(textPane).getView(0);
341: clearAndSetParagraphSubscript(doc);
342: textAttrs = getParagraphAttributes(1);
343: assertFalse(StyleConstants.isSubscript(textAttrs));
344: textAttrs = getParagraphAttributes(18);
345: assertTrue(StyleConstants.isSubscript(textAttrs));
346: textPane.select(12, 19);
347: clearAndSetParagraphSubscript(doc);
348: textAttrs = getParagraphAttributes(1);
349: assertTrue(StyleConstants.isSubscript(textAttrs));
350: textAttrs = getParagraphAttributes(18);
351: assertTrue(StyleConstants.isSubscript(textAttrs));
352: textPane.select(1, 18);
353: clearAndSetParagraphSubscript(doc);
354: textAttrs = getParagraphAttributes(1);
355: assertTrue(StyleConstants.isSubscript(textAttrs));
356: textAttrs = getParagraphAttributes(18);
357: assertTrue(StyleConstants.isSubscript(textAttrs));
358: testExceptionalCase(new NullPointerCase() {
359: @Override
360: public void exceptionalAction() throws Exception {
361: textPane.setParagraphAttributes(null, true);
362: }
363: });
364: textPane.select(1, 1);
365: testExceptionalCase(new NullPointerCase() {
366: @Override
367: public void exceptionalAction() throws Exception {
368: textPane.setParagraphAttributes(null, true);
369: }
370: });
371: }
372:
373: public void testGetCharacterAttributes() {
374: AttributeSet textAttrs;
375: textPane.setCaretPosition(1);
376: textAttrs = textPane.getCharacterAttributes();
377: assertTrue(StyleConstants.isUnderline(textAttrs));
378: assertEquals(StyleConstants.ALIGN_CENTER, StyleConstants
379: .getAlignment(textAttrs));
380: assertTrue(StyleConstants.isStrikeThrough(textAttrs));
381: textPane.setCaretPosition(8);
382: textAttrs = textPane.getCharacterAttributes();
383: assertFalse(StyleConstants.isUnderline(textAttrs));
384: assertEquals(StyleConstants.ALIGN_CENTER, StyleConstants
385: .getAlignment(textAttrs));
386: assertTrue(StyleConstants.isStrikeThrough(textAttrs));
387: }
388:
389: public void testSetCharacterAttributes() {
390: StyledDocument doc = textPane.getStyledDocument();
391: // The attributes are applied to the paragraph at the current caret
392: // position.
393: textPane.setCaretPosition(1);
394: StyleConstants.setSubscript(textPane.getInputAttributes(),
395: false);
396: assertFalse(StyleConstants.isSubscript(textPane
397: .getCharacterAttributes()));
398: assertFalse(StyleConstants.isSubscript(textPane
399: .getInputAttributes()));
400: clearAndSetCharacterSubscript(doc);
401: assertFalse(StyleConstants.isSubscript(textPane
402: .getCharacterAttributes()));
403: assertTrue(StyleConstants.isSubscript(textPane
404: .getInputAttributes()));
405: textPane.select(2, 1);
406: StyleConstants.setSubscript(textPane.getInputAttributes(),
407: false);
408: assertFalse(StyleConstants.isSubscript(textPane
409: .getInputAttributes()));
410: assertFalse(StyleConstants
411: .isSubscript(getCharacterAttributes(1)));
412: assertFalse(StyleConstants
413: .isSubscript(getCharacterAttributes(18)));
414: clearAndSetCharacterSubscript(doc);
415: assertTrue(StyleConstants.isSubscript(textPane
416: .getInputAttributes()));
417: assertFalse(StyleConstants
418: .isSubscript(getCharacterAttributes(1)));
419: assertFalse(StyleConstants
420: .isSubscript(getCharacterAttributes(18)));
421: // The attributes are applied to the to the paragraphs that intersect
422: // the selection
423: textPane.select(1, 2);
424: StyleConstants.setSubscript(textPane.getInputAttributes(),
425: false);
426: assertFalse(StyleConstants.isSubscript(textPane
427: .getInputAttributes()));
428: assertFalse(StyleConstants
429: .isSubscript(getCharacterAttributes(1)));
430: assertFalse(StyleConstants
431: .isSubscript(getCharacterAttributes(18)));
432: clearAndSetCharacterSubscript(doc);
433: assertFalse(StyleConstants.isSubscript(textPane
434: .getInputAttributes()));
435: assertTrue(StyleConstants
436: .isSubscript(getCharacterAttributes(1)));
437: assertFalse(StyleConstants
438: .isSubscript(getCharacterAttributes(18)));
439: textPane.select(2, 13);
440: StyleConstants.setSubscript(textPane.getInputAttributes(),
441: false);
442: assertFalse(StyleConstants.isSubscript(textPane
443: .getInputAttributes()));
444: assertFalse(StyleConstants
445: .isSubscript(getCharacterAttributes(2)));
446: assertFalse(StyleConstants
447: .isSubscript(getCharacterAttributes(18)));
448: clearAndSetCharacterSubscript(doc);
449: assertFalse(StyleConstants.isSubscript(textPane
450: .getInputAttributes()));
451: assertTrue(StyleConstants
452: .isSubscript(getCharacterAttributes(2)));
453: assertFalse(StyleConstants
454: .isSubscript(getCharacterAttributes(18)));
455: textPane.select(13, 19);
456: StyleConstants.setSubscript(textPane.getInputAttributes(),
457: false);
458: assertFalse(StyleConstants.isSubscript(textPane
459: .getInputAttributes()));
460: assertFalse(StyleConstants
461: .isSubscript(getCharacterAttributes(1)));
462: assertFalse(StyleConstants
463: .isSubscript(getCharacterAttributes(18)));
464: clearAndSetCharacterSubscript(doc);
465: assertFalse(StyleConstants.isSubscript(textPane
466: .getInputAttributes()));
467: assertFalse(StyleConstants
468: .isSubscript(getCharacterAttributes(1)));
469: assertTrue(StyleConstants
470: .isSubscript(getCharacterAttributes(18)));
471: textPane.select(1, 18);
472: StyleConstants.setSubscript(textPane.getInputAttributes(),
473: false);
474: assertFalse(StyleConstants.isSubscript(textPane
475: .getInputAttributes()));
476: assertFalse(StyleConstants
477: .isSubscript(getCharacterAttributes(1)));
478: StyleConstants.setSubscript(textPane.getInputAttributes(),
479: false);
480: assertFalse(StyleConstants
481: .isSubscript(getCharacterAttributes(19)));
482: clearAndSetCharacterSubscript(doc);
483: assertFalse(StyleConstants.isSubscript(textPane
484: .getInputAttributes()));
485: assertTrue(StyleConstants
486: .isSubscript(getCharacterAttributes(1)));
487: assertFalse(StyleConstants
488: .isSubscript(getCharacterAttributes(19)));
489: }
490:
491: public void testGetInputAttributes() {
492: MutableAttributeSet inpAttr = ((StyledEditorKit) textPane
493: .getEditorKit()).getInputAttributes();
494: assertSame(textPane.getInputAttributes(), inpAttr);
495: }
496:
497: public void testInsertComponent() {
498: AttributeSet attributes;
499: textPane.setCaretPosition(1);
500: attrs = textPane.getInputAttributes();
501: assertAttributes(attrs, false, false, true, false, false, true);
502: textPane.insertComponent(new JButton("Format C:\\>"));
503: assertAttributes(attrs, false, false, false, false, false,
504: false);
505: assertNull(StyleConstants.getComponent(attrs));
506: attributes = textPane.getStyledDocument()
507: .getCharacterElement(1).getAttributes();
508: assertAttributes(attributes, false, false, false, false, false,
509: false);
510: assertNotNull(StyleConstants.getComponent(attributes));
511: attrs = new SimpleAttributeSet(attributes);
512: StyleConstants.setUnderline(attrs, true);
513: textPane.getStyledDocument().setCharacterAttributes(1, 1,
514: attrs, true);
515: textPane.setCaretPosition(1);
516: assertAttributes(textPane.getInputAttributes(), false, false,
517: true, false, false, true);
518: textPane.select(2, 2);
519: assertAttributes(textPane.getInputAttributes(), false, false,
520: false, false, false, true);
521: textPane.replaceSelection("*");
522: attrs = textPane.getInputAttributes();
523: assertAttributes(attrs, false, false, false, false, false, true);
524: assertNull(StyleConstants.getComponent(attrs));
525: attributes = textPane.getStyledDocument()
526: .getCharacterElement(2).getAttributes();
527: assertAttributes(attributes, false, false, false, false, false,
528: true);
529: assertNull(StyleConstants.getComponent(attributes));
530: }
531:
532: public void testInsertIcon() {
533: textPane.setEditable(false);
534: textPane.setCaretPosition(3);
535: attrs = textPane.getInputAttributes();
536: assertAttributes(attrs, false, false, true, false, false, true);
537: textPane.insertIcon(new Icon() {
538: public void paintIcon(Component c, Graphics g, int x, int y) {
539: g.drawRect(x, y, getIconWidth(), getIconHeight());
540: }
541:
542: public int getIconWidth() {
543: return 20;
544: }
545:
546: public int getIconHeight() {
547: return 40;
548: }
549: });
550: assertAttributes(attrs, false, false, false, false, false,
551: false);
552: Element iconElement = textPane.getStyledDocument()
553: .getDefaultRootElement().getElement(0).getElement(1);
554: AttributeSet attributes = iconElement.getAttributes();
555: assertNotNull(attributes
556: .getAttribute(StyleConstants.IconAttribute));
557: assertAttributes(attributes, false, false, false, false, false,
558: false);
559: }
560:
561: public void testReplaceSelection() throws BadLocationException {
562: AttributeSet textAttrs;
563: // There is replacement and selection
564: textPane.select(6, 22);
565: textAttrs = getCharacterAttributes(6);
566: assertTrue(StyleConstants
567: .isUnderline(getCharacterAttributes(5)));
568: assertFalse(StyleConstants
569: .isUnderline(getCharacterAttributes(6)));
570: textPane.replaceSelection("_BUGGGGS_are_");
571: assertFalse(StyleConstants
572: .isUnderline(getCharacterAttributes(6)));
573: compareAttributes(textAttrs, 6, 18);
574: assertEquals("Hello _BUGGGGS_are_ beautifull!!", textPane
575: .getText());
576: // There is replacement and no selection
577: textPane.setCaretPosition(1);
578: textPane.select(6, 6);
579: textPane.replaceSelection("_BUGGGGS!_The");
580: assertEquals("Hello _BUGGGGS!_The_BUGGGGS_are_ beautifull!!",
581: textPane.getText());
582: textPane.setCaretPosition(3);
583: textPane.setCaretPosition(6);
584: textPane.replaceSelection("q");
585: assertEquals(textPane.getStyledDocument()
586: .getCharacterElement(6).getAttributes(), textPane
587: .getStyledDocument().getCharacterElement(7)
588: .getAttributes());
589: textPane.select(2, 3);
590: textPane.replaceSelection("");
591: // There is selection and no replacement
592: textPane.select(0, 6);
593: textPane.replaceSelection("");
594: assertEquals("_BUGGGGS!_The_BUGGGGS_are_ beautifull!!",
595: textPane.getText());
596: textPane.select(1, 27);
597: textPane.replaceSelection("");
598: assertEquals("_beautifull!!", textPane.getText());
599: // There is no selection and no replacement
600: textPane.select(1, 1);
601: textPane.replaceSelection("");
602: //Document is not editable
603: textPane.setEditable(false);
604: textPane.select(1, 1);
605: textPane.replaceSelection("Hello");
606: assertEquals("_beautifull!!", textPane.getText());
607: textPane.select(2, 4);
608: textPane.replaceSelection("Hello");
609: assertEquals("_beautifull!!", textPane.getText());
610: textPane = new JTextPane();
611: textPane.replaceSelection("1");
612: assertEquals(textPane.getStyledDocument()
613: .getCharacterElement(0).getAttributes(), textPane
614: .getStyledDocument().getCharacterElement(1)
615: .getAttributes());
616: textPane.select(0, 1);
617: textPane.replaceSelection("");
618: attrs = new SimpleAttributeSet();
619: StyleConstants.setUnderline(attrs, true);
620: textPane.getStyledDocument().insertString(0, "Hello!", attrs);
621: textPane.select(0, 0);
622: textPane.replaceSelection("1");
623: assertEquals(textPane.getStyledDocument()
624: .getCharacterElement(0).getAttributes(), textPane
625: .getStyledDocument().getCharacterElement(1)
626: .getAttributes());
627: textPane.select(0, 1);
628: textPane.replaceSelection("2");
629: assertEquals(textPane.getStyledDocument()
630: .getCharacterElement(0).getAttributes(), textPane
631: .getStyledDocument().getCharacterElement(1)
632: .getAttributes());
633: textPane.setCaretPosition(1);
634: textPane.insertIcon(MetalIconFactory
635: .getFileChooserNewFolderIcon());
636: textPane.select(2, 2);
637: textPane.replaceSelection("3");
638: attrs = new SimpleAttributeSet(textPane.getStyledDocument()
639: .getCharacterElement(1).getAttributes());
640: assertAttributes(attrs, false, false, false, false, false,
641: false);
642: assertNotNull(StyleConstants.getIcon(attrs));
643: attrs = new SimpleAttributeSet(textPane.getStyledDocument()
644: .getCharacterElement(2).getAttributes());
645: assertAttributes(attrs, false, false, false, false, false,
646: false);
647: assertNull(StyleConstants.getIcon(attrs));
648: }
649:
650: public void testParamString() {
651: String tmp = textPane.paramString();
652: assertNotNull(tmp);
653: }
654:
655: private AttributeSet getParagraphAttributes(final int position) {
656: return textPane.getStyledDocument().getParagraphElement(
657: position).getAttributes();
658: }
659:
660: private AttributeSet getCharacterAttributes(final int position) {
661: return textPane.getStyledDocument().getCharacterElement(
662: position).getAttributes();
663: }
664:
665: private void clearAndSetParagraphSubscript(final StyledDocument doc) {
666: StyleConstants.setSubscript(attrs, false);
667: doc.setParagraphAttributes(0, doc.getLength(), attrs, false);
668: StyleConstants.setSubscript(attrs, true);
669: textPane.setParagraphAttributes(attrs, true);
670: }
671:
672: private void clearAndSetCharacterSubscript(final StyledDocument doc) {
673: StyleConstants.setSubscript(attrs, false);
674: doc.setCharacterAttributes(0, doc.getLength(), attrs, false);
675: StyleConstants.setSubscript(attrs, true);
676: textPane.setCharacterAttributes(attrs, true);
677: }
678:
679: private void compareAttributes(final AttributeSet textAttrs,
680: final int startOffset, final int endOffset) {
681: for (int i = startOffset; i < endOffset + 1; i++) {
682: compareAttributeSets(textAttrs, getCharacterAttributes(i));
683: }
684: }
685:
686: private void compareAttributeSets(
687: final AttributeSet expectedTextAttrs,
688: final AttributeSet textAttrs) {
689: assertEquals(expectedTextAttrs.getAttributeCount(), textAttrs
690: .getAttributeCount());
691: for (Enumeration<?> expectedNames = expectedTextAttrs
692: .getAttributeNames(); expectedNames.hasMoreElements();) {
693: Object expectedName = expectedNames.nextElement();
694: assertEquals(expectedTextAttrs.getAttribute(expectedName),
695: textAttrs.getAttribute(expectedName));
696: }
697: for (Enumeration<?> names = textAttrs.getAttributeNames(); names
698: .hasMoreElements();) {
699: Object name = names.nextElement();
700: assertEquals(textAttrs.getAttribute(name),
701: expectedTextAttrs.getAttribute(name));
702: }
703: }
704:
705: private void assertAttributes(final AttributeSet attrs,
706: final boolean isBold, final boolean isItalic,
707: final boolean isStrikeThrough, final boolean isSubscript,
708: final boolean isSuperScript, final boolean isUnderline) {
709: assertEquals(isBold, StyleConstants.isBold(attrs));
710: assertEquals(isItalic, StyleConstants.isItalic(attrs));
711: assertEquals(isStrikeThrough, StyleConstants
712: .isStrikeThrough(attrs));
713: assertEquals(isSubscript, StyleConstants.isSubscript(attrs));
714: assertEquals(isSuperScript, StyleConstants.isSuperscript(attrs));
715: assertEquals(isUnderline, StyleConstants.isUnderline(attrs));
716: }
717:
718: public void testConstructor() {
719: try {
720: new JTextPane(null);
721: fail("NPE should be thrown");
722: } catch (NullPointerException npe) {
723: // PASSED
724: }
725: }
726: }
|