]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile/src/org/simantics/graphfile/hack/SystemFile.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / hack / SystemFile.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graphfile.hack;\r
13 \r
14 import java.io.FileInputStream;\r
15 import java.io.FileNotFoundException;\r
16 import java.io.FileOutputStream;\r
17 import java.io.IOException;\r
18 import java.io.InputStream;\r
19 import java.io.Reader;\r
20 import java.net.URI;\r
21 import java.util.Map;\r
22 \r
23 import org.eclipse.core.resources.IContainer;\r
24 import org.eclipse.core.resources.IFile;\r
25 import org.eclipse.core.resources.IFileState;\r
26 import org.eclipse.core.resources.IMarker;\r
27 import org.eclipse.core.resources.IPathVariableManager;\r
28 import org.eclipse.core.resources.IProject;\r
29 import org.eclipse.core.resources.IProjectDescription;\r
30 import org.eclipse.core.resources.IResourceProxy;\r
31 import org.eclipse.core.resources.IResourceProxyVisitor;\r
32 import org.eclipse.core.resources.IResourceVisitor;\r
33 import org.eclipse.core.resources.IWorkspace;\r
34 import org.eclipse.core.resources.ResourceAttributes;\r
35 import org.eclipse.core.runtime.CoreException;\r
36 import org.eclipse.core.runtime.IPath;\r
37 import org.eclipse.core.runtime.IProgressMonitor;\r
38 import org.eclipse.core.runtime.Path;\r
39 import org.eclipse.core.runtime.QualifiedName;\r
40 import org.eclipse.core.runtime.Status;\r
41 import org.eclipse.core.runtime.content.IContentDescription;\r
42 import org.eclipse.core.runtime.jobs.ISchedulingRule;\r
43 import org.simantics.graphfile.Activator;\r
44 \r
45 /**\r
46  * This is an implementation of IFile that can be used to open external editor for any file.\r
47  * (Original implementation (org.eclipse.core.filesystem.File) doesn't work without project/file structure) \r
48  * \r
49  * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>\r
50  *\r
51  */\r
52 public class SystemFile implements IFile {\r
53         private java.io.File file;\r
54         private IWorkspace workspace;\r
55         \r
56         \r
57         public SystemFile(java.io.File file, IWorkspace ws) {\r
58                 this.file = file;\r
59                 this.workspace = ws;\r
60         }\r
61         \r
62         @Override\r
63         public void accept(IResourceProxyVisitor visitor, int memberFlags) throws CoreException {\r
64                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
65         }\r
66         \r
67         @Override\r
68         public void accept(IResourceVisitor visitor) throws CoreException {\r
69                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
70         }\r
71         \r
72         @Override\r
73         public void accept(IResourceVisitor visitor, int depth, boolean includePhantoms) throws CoreException {\r
74                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
75         }\r
76         \r
77         @Override\r
78         public void accept(IResourceVisitor visitor, int depth, int memberFlags) throws CoreException {\r
79                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
80         }\r
81         \r
82         @Override\r
83         public void accept(IResourceProxyVisitor visitor, int depth, int memberFlags) throws CoreException {\r
84                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
85         }\r
86 \r
87         @Override\r
88         public void clearHistory(IProgressMonitor monitor) throws CoreException {\r
89                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
90         }\r
91 \r
92         @Override\r
93         public void copy(IPath destination, boolean force, IProgressMonitor monitor) throws CoreException {\r
94                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
95                 \r
96         }\r
97 \r
98         @Override\r
99         public void copy(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
100                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
101                 \r
102         }\r
103 \r
104         @Override\r
105         public void copy(IProjectDescription description, boolean force, IProgressMonitor monitor) throws CoreException {\r
106                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
107                 \r
108         }\r
109 \r
110         @Override\r
111         public void copy(IProjectDescription description, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
112                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
113                 \r
114         }\r
115 \r
116         @Override\r
117         public IMarker createMarker(String type) throws CoreException {\r
118                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
119         }\r
120 \r
121         @Override\r
122         public IResourceProxy createProxy() {\r
123                 return null;\r
124         }\r
125 \r
126         @Override\r
127         public void delete(boolean force, IProgressMonitor monitor) throws CoreException {\r
128                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
129         }\r
130 \r
131         @Override\r
132         public void delete(int updateFlags, IProgressMonitor monitor) throws CoreException {\r
133                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
134                 \r
135         }\r
136 \r
137         @Override\r
138         public void deleteMarkers(String type, boolean includeSubtypes, int depth) throws CoreException {\r
139                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
140         }\r
141 \r
142         @Override\r
143         public boolean exists() {\r
144                 return true;\r
145         }\r
146 \r
147         @Override\r
148         public IMarker findMarker(long id) throws CoreException {\r
149                 return null;\r
150         }\r
151 \r
152         @Override\r
153         public IMarker[] findMarkers(String type, boolean includeSubtypes, int depth) throws CoreException {\r
154                 return null;\r
155         }\r
156 \r
157         @Override\r
158         public int findMaxProblemSeverity(String type, boolean includeSubtypes, int depth) throws CoreException {\r
159                 return 0;\r
160         }\r
161 \r
162         @Override\r
163         public String getFileExtension() {\r
164                 String name = file.getName();\r
165                 // FIXME : names that do not contain "." won't work\r
166                 int i = name.lastIndexOf(".");\r
167                 if (i > 0)\r
168                         return name.substring(i);\r
169                 else\r
170                         return null;\r
171                         \r
172         }\r
173 \r
174         @Override\r
175         public long getLocalTimeStamp() {\r
176                 return file.lastModified();\r
177         }\r
178 \r
179         @Override\r
180         public IPath getLocation() {\r
181                 return new Path(file.getAbsolutePath());\r
182         }\r
183 \r
184         @Override\r
185         public URI getLocationURI() {\r
186                 return file.toURI();\r
187         }\r
188 \r
189         @Override\r
190         public IMarker getMarker(long id) {\r
191                 return null;\r
192         }\r
193 \r
194         @Override\r
195         public long getModificationStamp() {\r
196                 return file.lastModified();\r
197         }\r
198 \r
199         @Override\r
200         public IContainer getParent() {\r
201                 return SystemProject.getDefault();\r
202         }\r
203 \r
204         @Override\r
205         public String getPersistentProperty(QualifiedName key)throws CoreException {\r
206                 return null;\r
207         }\r
208 \r
209         @Override\r
210         public IProject getProject() {\r
211                 return SystemProject.getDefault();\r
212         }\r
213 \r
214         @Override\r
215         public IPath getProjectRelativePath() {\r
216                 return null;\r
217         }\r
218 \r
219         @Override\r
220         public IPath getRawLocation() {\r
221                 return new Path(file.getAbsolutePath());\r
222         }\r
223 \r
224         @Override\r
225         public URI getRawLocationURI() {\r
226                 return file.toURI();\r
227         }\r
228 \r
229         @Override\r
230         public ResourceAttributes getResourceAttributes() {\r
231                 return null;\r
232         }\r
233 \r
234         @Override\r
235         public Object getSessionProperty(QualifiedName key) throws CoreException {\r
236                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
237         }\r
238 \r
239         @Override\r
240         public int getType() {\r
241                 return IFile.FILE;\r
242         }\r
243 \r
244         @Override\r
245         public IWorkspace getWorkspace() {\r
246                 return workspace;\r
247         }\r
248 \r
249         @Override\r
250         public boolean isAccessible() {\r
251                 return true;\r
252         }\r
253 \r
254         @Override\r
255         public boolean isDerived() {\r
256                 return false;\r
257         }\r
258 \r
259         @Override\r
260         public boolean isLinked() {\r
261                 return false;\r
262         }\r
263 \r
264         @Override\r
265         public boolean isLinked(int options) {\r
266                 return false;\r
267         }\r
268 \r
269         @Override\r
270         public boolean isLocal(int depth) {\r
271                 return true;\r
272         }\r
273 \r
274         @Override\r
275         public boolean isPhantom() {\r
276                 return false;\r
277         }\r
278 \r
279         @Override\r
280         public boolean isSynchronized(int depth) {\r
281                 return true; // FIXME\r
282         }\r
283 \r
284         @Override\r
285         public boolean isTeamPrivateMember() {\r
286                 return false;\r
287         }\r
288 \r
289         @Override\r
290         public void move(IPath destination, boolean force, IProgressMonitor monitor) throws CoreException {\r
291                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
292         }\r
293 \r
294         @Override\r
295         public void move(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
296                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
297         }\r
298 \r
299         @Override\r
300         public void move(IProjectDescription description, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {\r
301                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
302         }\r
303 \r
304         @Override\r
305         public void move(IProjectDescription description, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
306                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
307         }\r
308 \r
309         @Override\r
310         public void refreshLocal(int depth, IProgressMonitor monitor) throws CoreException {\r
311                 \r
312                 //throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
313         }\r
314 \r
315         @Override\r
316         public void revertModificationStamp(long value) throws CoreException {\r
317                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
318         }\r
319 \r
320         @Override\r
321         public void setDerived(boolean isDerived) throws CoreException {\r
322                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
323         }\r
324 \r
325         @Override\r
326         public void setLocal(boolean flag, int depth, IProgressMonitor monitor) throws CoreException {\r
327                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
328         }\r
329 \r
330         @Override\r
331         public long setLocalTimeStamp(long value) throws CoreException {\r
332                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
333         }\r
334 \r
335         @Override\r
336         public void setPersistentProperty(QualifiedName key, String value) throws CoreException {\r
337                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
338         }\r
339 \r
340         @Override\r
341         public void setReadOnly(boolean readOnly) {\r
342 \r
343         }\r
344 \r
345         @Override\r
346         public void setResourceAttributes(ResourceAttributes attributes) throws CoreException {\r
347                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
348                 \r
349         }\r
350 \r
351         @Override\r
352         public void setSessionProperty(QualifiedName key, Object value) throws CoreException {\r
353                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));  \r
354         }\r
355 \r
356         @Override\r
357         public void setTeamPrivateMember(boolean isTeamPrivate) throws CoreException {\r
358                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
359         }\r
360 \r
361         @Override\r
362         public void touch(IProgressMonitor monitor) throws CoreException {\r
363                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));  \r
364         }\r
365 \r
366         @SuppressWarnings({ "rawtypes" })\r
367         @Override\r
368         public Object getAdapter(Class adapter) {\r
369                 return null;\r
370         }\r
371 \r
372         @Override\r
373         public boolean contains(ISchedulingRule rule) {\r
374                 if (this.equals(rule))\r
375                         return true;\r
376                 return false;\r
377         }\r
378 \r
379         @Override\r
380         public boolean isConflicting(ISchedulingRule rule) {\r
381                 if (this.equals(rule)) // TODO : check cached timestamp\r
382                         return true;\r
383                 return false;\r
384         }\r
385 \r
386         @Override\r
387         public void appendContents(InputStream source, boolean force,boolean keepHistory, IProgressMonitor monitor) throws CoreException {\r
388                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
389         }\r
390 \r
391         @Override\r
392         public void appendContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
393                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));  \r
394         }\r
395 \r
396         @Override\r
397         public void create(InputStream source, boolean force, IProgressMonitor monitor) throws CoreException {\r
398                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
399         }\r
400 \r
401         @Override\r
402         public void create(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
403                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
404                 \r
405         }\r
406 \r
407         @Override\r
408         public void createLink(IPath localLocation, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
409                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
410                 \r
411         }\r
412 \r
413         @Override\r
414         public void createLink(URI location, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
415                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
416         }\r
417 \r
418         @Override\r
419         public void delete(boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {\r
420                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
421                 \r
422         }\r
423 \r
424         @Override\r
425         public String getCharset() throws CoreException {\r
426                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
427         }\r
428 \r
429         @Override\r
430         public String getCharset(boolean checkImplicit) throws CoreException {\r
431                 return "UTF-8";\r
432                 //throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
433         }\r
434 \r
435         @Override\r
436         public String getCharsetFor(Reader reader) throws CoreException {\r
437                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
438         }\r
439 \r
440         @Override\r
441         public IContentDescription getContentDescription() throws CoreException {\r
442                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
443         }\r
444 \r
445         @Override\r
446         public InputStream getContents() throws CoreException {\r
447                 return getContents(false);\r
448         }\r
449 \r
450         @Override\r
451         public InputStream getContents(boolean force) throws CoreException {\r
452                 try {\r
453                         return new FileInputStream(file);\r
454                 } catch (FileNotFoundException e) {\r
455                         throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"File " + getName() + " not found"));\r
456                 }\r
457         }\r
458 \r
459         @Override\r
460         public int getEncoding() throws CoreException {\r
461                 return 0;\r
462         }\r
463 \r
464         @Override\r
465         public IPath getFullPath() {\r
466                 return new Path(file.getAbsolutePath());\r
467         }\r
468 \r
469         @Override\r
470         public IFileState[] getHistory(IProgressMonitor monitor) throws CoreException {\r
471                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
472         }\r
473 \r
474         @Override\r
475         public String getName() {\r
476                 return file.getName();\r
477         }\r
478 \r
479         @Override\r
480         public boolean isReadOnly() {\r
481                 return !file.canWrite();\r
482         }\r
483 \r
484         @Override\r
485         public void move(IPath destination, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {\r
486                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
487         }\r
488 \r
489         @Override\r
490         public void setCharset(String newCharset, IProgressMonitor monitor) throws CoreException {\r
491                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
492         }\r
493 \r
494         @Override\r
495         public void setCharset(String newCharset) throws CoreException {\r
496                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
497         }\r
498 \r
499         @Override\r
500         public void setContents(IFileState source, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {\r
501                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
502                 \r
503         }\r
504 \r
505         @Override\r
506         public void setContents(IFileState source, int updateFlags,\r
507                         IProgressMonitor monitor) throws CoreException {\r
508                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
509         }\r
510 \r
511         @Override\r
512         public void setContents(InputStream source, boolean force,\r
513                         boolean keepHistory, IProgressMonitor monitor)\r
514                         throws CoreException {\r
515                 FileOutputStream fos;\r
516                 try {\r
517                         fos = new FileOutputStream(file);\r
518                         byte buf[] = new byte[1024];\r
519                         int count = 0;\r
520                         while ((count = source.read(buf)) > 0) {\r
521                                 fos.write(buf,0,count);\r
522                         }\r
523                         fos.close();\r
524                 } catch (FileNotFoundException e) {\r
525                         throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"File \"" + file.getAbsolutePath() + "\" not found",e));\r
526                 } catch (IOException e) {\r
527                         throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"File \"" + file.getAbsolutePath() + "\" IO error",e));\r
528                 }\r
529                 \r
530                 \r
531                 \r
532         }\r
533 \r
534         @Override\r
535         public void setContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException {\r
536                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));\r
537         }\r
538 \r
539         @SuppressWarnings({ "rawtypes" })\r
540         @Override\r
541         public Map getPersistentProperties() throws CoreException {\r
542                 return null;\r
543         }\r
544 \r
545         @SuppressWarnings({ "rawtypes" })\r
546         @Override\r
547         public Map getSessionProperties() throws CoreException {\r
548                 return null;\r
549         }\r
550 \r
551         //@Override\r
552         public boolean isDerived(int options) {\r
553                 return false;\r
554         }\r
555 \r
556         //@Override\r
557         public boolean isHidden() {\r
558                 return false;\r
559         }\r
560 \r
561         //@Override\r
562         public void setHidden(boolean isHidden) throws CoreException {\r
563 \r
564         }\r
565 \r
566         //@Override\r
567         public boolean isHidden(int options) {\r
568                 return false;\r
569         }\r
570 \r
571         //@Override\r
572         public boolean isTeamPrivateMember(int options) {\r
573                 return false;\r
574         }\r
575         \r
576         //@Override\r
577         public IPathVariableManager getPathVariableManager() {\r
578                 return null;\r
579         }\r
580         \r
581         //@Override\r
582         public boolean isVirtual() {\r
583                 return false;\r
584         }\r
585         \r
586         //@Override\r
587         public void setDerived(boolean isDerived, IProgressMonitor monitor)\r
588                         throws CoreException {\r
589                 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));          \r
590         }\r
591 \r
592 }\r
593 \r