Fixed problem opening other sources
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / builder / FileStorage.java
1 package net.sourceforge.phpeclipse.builder;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9
10 import net.sourceforge.phpdt.internal.ui.util.StreamUtil;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12
13 import org.eclipse.core.resources.IStorage;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.PlatformObject;
19 import org.eclipse.core.runtime.Status;
20
21 /*
22  * (c) Copyright QNX Software Systems Ltd. 2002.
23  * All Rights Reserved.
24  */
25
26 /**
27  *
28  * @see IStorage
29  */
30 public class FileStorage extends PlatformObject implements IStorage {
31          private boolean forceReadOnly;
32          private final IPath path;
33          private final File file;
34
35          /**
36                 * Two FileStorages are equal if their IPaths are equal.
37                 * @see java.lang.Object#equals(java.lang.Object)
38                 */
39          public boolean equals(Object obj) {
40                         if (this == obj)
41                                  return true;
42                         if (!(obj instanceof FileStorage))
43                                  return false;
44                         FileStorage other = (FileStorage) obj;
45                         return path.equals(other.path);
46          }
47
48          /* (non-Javadoc)
49                 * @see org.eclipse.core.resources.IStorage#getContents()
50                 */
51          public InputStream getContents() throws CoreException {
52                         try {
53                                  return new FileInputStream(file);
54                         } catch (FileNotFoundException e) {
55                                  throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
56                         }
57          }
58
59          /* (non-Javadoc)
60                 * @see org.eclipse.core.resources.IStorage#getFullPath()
61                 */
62          public IPath getFullPath() {
63                         return this.path;
64          }
65
66          /* (non-Javadoc)
67                 * @see org.eclipse.core.resources.IStorage#getName()
68                 */
69          public String getName() {
70                         return this.path.lastSegment();
71          }
72
73          /* (non-Javadoc)
74                 * @see org.eclipse.core.resources.IStorage#isReadOnly()
75                 */
76          public boolean isReadOnly() {
77                         return forceReadOnly || !file.canWrite();
78          }
79
80          /**
81                 * Method FileStorage.
82                 * @param path
83                 */
84          public FileStorage(IPath path) {
85                         this.path = path;
86                         this.file = path.toFile();
87          }
88
89          /* (non-Javadoc)
90                 * @see java.lang.Object#toString()
91                 */
92          public String toString() {
93                         return path.toOSString();
94          }
95
96          /**
97                 * @param stream
98                 * @param overwrite
99                 * @param b
100                 * @param monitor
101                 */
102          public void setContents(InputStream stream, boolean overwrite, boolean b, IProgressMonitor monitor) throws CoreException {
103                         try {
104                                 StreamUtil.transferStreams(stream, new FileOutputStream(file));
105                         } catch (FileNotFoundException e) {
106                                  throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
107                         } catch (IOException e) {
108                                  throw new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.PLUGIN_ID, IStatus.ERROR, e.toString(), e));
109                         }
110          }
111
112          /**
113                 * Some document providers (notably CompilationUnitDocumentProvider)
114                 * can't handle read/write storage.
115                 */
116          public void setReadOnly() {
117                         forceReadOnly = true;
118          }
119 }