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