1 19 20 package org.netbeans.modules.j2ee.earproject; 21 22 import java.io.IOException ; 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.api.project.ant.AntArtifact; 32 import org.netbeans.modules.j2ee.earproject.ui.customizer.VisualClassPathItem; 33 import org.netbeans.spi.project.support.ant.AntProjectHelper; 34 import org.netbeans.spi.project.support.ant.EditableProperties; 35 import org.openide.ErrorManager; 36 import org.openide.filesystems.FileChangeAdapter; 37 import org.openide.filesystems.FileChangeListener; 38 import org.openide.filesystems.FileEvent; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileRenameEvent; 41 42 48 public final class BrokenProjectSupport { 49 50 private final EarProject project; 51 private final Set <ChangeListener > listeners; 52 private final FileChangeListener artifactListener; 53 private final Collection <FileObject> watchedArtifacts = new HashSet <FileObject>(); 54 55 BrokenProjectSupport(final EarProject project) { 56 this.project = project; 57 this.listeners = new HashSet <ChangeListener >(1); 58 this.artifactListener = new ArtifactListener(); 59 } 60 61 public boolean hasBrokenArtifacts() { 62 boolean brokenArtifacts = false; 63 List <VisualClassPathItem> vcpis = project.getProjectProperties().getJarContentAdditional(); 64 for (VisualClassPathItem vcpi : vcpis) { 65 Object obj = vcpi.getObject(); 66 if (!(obj instanceof AntArtifact)) { 67 continue; 68 } 69 AntArtifact aa = (AntArtifact) obj; 70 FileObject script = aa.getScriptFile(); 71 if (script == null || !script.isValid()) { 72 brokenArtifacts = true; 73 break; 74 } 75 } 76 return brokenArtifacts; 77 } 78 79 public void watchAntArtifact(final AntArtifact artifact) { 80 FileObject artFile = artifact.getScriptFile(); 81 watchedArtifacts.add(artFile); 82 artFile.addFileChangeListener(artifactListener); 83 } 84 85 public void cleanUp() { 86 for (FileObject artFile : watchedArtifacts) { 87 artFile.removeFileChangeListener(artifactListener); 88 } 89 watchedArtifacts.clear(); 90 } 91 92 public void addChangeListener(ChangeListener l) { 93 synchronized (listeners) { 94 listeners.add(l); 95 } 96 } 97 98 public void removeChangeListener(ChangeListener l) { 99 synchronized (listeners) { 100 listeners.remove(l); 101 } 102 } 103 104 private void fireChangeEvent() { 105 Iterator <ChangeListener > it; 106 synchronized (listeners) { 107 it = new HashSet <ChangeListener >(listeners).iterator(); 108 } 109 ChangeEvent ev = new ChangeEvent (this); 110 while (it.hasNext()) { 111 it.next().stateChanged(ev); 112 } 113 } 114 115 119 public void adjustReferences() { 120 ProjectManager.mutex().writeAccess(new Runnable () { 121 public void run() { 122 for (VisualClassPathItem vcpi : project.getProjectProperties().getJarContentAdditional()) { 123 if (vcpi.getObject() instanceof AntArtifact) { 124 String raw = vcpi.getRaw(); 125 if (raw.matches("^\\$\\{reference\\..*\\}")) { String currEvaluated = project.evaluator().evaluate(raw); 127 FileObject currEvaluatedFO = project.getAntProjectHelper().resolveFileObject(currEvaluated); 128 if (currEvaluatedFO == null) { 129 String referenceKey = raw.substring(2, raw.length() - 1); adjustReference(vcpi, referenceKey); 131 } 132 } 133 } 134 } 135 } 136 }); 137 } 138 139 private void adjustReference(final VisualClassPathItem vcpi, final String referenceKey) { 140 EditableProperties prjProps = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 141 String referenceValue = prjProps.getProperty(referenceKey); 142 if (referenceValue.matches("^\\$\\{project\\..*\\}.+")) { String prjReference = referenceValue.substring(0, referenceValue.indexOf('}', 2) + 1); 144 String relPath = vcpi.getEvaluated(); 145 prjProps.setProperty(referenceKey, prjReference + '/' + relPath); 146 } 147 project.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, prjProps); 148 try { 149 ProjectManager.getDefault().saveProject(project); 150 } catch (IOException ioe) { 151 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 152 } 153 } 154 155 private class ArtifactListener extends FileChangeAdapter { 156 157 public void fileDeleted(FileEvent fe) { 158 fireChangeEvent(); 159 } 160 161 public void fileRenamed(FileRenameEvent fe) { 162 fireChangeEvent(); 163 } 164 165 } 166 167 } 168 | Popular Tags |