1 19 20 package org.netbeans.modules.xml.xam.ui.multiview; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import org.openide.nodes.Node; 25 import org.openide.util.Lookup; 26 import org.openide.util.lookup.ProxyLookup; 27 28 52 public class CookieProxyLookup extends ProxyLookup 53 implements PropertyChangeListener { 54 55 private Node delegate; 56 57 private volatile boolean propertyChanging; 58 59 65 public CookieProxyLookup(Lookup[] lookups, Node delegate) { 66 super(lookups); 67 this.delegate = delegate; 68 } 69 70 public synchronized void propertyChange(PropertyChangeEvent event) { 71 if (propertyChanging) { 72 return; 75 } 76 propertyChanging = true; 77 try { 78 Lookup[] lookups = getLookups(); 79 Node[] oldNodes = (Node[]) event.getOldValue(); 80 Node[] newNodes = (Node[]) event.getNewValue(); 81 Lookup lastLookup = lookups[lookups.length - 1]; 82 if (!(lastLookup instanceof NoNodeLookup) && 83 (oldNodes.length >= 1) && 84 (!oldNodes[0].equals(delegate))) { 85 switchLookup(); 86 } else if ((lastLookup instanceof NoNodeLookup) && 87 (newNodes.length == 0)) { 88 switchLookup(); 89 } 90 } finally { 91 propertyChanging = false; 92 } 93 } 94 95 99 private void switchLookup() { 100 Lookup[] lookups = getLookups(); 101 Lookup nodeLookup = delegate.getLookup(); 102 int index = lookups.length - 1; 103 if (lookups[index] instanceof NoNodeLookup) { 104 lookups[index] = nodeLookup; 105 } else { 106 lookups[index] = new NoNodeLookup(nodeLookup); 107 } 108 setLookups(lookups); 109 } 110 111 115 private static class NoNodeLookup extends Lookup { 116 private final Lookup delegate; 117 118 public NoNodeLookup(Lookup delegate) { 119 this.delegate = delegate; 120 } 121 122 public Object lookup(Class clazz) { 123 return (clazz == Node.class) ? null : delegate.lookup(clazz); 124 } 125 126 public Lookup.Result lookup(Lookup.Template template) { 127 if (template.getType() == Node.class) { 128 return Lookup.EMPTY.lookup(new Lookup.Template(Node.class)); 129 } else { 130 return delegate.lookup(template); 131 } 132 } 133 } 134 } 135 | Popular Tags |