KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.io.IOException JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
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 /** Testing different features of CloneableEditorSupport
34  *
35  * @author Jaroslav Tulach
36  */

37 public class CloneableEditorSupportPaneTest extends NbTestCase implements CloneableEditorSupport.Env {
38     /** the support to work with */
39     private CloneableEditorSupport support;
40     private CloneableEditorSupport support2;
41     /** the content of lookup of support */
42     private InstanceContent ic;
43
44     
45     // Env variables
46
private String JavaDoc content = "";
47     private boolean valid = true;
48     private boolean modified = false;
49     /** if not null contains message why this document cannot be modified */
50     private String JavaDoc cannotBeModified;
51     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
52     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
53     private java.beans.VetoableChangeListener JavaDoc vetoL;
54
55     
56     public CloneableEditorSupportPaneTest(java.lang.String JavaDoc testName) {
57         super(testName);
58     }
59     
60     public static void main(java.lang.String JavaDoc[] 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 JavaDoc {
78         content = "Ahoj\nMyDoc";
79         javax.swing.text.Document JavaDoc doc = support.openDocument ();
80         support.open();
81         Line line = support.getLineSet().getCurrent(0);
82         line.show(Line.SHOW_SHOW);
83         JEditorPane JavaDoc[] 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 JavaDoc {
92         content = "Ahoj\nMyDoc";
93         javax.swing.text.Document JavaDoc doc = support2.openDocument ();
94         support2.open();
95         Line line = support2.getLineSet().getCurrent(0);
96         line.show(Line.SHOW_SHOW);
97         JEditorPane JavaDoc[] panes = support2.getOpenedPanes();
98         assertNotNull(panes);
99         assertEquals(1, panes.length);
100         assertNotNull(instance2);
101                 
102     }
103
104     
105     public void testCreateCloneableTopComponent() throws Exception JavaDoc {
106         CloneableTopComponent comp = support.createCloneableTopComponent();
107         assertNotNull(comp);
108         assertEquals(MyPane.class, comp.getClass());
109     }
110     
111     //
112
// Implementation of the CloneableEditorSupport.Env
113
//
114

115     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
116         propL.add (l);
117     }
118     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
119         propL.remove (l);
120     }
121     
122     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
123 // assertNull ("This is the first veto listener", vetoL);
124
vetoL = l;
125     }
126     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
127 // assertEquals ("Removing the right veto one", vetoL, l);
128
vetoL = null;
129     }
130     
131     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
132         return support;
133     }
134     
135     public String JavaDoc getMimeType() {
136         return "text/plain";
137     }
138     
139     public java.util.Date JavaDoc getTime() {
140         return date;
141     }
142     
143     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
144         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
145     }
146     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
147         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
148             public void close () throws java.io.IOException JavaDoc {
149                 super.close ();
150                 content = new String JavaDoc (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 JavaDoc {
166         if (cannotBeModified != null) {
167             IOException JavaDoc e = new IOException JavaDoc ();
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     /** Implementation of the CES */
184     private static class CES extends CloneableEditorSupport {
185         public CES (Env env, Lookup l) {
186             super (env, l);
187         }
188         
189         protected String JavaDoc messageName() {
190             return "Name";
191         }
192         
193         protected String JavaDoc messageOpened() {
194             return "Opened";
195         }
196         
197         protected String JavaDoc messageOpening() {
198             return "Opening";
199         }
200         
201         protected String JavaDoc messageSave() {
202             return "Save";
203         }
204         
205         protected String JavaDoc 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 JavaDoc pane;
222         
223         MyPane() {
224             pane = new JEditorPane JavaDoc();
225             
226         }
227         
228         public org.openide.windows.CloneableTopComponent getComponent() {
229             return this;
230         }
231         
232         public javax.swing.JEditorPane JavaDoc 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        /**
246          * callback for the Pane implementation to adjust itself to the openAt() request.
247          */

248         public void ensureVisible() {
249             open();
250             requestVisible();
251         }
252     }
253     
254     
255 //-------------------------------------------------------------------------------
256
//-------------------------------------------------------------------------------
257

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 JavaDoc pane;
276         
277         MyPaneNonNonTC() {
278             pane = new JEditorPane JavaDoc();
279             tc = new TC();
280             
281         }
282         
283         public org.openide.windows.CloneableTopComponent getComponent() {
284             return tc;
285         }
286         
287         public javax.swing.JEditorPane JavaDoc 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