Lines 44-55
Link Here
|
44 |
|
44 |
|
45 |
package org.netbeans.editor; |
45 |
package org.netbeans.editor; |
46 |
|
46 |
|
|
|
47 |
import java.beans.PropertyChangeListener; |
48 |
import java.beans.VetoableChangeListener; |
49 |
import java.io.ByteArrayInputStream; |
50 |
import java.io.ByteArrayOutputStream; |
51 |
import java.io.IOException; |
52 |
import java.io.InputStream; |
53 |
import java.io.OutputStream; |
54 |
import java.util.ArrayList; |
55 |
import java.util.Date; |
56 |
import java.util.List; |
47 |
import javax.swing.event.UndoableEditEvent; |
57 |
import javax.swing.event.UndoableEditEvent; |
48 |
import javax.swing.event.UndoableEditListener; |
58 |
import javax.swing.event.UndoableEditListener; |
|
|
59 |
import javax.swing.text.Document; |
60 |
import javax.swing.text.EditorKit; |
49 |
import javax.swing.undo.UndoableEdit; |
61 |
import javax.swing.undo.UndoableEdit; |
50 |
import org.netbeans.api.editor.mimelookup.MimePath; |
62 |
import org.netbeans.api.editor.mimelookup.MimePath; |
51 |
import org.netbeans.api.editor.mimelookup.test.MockMimeLookup; |
63 |
import org.netbeans.api.editor.mimelookup.test.MockMimeLookup; |
52 |
import org.netbeans.junit.NbTestCase; |
64 |
import org.netbeans.junit.NbTestCase; |
|
|
65 |
import org.netbeans.modules.openide.text.NbDocumentRefactoringHack; |
66 |
import org.openide.awt.UndoRedo; |
67 |
import org.openide.cookies.EditorCookie; |
68 |
import org.openide.text.CloneableEditorSupport; |
69 |
import org.openide.util.Exceptions; |
70 |
import org.openide.util.Lookup; |
71 |
import org.openide.windows.CloneableOpenSupport; |
72 |
import org.openide.windows.CloneableTopComponent; |
53 |
|
73 |
|
54 |
/** |
74 |
/** |
55 |
* |
75 |
* |
Lines 57-62
Link Here
|
57 |
*/ |
77 |
*/ |
58 |
public class UndoableEditWrapperTest extends NbTestCase { |
78 |
public class UndoableEditWrapperTest extends NbTestCase { |
59 |
|
79 |
|
|
|
80 |
static { |
81 |
System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); |
82 |
} |
83 |
|
60 |
/** Creates a new instance of ZOrderTest */ |
84 |
/** Creates a new instance of ZOrderTest */ |
61 |
public UndoableEditWrapperTest(String name) { |
85 |
public UndoableEditWrapperTest(String name) { |
62 |
super(name); |
86 |
super(name); |
Lines 64-80
Link Here
|
64 |
|
88 |
|
65 |
public void testWrapping() throws Exception { |
89 |
public void testWrapping() throws Exception { |
66 |
MimePath mimePath = MimePath.EMPTY; |
90 |
MimePath mimePath = MimePath.EMPTY; |
67 |
MockMimeLookup.setInstances(mimePath, new TestingUndoableEditWrapper()); |
91 |
MockMimeLookup.setInstances(mimePath, new TestingUndoableEditWrapper(), new TestingUndoableEditWrapper2()); |
68 |
BaseDocument bDoc = new BaseDocument(false, ""); |
92 |
CESEnv env = new CESEnv(); |
69 |
bDoc.addUndoableEditListener(new UndoableEditListener() { |
93 |
Document doc = env.support.openDocument(); |
70 |
@Override |
94 |
// doc.addUndoableEditListener(new UndoableEditListener() { |
71 |
public void undoableEditHappened(UndoableEditEvent e) { |
95 |
// @Override |
72 |
UndoableEdit edit = e.getEdit(); |
96 |
// public void undoableEditHappened(UndoableEditEvent e) { |
73 |
assertEquals("Expected WrapCompoundEdit.class", |
97 |
// UndoableEdit edit = e.getEdit(); |
74 |
TestingUndoableEditWrapper.WrapCompoundEdit.class, edit.getClass()); |
98 |
// } |
|
|
99 |
// }); |
100 |
doc.insertString(0, "Test", null); |
101 |
Class wrapEditClass = TestingUndoableEditWrapper.WrapCompoundEdit.class; |
102 |
assertNotNull(NbDocumentRefactoringHack.getEditToBeUndoneOfType(env.support, wrapEditClass)); |
103 |
Class wrapEditClass2 = TestingUndoableEditWrapper2.WrapCompoundEdit2.class; |
104 |
assertNotNull(NbDocumentRefactoringHack.getEditToBeUndoneOfType(env.support, wrapEditClass2)); |
105 |
|
106 |
// A trick to get whole edit |
107 |
UndoableEdit wholeEdit = NbDocumentRefactoringHack.getEditToBeUndoneOfType(env.support, UndoableEdit.class); |
108 |
assertTrue(wholeEdit instanceof List); |
109 |
@SuppressWarnings("unchecked") |
110 |
List<? extends UndoableEdit> listEdit = (List<? extends UndoableEdit>) wholeEdit; |
111 |
assertEquals(3, listEdit.size()); |
112 |
assertEquals(wrapEditClass, listEdit.get(1).getClass()); |
113 |
assertEquals(wrapEditClass2, listEdit.get(2).getClass()); |
114 |
} |
115 |
|
116 |
private static final class CESEnv implements CloneableEditorSupport.Env { |
117 |
|
118 |
static final String mimeType = "text/plain"; |
119 |
|
120 |
/** the support to work with */ |
121 |
transient final CES support; |
122 |
|
123 |
private transient String content = ""; // initial document content |
124 |
private transient boolean modified = false; |
125 |
/** if not null contains message why this document cannot be modified */ |
126 |
private transient String cannotBeModified; |
127 |
private transient Date date = new Date (); |
128 |
private transient List/*<PropertyChangeListener>*/ propL = new ArrayList (); |
129 |
private transient VetoableChangeListener vetoL; |
130 |
|
131 |
public CESEnv() { |
132 |
support = new CES (this, Lookup.EMPTY); |
133 |
} |
134 |
|
135 |
@Override |
136 |
public synchronized void addPropertyChangeListener(PropertyChangeListener l) { |
137 |
propL.add (l); |
138 |
} |
139 |
@Override |
140 |
public synchronized void removePropertyChangeListener(PropertyChangeListener l) { |
141 |
propL.remove (l); |
142 |
} |
143 |
|
144 |
@Override |
145 |
public synchronized void addVetoableChangeListener(VetoableChangeListener l) { |
146 |
assertNull ("This is the first veto listener", vetoL); |
147 |
vetoL = l; |
148 |
} |
149 |
|
150 |
@Override |
151 |
public void removeVetoableChangeListener(VetoableChangeListener l) { |
152 |
assertEquals ("Removing the right veto one", vetoL, l); |
153 |
vetoL = null; |
154 |
} |
155 |
|
156 |
@Override |
157 |
public CloneableOpenSupport findCloneableOpenSupport() { |
158 |
return support; |
159 |
} |
160 |
|
161 |
@Override |
162 |
public String getMimeType() { |
163 |
return mimeType; |
164 |
} |
165 |
|
166 |
@Override |
167 |
public Date getTime() { |
168 |
return date; |
169 |
} |
170 |
|
171 |
@Override |
172 |
public InputStream inputStream() throws IOException { |
173 |
return new ByteArrayInputStream (content.getBytes ()); |
174 |
} |
175 |
@Override |
176 |
public OutputStream outputStream() throws IOException { |
177 |
class ContentStream extends ByteArrayOutputStream { |
178 |
@Override |
179 |
public void close () throws IOException { |
180 |
super.close (); |
181 |
content = new String (toByteArray ()); |
182 |
} |
75 |
} |
183 |
} |
76 |
}); |
184 |
return new ContentStream (); |
77 |
bDoc.insertString(0, "Test", null); |
185 |
} |
|
|
186 |
|
187 |
@Override |
188 |
public boolean isValid() { |
189 |
return true; |
190 |
} |
191 |
|
192 |
@Override |
193 |
public boolean isModified() { |
194 |
return modified; |
195 |
} |
196 |
|
197 |
@Override |
198 |
public void markModified() throws IOException { |
199 |
if (cannotBeModified != null) { |
200 |
final String notify = cannotBeModified; |
201 |
IOException e = new IOException () { |
202 |
@Override |
203 |
public String getLocalizedMessage () { |
204 |
return notify; |
205 |
} |
206 |
}; |
207 |
Exceptions.attachLocalizedMessage(e, cannotBeModified); |
208 |
throw e; |
209 |
} |
210 |
|
211 |
modified = true; |
212 |
} |
213 |
|
214 |
@Override |
215 |
public void unmarkModified() { |
216 |
modified = false; |
217 |
} |
218 |
|
219 |
} |
220 |
|
221 |
/** Implementation of the CES */ |
222 |
private static final class CES extends CloneableEditorSupport implements EditorCookie { |
223 |
|
224 |
public CES (CloneableEditorSupport.Env env, Lookup l) { |
225 |
super (env, l); |
226 |
} |
227 |
|
228 |
@Override |
229 |
protected EditorKit createEditorKit () { |
230 |
// Important to use NbLikeEditorKit since otherwise FilterDocument |
231 |
// would be created with improper runAtomic() |
232 |
return new MyKit (); |
233 |
} |
234 |
|
235 |
public CloneableTopComponent.Ref getRef () { |
236 |
return allEditors; |
237 |
} |
238 |
|
239 |
@Override |
240 |
protected String messageName() { |
241 |
return "Name"; |
242 |
} |
243 |
|
244 |
@Override |
245 |
protected String messageOpened() { |
246 |
return "Opened"; |
247 |
} |
248 |
|
249 |
@Override |
250 |
protected String messageOpening() { |
251 |
return "Opening"; |
252 |
} |
253 |
|
254 |
@Override |
255 |
protected String messageSave() { |
256 |
return "Save"; |
257 |
} |
258 |
|
259 |
@Override |
260 |
protected String messageToolTip() { |
261 |
return "ToolTip"; |
262 |
} |
263 |
|
264 |
public UndoRedo getUndoRedoPublic() { |
265 |
return getUndoRedo(); |
266 |
} |
267 |
|
268 |
} |
269 |
|
270 |
private static final class MyKit extends BaseKit { |
271 |
|
272 |
@Override |
273 |
public String getContentType() { |
274 |
return CESEnv.mimeType; |
275 |
} |
276 |
|
78 |
} |
277 |
} |
79 |
|
278 |
|
80 |
} |
279 |
} |