1 19 20 package org.netbeans.modules.web.project.classpath; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.util.List ; 24 import java.util.Collections ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeSupport ; 27 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 28 import org.netbeans.spi.java.classpath.ClassPathImplementation; 29 import org.netbeans.spi.java.classpath.PathResourceImplementation; 30 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 31 import org.netbeans.spi.project.support.ant.AntProjectHelper; 32 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 33 import org.openide.ErrorManager; 34 import org.openide.filesystems.FileChangeListener; 35 import org.openide.filesystems.FileEvent; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileRenameEvent; 38 import org.openide.filesystems.FileStateInvalidException; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.util.WeakListeners; 41 42 47 final class JspSourcePathImplementation implements ClassPathImplementation, PropertyChangeListener { 48 49 private PropertyChangeSupport support = new PropertyChangeSupport (this); 50 private List resources; 51 private AntProjectHelper helper; 52 private PropertyEvaluator evaluator; 53 private ProjectDirectoryListener projectDirListener; 54 55 58 public JspSourcePathImplementation(AntProjectHelper helper, PropertyEvaluator eval) { 59 assert helper != null; 60 assert eval != null; 61 this.helper = helper; 62 this.evaluator = eval; 63 eval.addPropertyChangeListener(WeakListeners.propertyChange(this, eval)); 64 FileObject projectDir = helper.getProjectDirectory(); 65 projectDirListener = new ProjectDirectoryListener(); 66 projectDir.addFileChangeListener(FileUtil.weakFileChangeListener(projectDirListener, projectDir)); 67 } 68 69 public List getResources() { 70 synchronized (this) { 71 if (this.resources != null) { 72 return resources; 73 } 74 } 75 PathResourceImplementation webDocbaseDirRes = null; 76 String webDocbaseDir = evaluator.getProperty(WebProjectProperties.WEB_DOCBASE_DIR); 77 if (webDocbaseDir != null) { 78 FileObject webDocbaseDirFO = helper.resolveFileObject(webDocbaseDir); 79 if (webDocbaseDirFO != null) { 80 try { 81 webDocbaseDirRes = ClassPathSupport.createResource(webDocbaseDirFO.getURL()); 82 } catch (FileStateInvalidException e) { 83 ErrorManager.getDefault().notify(e); 84 } 85 } 86 } 87 synchronized (this) { 88 if (this.resources == null) { 89 if (webDocbaseDirRes != null) { 90 this.resources = Collections.singletonList(webDocbaseDirRes); 91 } else { 92 this.resources = Collections.EMPTY_LIST; 93 } 94 } 95 } 96 return this.resources; 97 } 98 99 public void addPropertyChangeListener(PropertyChangeListener listener) { 100 support.addPropertyChangeListener (listener); 101 } 102 103 public void removePropertyChangeListener(PropertyChangeListener listener) { 104 support.removePropertyChangeListener (listener); 105 } 106 107 108 public void propertyChange(PropertyChangeEvent evt) { 109 if (WebProjectProperties.WEB_DOCBASE_DIR.equals(evt.getPropertyName())) { 110 fireChange(); 111 } 112 } 113 114 private void fireChange() { 115 synchronized (this) { 116 this.resources = null; 117 } 118 this.support.firePropertyChange (PROP_RESOURCES,null,null); 119 } 120 121 private final class ProjectDirectoryListener implements FileChangeListener { 122 123 public void fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fe) { 124 } 125 126 public void fileChanged(FileEvent fe) { 127 } 128 129 public void fileDataCreated(FileEvent fe) { 130 } 131 132 public void fileDeleted(FileEvent fe) { 133 if (isWatchedFile(getFileName(fe))) { 134 fireChange(); 135 } 136 } 137 138 public void fileFolderCreated(FileEvent fe) { 139 if (isWatchedFile(getFileName(fe))) { 140 fireChange(); 141 } 142 } 143 144 public void fileRenamed(org.openide.filesystems.FileRenameEvent fe) { 145 if (isWatchedFile(getFileName(fe)) || isWatchedFile(getOldFileName(fe))) { 146 fireChange(); 147 } 148 } 149 150 private boolean isWatchedFile(String fileName) { 151 String webDir = evaluator.getProperty(WebProjectProperties.WEB_DOCBASE_DIR); 152 return fileName.equals(webDir); 153 } 154 155 private String getFileName(FileEvent fe) { 156 return fe.getFile().getNameExt(); 157 } 158 159 private String getOldFileName(FileRenameEvent fe) { 160 String result = fe.getName(); 161 if (!(fe.getExt()).equals("")) { result = result + "." + fe.getExt(); } 164 return result; 165 } 166 } 167 } 168 | Popular Tags |