View Javadoc

1   /*
2    * Created on Mar 3, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor;
13  import java.io.File;
14  
15  import org.eclipse.core.runtime.IPath;
16  import org.eclipse.jface.resource.ImageDescriptor;
17  import org.eclipse.ui.IEditorInput;
18  import org.eclipse.ui.IPersistableElement;
19  /***
20   * Implements editor input that can read file from local filesystem.
21   * @author Martin Vysny
22   */
23  public final class LocalFilesystemEditorInput implements IEditorInput {
24  	/***
25  	 * XML file represented by this object.
26  	 */
27  	private final IPath path;
28  	/***
29  	 * If not <code>null</code> then this class simply delegates method calls
30  	 * to this instance (except the <code>getFile()</code> function).
31  	 */
32  	private final IEditorInput underlyingInput;
33  	/***
34  	 * Constructs new instance of the object.
35  	 * @param path must point to a xml file that can be loaded.
36  	 * @param underlyingInput if not <code>null</code> then this class simply
37  	 * delegates method calls to this instance (except the
38  	 * <code>getFile()</code> function).
39  	 */
40  	public LocalFilesystemEditorInput(IPath path, IEditorInput underlyingInput) {
41  		super();
42  		File f = path.toFile();
43  		if (!f.exists() || !f.canRead() || !f.isFile())
44  			throw new IllegalArgumentException(
45  					"path does not denote existing file"); //$NON-NLS-1$
46  		this.path = path;
47  		this.underlyingInput = underlyingInput;
48  	}
49  	/*
50  	 * (non-Javadoc)
51  	 * @see org.eclipse.ui.IEditorInput#exists()
52  	 */
53  	public boolean exists() {
54  		if (this.underlyingInput == null)
55  			return true;
56  		return this.underlyingInput.exists();
57  	}
58  	/*
59  	 * (non-Javadoc)
60  	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
61  	 */
62  	public ImageDescriptor getImageDescriptor() {
63  		if (underlyingInput == null) {
64  			return XML_FILE_ICON;
65  		}
66  		return this.underlyingInput.getImageDescriptor();
67  	}
68  	/***
69  	 * Icon of the XML file.
70  	 */
71  	static final ImageDescriptor XML_FILE_ICON = ImageDescriptor.createFromFile(
72  			LocalFilesystemEditorInput.class, "/icons/newdocument.gif");//$NON-NLS-1$
73  	
74  	/*
75  	 * (non-Javadoc)
76  	 * @see org.eclipse.ui.IEditorInput#getName()
77  	 */
78  	public String getName() {
79  		if (this.underlyingInput == null) {
80  			return this.path.lastSegment();
81  		}
82  		return this.underlyingInput.getName();
83  	}
84  	/*
85  	 * (non-Javadoc)
86  	 * @see org.eclipse.ui.IEditorInput#getPersistable()
87  	 */
88  	public IPersistableElement getPersistable() {
89  		return null;
90  	}
91  	/*
92  	 * (non-Javadoc)
93  	 * @see org.eclipse.ui.IEditorInput#getToolTipText()
94  	 */
95  	public String getToolTipText() {
96  		if (this.underlyingInput == null) {
97  			return this.path.toPortableString();
98  		}
99  		return this.underlyingInput.getToolTipText();
100 	}
101 	/*
102 	 * (non-Javadoc)
103 	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
104 	 */
105 	public Object getAdapter(Class adapter) {
106 		if (adapter == LocalFilesystemEditorInput.class)
107 			return this;
108 		if (adapter == IPath.class)
109 			return this.path;
110 		if (this.underlyingInput != null)
111 			return this.underlyingInput.getAdapter(adapter);
112 		return null;
113 	}
114 	/***
115 	 * Returns file denoted by this object.
116 	 * @return XML file.
117 	 */
118 	public IPath getFile() {
119 		return this.path;
120 	}
121 }