Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.openide.text; |
46 |
|
47 |
import java.awt.Dialog; |
48 |
import java.awt.EventQueue; |
49 |
import java.awt.Frame; |
50 |
import java.io.IOException; |
51 |
import java.util.concurrent.atomic.AtomicReference; |
52 |
import javax.swing.JEditorPane; |
53 |
import javax.swing.text.StyledDocument; |
54 |
import org.netbeans.junit.NbTestCase; |
55 |
import org.openide.DialogDescriptor; |
56 |
import org.openide.DialogDisplayer; |
57 |
import org.openide.NotifyDescriptor; |
58 |
import org.openide.cookies.EditorCookie; |
59 |
import org.openide.filesystems.FileObject; |
60 |
import org.openide.filesystems.FileUtil; |
61 |
import org.openide.loaders.DataFolder; |
62 |
import org.openide.loaders.DataLoader; |
63 |
import org.openide.loaders.DataObject; |
64 |
import org.openide.loaders.DataObjectExistsException; |
65 |
import org.openide.loaders.FileEntry; |
66 |
import org.openide.loaders.MultiDataObject; |
67 |
import org.openide.loaders.MultiFileLoader; |
68 |
import org.openide.loaders.UniFileLoader; |
69 |
import org.openide.util.test.MockLookup; |
70 |
|
71 |
public class DataEditorSupportMoveTest extends NbTestCase { |
72 |
|
73 |
static { |
74 |
System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); |
75 |
} |
76 |
|
77 |
public DataEditorSupportMoveTest(String s) { |
78 |
super(s); |
79 |
} |
80 |
|
81 |
@Override |
82 |
protected void setUp() throws Exception { |
83 |
clearWorkDir(); |
84 |
MockLookup.setInstances(new Pool(), new DD()); |
85 |
} |
86 |
|
87 |
public void testModifiedMove() throws Exception { |
88 |
FileObject root = FileUtil.toFileObject(getWorkDir()); |
89 |
FileObject data = FileUtil.createData(root, "someFolder/someFile.obj"); |
90 |
|
91 |
DataObject obj = DataObject.find(data); |
92 |
assertEquals( MyDataObject.class, obj.getClass()); |
93 |
assertEquals(MyLoader.class, obj.getLoader().getClass()); |
94 |
|
95 |
EditorCookie ec = obj.getLookup().lookup(EditorCookie.class); |
96 |
assertNotNull("Editor cookie found", ec); |
97 |
ec.open(); |
98 |
JEditorPane[] arr = openedPanes(ec); |
99 |
assertEquals("One pane is opened", 1, arr.length); |
100 |
|
101 |
StyledDocument doc = ec.openDocument(); |
102 |
doc.insertString(0, "Ahoj", null); |
103 |
assertTrue("Modified", obj.isModified()); |
104 |
Thread.sleep(100); |
105 |
|
106 |
FileObject newFolder = FileUtil.createFolder(root, "otherFolder"); |
107 |
DataFolder df = DataFolder.findFolder(newFolder); |
108 |
|
109 |
obj.move(df); |
110 |
|
111 |
assertEquals(newFolder, obj.getPrimaryFile().getParent()); |
112 |
|
113 |
assertEquals("Still one editor", 1, openedPanes(ec).length); |
114 |
DD.assertNoCalls(); |
115 |
} |
116 |
|
117 |
private JEditorPane[] openedPanes(final EditorCookie ec) throws Exception { |
118 |
final AtomicReference<JEditorPane[]> ref = new AtomicReference<JEditorPane[]>(); |
119 |
EventQueue.invokeAndWait(new Runnable() { |
120 |
@Override |
121 |
public void run() { |
122 |
ref.set(ec.getOpenedPanes()); |
123 |
} |
124 |
}); |
125 |
return ref.get(); |
126 |
} |
127 |
|
128 |
private static final class Pool extends org.openide.loaders.DataLoaderPool { |
129 |
@Override |
130 |
protected java.util.Enumeration<? extends DataLoader> loaders() { |
131 |
return org.openide.util.Enumerations.array(DataLoader.getLoader(MyLoader.class), |
132 |
DataLoader.getLoader(MyMultiFileLoader.class)); |
133 |
} |
134 |
} |
135 |
|
136 |
public static final class MyLoader extends UniFileLoader { |
137 |
|
138 |
public MyLoader() { |
139 |
super(MyDataObject.class.getName ()); |
140 |
getExtensions ().addExtension ("obj"); |
141 |
getExtensions ().addExtension ("newExt"); |
142 |
} |
143 |
@Override |
144 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
145 |
return new MyDataObject(this, primaryFile); |
146 |
} |
147 |
} |
148 |
|
149 |
public static final class MyDataObject extends MultiDataObject { |
150 |
public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException { |
151 |
super(folder, l); |
152 |
registerEditor("text/plain", false); |
153 |
} |
154 |
|
155 |
} |
156 |
|
157 |
private static class MyMultiFileLoader extends MultiFileLoader { |
158 |
public MyMultiFileLoader () { |
159 |
super(MyMultiFileDataObject.class.getName()); |
160 |
} |
161 |
|
162 |
@Override |
163 |
protected MultiDataObject createMultiObject (FileObject primaryFile) throws DataObjectExistsException, IOException { |
164 |
return new MyMultiFileDataObject( primaryFile, this ); |
165 |
} |
166 |
|
167 |
@Override |
168 |
protected FileObject findPrimaryFile(FileObject fo) { |
169 |
if (!fo.isFolder()) { |
170 |
// here is the common code for the worse behaviour |
171 |
if (fo.hasExt("prima")) { |
172 |
return FileUtil.findBrother(fo, "seconda") != null ? fo : null; |
173 |
} |
174 |
|
175 |
if (fo.hasExt("seconda")) { |
176 |
return FileUtil.findBrother(fo, "prima"); |
177 |
} |
178 |
} |
179 |
return null; |
180 |
} |
181 |
|
182 |
@Override |
183 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
184 |
return new FileEntry (obj, primaryFile); |
185 |
} |
186 |
|
187 |
@Override |
188 |
protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { |
189 |
return new FileEntry (obj, secondaryFile); |
190 |
} |
191 |
} // end of MyDL3 |
192 |
|
193 |
private static class MyMultiFileDataObject extends MultiDataObject { |
194 |
public MyMultiFileDataObject( FileObject primaryFile, MultiFileLoader loader ) throws DataObjectExistsException { |
195 |
super( primaryFile, loader ); |
196 |
} |
197 |
} |
198 |
|
199 |
public static final class DD extends DialogDisplayer { |
200 |
static Exception called; |
201 |
|
202 |
@Override |
203 |
public Object notify(NotifyDescriptor descriptor) { |
204 |
called = new Exception("Notify called"); |
205 |
return NotifyDescriptor.CANCEL_OPTION; |
206 |
} |
207 |
|
208 |
@Override |
209 |
public Dialog createDialog(DialogDescriptor descriptor) { |
210 |
called = new Exception("Dialog created"); |
211 |
return new Dialog((Frame)null); |
212 |
} |
213 |
|
214 |
public static void assertNoCalls() throws Exception { |
215 |
if (called != null) { |
216 |
throw called; |
217 |
} |
218 |
} |
219 |
} |
220 |
} |