1 19 20 package org.netbeans.modules.db.sql.editor.ui.actions; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.event.ItemEvent ; 26 import java.awt.event.ItemListener ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.List ; 32 import javax.swing.AbstractListModel ; 33 import javax.swing.Action ; 34 import javax.swing.ComboBoxModel ; 35 import javax.swing.DefaultListCellRenderer ; 36 import javax.swing.JComboBox ; 37 import javax.swing.JLabel ; 38 import javax.swing.JList ; 39 import javax.swing.JPanel ; 40 import javax.swing.border.EmptyBorder ; 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 55 public class ConnectionAction extends SQLExecutionBaseAction { 56 57 protected String getDisplayName(SQLExecution sqlExecution) { 58 return null; 59 } 60 61 protected void actionPerformed(SQLExecution sqlExecution) { 62 } 63 64 public Action 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 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 () { 91 public void run() { 92 if (toolbarPresenter != null) { 93 toolbarPresenter.setSQLExecution(sqlExecution); 96 } 97 } 98 }); 99 super.setSQLExecution(sqlExecution); 100 } 101 } 102 103 private static final class ToolbarPresenter extends JPanel { 104 105 private JComboBox combo; 106 private JLabel comboLabel; 107 private DatabaseConnectionModel model; 108 109 public ToolbarPresenter() { 110 initComponents(); 111 } 112 113 public Dimension getMinimumSize() { 114 Dimension dim = super.getMinimumSize(); 115 return new Dimension (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 (4, 0)); 124 setBorder(new EmptyBorder (0, 2, 0, 8)); 125 setOpaque(false); 126 127 combo = new JComboBox (); 128 combo.addItemListener(new ItemListener () { 129 public void itemStateChanged(ItemEvent 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 (); 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 implements ComboBoxModel , ConnectionListener, PropertyChangeListener { 155 156 private ConnectionListener listener; 157 private List connectionList; 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 (); 164 connectionList.addAll(Arrays.asList(ConnectionManager.getDefault().getConnections())); 165 } 166 167 public Object 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 object) { 176 if (sqlExecution != null) { 177 sqlExecution.setDatabaseConnection((DatabaseConnection)object); 178 } 179 } 180 181 public Object 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); } 195 196 public void propertyChange(PropertyChangeEvent evt) { 197 String propertyName = evt.getPropertyName(); 198 if (propertyName == null || propertyName.equals(SQLExecution.PROP_DATABASE_CONNECTION)) { 199 Mutex.EVENT.readAccess(new Runnable () { 200 public void run() { 201 fireContentsChanged(this, 0, 0); } 203 }); 204 } 205 } 206 207 public void connectionsChanged() { 208 Mutex.EVENT.readAccess(new Runnable () { 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 { 224 225 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 226 Object displayName = null; 227 String 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 component = (JLabel )super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 237 component.setToolTipText(tooltipText); 238 239 return component; 240 } 241 } 242 } 243 | Popular Tags |