View Javadoc

1   /*
2    * (c) Copyright QNX Software Systems Ltd. 2002.
3    * All Rights Reserved.
4    */
5   package de.matthias_burbach.mosaique4eclipse.util;
6   
7   import org.eclipse.core.resources.IStorage;
8   import org.eclipse.core.runtime.IPath;
9   import org.eclipse.core.runtime.Path;
10  import org.eclipse.jface.resource.ImageDescriptor;
11  import org.eclipse.ui.IEditorRegistry;
12  import org.eclipse.ui.IPathEditorInput;
13  import org.eclipse.ui.IPersistableElement;
14  import org.eclipse.ui.IStorageEditorInput;
15  import org.eclipse.ui.PlatformUI;
16  
17  /***
18   * Is an EditorInput for an external file.
19   */
20  public class ExternalEditorInput
21      implements IStorageEditorInput, IPathEditorInput {
22  
23      /***
24       * The external file which is input to the editor
25       */
26      private IStorage externalFile;
27  
28      /***
29       * @param externalFile The external file which is input to the editor.
30       *                     Must not be <code>null</code>.
31       */
32      public ExternalEditorInput(final IStorage externalFile) {
33          this.externalFile = externalFile;
34      }
35  
36      /*
37       * (non-Javadoc)
38       * @see org.eclipse.ui.IEditorInput#exists()
39       */
40      /***
41       * {@inheritDoc}
42       */
43      public boolean exists() {
44          // External file can not be deleted
45          return true;
46      }
47  
48      /*
49       * (non-Javadoc)
50       * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
51       */
52      /***
53       * {@inheritDoc}
54       */
55      public Object getAdapter(final Class adapter) {
56          Object result = null;
57          if (adapter.isAssignableFrom(IPathEditorInput.class)) {
58              result = this;
59          }
60          return result;
61      }
62  
63      /***
64       * @return The file extension of the external file.
65       */
66      public String getContentType() {
67          return externalFile.getFullPath().getFileExtension();
68      }
69  
70      /***
71       * @return The OS specific representation of the external file's full path.
72       */
73      public String getFullPath() {
74          return externalFile.getFullPath().toString();
75      }
76  
77      /*
78       * (non-Javadoc)
79       * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
80       */
81      /***
82       * {@inheritDoc}
83       */
84      public ImageDescriptor getImageDescriptor() {
85          IEditorRegistry registry = PlatformUI.getWorkbench()
86                  .getEditorRegistry();
87          ImageDescriptor result = registry.getImageDescriptor(externalFile
88                  .getFullPath().getFileExtension());
89          return result;
90      }
91  
92      /*
93       * (non-Javadoc)
94       * @see org.eclipse.ui.IEditorInput#getName()
95       */
96      /***
97       * {@inheritDoc}
98       */
99      public String getName() {
100         return externalFile.getName();
101     }
102 
103     /*
104      * (non-Javadoc)
105      * @see org.eclipse.ui.IEditorInput#getPersistable()
106      */
107     /***
108      * {@inheritDoc}
109      */
110     public IPersistableElement getPersistable() {
111         return null;
112     }
113 
114     /*
115      * (non-Javadoc)
116      * @see org.eclipse.ui.IStorageEditorInput#getStorage()
117      */
118     /***
119      * {@inheritDoc}
120      */
121     public IStorage getStorage() {
122         return externalFile;
123     }
124 
125     /*
126      * (non-Javadoc)
127      * @see org.eclipse.ui.IEditorInput#getToolTipText()
128      */
129     /***
130      * {@inheritDoc}
131      */
132     public String getToolTipText() {
133         return externalFile.getFullPath().toString();
134     }
135 
136     /*
137      * (non-Javadoc)
138      * @see org.eclipse.ui.IPathEditorInput#getPath()
139      */
140     /***
141      * {@inheritDoc}
142      */
143     public IPath getPath() {
144         IPath result = new Path(getFullPath());
145         return result;
146     }
147 
148     /*
149      * (non-Javadoc)
150      * @see java.lang.Object#equals(java.lang.Object)
151      */
152     /***
153      * {@inheritDoc}
154      * Two ExternalEditorInputs are equal if their IStorage's are equal.
155      */
156     public boolean equals(final Object obj) {
157         if (this == obj) {
158             return true;
159         }
160         if (!(obj instanceof ExternalEditorInput)) {
161             return false;
162         }
163         ExternalEditorInput other = (ExternalEditorInput) obj;
164         return externalFile.equals(other.externalFile);
165     }
166 
167     /*
168      * (non-Javadoc)
169      * @see java.lang.Object#hashCode()
170      */
171     /***
172      * {@inheritDoc}
173      */
174     public int hashCode() {
175          int result = externalFile.hashCode();
176          return result;
177      }
178 }