KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > editor > ui > actions > SQLExecutionBaseAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.sql.editor.ui.actions;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
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 /**
44  *
45  * @author Andrei Badea
46  */

47 public abstract class SQLExecutionBaseAction extends AbstractAction JavaDoc implements ContextAwareAction, HelpCtx.Provider {
48
49     public SQLExecutionBaseAction() {
50         initialize();
51         
52         // allow subclasses to set the name for the "master" action
53
if (getValue(Action.NAME) == null) {
54             putValue(Action.NAME, getDisplayName(null));
55         }
56         String JavaDoc iconBase = getIconBase();
57         if (iconBase != null) {
58             putValue("iconBase", iconBase);
59         }
60     }
61     
62     protected void initialize() {
63         // allows subclasses to e.g. set noIconInMenu
64
}
65
66     protected abstract String JavaDoc getDisplayName(SQLExecution sqlExecution);
67
68     protected String JavaDoc 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 JavaDoc e) {
83     }
84
85     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
86         return new ContextAwareDelegate(this, actionContext);
87     }
88
89     public static void notifyNoDatabaseConnection() {
90         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc iterator = result.allInstances().iterator();
129             if (iterator.hasNext()) {
130                 setSQLExecution((SQLExecution)iterator.next());
131                 listener = new PropertyChangeListener JavaDoc() {
132                     public void propertyChange(PropertyChangeEvent JavaDoc 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."); // NOI18N
141
}
142             } else {
143                 setSQLExecution(null);
144                 listener = null;
145                 propertyChanged(null);
146             }
147         }
148
149         private void propertyChanged(String JavaDoc propertyName) {
150             if (propertyName == null || SQLExecution.PROP_EXECUTING.equals(propertyName)) {
151                 Mutex.EVENT.readAccess(new Runnable JavaDoc() {
152                     public void run() {
153                         boolean enabled = false;
154                         SQLExecution sqlExecution = getSQLExecution();
155                         if (sqlExecution != null) {
156                             enabled = parent.enable(sqlExecution);
157                         }
158                         String JavaDoc name = parent.getDisplayName(sqlExecution);
159
160                         setEnabled(enabled);
161                         putValue(Action.NAME, name);
162                     }
163                 });
164             }
165         }
166
167         public void actionPerformed(ActionEvent JavaDoc e) {
168             SQLExecution sqlExecution = getSQLExecution();
169             if (sqlExecution != null) {
170                 parent.actionPerformed(sqlExecution);
171             }
172         }
173
174         public Object JavaDoc getValue(String JavaDoc key) {
175             Object JavaDoc 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 JavaDoc getToolbarPresenter() {
187             return new Actions.ToolbarButton(this);
188         }
189     }
190 }
191
Popular Tags