01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Roman I. Chernyatchik
19: * @version $Revision$
20: */package javax.swing;
21:
22: import javax.swing.plaf.metal.MetalIconFactory;
23: import javax.swing.text.BadLocationException;
24: import javax.swing.text.MutableAttributeSet;
25: import javax.swing.text.SimpleAttributeSet;
26: import javax.swing.text.StyleConstants;
27: import javax.swing.text.StyledDocument;
28:
29: public class JTextPaneRTest extends SwingTestCase {
30: private JTextPane textPane;
31:
32: private MutableAttributeSet attrs;
33:
34: @Override
35: protected void setUp() throws Exception {
36: super .setUp();
37: textPane = new JTextPane();
38: // init character attributeSet
39: attrs = new SimpleAttributeSet();
40: StyleConstants.setStrikeThrough(attrs, true);
41: StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_CENTER);
42: StyleConstants.setUnderline(attrs, true);
43: textPane.getDocument().insertString(0, "Hello !", attrs);
44: StyleConstants.setUnderline(attrs, false);
45: textPane.getDocument().insertString(6, "world", attrs);
46: }
47:
48: public void testReplaceSelection_AtTheBeginningOfParagraph() {
49: StyledDocument doc = textPane.getStyledDocument();
50: try {
51: attrs = new SimpleAttributeSet();
52: StyleConstants.setUnderline(attrs, true);
53: doc.insertString(0, "Hello word!", attrs);
54: textPane.setCaretPosition(4);
55: textPane.insertIcon(MetalIconFactory.getTreeFolderIcon());
56: textPane.setCaretPosition(4);
57: textPane.replaceSelection("\n");
58: textPane.setCaretPosition(5);
59: textPane.replaceSelection("a");
60: textPane.setCaretPosition(6);
61: textPane.replaceSelection("b");
62: assertNull(StyleConstants.getIcon(doc
63: .getCharacterElement(5).getAttributes()));
64: assertNull(StyleConstants.getIcon(doc
65: .getCharacterElement(6).getAttributes()));
66: } catch (BadLocationException e) {
67: }
68: }
69:
70: public void testReplaceSelection_WithSelection() {
71: textPane.setCaretPosition(4);
72: textPane.insertIcon(MetalIconFactory.getTreeFolderIcon());
73: textPane.select(4, 6);
74: textPane.replaceSelection("TEXT");
75: assertNull(StyleConstants.getIcon(textPane.getStyledDocument()
76: .getCharacterElement(4).getAttributes()));
77: }
78:
79: public void testReplaceSelection_WithNoSelection() {
80: assertTrue(StyleConstants.isUnderline(textPane
81: .getStyledDocument().getCharacterElement(5)
82: .getAttributes()));
83: assertFalse(StyleConstants.isUnderline(textPane
84: .getStyledDocument().getCharacterElement(6)
85: .getAttributes()));
86: textPane.select(6, 6);
87: textPane.replaceSelection("TEXT");
88: assertTrue(StyleConstants.isUnderline(textPane
89: .getStyledDocument().getCharacterElement(6)
90: .getAttributes()));
91: }
92: }
|