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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Rectangle;
023: import javax.swing.event.DocumentEvent;
024: import javax.swing.event.DocumentListener;
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests GlyphView methods which react to changes in a document.
029: *
030: */
031: public class GlyphView_ChangesTest extends TestCase implements
032: DocumentListener {
033: private static final class PreferenceChange {
034: private View child;
035:
036: private boolean width;
037:
038: private boolean height;
039:
040: void setData(final View child, final boolean width,
041: final boolean height) {
042: this .child = child;
043: this .width = width;
044: this .height = height;
045: }
046:
047: void check(final View child, final boolean width,
048: final boolean height) {
049: assertSame("Child", child, this .child);
050: assertEquals("Width", width, this .width);
051: assertEquals("Height", height, this .height);
052: }
053: }
054:
055: private GlyphView[] views;
056:
057: private StyledDocument doc;
058:
059: private Element root;
060:
061: private Element branch;
062:
063: private Element[] leaves;
064:
065: private BoxView boxView;
066:
067: private DocumentEvent event;
068:
069: private static final PreferenceChange change = new PreferenceChange();
070:
071: private static final String TEXT = "abcdef012345lalala";
072:
073: // 012345678901234567
074: @Override
075: protected void setUp() throws Exception {
076: super .setUp();
077: doc = new DefaultStyledDocument();
078: root = doc.getDefaultRootElement();
079: doc.insertString(0, TEXT, null);
080: doc.setCharacterAttributes(6, 12, SimpleAttributeSet.EMPTY,
081: false);
082: branch = root.getElement(0);
083: leaves = new Element[branch.getElementCount()];
084: boxView = new BoxView(branch, View.X_AXIS) {
085: @Override
086: public void preferenceChanged(final View child,
087: final boolean width, final boolean height) {
088: change.setData(child, width, height);
089: super .preferenceChanged(child, width, height);
090: }
091: };
092: views = new GlyphView[leaves.length];
093: for (int i = 0; i < views.length; i++) {
094: views[i] = new GlyphView(leaves[i]);
095: }
096: boxView.replace(0, 0, views);
097: doc.addDocumentListener(this );
098: }
099:
100: public void testInsertUpdate() throws BadLocationException {
101: doc.insertString(1, "^", null);
102: for (int i = 0; i < views.length; i++) {
103: views[i].insertUpdate(event, new Rectangle(), null);
104: change.check(views[i], true, false);
105: }
106: }
107:
108: public void testRemoveUpdate() throws BadLocationException {
109: doc.remove(0, 1);
110: for (int i = 0; i < views.length; i++) {
111: views[i].removeUpdate(event, new Rectangle(), null);
112: change.check(views[i], true, false);
113: }
114: }
115:
116: public void testChangedUpdate() {
117: MutableAttributeSet attrs = new SimpleAttributeSet();
118: attrs.addAttribute("key", "value");
119: doc.setParagraphAttributes(0, 1, attrs, false);
120: for (int i = 0; i < views.length; i++) {
121: views[i].changedUpdate(event, new Rectangle(), null);
122: change.check(views[i], true, true);
123: }
124: }
125:
126: public void insertUpdate(DocumentEvent e) {
127: event = e;
128: }
129:
130: public void removeUpdate(DocumentEvent e) {
131: event = e;
132: }
133:
134: public void changedUpdate(DocumentEvent e) {
135: event = e;
136: }
137: }
|