1 20 21 package org.apache.directory.ldapstudio.browser.common.dialogs; 22 23 24 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionActionGroup; 25 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionConfiguration; 26 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionUniversalListener; 27 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionWidget; 28 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 29 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 30 31 import org.eclipse.jface.dialogs.Dialog; 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.jface.viewers.DoubleClickEvent; 34 import org.eclipse.jface.viewers.IDoubleClickListener; 35 import org.eclipse.jface.viewers.ISelectionChangedListener; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 import org.eclipse.jface.viewers.SelectionChangedEvent; 38 import org.eclipse.jface.viewers.StructuredSelection; 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.layout.GridData; 41 import org.eclipse.swt.layout.GridLayout; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.Shell; 45 46 47 public class SelectConnectionDialog extends Dialog 48 { 49 50 private String title; 51 52 private IConnection initialConnection; 53 54 private IConnection selectedConnection; 55 56 private ConnectionConfiguration configuration; 57 58 private ConnectionUniversalListener universalListener; 59 60 private ConnectionActionGroup actionGroup; 61 62 private ConnectionWidget mainWidget; 63 64 65 public SelectConnectionDialog( Shell parentShell, String title, IConnection initialConnection ) 66 { 67 super( parentShell ); 68 super.setShellStyle( super.getShellStyle() | SWT.RESIZE ); 69 this.title = title; 70 this.initialConnection = initialConnection; 71 this.selectedConnection = null; 72 } 73 74 75 protected void configureShell( Shell shell ) 76 { 77 super.configureShell( shell ); 78 shell.setText( title ); 79 } 80 81 82 public boolean close() 83 { 84 if ( this.mainWidget != null ) 85 { 86 this.configuration.dispose(); 87 this.configuration = null; 88 this.actionGroup.deactivateGlobalActionHandlers(); 89 this.actionGroup.dispose(); 90 this.actionGroup = null; 91 this.universalListener.dispose(); 92 this.universalListener = null; 93 this.mainWidget.dispose(); 94 this.mainWidget = null; 95 } 96 return super.close(); 97 } 98 99 100 protected void okPressed() 101 { 102 this.selectedConnection = initialConnection; 103 super.okPressed(); 104 } 105 106 107 protected void cancelPressed() 108 { 109 this.selectedConnection = null; 110 super.cancelPressed(); 111 } 112 113 114 protected void createButtonsForButtonBar( Composite parent ) 115 { 116 createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false ); 117 createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false ); 118 } 119 120 121 protected Control createDialogArea( Composite parent ) 122 { 123 124 Composite composite = ( Composite ) super.createDialogArea( parent ); 125 GridLayout gl = new GridLayout(); 126 composite.setLayout( gl ); 127 GridData gd = new GridData( GridData.FILL_BOTH ); 128 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 129 gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH / 2 ); 130 composite.setLayoutData( gd ); 131 132 this.configuration = new ConnectionConfiguration(); 134 135 this.mainWidget = new ConnectionWidget( this.configuration, null ); 137 this.mainWidget.createWidget( composite ); 138 this.mainWidget.setInput( BrowserCorePlugin.getDefault().getConnectionManager() ); 139 140 this.actionGroup = new ConnectionActionGroup( this.mainWidget, this.configuration ); 142 this.actionGroup.fillToolBar( this.mainWidget.getToolBarManager() ); 143 this.actionGroup.fillMenu( this.mainWidget.getMenuManager() ); 144 this.actionGroup.fillContextMenu( this.mainWidget.getContextMenuManager() ); 145 this.actionGroup.activateGlobalActionHandlers(); 146 147 this.universalListener = new ConnectionUniversalListener( this.mainWidget.getViewer() ); 149 150 this.mainWidget.getViewer().addSelectionChangedListener( new ISelectionChangedListener() 151 { 152 public void selectionChanged( SelectionChangedEvent event ) 153 { 154 if ( !event.getSelection().isEmpty() ) 155 { 156 Object o = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement(); 157 if ( o instanceof IConnection ) 158 { 159 initialConnection = ( IConnection ) o; 160 } 161 } 162 } 163 } ); 164 165 this.mainWidget.getViewer().addDoubleClickListener( new IDoubleClickListener() 166 { 167 public void doubleClick( DoubleClickEvent event ) 168 { 169 if ( !event.getSelection().isEmpty() ) 170 { 171 Object o = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement(); 172 if ( o instanceof IConnection ) 173 { 174 initialConnection = ( IConnection ) o; 175 okPressed(); 176 } 177 } 178 } 179 } ); 180 181 if ( this.initialConnection != null ) 182 { 183 IConnection connection = this.initialConnection; 184 this.mainWidget.getViewer().reveal( connection ); 185 this.mainWidget.getViewer().setSelection( new StructuredSelection( connection ), true ); 186 } 187 188 applyDialogFont( composite ); 189 190 this.mainWidget.setFocus(); 191 192 return composite; 193 194 } 195 196 197 public IConnection getSelectedConnection() 198 { 199 return this.selectedConnection; 200 } 201 202 } 203 | Popular Tags |