1 19 20 package org.netbeans.modules.enode; 21 22 import java.util.ArrayList ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Iterator ; 26 import java.beans.PropertyChangeEvent ; 27 import java.beans.PropertyChangeListener ; 28 import java.beans.PropertyChangeSupport ; 29 import java.util.Set ; 30 31 import javax.swing.ImageIcon ; 32 33 import org.openide.ErrorManager; 34 import org.openide.util.WeakListeners; 35 import org.openide.util.RequestProcessor; 36 37 import org.netbeans.api.enode.*; 38 import org.netbeans.spi.enode.IconSet; 39 import org.netbeans.api.registry.*; 40 41 45 public class ExtensibleIconsImpl extends ExtensibleIcons { 46 47 private static ErrorManager log = ErrorManager.getDefault().getInstance(ExtensibleIconsImpl.class.getName()); 48 private static boolean LOGGABLE = log.isLoggable(ErrorManager.INFORMATIONAL); 49 50 private PropertyChangeSupport pcs = new PropertyChangeSupport (this); 51 52 55 private String [] paths; 56 57 60 private IconSet iconSet; 61 62 66 private Listener listener; 67 68 71 private boolean listenersAttached = false; 72 73 79 private Set listenersAttachedTo = new HashSet (); 80 81 84 public ExtensibleIconsImpl(String [] paths) { 85 this.paths = paths; 86 } 87 88 93 public int getDefaultSize( ) { 94 return getIconSet().getDefaultSize(); 95 } 96 97 98 107 public ImageIcon getIcon( String name, int size ) { 108 return getIconSet().getIcon(name, size); 109 } 110 111 112 118 public ImageIcon getDefaultIcon( int size ) { 119 return getIconSet().getDefaultIcon(size); 120 } 121 122 123 131 public ImageIcon getDefaultIcon( ) { 132 return getIconSet().getDefaultIcon(); 133 } 134 135 140 public String getDescription( ) { 141 return getIconSet().getDescription(); 142 } 143 144 154 public String getIconDisplayName( String name ) { 155 return getIconSet().getIconDisplayName(name); 156 } 157 158 159 167 public String [] getAllIconNames( int size ) { 168 return getIconSet().getAllIconNames(size); 169 } 170 171 public void addPropertyChangeListener(PropertyChangeListener pcl) { 172 pcs.addPropertyChangeListener(pcl); 173 } 174 175 public void removePropertyChangeListener(PropertyChangeListener pcl) { 176 pcs.removePropertyChangeListener(pcl); 177 } 178 179 182 IconSet getIconSet() { 183 if (LOGGABLE) log.log("getIconSet() called on " + this); 184 if (iconSet != null) { 185 if (LOGGABLE) log.log("getIconSet() returning cached value"); 186 return iconSet; 187 } 188 ArrayList arr = new ArrayList (); 189 for (int i = 0; i < paths.length; i++) { 190 String path = ExtensibleNode.E_NODE_ICONS + paths[i]; 191 try { 192 boolean exists = true; 193 Context con = Context.getDefault().getSubcontext(path); 194 if (con == null) { 195 con = ExtensibleLookupImpl.findExistingContext(path); 196 exists = false; 197 } 198 if (!listenersAttached) { 199 ContextListener l1 = getContextListener(con); 200 con.addContextListener(l1); 201 listenersAttachedTo.add(con); 202 } 203 if (! exists) { 204 if (LOGGABLE) log.log("getIconSet() path " + path + " does not exist."); 205 continue; 206 } 207 List objects = con.getOrderedObjects(); 208 Iterator it = objects.iterator(); 209 if (LOGGABLE) log.log("getIconSet() examining object on path " + path); 210 while (it.hasNext()) { 211 Object obj = it.next(); 212 if (LOGGABLE) log.log("getIconSet() trying to add " + obj); 213 if (obj instanceof IconSet) { 214 arr.add(obj); 215 } else { 216 if (LOGGABLE) log.log(obj + " is not icon set!"); 217 } 218 } 219 } catch (Exception ce) { 220 log.notify(ErrorManager.INFORMATIONAL, ce); } 222 } 223 listenersAttached = true; 224 if (arr.isEmpty()) { 225 return new IconSet(); 226 } 227 228 IconSet previous = null; 229 for (Iterator i = arr.iterator(); i.hasNext(); ) { 230 IconSet next = (IconSet) i.next(); 231 if (LOGGABLE) log.log("getIconSet() next " + next); 232 if (previous != null) { 233 if (previous.getDelegate() == null) { 234 if (LOGGABLE) log.log("getIconSet() setting " + next + " as delegate for " + previous); 235 previous.setDelegate(next); 236 } 237 } 238 previous = next; 239 } 240 241 iconSet = (IconSet)arr.get(0); 242 return iconSet; 243 } 244 245 252 private ContextListener getContextListener(Object source) { 253 if (listener == null) { 254 listener = new Listener (); 255 } 256 return (ContextListener)WeakListeners.create(ContextListener.class, listener, source); 257 } 258 259 263 private void changeIcon() { 264 iconSet = null; 266 RequestProcessor.getDefault().post(new Runnable () { 268 public void run() { 269 pcs.firePropertyChange("icons", null, null); 270 } 271 }); 272 } 273 274 278 private class Listener implements ContextListener { 279 public void attributeChanged(AttributeEvent evt) { 280 changeIcon(); 281 } 282 283 public void bindingChanged(BindingEvent evt) { 284 changeIcon(); 285 } 286 287 public void subcontextChanged(SubcontextEvent evt) { 288 changeIcon(); 289 } 290 } 291 } 292 | Popular Tags |