1 19 20 package org.netbeans.modules.xml.catalog; 21 22 import java.awt.Image ; 23 import java.beans.IntrospectionException ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 import java.util.TreeSet ; 33 import javax.swing.Action ; 34 import org.netbeans.modules.xml.catalog.settings.CatalogSettings; 35 import org.netbeans.modules.xml.catalog.spi.CatalogDescriptor; 36 import org.netbeans.modules.xml.catalog.spi.CatalogListener; 37 import org.netbeans.modules.xml.catalog.spi.CatalogReader; 38 import org.netbeans.modules.xml.catalog.spi.CatalogWriter; 39 import org.openide.actions.PropertiesAction; 40 import org.openide.nodes.BeanNode; 41 import org.openide.nodes.Children; 42 import org.openide.nodes.Node; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.WeakListeners; 45 import org.openide.util.actions.NodeAction; 46 import org.openide.util.actions.SystemAction; 47 48 57 final class CatalogNode extends BeanNode implements Refreshable, PropertyChangeListener , Node.Cookie { 58 59 private CatalogReader catalog; 60 61 public CatalogNode(CatalogReader catalog) throws IntrospectionException { 62 super(catalog, new CatalogChildren(catalog)); 63 this.catalog=catalog; 64 getCookieSet().add(this); 65 66 if (catalog instanceof CatalogDescriptor) { 67 68 70 CatalogDescriptor desc = (CatalogDescriptor) catalog; 71 setSynchronizeName(false); 72 setName(desc.getDisplayName()); 73 String bundleString = catalog instanceof CatalogWriter ?"LBL_catalogReadWrite":"LBL_catalogReadOnly"; setDisplayName(Util.THIS.getString(bundleString, desc.getDisplayName())); 75 setShortDescription(desc.getShortDescription()); 76 fireIconChange(); 77 78 80 desc.addPropertyChangeListener(WeakListeners.propertyChange(this, desc)); 81 } 82 } 83 84 CatalogReader getCatalogReader() { 85 return catalog; 86 } 87 88 public Action [] getActions(boolean context) { 89 if (catalog instanceof CatalogWriter) 90 return new Action [] { 91 SystemAction.get(AddCatalogEntryAction.class), 92 SystemAction.get(RefreshAction.class), 93 SystemAction.get(CatalogNode.UnmountAction.class), 94 null, 95 SystemAction.get(PropertiesAction.class) 97 }; 98 else 99 return new Action [] { 100 SystemAction.get(RefreshAction.class), 101 SystemAction.get(CatalogNode.UnmountAction.class), 102 null, 103 SystemAction.get(PropertiesAction.class) 105 }; 106 } 107 108 111 public Image getIcon(int type) { 112 if (catalog instanceof CatalogDescriptor) { 113 Image icon = ((CatalogDescriptor)catalog).getIcon(type); 114 if (icon != null) return icon; 115 } 116 117 return super.getIcon(type); 118 } 119 120 public HelpCtx getHelpCtx() { 121 return HelpCtx.DEFAULT_HELP; 123 } 124 125 128 public void refresh() { 129 catalog.refresh(); 130 ((CatalogChildren)getChildren()).reload(); } 132 133 136 public boolean canDestroy () { 137 return false; 138 } 139 140 public boolean canRename() { 141 return false; 142 } 143 144 147 public void destroy() throws IOException { 148 CatalogSettings mounted = CatalogSettings.getDefault(); 149 mounted.removeCatalog(catalog); 150 super.destroy(); 151 } 152 153 156 public void propertyChange(PropertyChangeEvent e) { 157 if ( Util.THIS.isLoggable() ) Util.THIS.debug(e.toString()); 158 if (CatalogDescriptor.PROP_CATALOG_NAME.equals(e.getPropertyName())) { 159 if ( Util.THIS.isLoggable() ) Util.THIS.debug(" Setting name: " + (String ) e.getNewValue()); 161 setName((String ) e.getNewValue()); 162 setDisplayName((String ) e.getNewValue()); 163 } else if (CatalogDescriptor.PROP_CATALOG_DESC.equals(e.getPropertyName())) { 164 if ( Util.THIS.isLoggable() ) Util.THIS.debug(" Setting desc: " + (String ) e.getNewValue()); 166 setShortDescription((String ) e.getNewValue()); 167 } else if (CatalogDescriptor.PROP_CATALOG_ICON.equals(e.getPropertyName())) { 168 if ( Util.THIS.isLoggable() ) Util.THIS.debug(" Updating icon"); 170 fireIconChange(); 171 } 172 } 173 174 175 177 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 178 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Reading Catalog node " + this); 180 in.defaultReadObject(); 181 } 182 183 private void writeObject(ObjectOutputStream out) throws IOException { 184 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Writing " + this); 186 out.defaultWriteObject(); 187 } 188 189 191 194 public static class CatalogChildren extends Children.Keys { 195 196 private CatalogReader peer; 197 private CatalogListener catalogListener; 198 199 public CatalogChildren(CatalogReader catalog) { 200 peer = catalog; 201 202 } 203 204 205 private final TreeSet keys = new TreeSet (); 206 207 public void addNotify() { 208 catalogListener = new Lis(); 209 try { 210 peer.addCatalogListener(catalogListener); 211 } catch (UnsupportedOperationException ex) { 212 } 214 reload(); 215 } 216 217 public void removeNotify() { 218 try { 219 peer.removeCatalogListener(catalogListener); 220 } catch (UnsupportedOperationException ex) { 221 } 223 keys.clear(); 224 setKeys(keys); 225 } 226 227 public Node[] createNodes(Object key) { 228 try { 229 CatalogEntry catalogEntry = new CatalogEntry((String ) key, peer); 230 return new Node[] { 231 new CatalogEntryNode(catalogEntry) 232 }; 233 } catch (IntrospectionException ex) { 234 return null; 235 } 236 } 237 238 241 public void reload() { 242 if ( Util.THIS.isLoggable() ) Util.THIS.debug(" Reloading kids of " + peer + "..."); 244 Set previous = new HashSet (keys); 245 keys.clear(); 246 Iterator it = peer.getPublicIDs(); 247 if (it != null) { 248 while (it.hasNext()) { 249 String publicID = (String ) it.next(); 250 keys.add(publicID); 251 if (previous.contains(publicID)) { 252 refreshKey(publicID); } 254 } 255 } 256 setKeys(keys); 257 } 258 259 private class Lis implements CatalogListener { 260 261 262 public void notifyNew(String publicID) { 263 keys.add(publicID); 264 setKeys(keys); 265 } 266 267 268 public void notifyRemoved(String publicID) { 269 keys.remove(publicID); 270 setKeys(keys); 271 } 272 273 274 public void notifyUpdate(String publicID) { 275 refreshKey(publicID); 276 } 277 278 279 public void notifyInvalidate() { 280 reload(); 281 } 282 283 } 284 285 } 286 287 290 private static final class UnmountAction extends NodeAction { 291 292 private static final long serialVersionUID = 3556006276357785484L; 293 294 public UnmountAction() { 295 } 296 297 public String getName() { 298 return Util.THIS.getString("LBL_unmount"); 299 } 300 301 public HelpCtx getHelpCtx() { 302 return new HelpCtx(UnmountAction.class); 303 } 304 305 protected boolean enable(Node[] activatedNodes) { 306 if (activatedNodes.length > 0) { 307 for (int i = 0; i<activatedNodes.length; i++) { 308 Node me = activatedNodes[i]; 309 Object self = me.getCookie(CatalogNode.class); 310 if (self instanceof CatalogNode) { 311 CatalogReader reader = (CatalogReader) ((CatalogNode)self).getBean(); 312 if (CatalogSettings.getDefault().isRemovable(reader)) { 313 return true; 314 } 315 } 316 } 317 } 318 return false; 319 } 320 321 protected void performAction(Node[] activatedNodes) { 322 if (enable(activatedNodes) == false) return; 323 for (int i = 0; i<activatedNodes.length; i++) { 324 try { 325 Node me = activatedNodes[i]; 326 CatalogNode self = (CatalogNode) me.getCookie(CatalogNode.class); 327 self.destroy(); 328 } catch (IOException ex) { 329 Util.THIS.debug("Cannot unmount XML entity catalog!", ex); 330 } 331 } 332 } 333 334 protected boolean asynchronous() { 335 return false; 336 } 337 338 } 339 340 } 341 | Popular Tags |