1 20 21 package org.apache.directory.ldapstudio.aciitemeditor.valueeditors; 22 23 24 import java.util.Arrays ; 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 27 28 import org.apache.directory.ldapstudio.aciitemeditor.Activator; 29 import org.apache.directory.ldapstudio.browser.common.dialogs.TextDialog; 30 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 31 import org.apache.directory.ldapstudio.browser.common.widgets.ListContentProposalProvider; 32 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 33 import org.apache.directory.ldapstudio.browser.core.model.IValue; 34 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 35 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor; 36 import org.eclipse.jface.dialogs.Dialog; 37 import org.eclipse.jface.dialogs.IDialogConstants; 38 import org.eclipse.jface.fieldassist.ComboContentAdapter; 39 import org.eclipse.jface.fieldassist.ContentProposalAdapter; 40 import org.eclipse.jface.fieldassist.DecoratedField; 41 import org.eclipse.jface.fieldassist.FieldDecoration; 42 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 43 import org.eclipse.jface.fieldassist.IControlCreator; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.layout.GridData; 46 import org.eclipse.swt.layout.GridLayout; 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.Shell; 51 import org.eclipse.swt.widgets.Spinner; 52 53 54 60 public class MaxValueCountValueEditor extends AbstractDialogStringValueEditor 61 { 62 63 private static final String L_CURLY_TYPE = "{ type "; private static final String SEP_MAXCOUNT = ", maxCount "; private static final String R_CURLY = " }"; private static final String EMPTY = ""; 68 69 74 public boolean openDialog( Shell shell ) 75 { 76 Object value = getValue(); 77 if ( value != null && value instanceof MaxValueCountValueEditorRawValueWrapper ) 78 { 79 MaxValueCountValueEditorRawValueWrapper wrapper = ( MaxValueCountValueEditorRawValueWrapper ) value; 80 MaxValueCountDialog dialog = new MaxValueCountDialog( shell, wrapper.schema, wrapper.type, wrapper.maxCount ); 81 if ( dialog.open() == TextDialog.OK && !EMPTY.equals( dialog.getType() ) && dialog.getMaxCount() > -1 ) 82 { 83 setValue( L_CURLY_TYPE + dialog.getType() + SEP_MAXCOUNT + dialog.getMaxCount() + R_CURLY ); 84 return true; 85 } 86 } 87 return false; 88 } 89 90 91 96 public Object getRawValue( IValue value ) 97 { 98 return value != null ? getRawValue( value.getAttribute().getEntry().getConnection(), value.getStringValue() ) 99 : null; 100 } 101 102 103 108 public Object getRawValue( IConnection connection, Object value ) 109 { 110 Schema schema = null; 111 if ( connection != null ) 112 { 113 schema = connection.getSchema(); 114 } 115 if ( schema == null || value == null || !( value instanceof String ) ) 116 { 117 return null; 118 } 119 120 String stringValue = ( String ) value; 121 String type = EMPTY; 122 int maxCount = 0; 123 try 124 { 125 Pattern pattern = Pattern.compile( "\\s*\\{\\s*type\\s*([^,]*),\\s*maxCount\\s*(\\d*)\\s*\\}\\s*" ); Matcher matcher = pattern.matcher( stringValue ); 128 type = matcher.matches() ? matcher.group( 1 ) : EMPTY; 129 maxCount = matcher.matches() ? Integer.valueOf( matcher.group( 2 ) ) : 0; 130 } 131 catch ( Exception e ) 132 { 133 } 134 135 MaxValueCountValueEditorRawValueWrapper wrapper = new MaxValueCountValueEditorRawValueWrapper( schema, type, 136 maxCount ); 137 return wrapper; 138 } 139 140 147 private class MaxValueCountValueEditorRawValueWrapper 148 { 149 153 private Schema schema; 154 155 156 private String type; 157 158 159 private int maxCount; 160 161 162 169 private MaxValueCountValueEditorRawValueWrapper( Schema schema, String type, int maxCount ) 170 { 171 this.schema = schema; 172 this.type = type; 173 this.maxCount = maxCount; 174 } 175 } 176 177 183 private class MaxValueCountDialog extends Dialog 184 { 185 186 187 private Schema schema; 188 189 190 private String initialType; 191 192 193 private int initialMaxCount; 194 195 196 private DecoratedField attributeTypeComboField; 197 198 199 private Combo attributeTypeCombo; 200 201 202 private ContentProposalAdapter attributeTypeCPA; 203 204 205 private Spinner maxCountSpinner; 206 207 208 private String returnType; 209 210 211 private int returnMaxCount; 212 213 214 222 public MaxValueCountDialog( Shell parentShell, Schema schema, String initialType, int initialMaxCount ) 223 { 224 super( parentShell ); 225 super.setShellStyle( super.getShellStyle() | SWT.RESIZE ); 226 this.initialType = initialType; 227 this.initialMaxCount = initialMaxCount; 228 this.schema = schema; 229 this.returnType = null; 230 this.returnMaxCount = -1; 231 } 232 233 234 237 protected void configureShell( Shell shell ) 238 { 239 super.configureShell( shell ); 240 shell.setText( Messages.getString( "MaxValueCountValueEditor.title" ) ); shell.setImage( Activator.getDefault().getImage( Messages.getString( "MaxValueCountValueEditor.icon" ) ) ); } 243 244 245 248 protected void createButtonsForButtonBar( Composite parent ) 249 { 250 super.createButtonsForButtonBar( parent ); 251 } 252 253 254 257 protected void okPressed() 258 { 259 returnType = attributeTypeCombo.getText(); 260 returnMaxCount = maxCountSpinner.getSelection(); 261 super.okPressed(); 262 } 263 264 265 268 protected Control createDialogArea( Composite parent ) 269 { 270 Composite composite = ( Composite ) super.createDialogArea( parent ); 272 GridData gd = new GridData( GridData.FILL_BOTH ); 273 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 274 composite.setLayoutData( gd ); 275 composite.setLayout( new GridLayout( 5, false ) ); 276 277 BaseWidgetUtils.createLabel( composite, L_CURLY_TYPE, 1 ); 278 279 String [] allAtNames = schema.getAttributeTypeDescriptionNames(); 281 Arrays.sort( allAtNames ); 282 283 final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration( 284 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL ); 285 attributeTypeComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator() 286 { 287 public Control createControl( Composite parent, int style ) 288 { 289 Combo combo = BaseWidgetUtils.createCombo( parent, new String [0], -1, 1 ); 290 combo.setVisibleItemCount( 20 ); 291 return combo; 292 } 293 } ); 294 attributeTypeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true ); 295 attributeTypeComboField.getLayoutControl() 296 .setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) ); 297 attributeTypeCombo = ( Combo ) attributeTypeComboField.getControl(); 298 attributeTypeCombo.setItems( allAtNames ); 299 attributeTypeCombo.setText( initialType ); 300 301 attributeTypeCPA = new ContentProposalAdapter( attributeTypeCombo, new ComboContentAdapter(), 303 new ListContentProposalProvider( attributeTypeCombo.getItems() ), null, null ); 304 attributeTypeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE ); 305 attributeTypeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE ); 306 307 BaseWidgetUtils.createLabel( composite, SEP_MAXCOUNT, 1 ); 308 309 maxCountSpinner = new Spinner( composite, SWT.BORDER ); 310 maxCountSpinner.setMinimum( 0 ); 311 maxCountSpinner.setMaximum( Integer.MAX_VALUE ); 312 maxCountSpinner.setDigits( 0 ); 313 maxCountSpinner.setIncrement( 1 ); 314 maxCountSpinner.setPageIncrement( 100 ); 315 maxCountSpinner.setSelection( initialMaxCount ); 316 maxCountSpinner.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 317 318 BaseWidgetUtils.createLabel( composite, R_CURLY, 1 ); 319 320 applyDialogFont( composite ); 321 return composite; 322 } 323 324 325 330 public String getType() 331 { 332 return returnType; 333 } 334 335 336 341 public int getMaxCount() 342 { 343 return returnMaxCount; 344 } 345 346 } 347 348 } 349 | Popular Tags |