Skip to content

Commit 2311f3a

Browse files
committed
Better support for external resources and views
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
1 parent ce7b2ab commit 2311f3a

File tree

7 files changed

+96
-57
lines changed

7 files changed

+96
-57
lines changed

AnyEditTools/src/de/loskutov/anyedit/actions/AbstractAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void selectionChanged(IAction action, ISelection selection) {
129129
public IFile getFile() {
130130
IFile myFile = file;
131131
if(myFile == null && getEditor() != null){
132-
myFile = getEditor().getFile();
132+
myFile = getEditor().getIFile();
133133
}
134134
return myFile;
135135
}

AnyEditTools/src/de/loskutov/anyedit/actions/compare/CompareWithEditorAction.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package de.loskutov.anyedit.actions.compare;
1010

1111
import java.io.File;
12-
import java.net.URI;
1312
import java.util.ArrayList;
1413
import java.util.List;
1514

@@ -202,10 +201,9 @@ public EditorsContentProvider(AbstractEditor myEditor, ContentWrapper selectedCo
202201
continue;
203202
}
204203
AbstractEditor abstractEditor = new AbstractEditor(reference.getEditor(initEditor));
205-
URI uri = abstractEditor.getURI();
206204

207205
File file = selectedContent.getFile();
208-
File anotherFile = EclipseUtils.getLocalFile(uri);
206+
File anotherFile = abstractEditor.getFile();
209207
if(file != null && file.equals(anotherFile)){
210208
// same file as selection
211209
continue;
@@ -254,9 +252,9 @@ private boolean sameEditor(AbstractEditor abstractEditor) {
254252
if(myEditor == null){
255253
return false;
256254
}
257-
URI myUri = myEditor.getURI();
258-
URI anotherURI = abstractEditor.getURI();
259-
return myUri != null && myUri.equals(anotherURI);
255+
File myFile = myEditor.getFile();
256+
File another = abstractEditor.getFile();
257+
return myFile != null && myFile.equals(another);
260258
}
261259

262260
private boolean similarEditor(IEditorReference reference) {

AnyEditTools/src/de/loskutov/anyedit/actions/replace/ReplaceWithEditorAction.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.FileNotFoundException;
1515
import java.io.InputStream;
1616
import java.io.UnsupportedEncodingException;
17-
import java.net.URI;
1817

1918
import org.eclipse.core.resources.IFile;
2019
import org.eclipse.core.runtime.CoreException;
@@ -30,7 +29,6 @@
3029
import de.loskutov.anyedit.actions.compare.CompareWithEditorAction;
3130
import de.loskutov.anyedit.actions.compare.CompareWithEditorAction.EditorsContentProvider;
3231
import de.loskutov.anyedit.ui.editor.AbstractEditor;
33-
import de.loskutov.anyedit.util.EclipseUtils;
3432

3533
/**
3634
* @author Andrey
@@ -67,7 +65,7 @@ protected InputStream createInputStream() {
6765
IEditorReference reference = (IEditorReference) objects[0];
6866
IEditorPart editorPart = reference.getEditor(true);
6967
AbstractEditor editor1 = new AbstractEditor(editorPart);
70-
IFile file = editor1.getFile();
68+
IFile file = editor1.getIFile();
7169
if (file != null) {
7270
if (file.getLocation() != null) {
7371
try {
@@ -82,29 +80,26 @@ protected InputStream createInputStream() {
8280
} catch (FileNotFoundException e) {
8381
AnyEditToolsPlugin.logError("File not found: " + file, e);
8482
}
83+
}
84+
85+
File localFile = editor1.getFile();
86+
if (localFile != null) {
87+
try {
88+
return new FileInputStream(localFile);
89+
} catch (FileNotFoundException e) {
90+
AnyEditToolsPlugin.logError("File not found: "
91+
+ localFile, e);
92+
}
8593
} else {
86-
URI uri = editor1.getURI();
87-
if (uri != null) {
88-
File localFile = EclipseUtils.getLocalFile(uri);
89-
if (localFile != null) {
94+
IDocument document = editor1.getDocument();
95+
if(document != null) {
96+
String content = document.get();
97+
if (content != null) {
9098
try {
91-
return new FileInputStream(localFile);
92-
} catch (FileNotFoundException e) {
93-
AnyEditToolsPlugin.logError("File not found: "
94-
+ localFile, e);
95-
}
96-
}
97-
} else {
98-
IDocument document = editor1.getDocument();
99-
if(document != null) {
100-
String content = document.get();
101-
if (content != null) {
102-
try {
103-
return new ByteArrayInputStream(content.getBytes(editor1
104-
.computeEncoding()));
105-
} catch (UnsupportedEncodingException e) {
106-
return new ByteArrayInputStream(content.getBytes());
107-
}
99+
return new ByteArrayInputStream(content.getBytes(editor1
100+
.computeEncoding()));
101+
} catch (UnsupportedEncodingException e) {
102+
return new ByteArrayInputStream(content.getBytes());
108103
}
109104
}
110105
}

AnyEditTools/src/de/loskutov/anyedit/compare/ContentWrapper.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package de.loskutov.anyedit.compare;
1010

1111
import java.io.File;
12-
import java.net.URI;
1312

1413
import org.eclipse.compare.ITypedElement;
1514
import org.eclipse.core.resources.IFile;
@@ -42,8 +41,8 @@ public ContentWrapper(String name, String fileExtension, AbstractEditor editor)
4241
this.name = name;
4342
if (editor != null) {
4443
this.selection = editor.getSelection();
45-
this.ifile = editor.getFile();
46-
this.file = EclipseUtils.getLocalFile(editor.getURI());
44+
this.ifile = editor.getIFile();
45+
this.file = editor.getFile();
4746
} else {
4847
this.selection = null;
4948
}
@@ -119,20 +118,17 @@ public static ContentWrapper create(AbstractEditor editor1) {
119118
return new ContentWrapper(title, type, editor1);
120119
}
121120

122-
IFile file = editor1.getFile();
123-
if (file != null) {
124-
if (file.getLocation() != null) {
125-
return new ContentWrapper(file);
121+
IFile ifile = editor1.getIFile();
122+
if (ifile != null) {
123+
if (ifile.getLocation() != null) {
124+
return new ContentWrapper(ifile);
126125
}
127-
return new ContentWrapper(file.getFullPath().toFile());
126+
return new ContentWrapper(ifile.getFullPath().toFile());
128127
}
129128

130-
URI uri = editor1.getURI();
131-
if (uri != null) {
132-
File localFile = EclipseUtils.getLocalFile(uri);
133-
if (localFile != null) {
134-
return new ContentWrapper(localFile);
135-
}
129+
File file = editor1.getFile();
130+
if(file != null){
131+
return new ContentWrapper(file);
136132
}
137133
return null;
138134
}

AnyEditTools/src/de/loskutov/anyedit/compare/TextStreamContent.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,15 @@ private synchronized void addSelectionAnnotation() {
323323
if(editor.isDisposed()){
324324
return;
325325
}
326-
IEditorInput input = editor.getInput();
327326
IDocumentProvider documentProvider = editor.getDocumentProvider();
327+
if(documentProvider == null){
328+
return;
329+
}
330+
IEditorInput input = editor.getInput();
331+
if(input == null){
332+
return;
333+
}
334+
328335
IAnnotationModel extension = documentProvider.getAnnotationModel(input);
329336
if (!(extension instanceof IAnnotationModelExtension)) {
330337
return;
@@ -344,8 +351,14 @@ private synchronized void removeSelectionAnnotation() {
344351
if(editor.isDisposed()){
345352
return;
346353
}
347-
IEditorInput input = editor.getInput();
348354
IDocumentProvider documentProvider = editor.getDocumentProvider();
355+
if(documentProvider == null){
356+
return;
357+
}
358+
IEditorInput input = editor.getInput();
359+
if(input == null){
360+
return;
361+
}
349362
IAnnotationModel extension = documentProvider.getAnnotationModel(input);
350363
if (!(extension instanceof IAnnotationModelExtension)) {
351364
return;

AnyEditTools/src/de/loskutov/anyedit/ui/editor/AbstractEditor.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
package de.loskutov.anyedit.ui.editor;
1111

1212
import static de.loskutov.anyedit.util.EclipseUtils.getAdapter;
13-
import static de.loskutov.anyedit.util.EclipseUtils.getIFile;
1413

14+
import java.io.File;
1515
import java.lang.reflect.Method;
1616
import java.net.URI;
1717

@@ -162,18 +162,44 @@ public IEditorInput getInput() {
162162
* @return may return null
163163
*/
164164
@Nullable
165-
public IFile getFile(){
165+
public IFile getIFile(){
166+
if(wPart == null){
167+
return null;
168+
}
166169
IEditorInput input = getInput();
167170
if(input != null){
168-
IFile adapter = getIFile(input, true);
171+
IFile adapter = EclipseUtils.getIFile(input, true);
169172
if(adapter != null){
170173
return adapter;
171174
}
172175
}
176+
IFile adapter = EclipseUtils.getIFile(wPart, true);
177+
return adapter;
178+
}
179+
180+
@Nullable
181+
public File getFile(){
173182
if(wPart == null){
174183
return null;
175184
}
176-
IFile adapter = getIFile(wPart, true);
185+
IEditorInput input = getInput();
186+
if(input != null){
187+
File adapter = EclipseUtils.getFile(input, true);
188+
if(adapter != null){
189+
return adapter;
190+
}
191+
}
192+
File adapter = EclipseUtils.getFile(wPart, true);
193+
if(adapter != null){
194+
return adapter;
195+
}
196+
ISelectionProvider sp = getSelectionProvider();
197+
if(sp != null){
198+
ISelection selection = sp.getSelection();
199+
if(selection != null){
200+
adapter = EclipseUtils.getFile(selection, true);
201+
}
202+
}
177203
return adapter;
178204
}
179205

@@ -183,10 +209,16 @@ public IFile getFile(){
183209
@Nullable
184210
public String getContentType(){
185211
URI uri = getURI();
186-
if(uri == null){
187-
return null;
212+
String path;
213+
if(uri != null) {
214+
path = uri.toString();
215+
} else {
216+
File file = getFile();
217+
if(file == null) {
218+
return null;
219+
}
220+
path = file.getAbsolutePath();
188221
}
189-
String path = uri.toString();
190222
int dot = path.lastIndexOf('.') + 1;
191223
if(dot >= 0){
192224
return path.substring(dot);
@@ -207,13 +239,13 @@ public String getTitle(){
207239
* @return may return null
208240
*/
209241
@Nullable
210-
public URI getURI(){
242+
private URI getURI(){
211243
return EclipseUtils.getURI(getInput());
212244
}
213245

214246
@NonNull
215247
public String computeEncoding() {
216-
IFile file = getFile();
248+
IFile file = getIFile();
217249
if(file != null) {
218250
try {
219251
String charset = file.getCharset();

AnyEditTools/src/de/loskutov/anyedit/util/EclipseUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.eclipse.core.runtime.OperationCanceledException;
4242
import org.eclipse.core.runtime.Path;
4343
import org.eclipse.core.runtime.Platform;
44+
import org.eclipse.core.runtime.PlatformObject;
4445
import org.eclipse.debug.core.ILaunch;
4546
import org.eclipse.debug.core.ILaunchConfiguration;
4647
import org.eclipse.debug.core.model.IProcess;
@@ -206,6 +207,9 @@ public static File getFile(Object o, boolean askPlatform) {
206207
}
207208
return location.toFile();
208209
}
210+
if(o instanceof IEditorInput){
211+
return getFile((IEditorInput)o);
212+
}
209213
return null;
210214
}
211215

@@ -293,7 +297,8 @@ public static <V> V getAdapter(Object o, Class<V> target, boolean askPlatform) {
293297
return adapter;
294298
}
295299
}
296-
if(!askPlatform){
300+
// If the source object is a platform object then it's already tried calling AdapterManager.getAdapter
301+
if(!askPlatform || o instanceof PlatformObject){
297302
return null;
298303
}
299304
Object adapted = Platform.getAdapterManager().getAdapter(o, target);

0 commit comments

Comments
 (0)