1 19 20 package org.netbeans.modules.j2ee.ejbjar.project.ui; 21 22 import java.awt.Image ; 23 import org.openide.ErrorManager; 24 import org.openide.actions.FileSystemAction; 25 import org.openide.filesystems.FileChangeAdapter; 26 import org.openide.filesystems.Repository; 27 import org.openide.nodes.FilterNode; 28 import org.openide.nodes.Node; 29 import org.openide.util.Lookup; 30 import org.openide.util.NbBundle; 31 import org.openide.util.Utilities; 32 33 import javax.swing.Action ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 36 import javax.swing.event.EventListenerList ; 37 import org.netbeans.api.project.Project; 38 import org.netbeans.api.queries.VisibilityQuery; 39 import org.netbeans.spi.project.ui.support.CommonProjectActions; 40 import org.openide.actions.FindAction; 41 import org.openide.actions.PasteAction; 42 import org.openide.filesystems.FileChangeListener; 43 import org.openide.filesystems.FileEvent; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileRenameEvent; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.loaders.ChangeableDataFilter; 48 import org.openide.loaders.DataFilter; 49 import org.openide.loaders.DataFolder; 50 import org.openide.loaders.DataObject; 51 import org.openide.loaders.DataObjectNotFoundException; 52 import org.openide.nodes.AbstractNode; 53 import org.openide.nodes.Children; 54 import org.openide.util.actions.SystemAction; 55 import org.openide.util.lookup.Lookups; 56 57 62 public class ServerResourceNode extends FilterNode { 63 64 private static final ErrorManager LOGGER = ErrorManager.getDefault().getInstance(ServerResourceNode.class.getName()); 65 private static final boolean LOG = LOGGER.isLoggable(ErrorManager.INFORMATIONAL); 66 67 private static final Image RESOURCE_FILE_BADGE = Utilities.loadImage( "org/netbeans/modules/j2ee/ejbjar/project/ui/resourcesBadge.gif", true ); private static final String SETUP_DIR = "setup"; private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter(); 70 71 private Project project; 72 private FileChangeListener projectDirectoryListener; 73 74 75 public ServerResourceNode(Project project) { 76 this(getSetupDataFolder(project), project); 77 } 78 79 private ServerResourceNode(DataFolder folderDo, Project project) { 80 super(getDataFolderNode(folderDo, project), getDataFolderNodeChildren(folderDo)); 81 projectDirectoryListener = new ProjectDirectoryListener(); 82 if (LOG) { 83 LOGGER.log(ErrorManager.INFORMATIONAL, "Adding file listener to " + project.getProjectDirectory()); } 85 project.getProjectDirectory().addFileChangeListener(FileUtil.weakFileChangeListener(projectDirectoryListener, project.getProjectDirectory())); 86 this.project = project; 87 } 88 89 public Image getIcon(int type) { 90 return badgeIcon(super.getIcon(type)); 91 } 92 93 public Image getOpenedIcon( int type ) { 94 return badgeIcon(super.getOpenedIcon(type)); 95 } 96 97 private static Image badgeIcon(Image icon) { 98 return Utilities.mergeImages(icon, RESOURCE_FILE_BADGE, 7, 7); 99 } 100 101 public String getDisplayName() { 102 return NbBundle.getMessage(ServerResourceNode.class, "LBL_Node_ResourceNode"); 103 } 104 105 public boolean canCopy() { 106 return false; 107 } 108 109 public boolean canCut() { 110 return false; 111 } 112 113 public boolean canRename() { 114 return false; 115 } 116 117 public boolean canDestroy() { 118 return false; 119 } 120 121 public Action[] getActions( boolean context ) { 122 return new Action[] { 123 CommonProjectActions.newFileAction(), 124 null, 125 SystemAction.get(FileSystemAction.class), 126 null, 127 SystemAction.get(FindAction.class), 128 null, 129 SystemAction.get(PasteAction.class), 130 }; 131 } 132 133 private void refresh() { 134 if (LOG) { 135 LOGGER.log(ErrorManager.INFORMATIONAL, "Refreshing"); } 137 final DataFolder folderDo = getSetupDataFolder(project); 138 if (LOG) { 139 LOGGER.log(ErrorManager.INFORMATIONAL, "The DataFolder is: " + folderDo); } 141 Children.MUTEX.postWriteRequest(new Runnable () { 144 public void run() { 145 changeOriginal(getDataFolderNode(folderDo, project), false); 146 org.openide.nodes.Children children = getDataFolderNodeChildren(folderDo); 147 setChildren(children); 148 if (LOG) { 149 LOGGER.log(ErrorManager.INFORMATIONAL, "Children count: " + children.getNodes(true).length); } 151 } 152 }); 153 154 } 155 156 private static DataFolder getSetupDataFolder(Project project) { 157 FileObject folderFo = project.getProjectDirectory().getFileObject(SETUP_DIR); 158 DataFolder folderDo = null; 159 if (folderFo != null && folderFo.isFolder()) { 160 try { 161 folderDo = DataFolder.findFolder(folderFo); 162 } 163 catch (IllegalArgumentException e) {} 164 } 165 return folderDo; 166 } 167 168 private static Node getDataFolderNode(DataFolder folderDo, Project project) { 169 return (folderDo != null) ? folderDo.getNodeDelegate() : new PlaceHolderNode(Lookups.singleton(project)); 171 } 172 173 private static org.openide.nodes.Children getDataFolderNodeChildren(DataFolder folderDo) { 174 return (folderDo != null) ? folderDo.createNodeChildren(VISIBILITY_QUERY_FILTER) : Children.LEAF; 175 } 176 177 final private class ProjectDirectoryListener extends FileChangeAdapter { 178 179 public void fileDeleted(FileEvent fe) { 180 if (isWatchedFile(getFileName(fe))) 181 refresh(); 182 } 183 184 public void fileFolderCreated(FileEvent fe) { 185 if (isWatchedFile(getFileName(fe))) { 186 refresh(); 187 } 188 } 189 190 public void fileRenamed(org.openide.filesystems.FileRenameEvent fe) { 191 if (isWatchedFile(getFileName(fe)) || isWatchedFile(getOldFileName(fe))) 192 refresh(); 193 } 194 195 private boolean isWatchedFile(String fileName) { 196 return fileName.equals(SETUP_DIR); 197 } 198 199 private String getFileName(FileEvent fe) { 200 return fe.getFile().getNameExt(); 201 } 202 203 private String getOldFileName(FileRenameEvent fe) { 204 String result = fe.getName(); 205 if (fe.getExt() != "") result = result + "." + fe.getExt(); 208 return result; 209 } 210 } 211 212 final private static class VisibilityQueryDataFilter implements ChangeListener , ChangeableDataFilter { 213 214 private EventListenerList ell = new EventListenerList (); 215 216 public VisibilityQueryDataFilter() { 217 VisibilityQuery.getDefault().addChangeListener(this); 218 } 219 220 public boolean acceptDataObject(DataObject obj) { 221 FileObject fo = obj.getPrimaryFile(); 222 return VisibilityQuery.getDefault().isVisible(fo); 223 } 224 225 public void stateChanged( ChangeEvent e) { 226 Object [] listeners = ell.getListenerList(); 227 ChangeEvent event = null; 228 for (int i = listeners.length - 2; i >= 0; i-= 2) { 229 if (listeners[i] == ChangeListener .class) { 230 if (event == null) { 231 event = new ChangeEvent ( this ); 232 } 233 ((ChangeListener )listeners[i + 1]).stateChanged(event); 234 } 235 } 236 } 237 238 public void addChangeListener( ChangeListener listener ) { 239 ell.add(ChangeListener .class, listener); 240 } 241 242 public void removeChangeListener( ChangeListener listener ) { 243 ell.remove(ChangeListener .class, listener); 244 } 245 } 246 247 250 private static final class PlaceHolderNode extends AbstractNode { 251 252 public PlaceHolderNode(Lookup lookup) { 253 super(Children.LEAF, lookup); 254 } 255 256 public Image getIcon(int type) { 257 Image image = null; 258 Node imageDelegate = getImageDelegate(); 259 if (imageDelegate != null) { 260 image = imageDelegate.getIcon(type); 261 } 262 if (image == null) { 263 image = super.getIcon(type); 264 } 265 return image; 266 } 267 268 public Image getOpenedIcon(int type) { 269 Image image = null; 270 Node imageDelegate = getImageDelegate(); 271 if (imageDelegate != null) { 272 image = imageDelegate.getOpenedIcon(type); 273 } 274 if (image == null) { 275 image = super.getOpenedIcon(type); 276 } 277 return image; 278 } 279 280 private static Node getImageDelegate() { 281 FileObject imageFo = Repository.getDefault().getDefaultFileSystem().getRoot(); 282 if (imageFo != null) { 283 try { 284 DataObject imageDo = DataObject.find(imageFo); 285 return imageDo.getNodeDelegate(); 286 } catch (DataObjectNotFoundException donfe) { 287 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, donfe); 288 } 289 } 290 return null; 291 } 292 } 293 } 294 295 | Popular Tags |