KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > WrapEditorComponentTest


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.openide.text;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.JScrollPane JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.EditorKit JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.util.Lookup;
31 import org.openide.windows.CloneableTopComponent;
32
33 /**
34  * Tests that the CloneableEditorSupport.wrapEditorComponent() method
35  * is called by CloneableEditor and its result value used.
36  *
37  * @author Andrei Badea
38  */

39 public class WrapEditorComponentTest extends NbTestCase
40 implements CloneableEditorSupport.Env {
41     
42     private String JavaDoc content = "";
43     private boolean valid = true;
44     private boolean modified = false;
45     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
46     
47     private WrapEditorComponentCES support;
48     
49     public WrapEditorComponentTest(String JavaDoc 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     /**
62      * Tests the wrapEditorComponent() method is called for a default editor.
63      */

64     public void testWrapEditorComponentInDefaultEditor() {
65         searchForWrapperComponent();
66     }
67     
68     /**
69      * Tests the wrapEditorComponent() method is called for a custom editor
70      */

71     public void testWrapEditorComponentInCustomEditor() {
72         // first make the support return a document which has a custom editor
73
support.setEditorKit(new NbLikeEditorKitWithCustomEditor());
74         
75         searchForWrapperComponent();
76     }
77     
78     /**
79      * Helper method which opens the support and searches for the wrapper
80      * component.
81      */

82     private void searchForWrapperComponent() {
83         support.open();
84         
85         CloneableEditor ed = (CloneableEditor)support.getRef ().getAnyComponent();
86         Component JavaDoc 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     //
103
// Implementation of the CloneableEditorSupport.Env
104
//
105

106     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
107     }
108     
109     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
110     }
111     
112     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
113     }
114     
115     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
116     }
117     
118     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
119         return support;
120     }
121     
122     public String JavaDoc getMimeType() {
123         return "text/plain";
124     }
125     
126     public java.util.Date JavaDoc getTime() {
127         return date;
128     }
129     
130     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
131         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
132     }
133     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
134         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
135             public void close () throws java.io.IOException JavaDoc {
136                 super.close ();
137                 content = new String JavaDoc (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 JavaDoc {
153         modified = true;
154     }
155     
156     public void unmarkModified() {
157         modified = false;
158     }
159
160     /**
161      * Implementation of the CES which overrides the wrapEditorComponent()
162      * method, wrapping the editor in a component named WRAPPER_NAME.
163      */

164     private static final class WrapEditorComponentCES extends CloneableEditorSupport {
165         
166         public static final String JavaDoc WRAPPER_NAME = "panelWrappingTheEditor";
167         
168         private EditorKit JavaDoc kit;
169         
170         public WrapEditorComponentCES(Env env, Lookup l) {
171             super(env, l);
172         }
173         
174         protected Component JavaDoc wrapEditorComponent(Component JavaDoc editorComponent) {
175             JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
176             panel.setName(WRAPPER_NAME);
177             panel.add(editorComponent, BorderLayout.CENTER);
178             return panel;
179         }
180         
181         protected EditorKit JavaDoc createEditorKit() {
182             if (kit != null) {
183                 return kit;
184             } else {
185                 return super.createEditorKit();
186             }
187         }
188         
189         public void setEditorKit(EditorKit JavaDoc kit) {
190             this.kit = kit;
191         }
192         
193         public CloneableTopComponent.Ref getRef () {
194             return allEditors;
195         }
196         
197         protected String JavaDoc messageName() {
198             return "Name";
199         }
200         
201         protected String JavaDoc messageOpened() {
202             return "Opened";
203         }
204         
205         protected String JavaDoc messageOpening() {
206             return "Opening";
207         }
208         
209         protected String JavaDoc messageSave() {
210             return "Save";
211         }
212         
213         protected String JavaDoc messageToolTip() {
214             return "ToolTip";
215         }
216     }
217     
218     private final static class NbLikeEditorKitWithCustomEditor extends NbLikeEditorKit {
219         
220         public Document JavaDoc createDefaultDocument() {
221             return new CustomDoc();
222         }
223         
224         private final class CustomDoc extends Doc implements NbDocument.CustomEditor {
225             
226             public Component JavaDoc createEditor(JEditorPane JavaDoc j) {
227                 JScrollPane JavaDoc result = new JScrollPane JavaDoc();
228                 result.add(j);
229                 return result;
230             }
231         }
232     }
233 }
234
Popular Tags