1 19 20 package org.netbeans.modules.scripting.php.webproject; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.Properties ; 30 import javax.swing.Icon ; 31 import javax.swing.ImageIcon ; 32 import javax.swing.event.ChangeListener ; 33 import org.netbeans.modules.scripting.php.webproject.ui.PhpProjectLogicalView; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.api.project.ProjectInformation; 36 import org.netbeans.api.project.SourceGroup; 37 import org.netbeans.api.project.Sources; 38 import org.netbeans.spi.project.ProjectState; 39 import org.openide.ErrorManager; 40 import org.openide.filesystems.FileObject; 41 import org.openide.util.Lookup; 42 import org.openide.util.NbBundle; 43 import org.openide.util.Utilities; 44 import org.openide.util.lookup.Lookups; 45 46 50 public class PhpProject implements Project { 51 private static final Icon PROJECT_ICON = new ImageIcon (Utilities.loadImage("org/netbeans/modules/scripting/php/webproject/resources/php16.gif")); 53 private Lookup lookup; 54 private FileObject projectDir; 55 private Properties properties; 56 57 58 public PhpProject(FileObject fo, ProjectState projectState) { 59 projectDir = fo; 60 properties = loadProperties(fo); 61 lookup = createLookup(fo, projectState); 62 } 63 64 public Lookup getLookup() { 65 return lookup; 66 } 67 68 public FileObject getProjectDirectory() { 69 return projectDir; 70 } 71 72 public void save() { 73 saveProperties(); 74 } 75 76 public String getProperty(String name) { 77 return properties.getProperty(name); 78 } 79 80 public void setProperty(String name, String value) { 81 properties.setProperty(name, value); 82 } 83 84 private String getName() { 85 return projectDir.getName(); 87 } 88 89 private Lookup createLookup(FileObject fo, ProjectState projectState) { 90 return Lookups.fixed(new Object [] { 91 new Info(), 92 new PhpProjectLogicalView(this), 93 new PhpSources(), 95 new PhpProjectActionProvider(this), 96 }); 100 } 101 102 private Properties loadProperties(FileObject fo) { 103 Properties ret = new Properties (); 104 105 try { 106 FileObject propFO = fo.getFileObject("nbproject/index.php"); InputStream is = propFO.getInputStream(); 108 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 109 boolean prefixEnd = false; 110 int c = is.read(); 111 112 while (c != -1 && !prefixEnd) { 113 while (c != -1 && c != '?') { 114 bos.write(c); 115 c = is.read(); 116 } 117 118 if (c == '?') { 119 bos.write(c); 120 } 121 122 if ((c = is.read()) == '>') { 123 bos.write(c); 124 prefixEnd = true; 125 } 126 } 127 128 if (c != -1) { 129 while ((c = is.read()) != -1 && (c == '\n' || c == '\r')) { 130 bos.write(c); 131 } 132 } 133 134 System.err.write(bos.toByteArray()); 135 136 ret.load(is); 137 ret.list(System.err); 138 is.close(); 139 } 140 catch (FileNotFoundException fnfe) { 141 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe); 142 } 143 catch (IOException ioe) { 144 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 145 } 146 return ret; 147 } 148 149 private void saveProperties() { 150 try { 151 FileObject propFO = projectDir.getFileObject("nbproject/index.php"); InputStream is = propFO.getInputStream(); 153 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 154 boolean prefixEnd = false; 155 int c = is.read(); 156 157 while (c != -1 && !prefixEnd) { 158 while (c != -1 && c != '?') { 159 bos.write(c); 160 c = is.read(); 161 } 162 163 if (c == '?') { 164 bos.write(c); 165 } 166 167 if ((c = is.read()) == '>') { 168 bos.write(c); 169 prefixEnd = true; 170 } 171 } 172 173 if (c != -1) { 174 while ((c = is.read()) != -1 && (c == '\n' || c == '\r')) { 175 bos.write(c); 176 } 177 } 178 179 is.close(); 180 181 OutputStream os = propFO.getOutputStream(); 182 183 os.write(bos.toByteArray()); 184 properties.store(os, ""); 185 os.close(); 186 } 187 catch (FileNotFoundException fnfe) { 188 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe); 189 } 190 catch (IOException ioe) { 191 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 192 } 193 } 194 195 196 private final class Info implements ProjectInformation { 197 private final PropertyChangeSupport pcs = new PropertyChangeSupport (this); 198 199 Info() {} 200 201 void firePropertyChange(String prop) { 202 pcs.firePropertyChange(prop, null, null); 203 } 204 205 public String getName() { 206 return PhpProject.this.getName(); 207 } 208 209 public String getDisplayName() { 210 return getName(); 212 } 213 214 public Icon getIcon() { 215 return PROJECT_ICON; 216 } 217 218 public Project getProject() { 219 return PhpProject.this; 220 } 221 222 public void addPropertyChangeListener(PropertyChangeListener listener) { 223 pcs.addPropertyChangeListener(listener); 224 } 225 226 public void removePropertyChangeListener(PropertyChangeListener listener 227 ) { 228 pcs.removePropertyChangeListener(listener); 229 } 230 231 } 232 233 private final class PhpSources implements Sources { 234 private PhpSourceGroup sGroup; 235 236 public PhpSources() {} 237 238 public SourceGroup[] getSourceGroups(String type) { 239 if (Sources.TYPE_GENERIC.equals(type)) { 240 if (sGroup == null) { 241 sGroup = new PhpSourceGroup(); 242 } 243 244 return new SourceGroup[] { sGroup }; 245 } 246 247 return new SourceGroup[0]; 248 } 249 250 public void addChangeListener(ChangeListener listener){ 253 } 254 255 public void removeChangeListener(ChangeListener listener) { 256 } 257 } 258 259 private final class PhpSourceGroup implements SourceGroup { 260 private final PropertyChangeSupport support; 263 264 PhpSourceGroup() { 265 support = new PropertyChangeSupport (this); 266 } 267 268 public boolean contains(FileObject file) throws IllegalArgumentException { 269 if (file == null) { 270 new IllegalArgumentException (NbBundle.getMessage(PhpProject.class, "ERR_FileObjectNull")); 271 } 272 273 FileObject pRoot = PhpProject.this.getProjectDirectory(); 274 275 do { 276 if (file.equals(pRoot)) { 277 return true; 278 } 279 280 file = file.getParent(); 281 } while (file != null); 282 283 if (file == null) { 284 new IllegalArgumentException (NbBundle.getMessage(PhpProject.class, "ERR_FileObjectNotContained")); 285 } 286 287 return false; 288 } 289 290 public String getDisplayName() { 291 return PhpProject.this.getName(); 292 } 293 294 public Icon getIcon(boolean opened) { 295 return PROJECT_ICON; 297 } 298 299 public String getName() { 300 return PhpProject.this.getName(); 301 } 302 303 public FileObject getRootFolder() { 304 return PhpProject.this.getProjectDirectory(); 305 } 306 307 public void addPropertyChangeListener(PropertyChangeListener listener) { 308 support.addPropertyChangeListener(listener); 309 } 310 311 public void removePropertyChangeListener(PropertyChangeListener listener) { 312 support.removePropertyChangeListener(listener); 313 } 314 } 315 } 316 | Popular Tags |