1 19 20 package org.openide.text; 21 22 23 import java.io.IOException ; 24 import javax.swing.JEditorPane ; 25 import junit.framework.*; 26 import org.netbeans.junit.*; 27 import org.openide.util.Exceptions; 28 import org.openide.util.Lookup; 29 import org.openide.util.lookup.*; 30 import org.openide.windows.CloneableTopComponent; 31 32 33 37 public class CloneableEditorSupportPaneTest extends NbTestCase implements CloneableEditorSupport.Env { 38 39 private CloneableEditorSupport support; 40 private CloneableEditorSupport support2; 41 42 private InstanceContent ic; 43 44 45 private String content = ""; 47 private boolean valid = true; 48 private boolean modified = false; 49 50 private String cannotBeModified; 51 private java.util.Date date = new java.util.Date (); 52 private java.util.List propL = new java.util.ArrayList (); 53 private java.beans.VetoableChangeListener vetoL; 54 55 56 public CloneableEditorSupportPaneTest(java.lang.String testName) { 57 super(testName); 58 } 59 60 public static void main(java.lang.String [] args) { 61 junit.textui.TestRunner.run(suite()); 62 } 63 64 public static Test suite() { 65 TestSuite suite = new NbTestSuite(CloneableEditorSupportPaneTest.class); 66 67 return suite; 68 } 69 70 71 protected void setUp () { 72 ic = new InstanceContent (); 73 support = new CES (this, new AbstractLookup (ic)); 74 support2 = new CES2(this, new AbstractLookup(new InstanceContent ())); 75 } 76 77 public void testGetOpenedPanes () throws Exception { 78 content = "Ahoj\nMyDoc"; 79 javax.swing.text.Document doc = support.openDocument (); 80 support.open(); 81 Line line = support.getLineSet().getCurrent(0); 82 line.show(Line.SHOW_SHOW); 83 JEditorPane [] panes = support.getOpenedPanes(); 84 assertNotNull(panes); 85 assertEquals(1, panes.length); 86 assertNotNull(instance); 87 assertTrue(instance.activated); 88 89 } 90 91 public void testGetOpenedPanes2ForSeparatePane() throws Exception { 92 content = "Ahoj\nMyDoc"; 93 javax.swing.text.Document doc = support2.openDocument (); 94 support2.open(); 95 Line line = support2.getLineSet().getCurrent(0); 96 line.show(Line.SHOW_SHOW); 97 JEditorPane [] panes = support2.getOpenedPanes(); 98 assertNotNull(panes); 99 assertEquals(1, panes.length); 100 assertNotNull(instance2); 101 102 } 103 104 105 public void testCreateCloneableTopComponent() throws Exception { 106 CloneableTopComponent comp = support.createCloneableTopComponent(); 107 assertNotNull(comp); 108 assertEquals(MyPane.class, comp.getClass()); 109 } 110 111 115 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 116 propL.add (l); 117 } 118 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 119 propL.remove (l); 120 } 121 122 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 123 vetoL = l; 125 } 126 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 127 vetoL = null; 129 } 130 131 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 132 return support; 133 } 134 135 public String getMimeType() { 136 return "text/plain"; 137 } 138 139 public java.util.Date getTime() { 140 return date; 141 } 142 143 public java.io.InputStream inputStream() throws java.io.IOException { 144 return new java.io.ByteArrayInputStream (content.getBytes ()); 145 } 146 public java.io.OutputStream outputStream() throws java.io.IOException { 147 class ContentStream extends java.io.ByteArrayOutputStream { 148 public void close () throws java.io.IOException { 149 super.close (); 150 content = new String (toByteArray ()); 151 } 152 } 153 154 return new ContentStream (); 155 } 156 157 public boolean isValid() { 158 return valid; 159 } 160 161 public boolean isModified() { 162 return modified; 163 } 164 165 public void markModified() throws java.io.IOException { 166 if (cannotBeModified != null) { 167 IOException e = new IOException (); 168 Exceptions.attachLocalizedMessage(e, cannotBeModified); 169 throw e; 170 } 171 172 modified = true; 173 } 174 175 public void unmarkModified() { 176 modified = false; 177 } 178 179 protected boolean runInEQ() { 180 return true; 181 } 182 183 184 private static class CES extends CloneableEditorSupport { 185 public CES (Env env, Lookup l) { 186 super (env, l); 187 } 188 189 protected String messageName() { 190 return "Name"; 191 } 192 193 protected String messageOpened() { 194 return "Opened"; 195 } 196 197 protected String messageOpening() { 198 return "Opening"; 199 } 200 201 protected String messageSave() { 202 return "Save"; 203 } 204 205 protected String messageToolTip() { 206 return "ToolTip"; 207 } 208 209 protected org.openide.text.CloneableEditorSupport.Pane createPane() { 210 instance = new MyPane(); 211 return instance; 212 } 213 214 } 215 216 private static MyPane instance; 217 218 private static final class MyPane extends CloneableTopComponent implements CloneableEditorSupport.Pane { 219 220 private CloneableTopComponent tc; 221 private JEditorPane pane; 222 223 MyPane() { 224 pane = new JEditorPane (); 225 226 } 227 228 public org.openide.windows.CloneableTopComponent getComponent() { 229 return this; 230 } 231 232 public javax.swing.JEditorPane getEditorPane() { 233 return pane; 234 } 235 236 public void updateName() { 237 } 238 239 public boolean activated = false; 240 public void requestActive() { 241 super.requestActive(); 242 activated = true; 243 } 244 245 248 public void ensureVisible() { 249 open(); 250 requestVisible(); 251 } 252 } 253 254 255 258 private static class CES2 extends CES { 259 public CES2 (Env env, Lookup l) { 260 super (env, l); 261 } 262 263 protected org.openide.text.CloneableEditorSupport.Pane createPane() { 264 instance2 = new MyPaneNonNonTC(); 265 return instance2; 266 } 267 } 268 269 private static MyPaneNonNonTC instance2; 270 271 272 private static final class MyPaneNonNonTC implements CloneableEditorSupport.Pane { 273 274 private CloneableTopComponent tc; 275 private JEditorPane pane; 276 277 MyPaneNonNonTC() { 278 pane = new JEditorPane (); 279 tc = new TC(); 280 281 } 282 283 public org.openide.windows.CloneableTopComponent getComponent() { 284 return tc; 285 } 286 287 public javax.swing.JEditorPane getEditorPane() { 288 return pane; 289 } 290 291 public void updateName() { 292 } 293 294 public void ensureVisible() { 295 tc.open(); 296 tc.requestVisible(); 297 } 298 299 } 300 301 private static class TC extends CloneableTopComponent { 302 303 304 public void requestActive() { 305 super.requestActive(); 306 } 307 308 } 309 310 } 311 | Popular Tags |