1 20 package org.apache.directory.ldapstudio.aciitemeditor.valueeditors; 21 22 23 import java.util.Arrays ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 27 import org.apache.directory.ldapstudio.aciitemeditor.Activator; 28 import org.apache.directory.ldapstudio.browser.common.dialogs.TextDialog; 29 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 30 import org.apache.directory.ldapstudio.browser.common.widgets.ListContentProposalProvider; 31 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 32 import org.apache.directory.ldapstudio.browser.core.model.IValue; 33 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 34 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor; 35 import org.eclipse.jface.dialogs.Dialog; 36 import org.eclipse.jface.dialogs.IDialogConstants; 37 import org.eclipse.jface.fieldassist.ComboContentAdapter; 38 import org.eclipse.jface.fieldassist.ContentProposalAdapter; 39 import org.eclipse.jface.fieldassist.DecoratedField; 40 import org.eclipse.jface.fieldassist.FieldDecoration; 41 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 42 import org.eclipse.jface.fieldassist.IControlCreator; 43 import org.eclipse.swt.SWT; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Combo; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.swt.widgets.Control; 49 import org.eclipse.swt.widgets.Shell; 50 51 52 58 public class RestrictedByValueEditor extends AbstractDialogStringValueEditor 59 { 60 61 private static final String L_CURLY_TYPE = "{ type "; private static final String SEP_VALUESIN = ", valuesIn "; private static final String R_CURLY = " }"; private static final String EMPTY = ""; 66 67 72 public boolean openDialog( Shell shell ) 73 { 74 Object value = getValue(); 75 if ( value != null && value instanceof RestrictedByValueEditorRawValueWrapper ) 76 { 77 RestrictedByValueEditorRawValueWrapper wrapper = ( RestrictedByValueEditorRawValueWrapper ) value; 78 RestrictedByDialog dialog = new RestrictedByDialog( shell, wrapper.schema, wrapper.type, wrapper.valuesIn ); 79 if ( dialog.open() == TextDialog.OK && !EMPTY.equals( dialog.getType() ) 80 && !EMPTY.equals( dialog.getValuesIn() ) ) 81 { 82 setValue( L_CURLY_TYPE + dialog.getType() + SEP_VALUESIN + dialog.getValuesIn() + R_CURLY ); 83 return true; 84 } 85 } 86 return false; 87 } 88 89 90 95 public Object getRawValue( IValue value ) 96 { 97 return value != null ? getRawValue( value.getAttribute().getEntry().getConnection(), value.getStringValue() ) 98 : null; 99 } 100 101 102 107 public Object getRawValue( IConnection connection, Object value ) 108 { 109 Schema schema = null; 110 if ( connection != null ) 111 { 112 schema = connection.getSchema(); 113 } 114 if ( schema == null || value == null || !( value instanceof String ) ) 115 { 116 return null; 117 } 118 119 String stringValue = ( String ) value; 120 String type = EMPTY; 121 String valuesIn = EMPTY; 122 try 123 { 124 Pattern pattern = Pattern 126 .compile( "\\s*\\{\\s*type\\s*([^,\\s]*)\\s*,\\s*valuesIn\\s*([^,\\s]*)\\s*\\}\\s*" ); Matcher matcher = pattern.matcher( stringValue ); 128 type = matcher.matches() ? matcher.group( 1 ) : EMPTY; 129 valuesIn = matcher.matches() ? matcher.group( 2 ) : EMPTY; 130 } 131 catch ( Throwable e ) 132 { 133 e.printStackTrace(); 134 } 135 136 RestrictedByValueEditorRawValueWrapper wrapper = new RestrictedByValueEditorRawValueWrapper( schema, type, 137 valuesIn ); 138 return wrapper; 139 } 140 141 148 private class RestrictedByValueEditorRawValueWrapper 149 { 150 154 private Schema schema; 155 156 157 private String type; 158 159 160 private String valuesIn; 161 162 163 170 private RestrictedByValueEditorRawValueWrapper( Schema schema, String type, String valuesIn ) 171 { 172 this.schema = schema; 173 this.type = type; 174 this.valuesIn = valuesIn; 175 } 176 } 177 178 184 private class RestrictedByDialog extends Dialog 185 { 186 187 188 private Schema schema; 189 190 191 private String initialType; 192 193 194 private String initialValuesIn; 195 196 197 private DecoratedField typeComboField; 198 199 200 private Combo typeCombo; 201 202 203 private ContentProposalAdapter typeCPA; 204 205 206 private DecoratedField valuesInComboField; 207 208 209 private Combo valuesInCombo; 210 211 212 private ContentProposalAdapter valuesInCPA; 213 214 215 private String returnType; 216 217 218 private String returnValuesIn; 219 220 221 229 public RestrictedByDialog( Shell parentShell, Schema schema, String initialType, String initialValuesIn ) 230 { 231 super( parentShell ); 232 super.setShellStyle( super.getShellStyle() | SWT.RESIZE ); 233 this.initialType = initialType; 234 this.initialValuesIn = initialValuesIn; 235 this.schema = schema; 236 this.returnType = null; 237 this.returnValuesIn = null; 238 } 239 240 241 244 protected void configureShell( Shell shell ) 245 { 246 super.configureShell( shell ); 247 shell.setText( Messages.getString( "RestrictedByValueEditor.title" ) ); shell.setImage( Activator.getDefault().getImage( Messages.getString( "RestrictedByValueEditor.icon" ) ) ); } 250 251 252 255 protected void createButtonsForButtonBar( Composite parent ) 256 { 257 super.createButtonsForButtonBar( parent ); 258 } 259 260 261 264 protected void okPressed() 265 { 266 returnType = typeCombo.getText(); 267 returnValuesIn = valuesInCombo.getText(); 268 super.okPressed(); 269 } 270 271 272 275 protected Control createDialogArea( Composite parent ) 276 { 277 Composite composite = ( Composite ) super.createDialogArea( parent ); 279 GridData gd = new GridData( GridData.FILL_BOTH ); 280 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 281 composite.setLayoutData( gd ); 282 composite.setLayout( new GridLayout( 5, false ) ); 283 284 BaseWidgetUtils.createLabel( composite, L_CURLY_TYPE, 1 ); 285 286 String [] allAtNames = schema.getAttributeTypeDescriptionNames(); 288 Arrays.sort( allAtNames ); 289 290 final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration( 291 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL ); 292 293 typeComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator() 294 { 295 public Control createControl( Composite parent, int style ) 296 { 297 Combo combo = BaseWidgetUtils.createCombo( parent, new String [0], -1, 1 ); 298 combo.setVisibleItemCount( 20 ); 299 return combo; 300 } 301 } ); 302 typeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true ); 303 typeComboField.getLayoutControl().setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) ); 304 typeCombo = ( Combo ) typeComboField.getControl(); 305 typeCombo.setItems( allAtNames ); 306 typeCombo.setText( initialType ); 307 308 typeCPA = new ContentProposalAdapter( typeCombo, new ComboContentAdapter(), 310 new ListContentProposalProvider( typeCombo.getItems() ), null, null ); 311 typeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE ); 312 typeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE ); 313 314 BaseWidgetUtils.createLabel( composite, SEP_VALUESIN, 1 ); 315 316 valuesInComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator() 317 { 318 public Control createControl( Composite parent, int style ) 319 { 320 Combo combo = BaseWidgetUtils.createCombo( parent, new String [0], -1, 1 ); 321 combo.setVisibleItemCount( 20 ); 322 return combo; 323 } 324 } ); 325 valuesInComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true ); 326 valuesInComboField.getLayoutControl().setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) ); 327 valuesInCombo = ( Combo ) valuesInComboField.getControl(); 328 valuesInCombo.setItems( allAtNames ); 329 valuesInCombo.setText( initialValuesIn ); 330 331 valuesInCPA = new ContentProposalAdapter( valuesInCombo, new ComboContentAdapter(), 333 new ListContentProposalProvider( valuesInCombo.getItems() ), null, null ); 334 valuesInCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE ); 335 valuesInCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE ); 336 337 BaseWidgetUtils.createLabel( composite, R_CURLY, 1 ); 338 339 applyDialogFont( composite ); 340 return composite; 341 } 342 343 344 349 public String getType() 350 { 351 return returnType; 352 } 353 354 355 360 public String getValuesIn() 361 { 362 return returnValuesIn; 363 } 364 365 } 366 367 } 368 | Popular Tags |