KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > WrappedTextViewTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.output2;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import javax.imageio.ImageIO JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import javax.swing.text.Position JavaDoc;
32 import javax.swing.text.View JavaDoc;
33 import junit.framework.TestCase;
34 import org.netbeans.core.output2.ui.AbstractOutputPane;
35 import org.netbeans.core.output2.ui.AbstractOutputTab;
36 import org.openide.windows.OutputEvent;
37 import org.openide.windows.OutputListener;
38
39 /**
40  *
41  * @author Tim Boudreau
42  */

43 public class WrappedTextViewTest extends TestCase {
44     
45     public WrappedTextViewTest(String JavaDoc testName) {
46         super(testName);
47     }
48     
49     private OutputWindow win;
50     private NbIO io;
51     private OutWriter out = null;
52     JFrame JavaDoc jf = null;
53
54     protected void setUp() throws Exception JavaDoc {
55         jf = new JFrame JavaDoc();
56         win = new OutputWindow();
57         OutputWindow.DEFAULT = win;
58         jf.getContentPane().setLayout (new BorderLayout JavaDoc());
59         jf.getContentPane().add (win, BorderLayout.CENTER);
60         jf.setBounds (20, 20, 700, 300);
61         io = (NbIO) new NbIOProvider().getIO ("Test", false);
62         SwingUtilities.invokeAndWait (new Shower());
63         sleep();
64         io.getOut().println ("Test line 1");
65         sleep();
66         sleep();
67         sleep();
68         
69         for (int i=0; i < 100; i++) {
70             if (i == 42) {
71                 io.getOut().println ("This is a hyperlink to click which may or may not trigger the problem", new L());
72             }
73             if (i % 2 == 0) {
74                 io.getOut().println ("Hello there. What a short line");
75                 io.getOut().println("Splead 2 - 148: Wow, we will write a long line of text here. Very long in fact - who knows just how long it might end up being? Well, we'll have to see. Why it's extraordinarily long! It might even wrap several times! How do you like them apples, eh? Maybe we should just go on and on and on, and never stop. That would be cool, huh?\n");
76             } else {
77                 //io.getErr().println ("aaa: This is a not so long line");
78
}
79         }
80         io.getOut().close();
81         sleep();
82         
83         SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
84             public void run() {
85                 win.getSelectedTab().getOutputPane().setWrapped(true);
86             }
87         });
88     }
89     
90     private final void sleep() {
91         try {
92             Thread.currentThread().sleep(200);
93             SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
94                 public void run() {
95                     System.currentTimeMillis();
96                 }
97             });
98             Thread.currentThread().sleep(200);
99         } catch (Exception JavaDoc e) {
100             fail (e.getMessage());
101         }
102     }
103     
104     public class Shower implements Runnable JavaDoc {
105         public void run() {
106             jf.setVisible(true);
107         }
108     }
109     
110     public void testModelToView() throws Exception JavaDoc {
111         System.out.println("testModelToView");
112         
113         if (true) {
114             //THIS TEST TAKES ABOUT 10 MINUTES TO RUN! LEAVE IT COMMENTED OUT
115
//FOR PRODUCTION AND USE IT JUST FOR DEBUGGING
116
return;
117         }
118         
119         AbstractOutputTab tab = win.getSelectedTab();
120         AbstractOutputPane pane = tab.getOutputPane();
121         JTextComponent JavaDoc text = pane.getTextView();
122         
123         View JavaDoc view = text.getUI().getRootView(text);
124         
125         Rectangle JavaDoc r = new Rectangle JavaDoc(1,1,1,1);
126         Rectangle JavaDoc alloc = new Rectangle JavaDoc();
127         
128         java.awt.image.ColorModel JavaDoc model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().
129                                           getDefaultScreenDevice().getDefaultConfiguration().getColorModel(java.awt.Transparency.TRANSLUCENT);
130         java.awt.image.BufferedImage JavaDoc img = new java.awt.image.BufferedImage JavaDoc(model,
131                 model.createCompatibleWritableRaster(text.getWidth() + 10, text.getHeight() + 10), model.isAlphaPremultiplied(), null);
132         
133         text.paint (img.getGraphics());
134         boolean errorsFound = false;
135         ArrayList JavaDoc errors = new ArrayList JavaDoc();
136         
137         System.out.println("...scanning " + (text.getWidth() * text.getHeight() + " pixels to make sure viewToModel() matches modeltoView(). Expect it to take about 10 minutes."));
138         
139         for (int y=0; y < text.getHeight(); y++) {
140             r.y = y;
141             for (int x=0; x < text.getWidth(); x++) {
142                 r.x = x;
143                 alloc.setBounds (0, 0, text.getWidth(), text.getHeight());
144                 
145                 int vtm = view.viewToModel (x, y, alloc, new Position.Bias JavaDoc[1]);
146                 
147                 Rectangle JavaDoc mtv = (Rectangle JavaDoc) view.modelToView (vtm, Position.Bias.Forward, vtm, Position.Bias.Forward, new Rectangle JavaDoc (0, 0, text.getWidth(), text.getHeight()));
148                 
149                 int xvtm = view.viewToModel (mtv.x, mtv.y, alloc, new Position.Bias JavaDoc[1]);
150
151                 if (vtm != xvtm) {
152                     errorsFound = true;
153                     try {
154                         errors.add ("ViewToModel(" + x + "," + y + ") returns character position " + vtm + "; modelToView on " + vtm + " returns " + mtv + "; that Rectangle's corner, passed back to viewToModel maps to a different character position: " + xvtm + "\n");
155                         img.setRGB(x, y, vtm > xvtm ? Color.RED.getRGB() : Color.BLUE.getRGB());
156                         img.setRGB(x-1, y-1, vtm > xvtm ? Color.RED.getRGB() : Color.BLUE.getRGB());
157                         img.setRGB(x+1, y-1, vtm > xvtm ? Color.RED.getRGB() : Color.BLUE.getRGB());
158                         img.setRGB(x+1, y+1, vtm > xvtm ? Color.RED.getRGB() : Color.BLUE.getRGB());
159                         img.setRGB(x-1, y+1, vtm > xvtm ? Color.RED.getRGB() : Color.BLUE.getRGB());
160                     } catch (ArrayIndexOutOfBoundsException JavaDoc aioobe) {
161                         System.err.println("OUT OF BOUNDS: " + x + "," + y + " image width " + img.getWidth() + " img height " + img.getHeight());
162                     }
163                         
164                     System.err.println(x + "," + y + "=" + vtm + " -> [" + mtv.x + "," + mtv.y + "," + mtv.width + "," + mtv.height + "]->" + xvtm);
165                 }
166                 
167                 r.y = y; //just in case
168
r.width = 1;
169                 r.height = 1;
170             }
171         }
172         
173         if (errorsFound) {
174             String JavaDoc dir = System.getProperty ("java.io.tmpdir");
175             if (!dir.endsWith(File.separator)) {
176                 dir += File.separator;
177             }
178             String JavaDoc fname = dir + "outputWindowDiffs.png";
179             ImageIO.write (img, "png", new File JavaDoc (fname));
180             fail ("In a wrapped view, some points as mapped by viewToModel do " +
181                 "not map back to the same coordinates in viewToModel. \nA bitmap " +
182                 "of the problem coordinates is saved in " + fname + " Problem" +
183                 "spots are marked in red and blue.\n" + errors);
184         }
185         
186
187         
188 /*
189         try {
190             Thread.currentThread().sleep (40000);
191         } catch (Exception e) {}
192  */

193         
194 // WrappedTextView wtv = win.getSelectedTab().getOutputPane().getTextView().getUI().getView();
195
}
196
197
198
199     public class L implements OutputListener {
200
201         public void outputLineSelected(OutputEvent ev) {
202         }
203
204         public void outputLineAction(OutputEvent ev) {
205         }
206
207         public void outputLineCleared(OutputEvent ev) {
208         }
209
210     }
211     
212 }
213
Popular Tags