1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.awt.EventQueue ; 23 import java.awt.event.ActionEvent ; 24 import java.util.logging.Level ; 25 import java.util.logging.LogRecord ; 26 import java.util.logging.Logger ; 27 import javax.swing.Icon ; 28 import javax.swing.JButton ; 29 import javax.swing.JMenuItem ; 30 import org.openide.util.Lookup; 31 import org.openide.util.LookupEvent; 32 import org.openide.util.LookupListener; 33 import org.openide.util.NbBundle; 34 import org.openide.util.Utilities; 35 import org.openide.util.WeakListeners; 36 37 41 public abstract class LookupSensitiveAction extends BasicAction implements LookupListener { 42 private static Logger UILOG = Logger.getLogger("org.netbeans.ui.actions"); 44 private Lookup lookup; 45 private Class <?>[] watch; 46 private Lookup.Result results[]; 47 private boolean needsRefresh = true; 48 private boolean initialized = false; 49 50 private boolean refreshing = false; 51 52 54 59 60 65 public LookupSensitiveAction(Icon icon, Lookup lookup, Class [] watch ) { 66 super( null, icon ); 67 if (lookup == null) { 68 lookup = Utilities.actionsGlobalContext(); 69 } 70 this.lookup = lookup; 71 this.watch = watch; 72 } 73 74 private void init () { 75 if (initialized) { 76 return ; 77 } 78 assert EventQueue.isDispatchThread () : "Cannot be called outside EQ!"; 79 this.results = new Lookup.Result[watch.length]; 80 for ( int i = 0; i < watch.length; i++ ) { 82 results[i] = lookup.lookupResult(watch[i]); 83 results[i].allItems(); 84 LookupListener resultListener = WeakListeners.create(LookupListener.class, this, results[i]); 85 results[i].addLookupListener( resultListener ); 86 } 87 initialized = true; 88 } 89 90 92 public Object getValue( String key ) { 93 init (); 94 if ( needsRefresh ) { 95 doRefresh(); 96 } 97 return super.getValue( key ); 98 } 99 100 102 public boolean isEnabled() { 103 init (); 104 if ( needsRefresh ) { 105 doRefresh(); 106 } 107 return super.isEnabled(); 108 } 109 110 public final void actionPerformed( ActionEvent e ) { 111 init (); 112 113 if (UILOG.isLoggable(Level.FINE)) { 114 LogRecord r; 115 boolean isKey; 116 if (e.getSource() instanceof JMenuItem ) { 117 isKey = false; 118 } else if (e.getSource() instanceof JButton ) { 119 isKey = false; 120 } else { 121 isKey = true; 122 } 123 124 if (!isKey) { 125 r = new LogRecord (Level.FINE, "UI_ACTION_BUTTON_PRESS"); r.setResourceBundle(NbBundle.getBundle(LookupSensitiveAction.class)); 127 r.setParameters(new Object [] { 128 e.getSource(), 129 e.getSource().getClass().getName(), 130 this, 131 getClass().getName(), 132 getValue(NAME) 133 }); 134 r.setLoggerName(UILOG.getName()); 135 UILOG.log(r); 136 } 137 } 138 139 actionPerformed( lookup ); 140 } 141 142 protected final Lookup getLookup() { 143 return lookup; 144 } 145 146 private void doRefresh() { 147 refreshing = true; 148 try { 149 refresh( lookup ); 150 } finally { 151 refreshing = false; 152 } 153 needsRefresh = false; 154 } 155 156 158 160 protected abstract void actionPerformed( Lookup context ); 161 162 165 protected abstract void refresh( Lookup context ); 166 167 169 public void resultChanged( LookupEvent e ) { 170 if ( refreshing ) { 171 return; 172 } 173 else if ( getPropertyChangeListeners().length == 0 ) { 174 needsRefresh = true; 175 } 176 else { 177 doRefresh(); 178 } 179 } 180 181 } | Popular Tags |