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