1 19 20 package org.netbeans.core.output2; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Rectangle ; 25 import java.io.File ; 26 import java.util.ArrayList ; 27 import javax.imageio.ImageIO ; 28 import javax.swing.JFrame ; 29 import javax.swing.SwingUtilities ; 30 import javax.swing.text.JTextComponent ; 31 import javax.swing.text.Position ; 32 import javax.swing.text.View ; 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 43 public class WrappedTextViewTest extends TestCase { 44 45 public WrappedTextViewTest(String testName) { 46 super(testName); 47 } 48 49 private OutputWindow win; 50 private NbIO io; 51 private OutWriter out = null; 52 JFrame jf = null; 53 54 protected void setUp() throws Exception { 55 jf = new JFrame (); 56 win = new OutputWindow(); 57 OutputWindow.DEFAULT = win; 58 jf.getContentPane().setLayout (new BorderLayout ()); 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 } 79 } 80 io.getOut().close(); 81 sleep(); 82 83 SwingUtilities.invokeAndWait (new Runnable () { 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 () { 94 public void run() { 95 System.currentTimeMillis(); 96 } 97 }); 98 Thread.currentThread().sleep(200); 99 } catch (Exception e) { 100 fail (e.getMessage()); 101 } 102 } 103 104 public class Shower implements Runnable { 105 public void run() { 106 jf.setVisible(true); 107 } 108 } 109 110 public void testModelToView() throws Exception { 111 System.out.println("testModelToView"); 112 113 if (true) { 114 return; 117 } 118 119 AbstractOutputTab tab = win.getSelectedTab(); 120 AbstractOutputPane pane = tab.getOutputPane(); 121 JTextComponent text = pane.getTextView(); 122 123 View view = text.getUI().getRootView(text); 124 125 Rectangle r = new Rectangle (1,1,1,1); 126 Rectangle alloc = new Rectangle (); 127 128 java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(). 129 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(java.awt.Transparency.TRANSLUCENT); 130 java.awt.image.BufferedImage img = new java.awt.image.BufferedImage (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 errors = new ArrayList (); 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 [1]); 146 147 Rectangle mtv = (Rectangle ) view.modelToView (vtm, Position.Bias.Forward, vtm, Position.Bias.Forward, new Rectangle (0, 0, text.getWidth(), text.getHeight())); 148 149 int xvtm = view.viewToModel (mtv.x, mtv.y, alloc, new Position.Bias [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 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; r.width = 1; 169 r.height = 1; 170 } 171 } 172 173 if (errorsFound) { 174 String dir = System.getProperty ("java.io.tmpdir"); 175 if (!dir.endsWith(File.separator)) { 176 dir += File.separator; 177 } 178 String fname = dir + "outputWindowDiffs.png"; 179 ImageIO.write (img, "png", new File (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 193 194 } 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 |