1 20 21 package org.apache.directory.ldapstudio.browser.common.actions; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Comparator ; 27 import java.util.Iterator ; 28 import java.util.LinkedHashSet ; 29 import java.util.List ; 30 import java.util.Set ; 31 32 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserCategory; 33 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserEntryPage; 34 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserSearchResultPage; 35 import org.apache.directory.ldapstudio.browser.core.internal.model.Search; 36 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 37 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 38 import org.apache.directory.ldapstudio.browser.core.model.IBookmark; 39 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 40 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 41 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 42 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 43 import org.apache.directory.ldapstudio.browser.core.model.IValue; 44 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription; 45 import org.apache.directory.ldapstudio.browser.core.utils.LdapFilterUtils; 46 import org.eclipse.jface.viewers.ISelection; 47 import org.eclipse.jface.viewers.IStructuredSelection; 48 import org.eclipse.jface.viewers.StructuredSelection; 49 50 51 58 public abstract class SelectionUtils 59 { 60 61 77 public static ISearch getExampleSearch( ISelection selection ) 78 { 79 80 ISearch exampleSearch = new Search(); 81 String oldName = exampleSearch.getSearchParameter().getName(); 82 exampleSearch.getSearchParameter().setName( null ); 83 exampleSearch.setScope( ISearch.SCOPE_SUBTREE ); 84 85 if ( selection != null && !selection.isEmpty() && selection instanceof StructuredSelection ) 86 { 87 88 Object [] objects = ( ( IStructuredSelection ) selection ).toArray(); 89 Comparator <Object > comparator = new Comparator <Object >() 90 { 91 public int compare( Object o1, Object o2 ) 92 { 93 if ( ( o1 instanceof IValue ) && !( o2 instanceof IValue ) ) 94 { 95 return -1; 96 } 97 else if ( !( o1 instanceof IValue ) && ( o2 instanceof IValue ) ) 98 { 99 return 1; 100 } 101 else if ( ( o1 instanceof IAttribute ) && !( o2 instanceof IAttribute ) ) 102 { 103 return -1; 104 } 105 else if ( !( o1 instanceof IAttribute ) && ( o2 instanceof IAttribute ) ) 106 { 107 return 1; 108 } 109 else if ( ( o1 instanceof AttributeHierarchy ) && !( o2 instanceof AttributeHierarchy ) ) 110 { 111 return -1; 112 } 113 else if ( !( o1 instanceof AttributeHierarchy ) && ( o2 instanceof AttributeHierarchy ) ) 114 { 115 return 1; 116 } 117 return 0; 118 } 119 }; 120 Arrays.sort( objects, comparator ); 121 Object obj = objects[0]; 122 123 if ( obj instanceof ISearch ) 124 { 125 ISearch search = ( ISearch ) obj; 126 exampleSearch = ( ISearch ) search.clone(); 127 exampleSearch.setName( null ); 128 } 129 else if ( obj instanceof IEntry ) 130 { 131 IEntry entry = ( IEntry ) obj; 132 exampleSearch.setConnection( entry.getConnection() ); 133 exampleSearch.setSearchBase( entry.getDn() ); 134 } 135 else if ( obj instanceof ISearchResult ) 136 { 137 ISearchResult searchResult = ( ISearchResult ) obj; 138 exampleSearch.setConnection( searchResult.getEntry().getConnection() ); 139 exampleSearch.setSearchBase( searchResult.getEntry().getDn() ); 140 } 141 else if ( obj instanceof IBookmark ) 142 { 143 IBookmark bookmark = ( IBookmark ) obj; 144 exampleSearch.setConnection( bookmark.getConnection() ); 145 exampleSearch.setSearchBase( bookmark.getDn() ); 146 } 147 148 else if ( obj instanceof AttributeHierarchy || obj instanceof IAttribute || obj instanceof IValue ) 149 { 150 151 IEntry entry = null; 152 Set <String > filterSet = new LinkedHashSet <String >(); 153 for ( int i = 0; i < objects.length; i++ ) 154 { 155 Object object = objects[i]; 156 if ( object instanceof AttributeHierarchy ) 157 { 158 AttributeHierarchy ah = ( AttributeHierarchy ) object; 159 for ( Iterator it = ah.iterator(); it.hasNext(); ) 160 { 161 IAttribute attribute = ( IAttribute ) it.next(); 162 entry = attribute.getEntry(); 163 IValue[] values = attribute.getValues(); 164 for ( int v = 0; v < values.length; v++ ) 165 { 166 filterSet.add( LdapFilterUtils.getFilter( values[v] ) ); 167 } 168 } 169 } 170 else if ( object instanceof IAttribute ) 171 { 172 IAttribute attribute = ( IAttribute ) object; 173 entry = attribute.getEntry(); 174 IValue[] values = attribute.getValues(); 175 for ( int v = 0; v < values.length; v++ ) 176 { 177 filterSet.add( LdapFilterUtils.getFilter( values[v] ) ); 178 } 179 } 180 else if ( object instanceof IValue ) 181 { 182 IValue value = ( IValue ) object; 183 entry = value.getAttribute().getEntry(); 184 filterSet.add( LdapFilterUtils.getFilter( value ) ); 185 } 186 } 187 188 exampleSearch.setConnection( entry.getConnection() ); 189 exampleSearch.setSearchBase( entry.getDn() ); 190 StringBuffer filter = new StringBuffer (); 191 if ( filterSet.size() > 1 ) 192 { 193 filter.append( "(&" ); 194 for ( Iterator <String > filterIterator = filterSet.iterator(); filterIterator.hasNext(); ) 195 { 196 filter.append( filterIterator.next() ); 197 } 198 filter.append( ")" ); 199 } 200 else if ( filterSet.size() == 1 ) 201 { 202 filter.append( filterSet.toArray()[0] ); 203 } 204 else 205 { 206 filter.append( ISearch.FILTER_TRUE ); 207 } 208 exampleSearch.setFilter( filter.toString() ); 209 } 210 211 else if ( obj instanceof IConnection ) 212 { 213 IConnection connection = ( IConnection ) obj; 214 exampleSearch.setConnection( connection ); 215 if ( connection.getRootDSE().getChildrenCount() > 0 ) 216 { 217 exampleSearch.setSearchBase( connection.getRootDSE().getChildren()[0].getDn() ); 218 } 219 else 220 { 221 exampleSearch.setSearchBase( connection.getRootDSE().getDn() ); 222 } 223 } 224 else if ( obj instanceof BrowserCategory ) 225 { 226 BrowserCategory cat = ( BrowserCategory ) obj; 227 exampleSearch.setConnection( cat.getParent() ); 228 if ( cat.getParent().getRootDSE().getChildrenCount() > 0 ) 229 { 230 exampleSearch.setSearchBase( cat.getParent().getRootDSE().getChildren()[0].getDn() ); 231 } 232 else 233 { 234 exampleSearch.setSearchBase( cat.getParent().getRootDSE().getDn() ); 235 } 236 } 237 238 } 239 240 exampleSearch.getSearchParameter().setName( oldName ); 241 return exampleSearch; 242 } 243 244 245 251 public static BrowserCategory[] getBrowserViewCategories( ISelection selection ) 252 { 253 List <Object > list = getTypes( selection, BrowserCategory.class ); 254 return list.toArray( new BrowserCategory[list.size()] ); 255 } 256 257 258 264 public static IValue[] getValues( ISelection selection ) 265 { 266 List <Object > list = getTypes( selection, IValue.class ); 267 return list.toArray( new IValue[list.size()] ); 268 } 269 270 271 277 public static IAttribute[] getAttributes( ISelection selection ) 278 { 279 List <Object > list = getTypes( selection, IAttribute.class ); 280 return list.toArray( new IAttribute[list.size()] ); 281 } 282 283 284 290 public static AttributeHierarchy[] getAttributeHierarchie( ISelection selection ) 291 { 292 List <Object > list = getTypes( selection, AttributeHierarchy.class ); 293 return list.toArray( new AttributeHierarchy[list.size()] ); 294 } 295 296 297 303 public static String [] getProperties( ISelection selection ) 304 { 305 List <Object > list = getTypes( selection, String .class ); 306 return list.toArray( new String [list.size()] ); 307 } 308 309 310 316 public static AttributeTypeDescription[] getAttributeTypeDescription( ISelection selection ) 317 { 318 List <Object > list = getTypes( selection, AttributeTypeDescription.class ); 319 return list.toArray( new AttributeTypeDescription[list.size()] ); 320 } 321 322 323 329 public static IEntry[] getEntries( ISelection selection ) 330 { 331 List <Object > list = getTypes( selection, IEntry.class ); 332 return list.toArray( new IEntry[list.size()] ); 333 } 334 335 336 342 public static IBookmark[] getBookmarks( ISelection selection ) 343 { 344 List <Object > list = getTypes( selection, IBookmark.class ); 345 return list.toArray( new IBookmark[list.size()] ); 346 } 347 348 349 355 public static ISearchResult[] getSearchResults( ISelection selection ) 356 { 357 List <Object > list = getTypes( selection, ISearchResult.class ); 358 return list.toArray( new ISearchResult[list.size()] ); 359 } 360 361 362 369 private static List <Object > getTypes( ISelection selection, Class type ) 370 { 371 List <Object > list = new ArrayList <Object >(); 372 if ( selection instanceof IStructuredSelection ) 373 { 374 IStructuredSelection structuredSelection = ( IStructuredSelection ) selection; 375 Iterator it = structuredSelection.iterator(); 376 while ( it.hasNext() ) 377 { 378 Object o = it.next(); 379 if ( type.isInstance( o ) ) 380 { 381 list.add( o ); 382 } 383 } 384 } 385 return list; 386 } 387 388 389 395 public static ISearch[] getSearches( ISelection selection ) 396 { 397 List <Object > list = getTypes( selection, ISearch.class ); 398 return list.toArray( new ISearch[list.size()] ); 399 } 400 401 402 408 public static IConnection[] getConnections( ISelection selection ) 409 { 410 List <Object > list = getTypes( selection, IConnection.class ); 411 return list.toArray( new IConnection[list.size()] ); 412 } 413 414 415 421 public static BrowserEntryPage[] getBrowserEntryPages( ISelection selection ) 422 { 423 List <Object > list = getTypes( selection, BrowserEntryPage.class ); 424 return list.toArray( new BrowserEntryPage[list.size()] ); 425 } 426 427 428 434 public static BrowserSearchResultPage[] getBrowserSearchResultPages( ISelection selection ) 435 { 436 List <Object > list = getTypes( selection, BrowserSearchResultPage.class ); 437 return list.toArray( new BrowserSearchResultPage[list.size()] ); 438 } 439 440 441 447 public static Object [] getObjects( ISelection selection ) 448 { 449 List <Object > list = getTypes( selection, Object .class ); 450 return list.toArray( new Object [list.size()] ); 451 } 452 } 453 | Popular Tags |