]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile/src/org/simantics/graphfile/hack/GraphFileEditorInput.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / hack / GraphFileEditorInput.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.net.URI;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IStorage;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.PlatformObject;
20 import org.eclipse.core.runtime.content.IContentType;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.ui.IFileEditorInput;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.IPathEditorInput;
25 import org.eclipse.ui.IPersistableElement;
26 import org.eclipse.ui.IURIEditorInput;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.ide.IDE;
29 import org.eclipse.ui.model.IWorkbenchAdapter;
30
31
32 /**
33  * Editor input for files stored in the graph.
34  * 
35  * Note: this class is required, since FileEditorInput is not able to load/save GraphFile references.
36  * Contents of this class are mostly copy-pasted from FileEditorInput.
37  * 
38  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
39  *
40  */
41 public class GraphFileEditorInput  extends PlatformObject implements IFileEditorInput, IPathEditorInput, IURIEditorInput,
42  IPersistableElement {
43 private GraphFile file;
44         
45
46         /**
47          * Creates an editor input based of the given file resource.
48          *
49          * @param file the file resource
50          */
51         public GraphFileEditorInput(GraphFile file) {
52                 if (file == null)
53                         throw new IllegalArgumentException();
54                 this.file = file;
55         
56         }
57
58         /* (non-Javadoc)
59          * Method declared on Object.
60          */
61         public int hashCode() {
62                 return file.hashCode();
63         }
64
65         /* (non-Javadoc)
66          * Method declared on Object.
67          *
68          * The <code>FileEditorInput</code> implementation of this <code>Object</code>
69          * method bases the equality of two <code>FileEditorInput</code> objects on the
70          * equality of their underlying <code>IFile</code> resources.
71          */
72         public boolean equals(Object obj) {
73                 if (this == obj) {
74                         return true;
75                 }
76                 if (!(obj instanceof IFileEditorInput)) {
77                         return false;
78                 }
79                 IFileEditorInput other = (IFileEditorInput) obj;
80                 return file.equals(other.getFile());
81         }
82
83         /* (non-Javadoc)
84          * Method declared on IEditorInput.
85          */
86         public boolean exists() {
87                 return file.exists();
88         }
89
90         /* (non-Javadoc)
91          * Method declared on IPersistableElement.
92          */
93         public String getFactoryId() {
94                 return GraphFileEditorInputFactory.getFactoryId();
95         }
96
97         /* (non-Javadoc)
98          * Method declared on IFileEditorInput.
99          */
100         public IFile getFile() {
101                 return file;
102         }
103
104         /* (non-Javadoc)
105          * Method declared on IEditorInput.
106          */
107         public ImageDescriptor getImageDescriptor() {
108                 IContentType contentType = IDE.getContentType(file);
109                 return PlatformUI.getWorkbench().getEditorRegistry()
110                                 .getImageDescriptor(file.getName(), contentType);
111         }
112
113         /* (non-Javadoc)
114          * Method declared on IEditorInput.
115          */
116         public String getName() {
117                 return file.getName();
118         }
119
120         /* (non-Javadoc)
121          * Method declared on IEditorInput.
122          */
123         public IPersistableElement getPersistable() {
124                 return this;
125         }
126
127         /* (non-Javadoc)
128          * Method declared on IStorageEditorInput.
129          */
130         public IStorage getStorage() {
131                 return file;
132         }
133
134         /* (non-Javadoc)
135          * Method declared on IEditorInput.
136          */
137         public String getToolTipText() {
138                 return file.getFullPath().makeRelative().toString();
139         }
140
141         /* (non-Javadoc)
142          * Method declared on IPersistableElement.
143          */
144         public void saveState(IMemento memento) {
145                 GraphFileEditorInputFactory.saveState(memento, this);
146         }
147
148         
149
150         /* (non-Javadoc)
151          * @see org.eclipse.ui.IURIEditorInput#getURI()
152          */
153         public URI getURI() {
154                 return file.getLocationURI();
155         }
156         
157         
158         /* (non-Javadoc)
159          * @see org.eclipse.ui.IPathEditorInput#getPath()
160          */
161         public IPath getPath() {
162                 IPath location = file.getLocation();
163                 if (location != null)
164                         return location;
165
166                 throw new IllegalArgumentException();
167         }
168
169
170         /* (non-Javadoc)
171          * @see java.lang.Object#toString()
172          */
173         public String toString() {
174                 return getClass().getName() + "(" + getFile().getFullPath() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
175         }
176         
177         /*
178          * Allows for the return of an {@link IWorkbenchAdapter} adapter.
179          * 
180          * @since 3.5
181          * 
182          * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
183          */
184         @SuppressWarnings("rawtypes")
185         public Object getAdapter(Class adapter) {
186                 if (IWorkbenchAdapter.class.equals(adapter)) {
187                         return new IWorkbenchAdapter() {
188
189                                 public Object[] getChildren(Object o) {
190                                         return new Object[0];
191                                 }
192
193                                 public ImageDescriptor getImageDescriptor(Object object) {
194                                         return GraphFileEditorInput.this.getImageDescriptor();
195                                 }
196
197                                 public String getLabel(Object o) {
198                                         return GraphFileEditorInput.this.getName();
199                                 }
200
201                                 public Object getParent(Object o) {
202                                         return GraphFileEditorInput.this.getFile().getParent();
203                                 }
204                         };
205                 }
206
207                 return super.getAdapter(adapter);
208         }
209 }