1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.graphfile.hack;
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;
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;
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)
58 * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
61 public class GraphFile implements IFile {
62 private Resource fileResource;
63 private IWorkspace workspace;
66 public GraphFile(Resource fileResource, IWorkspace ws) {
67 this.fileResource = fileResource;
71 public Resource getFileResource() {
75 private String getResourceName() {
77 return Simantics.getSession().syncRequest(new Read<String>() {
79 public String perform(ReadGraph graph) throws DatabaseException {
80 Layer0 l0 = Layer0.getInstance(graph);
81 return graph.getPossibleRelatedValue(fileResource, l0.HasName);
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();
91 private byte[] getResourceData() throws DatabaseException {
92 return Simantics.getSession().syncRequest(new Read<byte[]>() {
94 public byte[] perform(ReadGraph graph) throws DatabaseException {
95 return getResourceData(graph);
100 private byte[] getResourceData(ReadGraph graph) throws DatabaseException {
101 GraphFileResource gf = GraphFileResource.getInstance(graph);
102 return graph.getRelatedValue(fileResource, gf.HasFiledata);
105 private long getResourceModificationTime() {
107 return Simantics.getSession().syncRequest(new Read<Long>() {
109 public Long perform(ReadGraph graph) throws DatabaseException {
110 return getResourceMofificationTime(graph);
113 } catch (DatabaseException e) {
114 return IFile.NULL_STAMP;
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);
125 private void setResourceModificationTime(final long time) throws DatabaseException{
127 Simantics.getSession().syncRequest(new WriteRequest() {
130 public void perform(WriteGraph graph) throws DatabaseException {
131 setResourceModificationTime(graph, time);
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);
145 public void accept(IResourceProxyVisitor visitor, int memberFlags) throws CoreException {
146 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
150 public void accept(IResourceVisitor visitor) throws CoreException {
151 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
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"));
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"));
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"));
170 public void clearHistory(IProgressMonitor monitor) throws CoreException {
171 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
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"));
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"));
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"));
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"));
199 public IMarker createMarker(String type) throws CoreException {
200 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
204 public IResourceProxy createProxy() {
209 public void delete(boolean force, IProgressMonitor monitor) throws CoreException {
210 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
214 public void delete(int updateFlags, IProgressMonitor monitor) throws CoreException {
215 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
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"));
225 public boolean exists() {
230 public IMarker findMarker(long id) throws CoreException {
235 public IMarker[] findMarkers(String type, boolean includeSubtypes, int depth) throws CoreException {
240 public int findMaxProblemSeverity(String type, boolean includeSubtypes, int depth) throws CoreException {
247 public String getFileExtension() {
248 String name = getResourceName();
251 int i = name.lastIndexOf(".");
253 return name.substring(i);
260 public long getLocalTimeStamp() {
265 public long setLocalTimeStamp(long value) throws CoreException {
267 setResourceModificationTime(value);
268 } catch (DatabaseException e) {
269 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"GraphFile transaction error",e));
275 public IPath getLocation() {
276 //return new Path(getResourceAbsolutePath());
277 return new GraphPath(this);
281 public URI getLocationURI() {
286 public IMarker getMarker(long id) {
291 public long getModificationStamp() {
292 return getResourceModificationTime();
296 public IContainer getParent() {
297 return SystemProject.getDefault();
301 public String getPersistentProperty(QualifiedName key)throws CoreException {
306 public IProject getProject() {
307 return SystemProject.getDefault();
311 public IPath getProjectRelativePath() {
316 public IPath getRawLocation() {
317 //return new Path(getResourceAbsolutePath());
318 return new GraphPath(this);
322 public URI getRawLocationURI() {
327 public ResourceAttributes getResourceAttributes() {
332 public Object getSessionProperty(QualifiedName key) throws CoreException {
333 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
337 public int getType() {
342 public IWorkspace getWorkspace() {
347 public boolean isAccessible() {
352 public boolean isDerived() {
357 public boolean isLinked() {
362 public boolean isLinked(int options) {
367 public boolean isLocal(int depth) {
372 public boolean isPhantom() {
377 public boolean isSynchronized(int depth) {
378 return true; // FIXME
382 public boolean isTeamPrivateMember() {
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"));
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"));
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"));
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"));
407 public void refreshLocal(int depth, IProgressMonitor monitor) throws CoreException {
409 //throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
413 public void revertModificationStamp(long value) throws CoreException {
414 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
418 public void setDerived(boolean isDerived) throws CoreException {
419 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
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"));
430 public void setPersistentProperty(QualifiedName key, String value) throws CoreException {
431 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
435 public void setReadOnly(boolean readOnly) {
440 public void setResourceAttributes(ResourceAttributes attributes) throws CoreException {
441 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
446 public void setSessionProperty(QualifiedName key, Object value) throws CoreException {
447 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
451 public void setTeamPrivateMember(boolean isTeamPrivate) throws CoreException {
452 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
456 public void touch(IProgressMonitor monitor) throws CoreException {
457 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
460 @SuppressWarnings({ "rawtypes" })
462 public Object getAdapter(Class adapter) {
467 public boolean contains(ISchedulingRule rule) {
468 if (this.equals(rule))
474 public boolean isConflicting(ISchedulingRule rule) {
475 if (this.equals(rule)) // TODO : check cached timestamp
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"));
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"));
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"));
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"));
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"));
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"));
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"));
519 public String getCharset() throws CoreException {
520 return getCharset(true);
524 public String getCharset(boolean checkImplicit) throws CoreException {
530 public int getEncoding() throws CoreException {
532 return ENCODING_UTF_8;
536 public String getCharsetFor(Reader reader) throws CoreException {
537 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
541 public IContentDescription getContentDescription() throws CoreException {
542 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
546 public InputStream getContents() throws CoreException {
547 return getContents(false);
551 public InputStream getContents(boolean force) throws CoreException {
553 return new ByteArrayInputStream(getResourceData());
554 } catch (DatabaseException e) {
555 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"GraphFile transaction error",e));
562 public IPath getFullPath() {
563 //return new Path(getResourceAbsolutePath());
564 return new GraphPath(this);
568 public IFileState[] getHistory(IProgressMonitor monitor) throws CoreException {
569 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
573 public String getName() {
574 return getResourceName();
578 public boolean isReadOnly() {
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"));
588 public void setCharset(String newCharset, IProgressMonitor monitor) throws CoreException {
589 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
593 public void setCharset(String newCharset) throws CoreException {
594 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
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());
604 public void setContents(IFileState source, int updateFlags,
605 IProgressMonitor monitor) throws CoreException {
606 setContents(source.getContents(), updateFlags, monitor);
607 setLocalTimeStamp(source.getModificationTime());
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);
618 public void setContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException {
621 final ByteArrayOutputStream os = new ByteArrayOutputStream();
623 byte buf[] = new byte[1024];
625 while ((count = source.read(buf)) > 0) {
626 os.write(buf,0,count);
629 Simantics.getSession().syncRequest(new WriteRequest() {
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);
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));
646 @SuppressWarnings({ "rawtypes" })
648 public Map getPersistentProperties() throws CoreException {
652 @SuppressWarnings({ "rawtypes" })
654 public Map getSessionProperties() throws CoreException {
659 public boolean isDerived(int options) {
664 public boolean isHidden() {
669 public void setHidden(boolean isHidden) throws CoreException {
674 public boolean isHidden(int options) {
679 public boolean isTeamPrivateMember(int options) {
684 public IPathVariableManager getPathVariableManager() {
689 public boolean isVirtual() {
694 public void setDerived(boolean isDerived, IProgressMonitor monitor)
695 throws CoreException {
696 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
701 public boolean equals(Object obj) {
704 if (obj.getClass() != getClass())
706 GraphFile other = (GraphFile)obj;
707 return fileResource.equals(other.fileResource);
711 public int hashCode() {
712 return fileResource.hashCode();