1 19 20 package org.openide.text; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import javax.swing.JEditorPane ; 25 import javax.swing.JPanel ; 26 import javax.swing.JScrollPane ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.EditorKit ; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.util.Lookup; 31 import org.openide.windows.CloneableTopComponent; 32 33 39 public class WrapEditorComponentTest extends NbTestCase 40 implements CloneableEditorSupport.Env { 41 42 private String content = ""; 43 private boolean valid = true; 44 private boolean modified = false; 45 private java.util.Date date = new java.util.Date (); 46 47 private WrapEditorComponentCES support; 48 49 public WrapEditorComponentTest(String s) { 50 super(s); 51 } 52 53 protected void setUp() { 54 support = new WrapEditorComponentCES(this, Lookup.EMPTY); 55 } 56 57 protected boolean runInEQ() { 58 return true; 59 } 60 61 64 public void testWrapEditorComponentInDefaultEditor() { 65 searchForWrapperComponent(); 66 } 67 68 71 public void testWrapEditorComponentInCustomEditor() { 72 support.setEditorKit(new NbLikeEditorKitWithCustomEditor()); 74 75 searchForWrapperComponent(); 76 } 77 78 82 private void searchForWrapperComponent() { 83 support.open(); 84 85 CloneableEditor ed = (CloneableEditor)support.getRef ().getAnyComponent(); 86 Component component = ed.getEditorPane(); 87 88 boolean found = false; 89 while (component != ed) { 90 if (WrapEditorComponentCES.WRAPPER_NAME.equals(component.getName())) { 91 found = true; 92 break; 93 } 94 component = component.getParent(); 95 } 96 97 assertTrue("The panel containing the editor was not found in the TopComponent.", found); 98 99 support.close(); 100 } 101 102 106 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 107 } 108 109 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 110 } 111 112 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 113 } 114 115 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 116 } 117 118 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 119 return support; 120 } 121 122 public String getMimeType() { 123 return "text/plain"; 124 } 125 126 public java.util.Date getTime() { 127 return date; 128 } 129 130 public java.io.InputStream inputStream() throws java.io.IOException { 131 return new java.io.ByteArrayInputStream (content.getBytes ()); 132 } 133 public java.io.OutputStream outputStream() throws java.io.IOException { 134 class ContentStream extends java.io.ByteArrayOutputStream { 135 public void close () throws java.io.IOException { 136 super.close (); 137 content = new String (toByteArray ()); 138 } 139 } 140 141 return new ContentStream (); 142 } 143 144 public boolean isValid() { 145 return valid; 146 } 147 148 public boolean isModified() { 149 return modified; 150 } 151 152 public void markModified() throws java.io.IOException { 153 modified = true; 154 } 155 156 public void unmarkModified() { 157 modified = false; 158 } 159 160 164 private static final class WrapEditorComponentCES extends CloneableEditorSupport { 165 166 public static final String WRAPPER_NAME = "panelWrappingTheEditor"; 167 168 private EditorKit kit; 169 170 public WrapEditorComponentCES(Env env, Lookup l) { 171 super(env, l); 172 } 173 174 protected Component wrapEditorComponent(Component editorComponent) { 175 JPanel panel = new JPanel (new BorderLayout ()); 176 panel.setName(WRAPPER_NAME); 177 panel.add(editorComponent, BorderLayout.CENTER); 178 return panel; 179 } 180 181 protected EditorKit createEditorKit() { 182 if (kit != null) { 183 return kit; 184 } else { 185 return super.createEditorKit(); 186 } 187 } 188 189 public void setEditorKit(EditorKit kit) { 190 this.kit = kit; 191 } 192 193 public CloneableTopComponent.Ref getRef () { 194 return allEditors; 195 } 196 197 protected String messageName() { 198 return "Name"; 199 } 200 201 protected String messageOpened() { 202 return "Opened"; 203 } 204 205 protected String messageOpening() { 206 return "Opening"; 207 } 208 209 protected String messageSave() { 210 return "Save"; 211 } 212 213 protected String messageToolTip() { 214 return "ToolTip"; 215 } 216 } 217 218 private final static class NbLikeEditorKitWithCustomEditor extends NbLikeEditorKit { 219 220 public Document createDefaultDocument() { 221 return new CustomDoc(); 222 } 223 224 private final class CustomDoc extends Doc implements NbDocument.CustomEditor { 225 226 public Component createEditor(JEditorPane j) { 227 JScrollPane result = new JScrollPane (); 228 result.add(j); 229 return result; 230 } 231 } 232 } 233 } 234 | Popular Tags |