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