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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graphfile.hack;
\r
14 import java.io.ByteArrayInputStream;
\r
15 import java.io.ByteArrayOutputStream;
\r
16 import java.io.IOException;
\r
17 import java.io.InputStream;
\r
18 import java.io.Reader;
\r
19 import java.net.URI;
\r
20 import java.util.Map;
\r
22 import org.eclipse.core.resources.IContainer;
\r
23 import org.eclipse.core.resources.IFile;
\r
24 import org.eclipse.core.resources.IFileState;
\r
25 import org.eclipse.core.resources.IMarker;
\r
26 import org.eclipse.core.resources.IPathVariableManager;
\r
27 import org.eclipse.core.resources.IProject;
\r
28 import org.eclipse.core.resources.IProjectDescription;
\r
29 import org.eclipse.core.resources.IResource;
\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.IStatus;
\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.Simantics;
\r
44 import org.simantics.db.ReadGraph;
\r
45 import org.simantics.db.Resource;
\r
46 import org.simantics.db.WriteGraph;
\r
47 import org.simantics.db.common.request.WriteRequest;
\r
48 import org.simantics.db.exception.DatabaseException;
\r
49 import org.simantics.db.request.Read;
\r
50 import org.simantics.graphfile.Activator;
\r
51 import org.simantics.graphfile.ontology.GraphFileResource;
\r
52 import org.simantics.layer0.Layer0;
\r
55 * This is an implementation of IFile that can be used to open external editor for any file.
\r
56 * (Original implementation (org.eclipse.core.filesystem.File) doesn't work without project/file structure)
\r
58 * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>
\r
61 public class GraphFile implements IFile {
\r
62 private Resource fileResource;
\r
63 private IWorkspace workspace;
\r
66 public GraphFile(Resource fileResource, IWorkspace ws) {
\r
67 this.fileResource = fileResource;
\r
68 this.workspace = ws;
\r
71 public Resource getFileResource() {
\r
72 return fileResource;
\r
75 private String getResourceName() {
\r
77 return Simantics.getSession().syncRequest(new Read<String>() {
\r
79 public String perform(ReadGraph graph) throws DatabaseException {
\r
80 Layer0 l0 = Layer0.getInstance(graph);
\r
81 return graph.getPossibleRelatedValue(fileResource, l0.HasName);
\r
84 } catch (DatabaseException e) {
\r
85 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to get resource name: " + fileResource, e));
\r
86 return e.getMessage();
\r
91 private byte[] getResourceData() throws DatabaseException {
\r
92 return Simantics.getSession().syncRequest(new Read<byte[]>() {
\r
94 public byte[] perform(ReadGraph graph) throws DatabaseException {
\r
95 return getResourceData(graph);
\r
100 private byte[] getResourceData(ReadGraph graph) throws DatabaseException {
\r
101 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
102 return graph.getRelatedValue(fileResource, gf.HasFiledata);
\r
105 private long getResourceModificationTime() {
\r
107 return Simantics.getSession().syncRequest(new Read<Long>() {
\r
109 public Long perform(ReadGraph graph) throws DatabaseException {
\r
110 return getResourceMofificationTime(graph);
\r
113 } catch (DatabaseException e) {
\r
114 return IFile.NULL_STAMP;
\r
118 private long getResourceMofificationTime(ReadGraph graph) throws DatabaseException {
\r
119 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
120 long time = graph.getRelatedValue(fileResource, gf.LastModified);
\r
121 //System.out.println("Modification time is " + time + " " + fileResource);
\r
125 private void setResourceModificationTime(final long time) throws DatabaseException{
\r
127 Simantics.getSession().syncRequest(new WriteRequest() {
\r
130 public void perform(WriteGraph graph) throws DatabaseException {
\r
131 setResourceModificationTime(graph, time);
\r
137 private void setResourceModificationTime(WriteGraph graph, long time) throws DatabaseException{
\r
138 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
139 graph.claimLiteral(fileResource, gf.LastModified, time);
\r
140 //System.out.println("Modification time set to " + time + " " + fileResource);
\r
145 public void accept(IResourceProxyVisitor visitor, int memberFlags) throws CoreException {
\r
146 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
150 public void accept(IResourceVisitor visitor) throws CoreException {
\r
151 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
155 public void accept(IResourceVisitor visitor, int depth, boolean includePhantoms) throws CoreException {
\r
156 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
160 public void accept(IResourceVisitor visitor, int depth, int memberFlags) throws CoreException {
\r
161 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
165 public void accept(IResourceProxyVisitor visitor, int depth, int memberFlags) throws CoreException {
\r
166 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
170 public void clearHistory(IProgressMonitor monitor) throws CoreException {
\r
171 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
175 public void copy(IPath destination, boolean force, IProgressMonitor monitor) throws CoreException {
\r
176 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
181 public void copy(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
182 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
187 public void copy(IProjectDescription description, boolean force, IProgressMonitor monitor) throws CoreException {
\r
188 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
193 public void copy(IProjectDescription description, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
194 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
199 public IMarker createMarker(String type) throws CoreException {
\r
200 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
204 public IResourceProxy createProxy() {
\r
209 public void delete(boolean force, IProgressMonitor monitor) throws CoreException {
\r
210 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
214 public void delete(int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
215 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
220 public void deleteMarkers(String type, boolean includeSubtypes, int depth) throws CoreException {
\r
221 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
225 public boolean exists() {
\r
230 public IMarker findMarker(long id) throws CoreException {
\r
235 public IMarker[] findMarkers(String type, boolean includeSubtypes, int depth) throws CoreException {
\r
240 public int findMaxProblemSeverity(String type, boolean includeSubtypes, int depth) throws CoreException {
\r
247 public String getFileExtension() {
\r
248 String name = getResourceName();
\r
251 int i = name.lastIndexOf(".");
\r
253 return name.substring(i);
\r
260 public long getLocalTimeStamp() {
\r
265 public long setLocalTimeStamp(long value) throws CoreException {
\r
267 setResourceModificationTime(value);
\r
268 } catch (DatabaseException e) {
\r
269 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"GraphFile transaction error",e));
\r
275 public IPath getLocation() {
\r
276 //return new Path(getResourceAbsolutePath());
\r
277 return new GraphPath(this);
\r
281 public URI getLocationURI() {
\r
286 public IMarker getMarker(long id) {
\r
291 public long getModificationStamp() {
\r
292 return getResourceModificationTime();
\r
296 public IContainer getParent() {
\r
297 return SystemProject.getDefault();
\r
301 public String getPersistentProperty(QualifiedName key)throws CoreException {
\r
306 public IProject getProject() {
\r
307 return SystemProject.getDefault();
\r
311 public IPath getProjectRelativePath() {
\r
316 public IPath getRawLocation() {
\r
317 //return new Path(getResourceAbsolutePath());
\r
318 return new GraphPath(this);
\r
322 public URI getRawLocationURI() {
\r
327 public ResourceAttributes getResourceAttributes() {
\r
332 public Object getSessionProperty(QualifiedName key) throws CoreException {
\r
333 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
337 public int getType() {
\r
342 public IWorkspace getWorkspace() {
\r
347 public boolean isAccessible() {
\r
352 public boolean isDerived() {
\r
357 public boolean isLinked() {
\r
362 public boolean isLinked(int options) {
\r
367 public boolean isLocal(int depth) {
\r
372 public boolean isPhantom() {
\r
377 public boolean isSynchronized(int depth) {
\r
378 return true; // FIXME
\r
382 public boolean isTeamPrivateMember() {
\r
387 public void move(IPath destination, boolean force, IProgressMonitor monitor) throws CoreException {
\r
388 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
392 public void move(IPath destination, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
393 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
397 public void move(IProjectDescription description, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {
\r
398 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
402 public void move(IProjectDescription description, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
403 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
407 public void refreshLocal(int depth, IProgressMonitor monitor) throws CoreException {
\r
409 //throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
413 public void revertModificationStamp(long value) throws CoreException {
\r
414 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
418 public void setDerived(boolean isDerived) throws CoreException {
\r
419 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
423 public void setLocal(boolean flag, int depth, IProgressMonitor monitor) throws CoreException {
\r
424 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
430 public void setPersistentProperty(QualifiedName key, String value) throws CoreException {
\r
431 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
435 public void setReadOnly(boolean readOnly) {
\r
440 public void setResourceAttributes(ResourceAttributes attributes) throws CoreException {
\r
441 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
446 public void setSessionProperty(QualifiedName key, Object value) throws CoreException {
\r
447 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
451 public void setTeamPrivateMember(boolean isTeamPrivate) throws CoreException {
\r
452 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
456 public void touch(IProgressMonitor monitor) throws CoreException {
\r
457 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
460 @SuppressWarnings({ "rawtypes" })
\r
462 public Object getAdapter(Class adapter) {
\r
467 public boolean contains(ISchedulingRule rule) {
\r
468 if (this.equals(rule))
\r
474 public boolean isConflicting(ISchedulingRule rule) {
\r
475 if (this.equals(rule)) // TODO : check cached timestamp
\r
481 public void appendContents(InputStream source, boolean force,boolean keepHistory, IProgressMonitor monitor) throws CoreException {
\r
482 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
486 public void appendContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
487 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
491 public void create(InputStream source, boolean force, IProgressMonitor monitor) throws CoreException {
\r
492 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
496 public void create(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
497 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
502 public void createLink(IPath localLocation, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
503 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
508 public void createLink(URI location, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
509 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
513 public void delete(boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {
\r
514 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
519 public String getCharset() throws CoreException {
\r
520 return getCharset(true);
\r
524 public String getCharset(boolean checkImplicit) throws CoreException {
\r
530 public int getEncoding() throws CoreException {
\r
532 return ENCODING_UTF_8;
\r
536 public String getCharsetFor(Reader reader) throws CoreException {
\r
537 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
541 public IContentDescription getContentDescription() throws CoreException {
\r
542 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
546 public InputStream getContents() throws CoreException {
\r
547 return getContents(false);
\r
551 public InputStream getContents(boolean force) throws CoreException {
\r
553 return new ByteArrayInputStream(getResourceData());
\r
554 } catch (DatabaseException e) {
\r
555 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"GraphFile transaction error",e));
\r
562 public IPath getFullPath() {
\r
563 //return new Path(getResourceAbsolutePath());
\r
564 return new GraphPath(this);
\r
568 public IFileState[] getHistory(IProgressMonitor monitor) throws CoreException {
\r
569 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
573 public String getName() {
\r
574 return getResourceName();
\r
578 public boolean isReadOnly() {
\r
583 public void move(IPath destination, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {
\r
584 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
588 public void setCharset(String newCharset, IProgressMonitor monitor) throws CoreException {
\r
589 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
593 public void setCharset(String newCharset) throws CoreException {
\r
594 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
598 public void setContents(IFileState source, boolean force, boolean keepHistory, IProgressMonitor monitor) throws CoreException {
\r
599 setContents(source.getContents(), force, keepHistory, monitor);
\r
600 setLocalTimeStamp(source.getModificationTime());
\r
604 public void setContents(IFileState source, int updateFlags,
\r
605 IProgressMonitor monitor) throws CoreException {
\r
606 setContents(source.getContents(), updateFlags, monitor);
\r
607 setLocalTimeStamp(source.getModificationTime());
\r
611 public void setContents(InputStream source, boolean force,
\r
612 boolean keepHistory, IProgressMonitor monitor)
\r
613 throws CoreException {
\r
614 setContents(source, (keepHistory ? KEEP_HISTORY : IResource.NONE) | (force ? FORCE : IResource.NONE), monitor);
\r
618 public void setContents(InputStream source, int updateFlags, IProgressMonitor monitor) throws CoreException {
\r
621 final ByteArrayOutputStream os = new ByteArrayOutputStream();
\r
623 byte buf[] = new byte[1024];
\r
625 while ((count = source.read(buf)) > 0) {
\r
626 os.write(buf,0,count);
\r
629 Simantics.getSession().syncRequest(new WriteRequest() {
\r
632 public void perform(WriteGraph graph) throws DatabaseException {
\r
633 long time = System.currentTimeMillis();
\r
634 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
635 graph.claimLiteral(fileResource, gf.HasFiledata, os.toByteArray());
\r
636 setResourceModificationTime(graph, time);
\r
639 } catch (DatabaseException e ) {
\r
640 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"GraphFile transaction error",e));
\r
641 } catch (IOException e) {
\r
642 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"GraphFile IO error",e));
\r
646 @SuppressWarnings({ "rawtypes" })
\r
648 public Map getPersistentProperties() throws CoreException {
\r
652 @SuppressWarnings({ "rawtypes" })
\r
654 public Map getSessionProperties() throws CoreException {
\r
659 public boolean isDerived(int options) {
\r
664 public boolean isHidden() {
\r
669 public void setHidden(boolean isHidden) throws CoreException {
\r
674 public boolean isHidden(int options) {
\r
679 public boolean isTeamPrivateMember(int options) {
\r
684 public IPathVariableManager getPathVariableManager() {
\r
689 public boolean isVirtual() {
\r
694 public void setDerived(boolean isDerived, IProgressMonitor monitor)
\r
695 throws CoreException {
\r
696 throw new CoreException(new Status(Status.ERROR,Activator.PLUGIN_ID,"not supported"));
\r
701 public boolean equals(Object obj) {
\r
704 if (obj.getClass() != getClass())
\r
706 GraphFile other = (GraphFile)obj;
\r
707 return fileResource.equals(other.fileResource);
\r
711 public int hashCode() {
\r
712 return fileResource.hashCode();
\r