1 package org.apache.directory.ldapstudio.aciitemeditor.valueeditors; 2 3 4 import java.util.regex.Matcher ; 5 import java.util.regex.Pattern ; 6 7 import org.apache.directory.ldapstudio.aciitemeditor.Activator; 8 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 9 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent; 10 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener; 11 import org.apache.directory.ldapstudio.browser.common.widgets.search.EntryWidget; 12 import org.apache.directory.ldapstudio.browser.core.model.DN; 13 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 14 import org.apache.directory.ldapstudio.browser.core.model.NameException; 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.dialogs.IDialogConstants; 17 import org.eclipse.jface.viewers.ArrayContentProvider; 18 import org.eclipse.jface.viewers.ComboViewer; 19 import org.eclipse.jface.viewers.LabelProvider; 20 import org.eclipse.jface.viewers.StructuredSelection; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Combo; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Shell; 28 29 30 36 class ExclusionDialog extends Dialog 37 { 38 39 40 private IConnection connection; 41 42 43 private DN base; 44 45 46 private String initialType; 47 48 49 private String initalDN; 50 51 52 private String returnType; 53 54 55 private String returnDN; 56 57 private static final String EMPTY = ""; private static final String CHOP_BEFORE = "chopBefore"; private static final String CHOP_AFTER = "chopAfter"; 61 private Combo typeCombo; 63 private ComboViewer typeComboViewer; 64 private EntryWidget entryWidget; 65 66 67 75 protected ExclusionDialog( Shell parentShell, IConnection connection, DN base, String exclusion ) 76 { 77 super( parentShell ); 78 this.connection = connection; 79 this.base = base; 80 81 try 82 { 83 Pattern pattern = Pattern.compile( "\\s*(chopBefore|chopAfter):\\s*\"(.*)\"\\s*" ); Matcher matcher = pattern.matcher( exclusion ); 86 initialType = matcher.matches() ? matcher.group( 1 ) : EMPTY; 87 initalDN = matcher.matches() ? matcher.group( 2 ) : EMPTY; 88 } 89 catch ( Exception e ) 90 { 91 initialType = EMPTY; 92 initalDN = EMPTY; 93 } 94 } 95 96 97 100 protected void configureShell( Shell shell ) 101 { 102 super.configureShell( shell ); 103 shell.setText( Messages.getString( "ExclusionValueEditor.title" ) ); shell.setImage( Activator.getDefault().getImage( Messages.getString( "ExclusionValueEditor.icon" ) ) ); } 106 107 108 111 protected void okPressed() 112 { 113 returnType = typeCombo.getText(); 114 returnDN = entryWidget.getDn().toString(); 115 116 entryWidget.saveDialogSettings(); 118 119 super.okPressed(); 120 } 121 122 123 126 protected Control createDialogArea( Composite parent ) 127 { 128 Composite composite = ( Composite ) super.createDialogArea( parent ); 129 GridData gd = new GridData( GridData.FILL_BOTH ); 130 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 131 composite.setLayoutData( gd ); 132 composite.setLayout( new GridLayout( 3, false ) ); 133 134 BaseWidgetUtils.createLabel( composite, Messages.getString( "ExclusionValueEditor.label.type" ), 1 ); typeCombo = new Combo( composite, SWT.READ_ONLY ); 136 String [] types = new String [2]; 137 types[0] = CHOP_BEFORE; 138 types[1] = CHOP_AFTER; 139 typeComboViewer = new ComboViewer( typeCombo ); 140 typeComboViewer.setContentProvider( new ArrayContentProvider() ); 141 typeComboViewer.setLabelProvider( new LabelProvider() ); 142 typeComboViewer.setInput( types ); 143 typeComboViewer.setSelection( new StructuredSelection( CHOP_BEFORE ), true ); 144 typeComboViewer.setSelection( new StructuredSelection( initialType ), true ); 145 GridData gridData = new GridData(); 146 gridData.horizontalSpan = 2; 147 gridData.grabExcessHorizontalSpace = true; 148 gridData.verticalAlignment = GridData.CENTER; 149 gridData.horizontalAlignment = GridData.BEGINNING; 150 typeCombo.setLayoutData( gridData ); 151 152 BaseWidgetUtils.createLabel( composite, Messages.getString( "ExclusionValueEditor.label.rdn" ), 1 ); entryWidget = new EntryWidget( connection, null, base ); 154 entryWidget.createWidget( composite ); 155 try 156 { 157 DN dn = new DN( initalDN ); 158 entryWidget.setInput( connection, dn, base ); 159 } 160 catch ( NameException e ) 161 { 162 } 163 entryWidget.addWidgetModifyListener( new WidgetModifyListener() 164 { 165 public void widgetModified( WidgetModifyEvent event ) 166 { 167 validate(); 168 } 169 } ); 170 171 validate(); 172 173 return composite; 174 } 175 176 177 180 private void validate() 181 { 182 boolean valid = entryWidget.getDn() != null && entryWidget.getDn().getRdns().length > 0; 183 184 if ( getButton( IDialogConstants.OK_ID ) != null ) 185 { 186 getButton( IDialogConstants.OK_ID ).setEnabled( valid ); 187 } 188 } 189 190 191 197 public String getType() 198 { 199 return returnType; 200 } 201 202 203 209 public String getDN() 210 { 211 return returnDN; 212 } 213 } | Popular Tags |