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 java.awt.Shape;
024: import java.util.ArrayList;
025: import java.util.List;
026: import javax.swing.event.DocumentEvent;
027: import javax.swing.event.DocumentListener;
028: import javax.swing.event.DocumentEvent.EventType;
029: import javax.swing.text.FlowViewTest.FlowViewImplWithFactory;
030: import javax.swing.text.ViewTestHelpers.ChildView;
031: import junit.framework.TestCase;
032:
033: /**
034: * Tests FlowView methods which react to changes in the associated document.
035: *
036: */
037: public class FlowView_ChangesTest extends TestCase implements
038: DocumentListener {
039: private AbstractDocument doc;
040:
041: private Element root;
042:
043: private FlowView view;
044:
045: private DocumentEvent event;
046:
047: private Rectangle alloc = new Rectangle(11, 21, 453, 217);
048:
049: /**
050: * Stores information which event-method was called and on which view.
051: */
052: private static class Changes {
053: static final Object INSERT = "insert";
054:
055: static final Object REMOVE = "remove";
056:
057: static final Object CHANGE = "change";
058:
059: private final View view;
060:
061: private final Object method;
062:
063: Changes(final View view, final Object method) {
064: this .view = view;
065: this .method = method;
066: }
067:
068: final void check(final View view, final Object method) {
069: assertSame(this .view, view);
070: assertSame(this .method, method);
071: }
072: }
073:
074: /**
075: * Stores information which view changed its preferences, which child
076: * cause the change, and how preferences were changed.
077: */
078: private static class Preferences {
079: private final View view;
080:
081: private final View child;
082:
083: private final boolean width;
084:
085: private final boolean height;
086:
087: Preferences(final View view, final View child,
088: final boolean width, final boolean height) {
089: this .view = view;
090: this .child = child;
091: this .width = width;
092: this .height = height;
093: }
094:
095: final void check(final View view, final View child,
096: final boolean width, final boolean height) {
097: assertSame("Host view", this .view, view);
098: assertSame("Child", this .child, child);
099: assertEquals("Width", this .width, width);
100: assertEquals("Height", this .height, height);
101: }
102: }
103:
104: /**
105: * List of views which got update event.
106: */
107: private final List<Changes> changes = new ArrayList<Changes>();
108:
109: /**
110: * List of views which changed their preferences.
111: */
112: private final List<Preferences> preferences = new ArrayList<Preferences>();
113:
114: private class FlowFactory implements ViewFactory {
115: private int count = 0;
116:
117: public View create(Element element) {
118: return new ChildView(element, count++) {
119: @Override
120: public void insertUpdate(DocumentEvent event,
121: Shape shape, ViewFactory factory) {
122: changes.add(new Changes(this , Changes.INSERT));
123: preferenceChanged(null, true, false);
124: }
125:
126: @Override
127: public void removeUpdate(DocumentEvent event,
128: Shape shape, ViewFactory factory) {
129: changes.add(new Changes(this , Changes.REMOVE));
130: preferenceChanged(null, false, true);
131: }
132:
133: @Override
134: public void changedUpdate(DocumentEvent event,
135: Shape shape, ViewFactory factory) {
136: changes.add(new Changes(this , Changes.CHANGE));
137: preferenceChanged(null, true, true);
138: }
139:
140: @Override
141: public String toString() {
142: return "child(" + getID() + ")";
143: }
144: };
145: }
146: }
147:
148: @Override
149: protected void setUp() throws Exception {
150: super .setUp();
151: doc = new PlainDocument();
152: doc.insertString(0, "line1\nline2\nline3", null);
153: root = doc.getDefaultRootElement();
154: view = new FlowViewImplWithFactory(root, View.Y_AXIS,
155: new FlowFactory()) {
156: private int count = 0;
157:
158: @Override
159: protected View createRow() {
160: return new BoxView(getElement(), X_AXIS) {
161: private final int id = count++;
162:
163: @Override
164: public void insertUpdate(DocumentEvent event,
165: Shape shape, ViewFactory factory) {
166: changes.add(new Changes(this , Changes.INSERT));
167: super .insertUpdate(event, shape, factory);
168: }
169:
170: @Override
171: public void removeUpdate(DocumentEvent event,
172: Shape shape, ViewFactory factory) {
173: changes.add(new Changes(this , Changes.REMOVE));
174: super .removeUpdate(event, shape, factory);
175: }
176:
177: @Override
178: public void changedUpdate(DocumentEvent event,
179: Shape shape, ViewFactory factory) {
180: changes.add(new Changes(this , Changes.CHANGE));
181: super .changedUpdate(event, shape, factory);
182: }
183:
184: @Override
185: public void preferenceChanged(View child,
186: boolean width, boolean height) {
187: preferences.add(new Preferences(this , child,
188: width, height));
189: super .preferenceChanged(child, width, height);
190: }
191:
192: @Override
193: public String toString() {
194: return "row(" + id + ")";
195: }
196:
197: @Override
198: protected void loadChildren(ViewFactory factory) {
199: return;
200: }
201: };
202: }
203:
204: @Override
205: public void preferenceChanged(View child, boolean width,
206: boolean height) {
207: preferences.add(new Preferences(this , child, width,
208: height));
209: super .preferenceChanged(child, width, height);
210: }
211:
212: @Override
213: public String toString() {
214: return "flow";
215: }
216: };
217: view.layoutPool = new BoxView(root, View.X_AXIS) {
218: @Override
219: public void insertUpdate(DocumentEvent event, Shape shape,
220: ViewFactory factory) {
221: changes.add(new Changes(this , Changes.INSERT));
222: super .insertUpdate(event, shape, factory);
223: }
224:
225: @Override
226: public void removeUpdate(DocumentEvent event, Shape shape,
227: ViewFactory factory) {
228: changes.add(new Changes(this , Changes.REMOVE));
229: super .removeUpdate(event, shape, factory);
230: }
231:
232: @Override
233: public void changedUpdate(DocumentEvent event, Shape shape,
234: ViewFactory factory) {
235: changes.add(new Changes(this , Changes.CHANGE));
236: super .changedUpdate(event, shape, factory);
237: }
238:
239: @Override
240: public void preferenceChanged(View child, boolean width,
241: boolean height) {
242: preferences.add(new Preferences(this , child, width,
243: height));
244: super .preferenceChanged(child, width, height);
245: }
246:
247: @Override
248: public String toString() {
249: return "pool";
250: }
251: };
252: view.layoutPool.setParent(view);
253: view.layout(alloc.width, alloc.height);
254: changes.clear();
255: preferences.clear();
256: doc.addDocumentListener(this );
257: }
258:
259: public void testInsertUpdate() throws BadLocationException {
260: assertEquals(alloc.width, view.layoutSpan);
261: assertEquals(1, view.getViewCount());
262: assertTrue(view.isAllocationValid());
263: View row = view.getView(0);
264: assertEquals(root.getElementCount(), row.getViewCount());
265: for (int i = 0; i < row.getViewCount(); i++) {
266: assertSame(view.layoutPool.getView(i), row.getView(i));
267: }
268: doc.insertString(1, "^^^", null);
269: view.insertUpdate(event, alloc, view.getViewFactory());
270: assertFalse(view.isAllocationValid());
271: assertEquals(2, changes.size());
272: changes.get(0).check(view.layoutPool, Changes.INSERT);
273: changes.get(1)
274: .check(view.layoutPool.getView(0), Changes.INSERT);
275: assertEquals(2, preferences.size());
276: preferences.get(0).check(view.getView(0),
277: view.layoutPool.getView(0), true, false);
278: preferences.get(1).check(view, view.getView(0), true, false);
279: }
280:
281: public void testRemoveUpdate() throws BadLocationException {
282: assertTrue(view.isAllocationValid());
283: doc.remove(1, 1);
284: view.removeUpdate(event, alloc, view.getViewFactory());
285: assertFalse(view.isAllocationValid());
286: assertEquals(2, changes.size());
287: changes.get(0).check(view.layoutPool, Changes.REMOVE);
288: changes.get(1)
289: .check(view.layoutPool.getView(0), Changes.REMOVE);
290: assertEquals(2, preferences.size());
291: preferences.get(0).check(view.getView(0),
292: view.layoutPool.getView(0), false, true);
293: preferences.get(1).check(view, view.getView(0), false, true);
294: }
295:
296: public void testChangedUpdate() {
297: assertTrue(view.isAllocationValid());
298: event = doc.new DefaultDocumentEvent(1, 1, EventType.CHANGE);
299: view.changedUpdate(event, alloc, view.getViewFactory());
300: assertFalse(view.isAllocationValid());
301: assertEquals(2, changes.size());
302: changes.get(0).check(view.layoutPool, Changes.CHANGE);
303: changes.get(1)
304: .check(view.layoutPool.getView(0), Changes.CHANGE);
305: assertEquals(2, preferences.size());
306: preferences.get(0).check(view.getView(0),
307: view.layoutPool.getView(0), true, true);
308: preferences.get(1).check(view, view.getView(0), true, true);
309: }
310:
311: public void insertUpdate(DocumentEvent e) {
312: event = e;
313: }
314:
315: public void removeUpdate(DocumentEvent e) {
316: event = e;
317: }
318:
319: public void changedUpdate(DocumentEvent e) {
320: event = e;
321: }
322: }
|