1 20 21 package org.apache.directory.ldapstudio.browser.ui.wizards; 22 23 24 import java.util.ArrayList ; 25 import java.util.LinkedHashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils; 30 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 31 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent; 32 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener; 33 import org.apache.directory.ldapstudio.browser.common.widgets.search.SearchPageWrapper; 34 import org.apache.directory.ldapstudio.browser.core.model.DN; 35 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 36 import org.apache.directory.ldapstudio.browser.core.model.IBookmark; 37 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 38 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 39 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 40 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 41 import org.apache.directory.ldapstudio.browser.core.model.IValue; 42 import org.apache.directory.ldapstudio.browser.core.model.NameException; 43 44 import org.eclipse.jface.viewers.ISelection; 45 import org.eclipse.jface.wizard.WizardPage; 46 import org.eclipse.swt.SWT; 47 import org.eclipse.swt.events.ModifyEvent; 48 import org.eclipse.swt.events.ModifyListener; 49 import org.eclipse.swt.events.SelectionAdapter; 50 import org.eclipse.swt.events.SelectionEvent; 51 import org.eclipse.swt.layout.GridData; 52 import org.eclipse.swt.layout.GridLayout; 53 import org.eclipse.swt.widgets.Button; 54 import org.eclipse.swt.widgets.Combo; 55 import org.eclipse.swt.widgets.Composite; 56 import org.eclipse.ui.PlatformUI; 57 58 59 public class BatchOperationApplyOnWizardPage extends WizardPage 60 { 61 62 private String [] initCurrentSelectionTexts; 63 64 private DN[][] initCurrentSelectionDns; 65 66 private ISearch initSearch; 67 68 private Button currentSelectionButton; 69 70 private Combo currentSelectionCombo; 71 72 private Button searchButton; 73 74 private SearchPageWrapper spw; 75 76 77 public BatchOperationApplyOnWizardPage( String pageName, BatchOperationWizard wizard ) 78 { 79 super( pageName ); 80 super.setTitle( "Select Application Entries" ); 81 super.setDescription( "Please select the entries where the batch operation should be applied to." ); 82 super.setPageComplete( false ); 83 84 this.prepareCurrentSelection(); 85 this.prepareSearch(); 86 } 87 88 89 private void validate() 90 { 91 setPageComplete( getApplyOnDns() != null || this.spw.isValid() ); 92 } 93 94 95 public void createControl( Composite parent ) 96 { 97 98 Composite composite = new Composite( parent, SWT.NONE ); 99 GridLayout gl = new GridLayout( 1, false ); 100 composite.setLayout( gl ); 101 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 102 103 Composite applyOnGroup = composite; 104 105 this.currentSelectionButton = BaseWidgetUtils.createRadiobutton( applyOnGroup, "Current Selection:", 1 ); 106 this.currentSelectionButton.addSelectionListener( new SelectionAdapter() 107 { 108 public void widgetSelected( SelectionEvent e ) 109 { 110 enableCurrentSelectionWidgets( currentSelectionButton.getSelection() ); 111 validate(); 112 } 113 } ); 114 115 Composite currentSelectionComposite = BaseWidgetUtils.createColumnContainer( applyOnGroup, 2, 1 ); 116 BaseWidgetUtils.createRadioIndent( currentSelectionComposite, 1 ); 117 this.currentSelectionCombo = BaseWidgetUtils.createReadonlyCombo( currentSelectionComposite, 118 this.initCurrentSelectionTexts, 0, 1 ); 119 this.currentSelectionCombo.addModifyListener( new ModifyListener() 120 { 121 public void modifyText( ModifyEvent e ) 122 { 123 validate(); 124 } 125 } ); 126 127 BaseWidgetUtils.createSpacer( applyOnGroup, 1 ); 128 BaseWidgetUtils.createSpacer( applyOnGroup, 1 ); 129 130 this.searchButton = BaseWidgetUtils.createRadiobutton( applyOnGroup, "Results of following Search:", 1 ); 131 this.searchButton.addSelectionListener( new SelectionAdapter() 132 { 133 public void widgetSelected( SelectionEvent e ) 134 { 135 enableSearchWidgets( searchButton.getSelection() ); 136 validate(); 137 } 138 } ); 139 140 Composite searchComposite = BaseWidgetUtils.createColumnContainer( applyOnGroup, 2, 1 ); 141 BaseWidgetUtils.createRadioIndent( searchComposite, 1 ); 142 Composite innerSearchComposite = BaseWidgetUtils.createColumnContainer( searchComposite, 3, 1 ); 143 this.spw = new SearchPageWrapper( SearchPageWrapper.NAME_INVISIBLE 144 | SearchPageWrapper.RETURNINGATTRIBUTES_INVISIBLE | SearchPageWrapper.REFERRALOPTIONS_READONLY ); 145 this.spw.createContents( innerSearchComposite ); 146 this.spw.loadFromSearch( this.initSearch ); 147 this.spw.addWidgetModifyListener( new WidgetModifyListener() 148 { 149 public void widgetModified( WidgetModifyEvent event ) 150 { 151 validate(); 152 } 153 } ); 154 155 this.currentSelectionButton.setSelection( this.currentSelectionCombo.getItemCount() > 0 ); 156 this.currentSelectionButton.setEnabled( this.currentSelectionCombo.getItemCount() > 0 ); 157 this.searchButton.setSelection( this.currentSelectionCombo.getItemCount() == 0 ); 158 this.enableCurrentSelectionWidgets( this.currentSelectionButton.getSelection() ); 159 this.enableSearchWidgets( this.searchButton.getSelection() ); 160 161 validate(); 162 163 setControl( composite ); 164 } 165 166 167 public DN[] getApplyOnDns() 168 { 169 if ( currentSelectionButton.getSelection() ) 170 { 171 int index = currentSelectionCombo.getSelectionIndex(); 172 return initCurrentSelectionDns[index]; 173 } 174 else 175 { 176 return null; 177 } 178 } 179 180 181 public ISearch getApplyOnSearch() 182 { 183 if ( searchButton.getSelection() ) 184 { 185 return this.initSearch; 186 } 187 else 188 { 189 return null; 190 } 191 } 192 193 194 private void enableCurrentSelectionWidgets( boolean b ) 195 { 196 currentSelectionCombo.setEnabled( b ); 197 } 198 199 200 private void enableSearchWidgets( boolean b ) 201 { 202 spw.setEnabled( b ); 203 } 204 205 206 private void prepareSearch() 207 { 208 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService() 209 .getSelection(); 210 this.initSearch = SelectionUtils.getExampleSearch( selection ); 211 this.initSearch.setName( null ); 212 213 this.initSearch.setReferralsHandlingMethod( IConnection.HANDLE_REFERRALS_IGNORE ); 215 } 216 217 218 private void prepareCurrentSelection() 219 { 220 221 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService() 222 .getSelection(); 223 ISearch[] searches = SelectionUtils.getSearches( selection ); 224 IEntry[] entries = SelectionUtils.getEntries( selection ); 225 ISearchResult[] searchResults = SelectionUtils.getSearchResults( selection ); 226 IBookmark[] bookmarks = SelectionUtils.getBookmarks( selection ); 227 IAttribute[] attributes = SelectionUtils.getAttributes( selection ); 228 IValue[] values = SelectionUtils.getValues( selection ); 229 230 List textList = new ArrayList (); 231 List dnsList = new ArrayList (); 232 233 if ( attributes.length + values.length > 0 ) 234 { 235 Set internalDnSet = new LinkedHashSet (); 236 for ( int v = 0; v < values.length; v++ ) 237 { 238 if ( values[v].isString() ) 239 { 240 try 241 { 242 DN dn = new DN( values[v].getStringValue() ); 243 internalDnSet.add( dn ); 244 } 245 catch ( NameException e ) 246 { 247 } 248 } 249 } 250 251 for ( int a = 0; a < attributes.length; a++ ) 252 { 253 IValue[] vals = attributes[a].getValues(); 254 for ( int v = 0; v < vals.length; v++ ) 255 { 256 if ( vals[v].isString() ) 257 { 258 try 259 { 260 DN dn = new DN( vals[v].getStringValue() ); 261 internalDnSet.add( dn ); 262 } 263 catch ( NameException e ) 264 { 265 } 266 } 267 } 268 } 269 270 if ( !internalDnSet.isEmpty() ) 271 { 272 dnsList.add( internalDnSet.toArray( new DN[internalDnSet.size()] ) ); 273 textList.add( "DNs of selected Attributes (" + internalDnSet.size() + " Entries)" ); 274 } 275 } 276 if ( searches.length == 1 && searches[0].getSearchResults() != null ) 277 { 278 Set internalDnSet = new LinkedHashSet (); 279 ISearchResult[] srs = searches[0].getSearchResults(); 280 for ( int i = 0; i < srs.length; i++ ) 281 { 282 internalDnSet.add( srs[i].getDn() ); 283 } 284 285 dnsList.add( internalDnSet.toArray( new DN[internalDnSet.size()] ) ); 286 textList.add( "Search Results of '" + searches[0].getName() + "' (" + searches[0].getSearchResults().length 287 + " Entries)" ); 288 } 289 if ( entries.length + searchResults.length + bookmarks.length > 0 ) 290 { 291 Set internalDnSet = new LinkedHashSet (); 292 for ( int i = 0; i < entries.length; i++ ) 293 { 294 internalDnSet.add( entries[i].getDn() ); 295 } 296 for ( int i = 0; i < searchResults.length; i++ ) 297 { 298 internalDnSet.add( searchResults[i].getDn() ); 299 } 300 for ( int i = 0; i < bookmarks.length; i++ ) 301 { 302 internalDnSet.add( bookmarks[i].getDn() ); 303 } 304 305 dnsList.add( internalDnSet.toArray( new DN[internalDnSet.size()] ) ); 306 textList.add( "Selected Entries (" + internalDnSet.size() + " Entries)" ); 307 } 308 309 this.initCurrentSelectionTexts = ( String [] ) textList.toArray( new String [textList.size()] ); 310 this.initCurrentSelectionDns = ( DN[][] ) dnsList.toArray( new DN[0][0] ); 311 312 } 313 314 315 public void saveDialogSettings() 316 { 317 this.spw.saveToSearch( initSearch ); 318 } 319 320 } | Popular Tags |