1 20 21 package org.apache.directory.ldapstudio.browser.common.wizards; 22 23 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 28 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 29 import org.apache.directory.ldapstudio.browser.common.widgets.ListContentProposalProvider; 30 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 31 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 32 import org.eclipse.jface.fieldassist.ComboContentAdapter; 33 import org.eclipse.jface.fieldassist.ContentProposalAdapter; 34 import org.eclipse.jface.fieldassist.DecoratedField; 35 import org.eclipse.jface.fieldassist.FieldDecoration; 36 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 37 import org.eclipse.jface.fieldassist.IControlCreator; 38 import org.eclipse.jface.wizard.WizardPage; 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.events.ModifyEvent; 41 import org.eclipse.swt.events.ModifyListener; 42 import org.eclipse.swt.events.SelectionAdapter; 43 import org.eclipse.swt.events.SelectionEvent; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Button; 47 import org.eclipse.swt.widgets.Combo; 48 import org.eclipse.swt.widgets.Composite; 49 import org.eclipse.swt.widgets.Control; 50 import org.eclipse.swt.widgets.Label; 51 import org.eclipse.swt.widgets.Text; 52 53 54 61 public class AttributeTypeWizardPage extends WizardPage 62 { 63 64 65 private AttributeWizard wizard; 66 67 68 private boolean initialShowSubschemaAttributesOnly; 69 70 71 private boolean initialHideExistingAttributes; 72 73 74 private String parsedAttributeType; 75 76 77 private String [] possibleAttributeTypes; 78 79 80 private String [] possibleAttributeTypesSubschemaOnly; 81 82 83 private String [] possibleAttributeTypesSubschemaOnlyAndExistingHidden; 84 85 86 private DecoratedField attributeTypeComboField; 87 88 89 private Combo attributeTypeCombo; 90 91 92 private ContentProposalAdapter attributeTypeCPA; 93 94 95 private Button showSubschemAttributesOnlyButton; 96 97 98 private Button hideExistingAttributesButton; 99 100 101 private Text previewText; 102 103 104 114 public AttributeTypeWizardPage( String pageName, IEntry initialEntry, String initialAttributeDescription, 115 boolean initialShowSubschemaAttributesOnly, boolean initialHideExistingAttributes, AttributeWizard wizard ) 116 { 117 super( pageName ); 118 super.setTitle( "Attribute Type" ); 119 super.setDescription( "Please enter or select the attribute type." ); 120 super.setPageComplete( false ); 122 123 this.wizard = wizard; 124 this.initialShowSubschemaAttributesOnly = initialShowSubschemaAttributesOnly; 125 this.initialHideExistingAttributes = initialHideExistingAttributes; 126 127 possibleAttributeTypes = initialEntry.getConnection().getSchema().getAttributeTypeDescriptionNames(); 128 Arrays.sort( possibleAttributeTypes ); 129 possibleAttributeTypesSubschemaOnly = initialEntry.getSubschema().getAllAttributeNames(); 130 Arrays.sort( possibleAttributeTypesSubschemaOnly ); 131 132 Set <String > set = new HashSet <String >( Arrays.asList( initialEntry.getSubschema().getAllAttributeNames() ) ); 133 IAttribute[] existingAttributes = initialEntry.getAttributes(); 134 for ( int i = 0; existingAttributes != null && i < existingAttributes.length; i++ ) 135 { 136 set.remove( existingAttributes[i].getDescription() ); 137 } 138 possibleAttributeTypesSubschemaOnlyAndExistingHidden = ( String [] ) set.toArray( new String [set.size()] ); 139 Arrays.sort( possibleAttributeTypesSubschemaOnlyAndExistingHidden ); 140 141 String attributeDescription = initialAttributeDescription; 142 if ( attributeDescription == null ) 143 { 144 attributeDescription = ""; 145 } 146 String [] attributeDescriptionComponents = attributeDescription.split( ";" ); 147 parsedAttributeType = attributeDescriptionComponents[0]; 148 } 149 150 151 154 private void validate() 155 { 156 previewText.setText( wizard.getAttributeDescription() ); 157 setPageComplete( !"".equals( attributeTypeCombo.getText() ) ); 158 } 159 160 161 164 public void setVisible( boolean visible ) 165 { 166 super.setVisible( visible ); 167 if ( visible ) 168 { 169 validate(); 170 } 171 } 172 173 174 177 public void createControl( Composite parent ) 178 { 179 Composite composite = new Composite( parent, SWT.NONE ); 180 GridLayout gl = new GridLayout( 2, false ); 181 composite.setLayout( gl ); 182 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 183 184 BaseWidgetUtils.createLabel( composite, "Attribute type:", 1 ); 185 188 final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration( 190 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL ); 191 attributeTypeComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator() 192 { 193 public Control createControl( Composite parent, int style ) 194 { 195 Combo combo = BaseWidgetUtils.createCombo( parent, new String [0], -1, 1 ); 196 combo.setVisibleItemCount( 20 ); 197 return combo; 198 } 199 } ); 200 attributeTypeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true ); 201 attributeTypeComboField.getLayoutControl().setLayoutData( 202 new GridData( SWT.FILL, SWT.CENTER, true, false ) ); 203 attributeTypeCombo = ( Combo ) attributeTypeComboField.getControl(); 204 attributeTypeCombo.setItems( possibleAttributeTypes ); 205 attributeTypeCombo.setText( parsedAttributeType ); 206 207 attributeTypeCPA = new ContentProposalAdapter (attributeTypeCombo, new ComboContentAdapter(), 209 new ListContentProposalProvider( attributeTypeCombo.getItems() ), null, null ); 210 attributeTypeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE ); 211 attributeTypeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE ); 212 213 BaseWidgetUtils.createSpacer( composite, 1 ); 214 showSubschemAttributesOnlyButton = BaseWidgetUtils.createCheckbox( composite, "Show subschema attributes only", 215 1 ); 216 showSubschemAttributesOnlyButton.setSelection( initialShowSubschemaAttributesOnly ); 217 218 BaseWidgetUtils.createSpacer( composite, 1 ); 219 hideExistingAttributesButton = BaseWidgetUtils.createCheckbox( composite, "Hide existing attributes", 1 ); 220 hideExistingAttributesButton.setSelection( initialHideExistingAttributes ); 221 222 Label l = new Label( composite, SWT.NONE ); 223 GridData gd = new GridData( GridData.FILL_BOTH ); 224 gd.horizontalSpan = 2; 225 l.setLayoutData( gd ); 226 227 BaseWidgetUtils.createLabel( composite, "Preview:", 1 ); 228 previewText = BaseWidgetUtils.createReadonlyText( composite, "", 1 ); 229 230 attributeTypeCombo.addModifyListener( new ModifyListener() 232 { 233 public void modifyText( ModifyEvent e ) 234 { 235 validate(); 236 } 237 } ); 238 239 showSubschemAttributesOnlyButton.addSelectionListener( new SelectionAdapter() 241 { 242 public void widgetSelected( SelectionEvent e ) 243 { 244 updateFilter(); 245 validate(); 246 } 247 } ); 248 hideExistingAttributesButton.addSelectionListener( new SelectionAdapter() 249 { 250 public void widgetSelected( SelectionEvent e ) 251 { 252 updateFilter(); 253 validate(); 254 } 255 } ); 256 updateFilter(); 257 258 setControl( composite ); 259 } 260 261 262 265 private void updateFilter() 266 { 267 hideExistingAttributesButton.setEnabled( showSubschemAttributesOnlyButton.getSelection() ); 269 if ( possibleAttributeTypesSubschemaOnly.length == 0 ) 270 { 271 showSubschemAttributesOnlyButton.setSelection( false ); 272 showSubschemAttributesOnlyButton.setEnabled( false ); 273 } 274 if ( possibleAttributeTypesSubschemaOnlyAndExistingHidden.length == 0 ) 275 { 276 hideExistingAttributesButton.setEnabled( false ); 277 hideExistingAttributesButton.setSelection( false ); 278 } 279 280 String value = attributeTypeCombo.getText(); 282 if ( hideExistingAttributesButton.getSelection() && showSubschemAttributesOnlyButton.getSelection() ) 283 { 284 attributeTypeCombo.setItems( possibleAttributeTypesSubschemaOnlyAndExistingHidden ); 285 } 286 else if ( showSubschemAttributesOnlyButton.getSelection() ) 287 { 288 attributeTypeCombo.setItems( possibleAttributeTypesSubschemaOnly ); 289 } 290 else 291 { 292 attributeTypeCombo.setItems( possibleAttributeTypes ); 293 } 294 attributeTypeCPA.setContentProposalProvider( new ListContentProposalProvider( attributeTypeCombo.getItems() ) ); 295 attributeTypeCombo.setText( value ); 296 } 297 298 299 304 String getAttributeType() 305 { 306 if ( attributeTypeCombo == null | attributeTypeCombo.isDisposed() ) 307 { 308 return ""; 309 } 310 311 return attributeTypeCombo.getText(); 312 } 313 314 } 315 | Popular Tags |