1 19 20 package org.netbeans.modules.db.explorer.actions; 21 22 import java.awt.Dialog ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.beans.PropertyChangeEvent ; 27 import java.beans.PropertyChangeListener ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.Connection ; 31 import java.text.MessageFormat ; 32 import java.util.Vector ; 33 import javax.swing.JComponent ; 34 35 import javax.swing.JTabbedPane ; 36 import javax.swing.event.ChangeEvent ; 37 import javax.swing.event.ChangeListener ; 38 import org.netbeans.api.progress.ProgressHandle; 39 import org.netbeans.api.progress.ProgressHandleFactory; 40 import org.netbeans.modules.db.explorer.ConnectionList; 41 42 import org.openide.DialogDescriptor; 43 import org.openide.DialogDisplayer; 44 import org.openide.NotifyDescriptor; 45 import org.openide.ErrorManager; 46 import org.openide.nodes.Node; 47 48 import org.netbeans.api.db.explorer.DatabaseException; 49 import org.netbeans.lib.ddl.DDLException; 50 import org.netbeans.modules.db.ExceptionListener; 51 import org.netbeans.modules.db.explorer.DatabaseConnection; 52 53 56 import org.netbeans.modules.db.explorer.dlg.ConnectionDialog; 57 import org.netbeans.modules.db.explorer.dlg.ConnectPanel; 58 import org.netbeans.modules.db.explorer.dlg.ConnectProgressDialog; 59 import org.netbeans.modules.db.explorer.dlg.ConnectionDialogMediator; 60 import org.netbeans.modules.db.explorer.dlg.SchemaPanel; 61 import org.netbeans.modules.db.explorer.infos.ConnectionNodeInfo; 62 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo; 63 import org.netbeans.modules.db.explorer.nodes.DatabaseNode; 64 65 public class ConnectAction extends DatabaseAction { 66 static final long serialVersionUID =-6822218300035053411L; 67 68 ConnectionDialog dlg; 69 boolean advancedPanel = false; 70 boolean okPressed = false; 71 72 protected boolean enable(Node[] activatedNodes) { 73 Node node; 74 if (activatedNodes != null && activatedNodes.length == 1) 75 node = activatedNodes[0]; 76 else 77 return false; 78 79 DatabaseNodeInfo info = (DatabaseNodeInfo) node.getCookie(DatabaseNodeInfo.class); 80 if (info != null) { 81 DatabaseNodeInfo nfo = info.getParent(DatabaseNode.CONNECTION); 82 if (nfo != null) 83 return (nfo.getConnection() == null); 84 } 85 86 return false; 87 } 88 89 protected int mode() { 90 return MODE_ALL; 91 } 92 93 public void performAction(Node[] activatedNodes) { 94 Node node = activatedNodes[0]; 95 DatabaseNodeInfo info = (DatabaseNodeInfo) node.getCookie(DatabaseNodeInfo.class); 96 ConnectionNodeInfo nfo = (ConnectionNodeInfo) info.getParent(DatabaseNode.CONNECTION); 97 98 112 new ConnectionDialogDisplayer().showDialog(nfo, false); 113 } 114 115 public static final class ConnectionDialogDisplayer extends ConnectionDialogMediator { 116 117 ConnectionDialog dlg; 118 boolean advancedPanel = false; 119 boolean okPressed = false; 120 121 public void showDialog(final ConnectionNodeInfo nfo, boolean showDialog) { 122 String user = nfo.getUser(); 123 String pwd = nfo.getPassword(); 124 Boolean rpwd = (Boolean ) nfo.get(DatabaseNodeInfo.REMEMBER_PWD); 125 boolean remember = ((rpwd != null) ? rpwd.booleanValue() : false); 126 127 final DatabaseConnection dbcon = (DatabaseConnection) nfo.getDatabaseConnection(); 128 129 final ExceptionListener excListener = new ExceptionListener() { 130 public void exceptionOccurred(Exception exc) { 131 if (exc instanceof DDLException) { 132 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc.getCause()); 133 } else { 134 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 135 } 136 137 String message = null; 138 if (exc instanceof ClassNotFoundException ) { 139 message = MessageFormat.format(bundle().getString("EXC_ClassNotFound"), new String [] {exc.getMessage()}); } else { 141 StringBuffer buffer = new StringBuffer (); 142 buffer.append(MessageFormat.format(bundle().getString("ERR_UnableToConnect"), new String [] {exc.getMessage()})); if (exc instanceof DDLException && exc.getCause() instanceof SQLException ) { 144 SQLException sqlEx = ((SQLException )exc.getCause()).getNextException(); 145 while (sqlEx != null) { 146 buffer.append("\n\n" + sqlEx.getMessage()); sqlEx = sqlEx.getNextException(); 148 } 149 } 150 message = buffer.toString(); 151 } 152 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 153 } 154 }; 155 156 dbcon.addExceptionListener(excListener); 157 158 if (user == null || pwd == null || !remember) { 159 final ConnectPanel basePanel = new ConnectPanel(this, dbcon); 160 final SchemaPanel schemaPanel = new SchemaPanel(this, dbcon); 161 162 PropertyChangeListener argumentListener = new PropertyChangeListener () { 163 public void propertyChange(PropertyChangeEvent event) { 164 if (event.getPropertyName().equals("argumentChanged")) { schemaPanel.setSchemas(new Vector (), ""); schemaPanel.resetProgress(); 167 try { 168 Connection conn = dbcon.getConnection(); 169 if (conn != null && !conn.isClosed()) 170 conn.close(); 171 } catch (SQLException exc) { 172 } 174 } 175 } 176 }; 177 basePanel.addPropertyChangeListener(argumentListener); 178 179 final PropertyChangeListener connectionListener = new PropertyChangeListener () { 180 public void propertyChange(PropertyChangeEvent event) { 181 if (event.getPropertyName().equals("connecting")) { fireConnectionStarted(); 183 } 184 if (event.getPropertyName().equals("failed")) { fireConnectionFailed(); 186 } 187 if (event.getPropertyName().equals("connected")) { if (advancedPanel && !okPressed) { 191 if (retrieveSchemas(schemaPanel, dbcon, nfo.getSchema())) { 195 dbcon.setSchema(nfo.getSchema()); 196 } 197 dlg.setSelectedComponent(schemaPanel); 198 fireConnectionFinished(); 199 return; 200 } else { 201 fireConnectionFinished(); 202 dbcon.setSchema(nfo.getSchema()); 203 } 204 205 try { 206 nfo.finishConnect(null, dbcon, dbcon.getConnection()); 207 } catch (DatabaseException exc) { 208 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 209 String message = MessageFormat.format(bundle().getString("ERR_UnableToInitializeConnection"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 211 return; 212 } 213 214 DatabaseConnection realDbcon = ConnectionList.getDefault().getConnection(dbcon); 215 if (realDbcon != null) { 216 realDbcon.setPassword(dbcon.getPassword()); 217 } 218 if (basePanel.rememberPassword()) { 219 nfo.put(DatabaseNodeInfo.REMEMBER_PWD, Boolean.TRUE); 220 } 221 222 if(dlg != null) { 223 dlg.close(); 224 } 226 } else 227 okPressed = false; 228 } 229 }; 230 231 dbcon.addPropertyChangeListener(connectionListener); 232 233 ActionListener actionListener = new ActionListener () { 234 public void actionPerformed(ActionEvent event) { 235 if (event.getSource() == DialogDescriptor.OK_OPTION) { 236 okPressed = true; 237 nfo.setUser(basePanel.getUser()); 238 nfo.setPassword(basePanel.getPassword()); 239 dbcon.setUser(basePanel.getUser()); 240 dbcon.setPassword(basePanel.getPassword()); 241 242 try { 243 if (dbcon.getConnection() == null || dbcon.getConnection().isClosed()) 244 dbcon.connect(); 245 else { 246 dbcon.setSchema(schemaPanel.getSchema()); 247 nfo.setSchema(schemaPanel.getSchema()); 248 249 try { 250 nfo.finishConnect(null, dbcon, dbcon.getConnection()); 251 } catch (DatabaseException exc) { 252 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 253 String message = MessageFormat.format(bundle().getString("ERR_UnableToInitializeConnection"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 255 return; 256 } 257 258 DatabaseConnection realDbcon = ConnectionList.getDefault().getConnection(dbcon); 259 if (realDbcon != null) { 260 realDbcon.setPassword(dbcon.getPassword()); 261 } 262 if (basePanel.rememberPassword()) { 263 nfo.put(DatabaseNodeInfo.REMEMBER_PWD, Boolean.TRUE); 264 } 265 266 if(dlg != null) 267 dlg.close(); 268 } 269 } catch (SQLException exc) { 270 dbcon.connect(); 272 } 273 return; 274 } 275 } 276 }; 277 278 ChangeListener changeTabListener = new ChangeListener () { 279 public void stateChanged (ChangeEvent e) { 280 if (((JTabbedPane ) e.getSource()).getSelectedComponent().equals(schemaPanel)) { 281 advancedPanel = true; 282 nfo.setUser(basePanel.getUser()); 283 nfo.setPassword(basePanel.getPassword()); 284 dbcon.setPassword(basePanel.getPassword()); 285 } else 286 advancedPanel = false; 287 } 288 }; 289 290 dlg = new ConnectionDialog(this, basePanel, schemaPanel, basePanel.getTitle(), actionListener, changeTabListener); 291 dlg.setVisible(true); 292 } else try { 294 DialogDescriptor descriptor = null; 295 ProgressHandle progress = null; 296 297 if (showDialog) { 298 progress = ProgressHandleFactory.createHandle("handle"); 299 JComponent progressComponent = ProgressHandleFactory.createProgressComponent(progress); 300 progressComponent.setPreferredSize(new Dimension (250, 20)); 301 ConnectProgressDialog panel = new ConnectProgressDialog(); 302 panel.add(progressComponent); 303 descriptor = new DialogDescriptor(panel, bundle().getString("ConnectDialogTitle"), true, new Object [] { DialogDescriptor.CANCEL_OPTION }, 304 DialogDescriptor.CANCEL_OPTION, DialogDescriptor.DEFAULT_ALIGN, null, null); 305 } 306 final Dialog dialog = showDialog ? DialogDisplayer.getDefault().createDialog(descriptor) : null; 307 308 final PropertyChangeListener connectionListener = new PropertyChangeListener () { 309 public void propertyChange(PropertyChangeEvent event) { 310 if (event.getPropertyName().equals("connected")) { try { 312 nfo.finishConnect(null, dbcon, dbcon.getConnection()); 313 if (dialog != null) { 314 dialog.setVisible(false); 315 } 316 } catch (DatabaseException exc) { 317 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 318 String message = MessageFormat.format(bundle().getString("ERR_UnableToInitializeConnection"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 320 return; 321 } 322 } else if (event.getPropertyName().equals("failed")) { if (dialog != null) { 324 dialog.setVisible(false); 325 } 326 } 327 } 328 }; 329 330 dbcon.addPropertyChangeListener(connectionListener); 331 dbcon.connect(); 332 333 if (showDialog) { 334 progress.start(); 335 progress.switchToIndeterminate(); 336 dialog.setVisible(true); 337 progress.finish(); 338 dialog.dispose(); 339 } 340 } catch (Exception exc) { 341 String message = MessageFormat.format(bundle().getString("ERR_UnableToConnect"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 343 } 344 } 345 346 protected boolean retrieveSchemas(SchemaPanel schemaPanel, DatabaseConnection dbcon, String defaultSchema) { 347 fireConnectionStep(bundle().getString("ConnectionProgress_Schemas")); Vector schemas = new Vector (); 349 try { 350 ResultSet rs = dbcon.getConnection().getMetaData().getSchemas(); 351 if (rs != null) 352 while (rs.next()) 353 schemas.add(rs.getString(1).trim()); 354 } catch (SQLException exc) { 355 String message = MessageFormat.format(bundle().getString("ERR_UnableObtainSchemas"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 362 } 366 367 return schemaPanel.setSchemas(schemas, defaultSchema); 368 } 369 } 370 } 371 | Popular Tags |