KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.event.ItemEvent JavaDoc;
26 import java.awt.event.ItemListener JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.swing.AbstractListModel JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.ComboBoxModel JavaDoc;
35 import javax.swing.DefaultListCellRenderer JavaDoc;
36 import javax.swing.JComboBox JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JList JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40 import javax.swing.border.EmptyBorder JavaDoc;
41 import org.netbeans.api.db.explorer.ConnectionListener;
42 import org.netbeans.api.db.explorer.ConnectionManager;
43 import org.netbeans.api.db.explorer.DatabaseConnection;
44 import org.netbeans.modules.db.api.sql.execute.SQLExecution;
45 import org.openide.awt.Mnemonics;
46 import org.openide.util.Lookup;
47 import org.openide.util.Mutex;
48 import org.openide.util.NbBundle;
49 import org.openide.util.WeakListeners;
50
51 /**
52  *
53  * @author Andrei Badea
54  */

55 public class ConnectionAction extends SQLExecutionBaseAction {
56
57     protected String JavaDoc getDisplayName(SQLExecution sqlExecution) {
58         return null;
59     }
60
61     protected void actionPerformed(SQLExecution sqlExecution) {
62     }
63
64     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
65         return new ConnectionContextAwareDelegate(this, actionContext);
66     }
67
68     private static final class ConnectionContextAwareDelegate extends ContextAwareDelegate {
69
70         private ToolbarPresenter toolbarPresenter;
71
72         public ConnectionContextAwareDelegate(ConnectionAction parent, Lookup actionContext) {
73             super(parent, actionContext);
74         }
75
76         public Component JavaDoc getToolbarPresenter() {
77             toolbarPresenter = new ToolbarPresenter();
78             toolbarPresenter.setSQLExecution(getSQLExecution());
79             return toolbarPresenter;
80         }
81
82         public void setEnabled(boolean enabled) {
83             if (toolbarPresenter != null) {
84                 toolbarPresenter.setEnabled(enabled);
85             }
86             super.setEnabled(enabled);
87         }
88
89         protected void setSQLExecution(final SQLExecution sqlExecution) {
90             Mutex.EVENT.readAccess(new Runnable JavaDoc() {
91                 public void run() {
92                     if (toolbarPresenter != null) {
93                         // test for null necessary since the sqlExecution property
94
// can change just before the toolbar presenter is created
95
toolbarPresenter.setSQLExecution(sqlExecution);
96                     }
97                 }
98             });
99             super.setSQLExecution(sqlExecution);
100         }
101     }
102
103     private static final class ToolbarPresenter extends JPanel JavaDoc {
104
105         private JComboBox JavaDoc combo;
106         private JLabel JavaDoc comboLabel;
107         private DatabaseConnectionModel model;
108
109         public ToolbarPresenter() {
110             initComponents();
111         }
112
113         public Dimension JavaDoc getMinimumSize() {
114             Dimension JavaDoc dim = super.getMinimumSize();
115             return new Dimension JavaDoc(0, dim.height);
116         }
117
118         public void setSQLExecution(SQLExecution sqlExecution) {
119             model.setSQLExecution(sqlExecution);
120         }
121
122         private void initComponents() {
123             setLayout(new BorderLayout JavaDoc(4, 0));
124             setBorder(new EmptyBorder JavaDoc(0, 2, 0, 8));
125             setOpaque(false);
126
127             combo = new JComboBox JavaDoc();
128             combo.addItemListener(new ItemListener JavaDoc() {
129                 public void itemStateChanged(ItemEvent JavaDoc e) {
130                     DatabaseConnection dbconn = (DatabaseConnection)combo.getSelectedItem();
131                     combo.setToolTipText(dbconn != null ? dbconn.getDisplayName() : null);
132                 }
133             });
134             combo.setOpaque(false);
135             model = new DatabaseConnectionModel();
136             combo.setModel(model);
137             combo.setRenderer(new DatabaseConnectionRenderer());
138
139             add(combo, BorderLayout.CENTER);
140
141             comboLabel = new JLabel JavaDoc();
142             Mnemonics.setLocalizedText(comboLabel, NbBundle.getMessage(ConnectionAction.class, "LBL_ConnectionAction"));
143             comboLabel.setOpaque(false);
144             comboLabel.setLabelFor(combo);
145             add(comboLabel, BorderLayout.WEST);
146         }
147
148         public void setEnabled(boolean enabled) {
149             combo.setEnabled(enabled);
150             super.setEnabled(enabled);
151         }
152     }
153
154     private static final class DatabaseConnectionModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc, ConnectionListener, PropertyChangeListener JavaDoc {
155
156         private ConnectionListener listener;
157         private List JavaDoc connectionList; // must be ArrayList
158
private SQLExecution sqlExecution;
159
160         public DatabaseConnectionModel() {
161             listener = (ConnectionListener)WeakListeners.create(ConnectionListener.class, this, ConnectionManager.getDefault());
162             ConnectionManager.getDefault().addConnectionListener(listener);
163             connectionList = new ArrayList JavaDoc();
164             connectionList.addAll(Arrays.asList(ConnectionManager.getDefault().getConnections()));
165         }
166
167         public Object JavaDoc getElementAt(int index) {
168             return connectionList.get(index);
169         }
170
171         public int getSize() {
172             return connectionList.size();
173         }
174
175         public void setSelectedItem(Object JavaDoc object) {
176             if (sqlExecution != null) {
177                 sqlExecution.setDatabaseConnection((DatabaseConnection)object);
178             }
179         }
180
181         public Object JavaDoc getSelectedItem() {
182             return sqlExecution != null ? sqlExecution.getDatabaseConnection() : null;
183         }
184
185         public void setSQLExecution(SQLExecution sqlExecution) {
186             if (this.sqlExecution != null) {
187                 this.sqlExecution.removePropertyChangeListener(this);
188             }
189             this.sqlExecution = sqlExecution;
190             if (this.sqlExecution != null) {
191                 this.sqlExecution.addPropertyChangeListener(this);
192             }
193             fireContentsChanged(this, 0, 0); // because the selected item might have changed
194
}
195
196         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
197             String JavaDoc propertyName = evt.getPropertyName();
198             if (propertyName == null || propertyName.equals(SQLExecution.PROP_DATABASE_CONNECTION)) {
199                 Mutex.EVENT.readAccess(new Runnable JavaDoc() {
200                     public void run() {
201                         fireContentsChanged(this, 0, 0); // because the selected item might have changed
202
}
203                 });
204             }
205         }
206
207         public void connectionsChanged() {
208             Mutex.EVENT.readAccess(new Runnable JavaDoc() {
209                 public void run() {
210                     connectionList.clear();
211                     connectionList.addAll(Arrays.asList(ConnectionManager.getDefault().getConnections()));
212
213                     DatabaseConnection selectedItem = (DatabaseConnection)getSelectedItem();
214                     if (selectedItem != null && !connectionList.contains(selectedItem)) {
215                         setSelectedItem(null);
216                     }
217                     fireContentsChanged(this, 0, connectionList.size());
218                 }
219             });
220         }
221     }
222
223     private static final class DatabaseConnectionRenderer extends DefaultListCellRenderer JavaDoc {
224
225         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
226             Object JavaDoc displayName = null;
227             String JavaDoc tooltipText = null;
228
229             if (value instanceof DatabaseConnection) {
230                 DatabaseConnection dbconn = (DatabaseConnection)value;
231                 tooltipText = dbconn.getDisplayName();
232                 displayName = tooltipText;
233             } else {
234                 displayName = value;
235             }
236             JLabel JavaDoc component = (JLabel JavaDoc)super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
237             component.setToolTipText(tooltipText);
238
239             return component;
240         }
241     }
242 }
243
Popular Tags