1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets.search; 22 23 24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 26 import org.apache.directory.ldapstudio.browser.common.dialogs.SelectEntryDialog; 27 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter; 28 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 29 import org.apache.directory.ldapstudio.browser.common.widgets.BrowserWidget; 30 import org.apache.directory.ldapstudio.browser.common.widgets.HistoryUtils; 31 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob; 32 import org.apache.directory.ldapstudio.browser.core.model.DN; 33 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 34 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 35 import org.apache.directory.ldapstudio.browser.core.model.NameException; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.events.ModifyEvent; 38 import org.eclipse.swt.events.ModifyListener; 39 import org.eclipse.swt.events.SelectionAdapter; 40 import org.eclipse.swt.events.SelectionEvent; 41 import org.eclipse.swt.layout.GridData; 42 import org.eclipse.swt.widgets.Button; 43 import org.eclipse.swt.widgets.Combo; 44 import org.eclipse.swt.widgets.Composite; 45 46 47 60 public class EntryWidget extends BrowserWidget 61 { 62 63 64 private Combo dnCombo; 65 66 67 private Button upButton; 68 69 70 private Button entryBrowseButton; 71 72 73 private IConnection connection; 74 75 76 private DN dn; 77 78 79 private DN suffix; 80 81 82 85 public EntryWidget() 86 { 87 this.connection = null; 88 this.dn = null; 89 } 90 91 92 98 public EntryWidget( IConnection connection, DN dn ) 99 { 100 this( connection, dn, null ); 101 } 102 103 104 111 public EntryWidget( IConnection connection, DN dn, DN suffix ) 112 { 113 this.connection = connection; 114 this.dn = dn; 115 this.suffix = suffix; 116 } 117 118 119 124 public void createWidget( final Composite parent ) 125 { 126 127 Composite textAndUpComposite = BaseWidgetUtils.createColumnContainer( parent, 2, 1 ); 129 dnCombo = BaseWidgetUtils.createCombo( textAndUpComposite, new String [0], -1, 1 ); 130 GridData gd = new GridData( GridData.FILL_HORIZONTAL ); 131 gd.horizontalSpan = 1; 132 gd.widthHint = 200; 133 dnCombo.setLayoutData( gd ); 134 135 String [] history = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_DN_HISTORY ); 137 dnCombo.setItems( history ); 138 dnCombo.addModifyListener( new ModifyListener() 139 { 140 public void modifyText( ModifyEvent e ) 141 { 142 try 143 { 144 dn = new DN( dnCombo.getText() ); 145 } 146 catch ( NameException e1 ) 147 { 148 dn = null; 149 } 150 151 internalSetEnabled(); 152 notifyListeners(); 153 } 154 } ); 155 156 upButton = new Button( textAndUpComposite, SWT.PUSH ); 158 upButton.setToolTipText( "Parent" ); 159 upButton.setImage( BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_PARENT ) ); 160 upButton.setEnabled( false ); 161 upButton.addSelectionListener( new SelectionAdapter() 162 { 163 public void widgetSelected( SelectionEvent e ) 164 { 165 if ( dn != null && dn.getParentDn() != null ) 166 { 167 dn = dn.getParentDn(); 168 dnChanged(); 169 internalSetEnabled(); 170 notifyListeners(); 171 } 172 } 173 } ); 174 175 entryBrowseButton = BaseWidgetUtils.createButton( parent, "Br&owse...", 1 ); 177 entryBrowseButton.addSelectionListener( new SelectionAdapter() 178 { 179 public void widgetSelected( SelectionEvent e ) 180 { 181 if ( connection != null ) 182 { 183 IEntry rootEntry = connection.getRootDSE(); 185 if( suffix != null && suffix.getRdns().length > 0 ) 186 { 187 rootEntry = connection.getEntryFromCache( suffix ); 188 if ( rootEntry == null ) 189 { 190 ReadEntryJob job = new ReadEntryJob( connection, suffix ); 191 RunnableContextJobAdapter.execute( job ); 192 rootEntry = job.getReadEntry(); 193 } 194 } 195 196 DN initialDN = dn; 198 if( suffix != null && suffix.getRdns().length > 0 ) 199 { 200 if( initialDN != null && initialDN.getRdns().length > 0 ) 201 { 202 initialDN = new DN( initialDN, suffix ); 203 } 204 } 205 206 IEntry entry = rootEntry; 208 if ( initialDN != null && initialDN.getRdns().length > 0 ) 209 { 210 entry = connection.getEntryFromCache( initialDN ); 211 if ( entry == null ) 212 { 213 ReadEntryJob job = new ReadEntryJob( connection, initialDN ); 214 RunnableContextJobAdapter.execute( job ); 215 entry = job.getReadEntry(); 216 } 217 } 218 219 220 SelectEntryDialog dialog = new SelectEntryDialog( parent.getShell(), "Select DN", rootEntry, entry ); 222 dialog.open(); 223 IEntry selectedEntry = dialog.getSelectedEntry(); 224 225 if ( selectedEntry != null ) 227 { 228 dn = selectedEntry.getDn(); 229 if( suffix != null && suffix.getRdns().length > 0 ) 230 { 231 dn = dn.getLocalName( suffix ); 232 } 233 dnChanged(); 234 internalSetEnabled(); 235 notifyListeners(); 236 } 237 } 238 } 239 } ); 240 241 dnChanged(); 242 internalSetEnabled(); 243 } 244 245 246 249 private void dnChanged() 250 { 251 if ( dnCombo != null && entryBrowseButton != null ) 252 { 253 dnCombo.setText( dn != null ? dn.toString() : "" ); 254 } 255 } 256 257 258 263 public void setEnabled( boolean b ) 264 { 265 dnCombo.setEnabled( b ); 266 267 if ( b ) 268 { 269 this.dnChanged(); 270 } 271 272 internalSetEnabled(); 273 } 274 275 276 279 private void internalSetEnabled() 280 { 281 upButton.setEnabled( dn != null && dn.getParentDn() != null && dnCombo.isEnabled() ); 282 entryBrowseButton.setEnabled( connection != null && dnCombo.isEnabled() ); 283 } 284 285 286 289 public void saveDialogSettings() 290 { 291 HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_DN_HISTORY, this.dnCombo.getText() ); 292 } 293 294 295 300 public DN getSuffix() 301 { 302 return suffix; 303 } 304 305 306 311 public DN getDn() 312 { 313 return dn; 314 } 315 316 317 322 public IConnection getConnection() 323 { 324 return connection; 325 } 326 327 328 334 public void setInput( IConnection connection, DN dn ) 335 { 336 setInput( connection, dn, null ); 337 } 338 339 340 347 public void setInput( IConnection connection, DN dn, DN suffix ) 348 { 349 if ( this.connection != connection || this.dn != dn || this.suffix != suffix ) 350 { 351 this.connection = connection; 352 this.dn = dn; 353 this.suffix = suffix; 354 dnChanged(); 355 } 356 } 357 358 } 359 | Popular Tags |