1 19 20 package org.netbeans.modules.web.project.ui; 21 22 import java.awt.Image ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import javax.swing.event.EventListenerList ; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.queries.VisibilityQuery; 34 import org.netbeans.modules.web.project.UpdateHelper; 35 import org.netbeans.modules.web.project.WebProject; 36 import org.netbeans.modules.web.project.ui.SourceNodeFactory.PreselectPropertiesAction; 37 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 38 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 39 import org.netbeans.spi.project.ui.support.NodeFactory; 40 import org.netbeans.spi.project.ui.support.NodeList; 41 import org.openide.filesystems.FileObject; 42 import org.openide.loaders.ChangeableDataFilter; 43 import org.openide.loaders.DataFilter; 44 import org.openide.loaders.DataFolder; 45 import org.openide.loaders.DataObject; 46 import org.openide.nodes.FilterNode; 47 import org.openide.nodes.Node; 48 import org.openide.util.NbBundle; 49 import org.openide.util.Utilities; 50 51 55 public final class DocBaseNodeFactory implements NodeFactory { 56 57 58 public DocBaseNodeFactory() { 59 } 60 61 public NodeList createNodes(Project p) { 62 WebProject project = (WebProject)p.getLookup().lookup(WebProject.class); 63 assert project != null; 64 return new DocBaseNodeList(project); 65 } 66 67 private static class DocBaseNodeList implements NodeList<String >, PropertyChangeListener { 68 private static final String DOC_BASE = "docBase"; 70 private final WebProject project; 71 private final ArrayList <ChangeListener > listeners = new ArrayList <ChangeListener >(); 72 73 private final PropertyEvaluator evaluator; 74 private final UpdateHelper helper; 75 76 DocBaseNodeList(WebProject proj) { 77 project = proj; 78 WebLogicalViewProvider logView = (WebLogicalViewProvider) project.getLookup().lookup(WebLogicalViewProvider.class); 79 assert logView != null; 80 evaluator = logView.getEvaluator(); 81 helper = logView.getUpdateHelper(); 82 } 83 84 public List <String > keys() { 85 List <String > result = new ArrayList <String >(); 86 result.add(DOC_BASE); 87 return result; 88 } 89 90 public synchronized void addChangeListener(ChangeListener l) { 91 listeners.add(l); 92 } 93 94 public synchronized void removeChangeListener(ChangeListener l) { 95 listeners.remove(l); 96 } 97 98 private void fireChange() { 99 ArrayList <ChangeListener > list = new ArrayList <ChangeListener >(); 100 synchronized (this) { 101 list.addAll(listeners); 102 } 103 Iterator <ChangeListener > it = list.iterator(); 104 while (it.hasNext()) { 105 ChangeListener elem = it.next(); 106 elem.stateChanged(new ChangeEvent ( this )); 107 } 108 } 109 110 public Node node(String key) { 111 if (key == DOC_BASE) { 112 return new DocBaseNode(getFolder(WebProjectProperties.WEB_DOCBASE_DIR), project); 113 } 114 assert false: "No node for key: " + key; 115 return null; 116 } 117 118 public void addNotify() { 119 } 120 121 public void removeNotify() { 122 } 123 124 public void propertyChange(PropertyChangeEvent evt) { 125 SwingUtilities.invokeLater(new Runnable () { 127 public void run() { 128 fireChange(); 129 } 130 }); 131 } 132 133 private DataFolder getFolder(String propName) { 134 FileObject fo = getFileObject(propName); 135 if (fo != null) { 136 DataFolder df = DataFolder.findFolder(fo); 137 return df; 138 } 139 return null; 140 } 141 142 private FileObject getFileObject(String propName) { 143 String foName = evaluator.getProperty (propName); 144 if (foName == null) { 145 return null; 146 } 147 FileObject fo = helper.getAntProjectHelper().resolveFileObject(foName); 148 return fo != null && fo.isValid() ? fo : null; 152 } 153 154 } 155 156 private static final class DocBaseNode extends FilterNode { 157 private javax.swing.Action actions[]; 158 private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter(); 159 160 private static Image WEB_PAGES_BADGE = Utilities.loadImage( "org/netbeans/modules/web/project/ui/resources/webPagesBadge.gif" ); 162 private final Project project; 163 164 DocBaseNode (DataFolder folder, Project project) { 165 super (folder.getNodeDelegate(), folder.createNodeChildren(VISIBILITY_QUERY_FILTER)); 166 this.project = project; 167 } 168 169 public Image getIcon(int type) { 170 return computeIcon(false, type); 171 } 172 173 public Image getOpenedIcon(int type) { 174 return computeIcon(true, type); 175 } 176 177 private Node getDataFolderNodeDelegate() { 178 return ((DataFolder) getLookup().lookup(DataFolder.class)).getNodeDelegate(); 179 } 180 181 private Image computeIcon(boolean opened, int type) { 182 Image image; 183 184 image = opened ? getDataFolderNodeDelegate().getOpenedIcon(type) : getDataFolderNodeDelegate().getIcon(type); 185 image = Utilities.mergeImages(image, WEB_PAGES_BADGE, 7, 7); 186 187 return image; 188 } 189 190 public String getDisplayName () { 191 return NbBundle.getMessage(DocBaseNodeFactory.class, "LBL_Node_DocBase"); } 193 194 public javax.swing.Action [] getActions( boolean context ) { 195 if ( actions == null ) { 196 actions = new javax.swing.Action [9]; 197 actions[0] = org.netbeans.spi.project.ui.support.CommonProjectActions.newFileAction(); 198 actions[1] = null; 199 actions[2] = org.openide.util.actions.SystemAction.get( org.openide.actions.FileSystemAction.class ); 200 actions[3] = null; 201 actions[4] = org.openide.util.actions.SystemAction.get( org.openide.actions.FindAction.class ); 202 actions[5] = null; 203 actions[6] = org.openide.util.actions.SystemAction.get( org.openide.actions.PasteAction.class ); 204 actions[7] = null; 205 actions[8] = new PreselectPropertiesAction(project, "Sources"); } 207 return actions; 208 } 209 210 214 public boolean canRename() { 215 return false; 216 } 217 218 } 219 220 static final class VisibilityQueryDataFilter implements ChangeListener , ChangeableDataFilter { 221 222 EventListenerList ell = new EventListenerList (); 223 224 public VisibilityQueryDataFilter() { 225 VisibilityQuery.getDefault().addChangeListener( this ); 226 } 227 228 public boolean acceptDataObject(DataObject obj) { 229 FileObject fo = obj.getPrimaryFile(); 230 return VisibilityQuery.getDefault().isVisible( fo ); 231 } 232 233 public void stateChanged( ChangeEvent e) { 234 Object [] listeners = ell.getListenerList(); 235 ChangeEvent event = null; 236 for (int i = listeners.length-2; i>=0; i-=2) { 237 if (listeners[i] == ChangeListener .class) { 238 if ( event == null) { 239 event = new ChangeEvent ( this ); 240 } 241 ((ChangeListener )listeners[i+1]).stateChanged( event ); 242 } 243 } 244 } 245 246 public void addChangeListener( ChangeListener listener ) { 247 ell.add( ChangeListener .class, listener ); 248 } 249 250 public void removeChangeListener( ChangeListener listener ) { 251 ell.remove( ChangeListener .class, listener ); 252 } 253 254 } 255 256 } 257 | Popular Tags |