1 19 20 package org.netbeans.modules.db.sql.editor.ui.actions; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.util.Iterator ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import org.netbeans.modules.db.api.sql.execute.SQLExecution; 30 import org.openide.DialogDisplayer; 31 import org.openide.ErrorManager; 32 import org.openide.NotifyDescriptor; 33 import org.openide.awt.Actions; 34 import org.openide.util.ContextAwareAction; 35 import org.openide.util.HelpCtx; 36 import org.openide.util.Lookup; 37 import org.openide.util.LookupEvent; 38 import org.openide.util.LookupListener; 39 import org.openide.util.Mutex; 40 import org.openide.util.NbBundle; 41 import org.openide.util.actions.Presenter; 42 43 47 public abstract class SQLExecutionBaseAction extends AbstractAction implements ContextAwareAction, HelpCtx.Provider { 48 49 public SQLExecutionBaseAction() { 50 initialize(); 51 52 if (getValue(Action.NAME) == null) { 54 putValue(Action.NAME, getDisplayName(null)); 55 } 56 String iconBase = getIconBase(); 57 if (iconBase != null) { 58 putValue("iconBase", iconBase); 59 } 60 } 61 62 protected void initialize() { 63 } 65 66 protected abstract String getDisplayName(SQLExecution sqlExecution); 67 68 protected String getIconBase() { 69 return null; 70 } 71 72 public HelpCtx getHelpCtx() { 73 return HelpCtx.DEFAULT_HELP; 74 } 75 76 protected boolean enable(SQLExecution sqlExecution) { 77 return !sqlExecution.isExecuting(); 78 } 79 80 protected abstract void actionPerformed(SQLExecution sqlExecution); 81 82 public void actionPerformed(ActionEvent e) { 83 } 84 85 public Action createContextAwareInstance(Lookup actionContext) { 86 return new ContextAwareDelegate(this, actionContext); 87 } 88 89 public static void notifyNoDatabaseConnection() { 90 String message = NbBundle.getMessage(SQLExecutionBaseAction.class, "LBL_NoDatabaseConnection"); 91 NotifyDescriptor desc = new NotifyDescriptor.Message(message, NotifyDescriptor.WARNING_MESSAGE); 92 DialogDisplayer.getDefault().notify(desc); 93 } 94 95 static class ContextAwareDelegate extends AbstractAction implements Presenter.Toolbar, HelpCtx.Provider { 96 97 private final SQLExecutionBaseAction parent; 98 private final Lookup.Result result; 99 100 private SQLExecution sqlExecution; 101 private PropertyChangeListener listener; 102 103 public ContextAwareDelegate(SQLExecutionBaseAction parent, Lookup actionContext) { 104 this.parent = parent; 105 106 result = actionContext.lookup(new Lookup.Template(SQLExecution.class)); 107 result.addLookupListener(new LookupListener() { 108 public void resultChanged(LookupEvent ev) { 109 ContextAwareDelegate.this.resultChanged(); 110 } 111 }); 112 resultChanged(); 113 } 114 115 protected synchronized void setSQLExecution(SQLExecution sqlExecution) { 116 this.sqlExecution = sqlExecution; 117 } 118 119 protected synchronized SQLExecution getSQLExecution() { 120 return sqlExecution; 121 } 122 123 private synchronized void resultChanged() { 124 if (sqlExecution != null) { 125 sqlExecution.removePropertyChangeListener(listener); 126 } 127 128 Iterator iterator = result.allInstances().iterator(); 129 if (iterator.hasNext()) { 130 setSQLExecution((SQLExecution)iterator.next()); 131 listener = new PropertyChangeListener () { 132 public void propertyChange(PropertyChangeEvent evt) { 133 propertyChanged(evt.getPropertyName()); 134 } 135 }; 136 sqlExecution.addPropertyChangeListener(listener); 137 propertyChanged(null); 138 139 if (iterator.hasNext()) { 140 ErrorManager.getDefault().log(ErrorManager.WARNING, "Multiple SQLExecution instances in the action context. Will only use the first one."); } 142 } else { 143 setSQLExecution(null); 144 listener = null; 145 propertyChanged(null); 146 } 147 } 148 149 private void propertyChanged(String propertyName) { 150 if (propertyName == null || SQLExecution.PROP_EXECUTING.equals(propertyName)) { 151 Mutex.EVENT.readAccess(new Runnable () { 152 public void run() { 153 boolean enabled = false; 154 SQLExecution sqlExecution = getSQLExecution(); 155 if (sqlExecution != null) { 156 enabled = parent.enable(sqlExecution); 157 } 158 String name = parent.getDisplayName(sqlExecution); 159 160 setEnabled(enabled); 161 putValue(Action.NAME, name); 162 } 163 }); 164 } 165 } 166 167 public void actionPerformed(ActionEvent e) { 168 SQLExecution sqlExecution = getSQLExecution(); 169 if (sqlExecution != null) { 170 parent.actionPerformed(sqlExecution); 171 } 172 } 173 174 public Object getValue(String key) { 175 Object value = super.getValue(key); 176 if (value == null) { 177 value = parent.getValue(key); 178 } 179 return value; 180 } 181 182 public HelpCtx getHelpCtx() { 183 return parent.getHelpCtx(); 184 } 185 186 public Component getToolbarPresenter() { 187 return new Actions.ToolbarButton(this); 188 } 189 } 190 } 191 | Popular Tags |