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