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.AttributeType; 32 import org.apache.directory.ldapstudio.schemas.model.SchemaPool; 33 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper; 34 import org.eclipse.jface.viewers.IStructuredContentProvider; 35 import org.eclipse.jface.viewers.Viewer; 36 37 38 44 public class AttributeTypeSelectionDialogContentProvider implements IStructuredContentProvider 45 { 46 47 private SchemaPool schemaPool; 48 49 50 private List <AttributeType> hiddenAttributeTypes; 51 52 53 56 public AttributeTypeSelectionDialogContentProvider( List <AttributeType> hiddenAttributeTypes ) 57 { 58 schemaPool = SchemaPool.getInstance(); 59 this.hiddenAttributeTypes = hiddenAttributeTypes; 60 } 61 62 63 66 public Object [] getElements( Object inputElement ) 67 { 68 if ( inputElement instanceof String ) 69 { 70 ArrayList <AttributeTypeWrapper> results = new ArrayList <AttributeTypeWrapper>(); 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 <AttributeType> atList = schemaPool.getAttributeTypes(); 86 87 Collections.sort( atList, new Comparator <AttributeType>() 89 { 90 public int compare( AttributeType at1, AttributeType at2 ) 91 { 92 if ( ( at1.getNames() == null || at1.getNames().length == 0 ) 93 && ( at2.getNames() == null || at2.getNames().length == 0 ) ) 94 { 95 return 0; 96 } 97 else if ( ( at1.getNames() == null || at1.getNames().length == 0 ) 98 && ( at2.getNames() != null && at2.getNames().length > 0 ) ) 99 { 100 return "".compareToIgnoreCase( at2.getNames()[0] ); } 102 else if ( ( at1.getNames() != null && at1.getNames().length > 0 ) 103 && ( at2.getNames() == null || at2.getNames().length == 0 ) ) 104 { 105 return at1.getNames()[0].compareToIgnoreCase( "" ); } 107 else 108 { 109 return at1.getNames()[0].compareToIgnoreCase( at2.getNames()[0] ); 110 } 111 } 112 } ); 113 114 for ( AttributeType at : atList ) 116 { 117 for ( String name : at.getNames() ) 118 { 119 Matcher m = pattern.matcher( name ); 120 if ( m.matches() ) 121 { 122 if ( !hiddenAttributeTypes.contains( at ) ) 123 { 124 AttributeTypeWrapper atw = new AttributeTypeWrapper( at, null ); 125 if ( !results.contains( atw ) ) 126 { 127 results.add( new AttributeTypeWrapper( at, null ) ); 128 } 129 } 130 break; 131 } 132 } 133 Matcher m = pattern.matcher( at.getOid() ); 134 if ( m.matches() ) 135 { 136 if ( !hiddenAttributeTypes.contains( at ) ) 137 { 138 AttributeTypeWrapper atw = new AttributeTypeWrapper( at, null ); 139 if ( !results.contains( atw ) ) 140 { 141 results.add( new AttributeTypeWrapper( at, 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 |