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.net.URI;
\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
33 * Editor input for files stored in the graph.
\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
38 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
41 public class GraphFileEditorInput extends PlatformObject implements IFileEditorInput, IPathEditorInput, IURIEditorInput,
\r
42 IPersistableElement {
\r
43 private GraphFile file;
\r
47 * Creates an editor input based of the given file resource.
\r
49 * @param file the file resource
\r
51 public GraphFileEditorInput(GraphFile file) {
\r
53 throw new IllegalArgumentException();
\r
59 * Method declared on Object.
\r
61 public int hashCode() {
\r
62 return file.hashCode();
\r
66 * Method declared on Object.
\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
72 public boolean equals(Object obj) {
\r
76 if (!(obj instanceof IFileEditorInput)) {
\r
79 IFileEditorInput other = (IFileEditorInput) obj;
\r
80 return file.equals(other.getFile());
\r
84 * Method declared on IEditorInput.
\r
86 public boolean exists() {
\r
87 return file.exists();
\r
91 * Method declared on IPersistableElement.
\r
93 public String getFactoryId() {
\r
94 return GraphFileEditorInputFactory.getFactoryId();
\r
98 * Method declared on IFileEditorInput.
\r
100 public IFile getFile() {
\r
105 * Method declared on IEditorInput.
\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
114 * Method declared on IEditorInput.
\r
116 public String getName() {
\r
117 return file.getName();
\r
121 * Method declared on IEditorInput.
\r
123 public IPersistableElement getPersistable() {
\r
128 * Method declared on IStorageEditorInput.
\r
130 public IStorage getStorage() {
\r
135 * Method declared on IEditorInput.
\r
137 public String getToolTipText() {
\r
138 return file.getFullPath().makeRelative().toString();
\r
142 * Method declared on IPersistableElement.
\r
144 public void saveState(IMemento memento) {
\r
145 GraphFileEditorInputFactory.saveState(memento, this);
\r
151 * @see org.eclipse.ui.IURIEditorInput#getURI()
\r
153 public URI getURI() {
\r
154 return file.getLocationURI();
\r
159 * @see org.eclipse.ui.IPathEditorInput#getPath()
\r
161 public IPath getPath() {
\r
162 IPath location = file.getLocation();
\r
163 if (location != null)
\r
166 throw new IllegalArgumentException();
\r
171 * @see java.lang.Object#toString()
\r
173 public String toString() {
\r
174 return getClass().getName() + "(" + getFile().getFullPath() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
\r
178 * Allows for the return of an {@link IWorkbenchAdapter} adapter.
\r
182 * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
\r
184 @SuppressWarnings("rawtypes")
\r
185 public Object getAdapter(Class adapter) {
\r
186 if (IWorkbenchAdapter.class.equals(adapter)) {
\r
187 return new IWorkbenchAdapter() {
\r
189 public Object[] getChildren(Object o) {
\r
190 return new Object[0];
\r
193 public ImageDescriptor getImageDescriptor(Object object) {
\r
194 return GraphFileEditorInput.this.getImageDescriptor();
\r
197 public String getLabel(Object o) {
\r
198 return GraphFileEditorInput.this.getName();
\r
201 public Object getParent(Object o) {
\r
202 return GraphFileEditorInput.this.getFile().getParent();
\r
207 return super.getAdapter(adapter);
\r