1 19 20 package org.netbeans.modules.j2ee.earproject.ui; 21 22 import java.awt.Image ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import javax.swing.Action ; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.EventListenerList ; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.queries.VisibilityQuery; 32 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties; 33 import org.netbeans.modules.j2ee.spi.ejbjar.support.J2eeProjectView; 34 import org.netbeans.spi.project.support.ant.AntProjectHelper; 35 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 36 import org.openide.actions.FindAction; 37 import org.openide.filesystems.FileAttributeEvent; 38 import org.openide.filesystems.FileChangeListener; 39 import org.openide.filesystems.FileEvent; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileRenameEvent; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.loaders.ChangeableDataFilter; 44 import org.openide.loaders.DataFilter; 45 import org.openide.loaders.DataFolder; 46 import org.openide.loaders.DataObject; 47 import org.openide.nodes.Children; 48 import org.openide.nodes.FilterNode; 49 import org.openide.nodes.Node; 50 import org.openide.util.NbBundle; 51 import org.openide.util.Utilities; 52 import org.openide.util.WeakListeners; 53 import org.openide.util.actions.SystemAction; 54 55 class ArchiveViews { 56 57 private ArchiveViews() { 58 } 59 60 static final class LogicalViewChildren extends Children.Keys implements FileChangeListener { 61 62 64 private static final String KEY_DOC_BASE = "docBase"; private static final String KEY_SETUP_DIR = "setupDir"; 67 private final Project project; 68 private final AntProjectHelper helper; 69 private final PropertyEvaluator evaluator; 70 private final FileObject projectDir; 71 72 public LogicalViewChildren (Project project, AntProjectHelper helper, PropertyEvaluator evaluator) { 73 assert project != null; 74 this.project = project; 75 assert helper != null; 76 this.helper = helper; 77 projectDir = helper.getProjectDirectory(); 78 this.evaluator = evaluator; 79 } 80 81 protected void addNotify() { 82 super.addNotify(); 83 projectDir.addFileChangeListener(FileUtil.weakFileChangeListener(this, projectDir)); 84 createNodes(); 85 } 86 87 private void createNodes() { 88 List <String > keys = new ArrayList <String >(); 89 90 DataFolder docBaseDir = getFolder(EarProjectProperties.META_INF); 91 if (docBaseDir != null) { 92 keys.add(KEY_DOC_BASE); 93 } 94 keys.add(KEY_SETUP_DIR); 95 96 setKeys(keys); 97 } 98 99 protected void removeNotify() { 100 setKeys(Collections.EMPTY_SET); 101 projectDir.removeFileChangeListener(this); 102 super.removeNotify(); 103 } 104 105 protected Node[] createNodes(Object key) { 106 Node n = null; 107 if (key == KEY_DOC_BASE) { 108 n = new DocBaseNode (getFolder(EarProjectProperties.META_INF)); 109 } else if (key == KEY_SETUP_DIR) { 110 n = J2eeProjectView.createServerResourcesNode(project); 111 } 112 return n == null ? new Node[0] : new Node[] {n}; 113 } 114 115 private DataFolder getFolder(String propName) { 116 String prop = evaluator.getProperty (propName); 117 if (prop != null) { 118 FileObject fo = helper.resolveFileObject(prop); 119 if (fo != null && fo.isValid() && fo.isFolder()) { 120 DataFolder df = DataFolder.findFolder(fo); 121 return df; 122 } 123 } 124 return null; 125 } 126 127 public void fileAttributeChanged(FileAttributeEvent fe) { 129 } 130 131 public void fileChanged(FileEvent fe) { 132 } 133 134 public void fileDataCreated(FileEvent fe) { 135 } 136 137 public void fileDeleted(FileEvent fe) { 138 createNodes(); 140 } 141 142 public void fileFolderCreated(FileEvent fe) { 143 createNodes(); 145 } 146 147 public void fileRenamed(FileRenameEvent fe) { 148 createNodes(); 150 } 151 } 152 153 private static final class VisibilityQueryDataFilter implements ChangeListener , ChangeableDataFilter { 154 155 EventListenerList ell = new EventListenerList (); 156 157 public VisibilityQueryDataFilter() { 158 VisibilityQuery.getDefault().addChangeListener( this ); 159 } 160 161 public boolean acceptDataObject(DataObject obj) { 162 FileObject fo = obj.getPrimaryFile(); 163 return VisibilityQuery.getDefault().isVisible( fo ); 164 } 165 166 public void stateChanged( ChangeEvent e) { 167 Object [] listeners = ell.getListenerList(); 168 ChangeEvent event = null; 169 for (int i = listeners.length-2; i>=0; i-=2) { 170 if (listeners[i] == ChangeListener .class) { 171 if ( event == null) { 172 event = new ChangeEvent ( this ); 173 } 174 ((ChangeListener )listeners[i+1]).stateChanged( event ); 175 } 176 } 177 } 178 179 public void addChangeListener( ChangeListener listener ) { 180 ell.add( ChangeListener .class, listener ); 181 } 182 183 public void removeChangeListener( ChangeListener listener ) { 184 ell.remove( ChangeListener .class, listener ); 185 } 186 187 } 188 189 private static final class DocBaseNode extends FilterNode { 190 191 private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter(); 192 private static final Image CONFIGURATION_FILES_BADGE = Utilities.loadImage( "org/netbeans/modules/j2ee/earproject/ui/resources/archive.gif", true ); 194 public DocBaseNode(DataFolder folder) { 195 super(folder.getNodeDelegate(), folder.createNodeChildren(VISIBILITY_QUERY_FILTER)); 196 } 197 198 public Image getIcon( int type ) { 199 return computeIcon( false, type ); 200 } 201 202 public Image getOpenedIcon( int type ) { 203 return computeIcon( true, type ); 204 } 205 206 private Image computeIcon( boolean opened, int type ) { 207 Node folderNode = getOriginal(); 208 Image image = opened ? folderNode.getOpenedIcon( type ) : folderNode.getIcon( type ); 209 return Utilities.mergeImages( image, CONFIGURATION_FILES_BADGE, 7, 7 ); 210 } 211 212 public boolean canCopy() { 213 return false; 214 } 215 216 public boolean canCut() { 217 return false; 218 } 219 220 public boolean canRename() { 221 return false; 222 } 223 224 public boolean canDestroy() { 225 return false; 226 } 227 228 public Action [] getActions( boolean context ) { 229 return new Action [] { 230 SystemAction.get(FindAction.class), 235 }; 240 } 241 242 public String getDisplayName() { 243 return NbBundle.getMessage(ArchiveViews.class, "LBL_Node_DocBase"); } 245 } 246 } 247 | Popular Tags |