KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > webproject > PhpProject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.webproject;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Properties JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.ImageIcon JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
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 /**
47  *
48  * @author marcow
49  */

50 public class PhpProject implements Project {
51     private static final Icon JavaDoc PROJECT_ICON = new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/scripting/php/webproject/resources/php16.gif")); // NOI18
52

53     private Lookup lookup;
54     private FileObject projectDir;
55     private Properties JavaDoc properties;
56     
57     /** Creates a new instance of PhpProject */
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 JavaDoc getProperty(String JavaDoc name) {
77         return properties.getProperty(name);
78     }
79     
80     public void setProperty(String JavaDoc name, String JavaDoc value) {
81         properties.setProperty(name, value);
82     }
83     
84     private String JavaDoc getName() {
85         // XXX
86
return projectDir.getName();
87     }
88     
89     private Lookup createLookup(FileObject fo, ProjectState projectState) {
90         return Lookups.fixed(new Object JavaDoc[] {
91             new Info(),
92             new PhpProjectLogicalView(this),
93             // CustomizerProvider??
94
new PhpSources(),
95             new PhpProjectActionProvider(this),
96             // AuxiliaryConfiguration??
97
// RecommendedTemplates
98
// PrivilegedTemplates
99
});
100     }
101     
102     private Properties JavaDoc loadProperties(FileObject fo) {
103         Properties JavaDoc ret = new Properties JavaDoc();
104         
105         try {
106             FileObject propFO = fo.getFileObject("nbproject/index.php"); // NOI18N
107
InputStream JavaDoc is = propFO.getInputStream();
108             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc fnfe) {
141             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe);
142         }
143         catch (IOException JavaDoc 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"); // NOI18N
152
InputStream JavaDoc is = propFO.getInputStream();
153             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc os = propFO.getOutputStream();
182             
183             os.write(bos.toByteArray());
184             properties.store(os, "");
185             os.close();
186         }
187         catch (FileNotFoundException JavaDoc fnfe) {
188             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe);
189         }
190         catch (IOException JavaDoc ioe) {
191             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
192         }
193     }
194     
195     
196     private final class Info implements ProjectInformation {
197         private final PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
198         
199         Info() {}
200         
201         void firePropertyChange(String JavaDoc prop) {
202             pcs.firePropertyChange(prop, null, null);
203         }
204         
205         public String JavaDoc getName() {
206             return PhpProject.this.getName();
207         }
208         
209         public String JavaDoc getDisplayName() {
210             // XXX For now.
211
return getName();
212         }
213         
214         public Icon JavaDoc getIcon() {
215             return PROJECT_ICON;
216         }
217         
218         public Project getProject() {
219             return PhpProject.this;
220         }
221         
222         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
223             pcs.addPropertyChangeListener(listener);
224         }
225         
226         public void removePropertyChangeListener(PropertyChangeListener JavaDoc 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 JavaDoc 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             // I don't really need the property change support. The sources setup does
251
// currently not change.
252
public void addChangeListener(ChangeListener JavaDoc listener){
253         }
254
255         public void removeChangeListener(ChangeListener JavaDoc listener) {
256         }
257     }
258     
259     private final class PhpSourceGroup implements SourceGroup {
260             // I don't really need the property change support. The sources setup does
261
// currently not change.
262
private final PropertyChangeSupport JavaDoc support;
263         
264         PhpSourceGroup() {
265             support = new PropertyChangeSupport JavaDoc(this);
266         }
267         
268         public boolean contains(FileObject file) throws IllegalArgumentException JavaDoc {
269             if (file == null) {
270                 new IllegalArgumentException JavaDoc(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 JavaDoc(NbBundle.getMessage(PhpProject.class, "ERR_FileObjectNotContained"));
285             }
286             
287             return false;
288         }
289         
290         public String JavaDoc getDisplayName() {
291             return PhpProject.this.getName();
292         }
293         
294         public Icon JavaDoc getIcon(boolean opened) {
295             // XXX
296
return PROJECT_ICON;
297         }
298         
299         public String JavaDoc getName() {
300             return PhpProject.this.getName();
301         }
302         
303         public FileObject getRootFolder() {
304             return PhpProject.this.getProjectDirectory();
305         }
306         
307         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
308             support.addPropertyChangeListener(listener);
309         }
310
311         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
312             support.removePropertyChangeListener(listener);
313         }
314     }
315 }
316
Popular Tags