1 19 20 package org.netbeans.modules.j2ee.clientproject.ui; 21 22 23 import java.awt.Image ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.List ; 30 import javax.swing.Action ; 31 import javax.swing.Icon ; 32 import javax.swing.ImageIcon ; 33 import org.netbeans.api.project.Project; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.AbstractNode; 36 import org.openide.nodes.Node; 37 import org.openide.util.WeakListeners; 38 import org.netbeans.api.project.SourceGroup; 39 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 41 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener; 42 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 43 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 44 import org.netbeans.spi.java.project.support.ui.PackageView; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.NbBundle; 48 import org.openide.util.Utilities; 49 import org.openide.util.actions.SystemAction; 50 51 58 class J2eePlatformNode extends AbstractNode implements PropertyChangeListener , InstanceListener { 59 60 private static final String ARCHIVE_ICON = "org/netbeans/modules/j2ee/clientproject/ui/resources/jar.gif"; private static final String DEFAULT_ICON = "org/netbeans/modules/j2ee/clientproject/ui/resources/j2eeServer.gif"; private static final String BROKEN_PROJECT_BADGE = "org/netbeans/modules/j2ee/clientproject/ui/resources/brokenProjectBadge.gif"; 64 private static final Icon icon = new ImageIcon (Utilities.loadImage(ARCHIVE_ICON)); 65 66 private static final Image brokenIcon = Utilities.mergeImages( 67 Utilities.loadImage(DEFAULT_ICON), 68 Utilities.loadImage(BROKEN_PROJECT_BADGE), 69 8, 0); 70 71 private final PropertyEvaluator evaluator; 72 private final String platformPropName; 73 private J2eePlatform platformCache; 74 75 private final PropertyChangeListener platformListener = new PropertyChangeListener () { 76 public void propertyChange(PropertyChangeEvent evt) { 77 if (J2eePlatform.PROP_DISPLAY_NAME.equals(evt.getPropertyName())) { 78 fireNameChange((String )evt.getOldValue(), (String )evt.getNewValue()); 79 fireDisplayNameChange((String )evt.getOldValue(), (String )evt.getNewValue()); 80 } 81 if (J2eePlatform.PROP_CLASSPATH.equals(evt.getPropertyName())) { 82 postAddNotify(); 83 } 84 } 85 }; 86 private PropertyChangeListener weakPlatformListener; 87 88 private J2eePlatformNode(Project project, PropertyEvaluator evaluator, String platformPropName) { 89 super(new PlatformContentChildren()); 90 this.evaluator = evaluator; 91 this.platformPropName = platformPropName; 92 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 93 94 J2eeModuleProvider moduleProvider = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 95 moduleProvider.addInstanceListener( 96 (InstanceListener)WeakListeners.create(InstanceListener.class, this, moduleProvider)); 97 } 98 99 public static J2eePlatformNode create(Project project, PropertyEvaluator evaluator, String platformPropName) { 100 return new J2eePlatformNode(project, evaluator, platformPropName); 101 } 102 103 public String getName () { 104 return this.getDisplayName(); 105 } 106 107 public String getDisplayName() { 108 return ""; 109 } 110 111 public String getHtmlDisplayName() { 112 if (getPlatform() != null) 113 return getPlatform().getDisplayName(); 114 else 115 return NbBundle.getMessage(J2eePlatformNode.class, "LBL_J2eeServerMissing"); 116 } 117 118 public Image getIcon(int type) { 119 Image result = null; 120 if (getPlatform() != null) { 121 result = getPlatform().getIcon(); 122 } 123 return result != null ? result : brokenIcon; 124 } 125 126 public Image getOpenedIcon(int type) { 127 return getIcon(type); 128 } 129 130 public boolean canCopy() { 131 return false; 132 } 133 134 public Action [] getActions(boolean context) { 135 return new SystemAction[0]; 136 } 137 138 public void propertyChange(PropertyChangeEvent evt) { 139 141 if (platformPropName.equals(evt.getPropertyName())) { 142 refresh(); 143 } 144 } 145 146 private void refresh() { 147 if (platformCache != null) 148 platformCache.removePropertyChangeListener(weakPlatformListener); 149 150 platformCache = null; 151 152 this.fireNameChange(null, null); 153 this.fireDisplayNameChange(null, null); 154 this.fireIconChange(); 155 156 postAddNotify(); 158 } 159 160 public void instanceAdded(String serverInstanceID) { 161 refresh(); 162 } 163 164 public void instanceRemoved(String serverInstanceID) { 165 refresh(); 166 } 167 168 public void changeDefaultInstance(String oldServerInstanceID, String newServerInstanceID) { 169 } 170 171 private void postAddNotify() { 172 LibrariesNode.rp.post (new Runnable () { 173 public void run () { 174 ((PlatformContentChildren)getChildren()).addNotify (); 175 } 176 }); 177 } 178 179 private J2eePlatform getPlatform () { 180 if (platformCache == null) { 181 String j2eePlatformInstanceId = this.evaluator.getProperty(this.platformPropName); 182 if (j2eePlatformInstanceId != null) { 183 platformCache = Deployment.getDefault().getJ2eePlatform(j2eePlatformInstanceId); 184 } 185 if (platformCache != null) { 186 weakPlatformListener = WeakListeners.propertyChange(platformListener, platformCache); 187 platformCache.addPropertyChangeListener(weakPlatformListener); 188 this.fireIconChange(); 190 } 191 } 192 return platformCache; 193 } 194 195 private static class PlatformContentChildren extends Children.Keys { 196 197 PlatformContentChildren () { 198 } 199 200 protected void addNotify() { 201 this.setKeys (this.getKeys()); 202 } 203 204 protected void removeNotify() { 205 this.setKeys(Collections.EMPTY_SET); 206 } 207 208 protected Node[] createNodes(Object key) { 209 SourceGroup sg = (SourceGroup) key; 210 return new Node[] {ActionFilterNode.create(PackageView.createPackageView(sg), null, null, null, null, null, null)}; 211 } 212 213 private List getKeys () { 214 List <LibrariesSourceGroup> result; 215 216 J2eePlatform j2eePlatform = ((J2eePlatformNode)this.getNode()).getPlatform(); 217 if (j2eePlatform != null) { 218 File [] classpathEntries = j2eePlatform.getClasspathEntries(); 219 result = new ArrayList <LibrariesSourceGroup>(classpathEntries.length); 220 for (int i = 0; i < classpathEntries.length; i++) { 221 FileObject file = FileUtil.toFileObject(classpathEntries[i]); 222 if (file != null) { 223 FileObject archiveFile = FileUtil.getArchiveRoot(file); 224 if (archiveFile != null) { 225 result.add(new LibrariesSourceGroup(archiveFile, file.getNameExt(), icon, icon)); 226 } 227 } 228 } 229 } else { 230 result = Collections.emptyList(); 231 } 232 233 return result; 234 } 235 } 236 } 237 | Popular Tags |