1 20 21 package org.apache.directory.ldapstudio.schemas.view.dialogs; 22 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Comparator ; 27 import java.util.List ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 31 import org.apache.directory.ldapstudio.schemas.model.ObjectClass; 32 import org.apache.directory.ldapstudio.schemas.model.SchemaPool; 33 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper; 34 import org.eclipse.jface.viewers.IStructuredContentProvider; 35 import org.eclipse.jface.viewers.Viewer; 36 37 38 44 public class ObjectClassSelectionDialogContentProvider implements IStructuredContentProvider 45 { 46 47 private SchemaPool schemaPool; 48 49 50 private List <ObjectClass> hiddenObjectClasses; 51 52 53 56 public ObjectClassSelectionDialogContentProvider( List <ObjectClass> hiddenObjectClasses ) 57 { 58 schemaPool = SchemaPool.getInstance(); 59 this.hiddenObjectClasses = hiddenObjectClasses; 60 } 61 62 63 66 public Object [] getElements( Object inputElement ) 67 { 68 if ( inputElement instanceof String ) 69 { 70 ArrayList <ObjectClassWrapper> results = new ArrayList <ObjectClassWrapper>(); 71 72 String searchText = ( String ) inputElement; 73 74 String searchRegexp; 75 if ( searchText.length() == 0 ) 76 { 77 searchRegexp = ".*"; } 79 else 80 { 81 searchRegexp = searchText + ".*"; } 83 Pattern pattern = Pattern.compile( searchRegexp, Pattern.CASE_INSENSITIVE ); 84 85 List <ObjectClass> ocList = schemaPool.getObjectClasses(); 86 87 Collections.sort( ocList, new Comparator <ObjectClass>() 89 { 90 public int compare( ObjectClass oc1, ObjectClass oc2 ) 91 { 92 if ( ( oc1.getNames() == null || oc1.getNames().length == 0 ) 93 && ( oc2.getNames() == null || oc2.getNames().length == 0 ) ) 94 { 95 return 0; 96 } 97 else if ( ( oc1.getNames() == null || oc1.getNames().length == 0 ) 98 && ( oc2.getNames() != null && oc2.getNames().length > 0 ) ) 99 { 100 return "".compareToIgnoreCase( oc2.getNames()[0] ); } 102 else if ( ( oc1.getNames() != null && oc1.getNames().length > 0 ) 103 && ( oc2.getNames() == null || oc2.getNames().length == 0 ) ) 104 { 105 return oc1.getNames()[0].compareToIgnoreCase( "" ); } 107 else 108 { 109 return oc1.getNames()[0].compareToIgnoreCase( oc2.getNames()[0] ); 110 } 111 } 112 } ); 113 114 for ( ObjectClass oc : ocList ) 116 { 117 for ( String name : oc.getNames() ) 118 { 119 Matcher m = pattern.matcher( name ); 120 if ( m.matches() ) 121 { 122 if ( !hiddenObjectClasses.contains( oc ) ) 123 { 124 ObjectClassWrapper ocw = new ObjectClassWrapper( oc, null ); 125 if ( !results.contains( ocw ) ) 126 { 127 results.add( new ObjectClassWrapper( oc, null ) ); 128 } 129 } 130 break; 131 } 132 } 133 Matcher m = pattern.matcher( oc.getOid() ); 134 if ( m.matches() ) 135 { 136 if ( !hiddenObjectClasses.contains( oc ) ) 137 { 138 ObjectClassWrapper ocw = new ObjectClassWrapper( oc, null ); 139 if ( !results.contains( ocw ) ) 140 { 141 results.add( new ObjectClassWrapper( oc, null ) ); 142 } 143 } 144 } 145 } 146 147 return results.toArray(); 149 } 150 151 return new Object [0]; 153 } 154 155 156 159 public void dispose() 160 { 161 } 162 163 164 167 public void inputChanged( Viewer viewer, Object oldInput, Object newInput ) 168 { 169 } 170 } 171 | Popular Tags |