KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
24 import java.io.*;
25 import java.util.*;
26 import javax.swing.JEditorPane JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.util.Exceptions;
29 import org.openide.util.Lookup;
30 import org.openide.util.io.NbMarshalledObject;
31 import org.openide.windows.*;
32
33 public class CloneableEditorTest extends NbTestCase
34 implements CloneableEditorSupport.Env {
35     /** the support to work with */
36     private transient CES support;
37
38     // Env variables
39
private transient String JavaDoc content = "";
40     private transient boolean valid = true;
41     private transient boolean modified = false;
42     /** if not null contains message why this document cannot be modified */
43     private transient String JavaDoc cannotBeModified;
44     private transient Date date = new Date ();
45     private transient List/*<PropertyChangeListener>*/ propL = new ArrayList ();
46     private transient VetoableChangeListener vetoL;
47     
48     private static CloneableEditorTest RUNNING;
49     
50     public CloneableEditorTest(String JavaDoc s) {
51         super(s);
52     }
53     
54     protected void setUp () {
55         support = new CES (this, Lookup.EMPTY);
56         RUNNING = this;
57     }
58     
59     protected boolean runInEQ() {
60         return true;
61     }
62     
63     private Object JavaDoc writeReplace () {
64         return new Replace ();
65     }
66     
67     public void testGetOpenedPanesWorksAfterDeserializationIssue39236 () throws Exception JavaDoc {
68         support.open ();
69
70         CloneableEditor ed = (CloneableEditor)support.getRef ().getAnyComponent ();
71         
72         JEditorPane JavaDoc[] panes = support.getOpenedPanes ();
73         assertNotNull (panes);
74         assertEquals ("One is there", 1, panes.length);
75         
76         NbMarshalledObject obj = new NbMarshalledObject (ed);
77         ed.close ();
78         
79         panes = support.getOpenedPanes ();
80         assertNull ("No panes anymore", panes);
81         
82         ed = (CloneableEditor)obj.get ();
83         
84         panes = support.getOpenedPanes ();
85         assertNotNull ("One again", panes);
86         assertEquals ("One is there again", 1, panes.length);
87     }
88     
89     public void testEditorPaneActionMapIsParentOfCloneableEditorActionMap() {
90         support.open();
91         
92         CloneableEditor ed = (CloneableEditor)support.getRef().getArbitraryComponent();
93         
94         assertSame(ed.getEditorPane().getActionMap(), ed.getActionMap().getParent());
95     }
96
97     
98     //
99
// Implementation of the CloneableEditorSupport.Env
100
//
101

102     public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
103         propL.add (l);
104     }
105     public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
106         propL.remove (l);
107     }
108     
109     public synchronized void addVetoableChangeListener(VetoableChangeListener l) {
110         assertNull ("This is the first veto listener", vetoL);
111         vetoL = l;
112     }
113     public void removeVetoableChangeListener(VetoableChangeListener l) {
114         assertEquals ("Removing the right veto one", vetoL, l);
115         vetoL = null;
116     }
117     
118     public CloneableOpenSupport findCloneableOpenSupport() {
119         return RUNNING.support;
120     }
121     
122     public String JavaDoc getMimeType() {
123         return "text/plain";
124     }
125     
126     public Date getTime() {
127         return date;
128     }
129     
130     public InputStream inputStream() throws IOException {
131         return new ByteArrayInputStream (content.getBytes ());
132     }
133     public OutputStream outputStream() throws IOException {
134         class ContentStream extends ByteArrayOutputStream {
135             public void close () throws IOException {
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 IOException {
153         if (cannotBeModified != null) {
154             final String JavaDoc notify = cannotBeModified;
155             IOException e = new IOException () {
156                 public String JavaDoc getLocalizedMessage () {
157                     return notify;
158                 }
159             };
160             Exceptions.attachLocalizedMessage(e, cannotBeModified);
161             throw e;
162         }
163         
164         modified = true;
165     }
166     
167     public void unmarkModified() {
168         modified = false;
169     }
170
171     /** Implementation of the CES */
172     private static final class CES extends CloneableEditorSupport {
173         public CES (Env env, Lookup l) {
174             super (env, l);
175         }
176         
177         public CloneableTopComponent.Ref getRef () {
178             return allEditors;
179         }
180         
181         protected String JavaDoc messageName() {
182             return "Name";
183         }
184         
185         protected String JavaDoc messageOpened() {
186             return "Opened";
187         }
188         
189         protected String JavaDoc messageOpening() {
190             return "Opening";
191         }
192         
193         protected String JavaDoc messageSave() {
194             return "Save";
195         }
196         
197         protected String JavaDoc messageToolTip() {
198             return "ToolTip";
199         }
200         
201     }
202
203     private static final class Replace implements Serializable {
204         public Object JavaDoc readResolve () {
205             return RUNNING;
206         }
207     }
208 }
209
Popular Tags