1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 27 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 28 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 29 import org.eclipse.jface.fieldassist.ComboContentAdapter; 30 import org.eclipse.jface.fieldassist.ContentProposalAdapter; 31 import org.eclipse.jface.fieldassist.DecoratedField; 32 import org.eclipse.jface.fieldassist.FieldDecoration; 33 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 34 import org.eclipse.jface.fieldassist.IControlCreator; 35 import org.eclipse.swt.SWT; 36 import org.eclipse.swt.events.ModifyEvent; 37 import org.eclipse.swt.events.ModifyListener; 38 import org.eclipse.swt.events.SelectionAdapter; 39 import org.eclipse.swt.events.SelectionEvent; 40 import org.eclipse.swt.layout.GridData; 41 import org.eclipse.swt.widgets.Button; 42 import org.eclipse.swt.widgets.Combo; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Group; 46 import org.eclipse.swt.widgets.Shell; 47 import org.eclipse.swt.widgets.Text; 48 49 50 57 public class ModWidget extends BrowserWidget implements ModifyListener 58 { 59 60 61 private Schema schema; 62 63 64 private Shell shell; 65 66 67 private Composite modComposite; 68 69 70 private ArrayList <ModSpec> modSpecList; 71 72 73 private String ldif; 74 75 76 81 public ModWidget( Schema schema ) 82 { 83 this.schema = schema; 84 this.modSpecList = new ArrayList <ModSpec>(); 85 this.ldif = null; 86 } 87 88 89 92 public void dispose() 93 { 94 } 95 96 97 102 public String getLdif() 103 { 104 return ldif; 105 } 106 107 108 115 public Composite createContents( Composite parent ) 116 { 117 shell = parent.getShell(); 118 119 modComposite = BaseWidgetUtils.createColumnContainer( parent, 3, 1 ); 120 addModSpec( modComposite, 0 ); 121 122 return modComposite; 123 } 124 125 126 129 public void modifyText( ModifyEvent e ) 130 { 131 validate(); 132 } 133 134 135 138 public void validate() 139 { 140 for ( int i = 0; i < modSpecList.size(); i++ ) 141 { 142 ModSpec modSpec = ( ModSpec ) modSpecList.get( i ); 143 if ( modSpecList.size() > 1 ) 144 { 145 modSpec.modDeleteButton.setEnabled( true ); 146 } 147 else 148 { 149 modSpec.modDeleteButton.setEnabled( false ); 150 } 151 for ( int k = 0; k < modSpec.valueLineList.size(); k++ ) 152 { 153 ValueLine valueLine = ( ValueLine ) modSpec.valueLineList.get( k ); 154 if ( modSpec.valueLineList.size() > 1 ) 155 { 156 valueLine.valueDeleteButton.setEnabled( true ); 157 } 158 else 159 { 160 valueLine.valueDeleteButton.setEnabled( false ); 161 } 162 } 163 } 164 165 notifyListeners(); 166 } 167 168 169 175 private void addModSpec( Composite modComposite, int index ) 176 { 177 178 ModSpec[] modSpecs = ( ModSpec[] ) modSpecList.toArray( new ModSpec[modSpecList.size()] ); 179 180 if ( modSpecs.length > 0 ) 181 { 182 for ( int i = 0; i < modSpecs.length; i++ ) 183 { 184 ModSpec oldModSpec = modSpecs[i]; 185 186 String oldType = oldModSpec.modType.getText(); 188 String oldAttribute = oldModSpec.modAttributeCombo.getText(); 189 String [] oldValues = new String [oldModSpec.valueLineList.size()]; 190 for ( int k = 0; k < oldValues.length; k++ ) 191 { 192 oldValues[k] = ( ( ValueLine ) oldModSpec.valueLineList.get( k ) ).valueText.getText(); 193 } 194 195 oldModSpec.modGroup.dispose(); 197 oldModSpec.modAddButton.dispose(); 198 oldModSpec.modDeleteButton.dispose(); 199 modSpecList.remove( oldModSpec ); 200 201 ModSpec newModSpec = createModSpec( modComposite ); 203 modSpecList.add( newModSpec ); 204 205 newModSpec.modType.setText( oldType ); 207 newModSpec.modAttributeCombo.setText( oldAttribute ); 208 deleteValueLine( newModSpec, 0 ); 209 for ( int k = 0; k < oldValues.length; k++ ) 210 { 211 addValueLine( newModSpec, k ); 212 ValueLine newValueLine = ( ValueLine ) newModSpec.valueLineList.get( k ); 213 newValueLine.valueText.setText( oldValues[k] ); 214 } 215 216 if ( index == i + 1 ) 218 { 219 ModSpec modSpec = createModSpec( modComposite ); 220 modSpecList.add( modSpec ); 221 } 222 } 223 } 224 else 225 { 226 ModSpec modSpec = createModSpec( modComposite ); 227 modSpecList.add( modSpec ); 228 } 229 230 shell.layout( true, true ); 231 } 232 233 234 241 private ModSpec createModSpec( final Composite modComposite ) 242 { 243 final ModSpec modSpec = new ModSpec(); 244 245 modSpec.modGroup = BaseWidgetUtils.createGroup( modComposite, "", 1 ); 246 Composite modSpecComposite = BaseWidgetUtils.createColumnContainer( modSpec.modGroup, 2, 1 ); 247 modSpec.modType = BaseWidgetUtils.createCombo( modSpecComposite, new String [] 248 { "add", "replace", "delete" }, 0, 1 ); 249 modSpec.modType.addModifyListener( this ); 250 String [] attributeDescriptions = schema.getAttributeTypeDescriptionNames(); 251 Arrays.sort( attributeDescriptions ); 252 253 final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration( 255 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL ); 256 modSpec.modAttributeComboField = new DecoratedField( modSpecComposite, SWT.NONE, new IControlCreator() 257 { 258 public Control createControl( Composite parent, int style ) 259 { 260 Combo combo = BaseWidgetUtils.createCombo( parent, new String [0], -1, 1 ); 261 combo.setVisibleItemCount( 20 ); 262 return combo; 263 } 264 } ); 265 modSpec.modAttributeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true ); 266 modSpec.modAttributeComboField.getLayoutControl().setLayoutData( 267 new GridData( SWT.FILL, SWT.CENTER, true, false ) ); 268 modSpec.modAttributeCombo = ( Combo ) modSpec.modAttributeComboField.getControl(); 269 modSpec.modAttributeCombo.setItems( attributeDescriptions ); 270 modSpec.modAttributeCombo.addModifyListener( this ); 271 272 modSpec.modAttributeCPA = new ContentProposalAdapter( modSpec.modAttributeCombo, new ComboContentAdapter(), 274 new ListContentProposalProvider( attributeDescriptions ), null, null ); 275 modSpec.modAttributeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE ); 276 modSpec.modAttributeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE ); 277 278 modSpec.modAddButton = new Button( modComposite, SWT.PUSH ); 280 modSpec.modAddButton.setText( " + " ); 281 modSpec.modAddButton.addSelectionListener( new SelectionAdapter() 282 { 283 public void widgetSelected( SelectionEvent e ) 284 { 285 int index = modSpecList.size(); 286 for ( int i = 0; i < modSpecList.size(); i++ ) 287 { 288 ModSpec modSpec = modSpecList.get( i ); 289 if ( modSpec.modAddButton == e.widget ) 290 { 291 index = i + 1; 292 } 293 } 294 295 addModSpec( modComposite, index ); 296 297 validate(); 298 } 299 } ); 300 301 modSpec.modDeleteButton = new Button( modComposite, SWT.PUSH ); 303 modSpec.modDeleteButton.setText( " \u2212 " ); modSpec.modDeleteButton.addSelectionListener( new SelectionAdapter() 305 { 306 public void widgetSelected( SelectionEvent e ) 307 { 308 int index = 0; 309 for ( int i = 0; i < modSpecList.size(); i++ ) 310 { 311 ModSpec modSpec = modSpecList.get( i ); 312 if ( modSpec.modDeleteButton == e.widget ) 313 { 314 index = i; 315 } 316 } 317 318 deleteModSpec( modComposite, index ); 319 320 validate(); 321 } 322 } ); 323 324 addValueLine( modSpec, 0 ); 325 326 return modSpec; 327 } 328 329 330 336 private void deleteModSpec( Composite modComposite, int index ) 337 { 338 ModSpec modSpec = modSpecList.remove( index ); 339 if ( modSpec != null ) 340 { 341 modSpec.modGroup.dispose(); 342 modSpec.modAddButton.dispose(); 343 modSpec.modDeleteButton.dispose(); 344 345 if ( !modComposite.isDisposed() ) 346 { 347 shell.layout( true, true ); 348 } 349 } 350 } 351 352 353 359 private void addValueLine( ModSpec modSpec, int index ) 360 { 361 362 ValueLine[] valueLines = modSpec.valueLineList.toArray( new ValueLine[modSpec.valueLineList.size()] ); 363 364 if ( valueLines.length > 0 ) 365 { 366 for ( int i = 0; i < valueLines.length; i++ ) 367 { 368 ValueLine oldValueLine = valueLines[i]; 369 370 String oldValue = oldValueLine.valueText.getText(); 372 373 oldValueLine.valueComposite.dispose(); 375 modSpec.valueLineList.remove( oldValueLine ); 376 377 ValueLine newValueLine = createValueLine( modSpec ); 379 modSpec.valueLineList.add( newValueLine ); 380 381 newValueLine.valueText.setText( oldValue ); 383 384 if ( index == i + 1 ) 386 { 387 ValueLine valueLine = createValueLine( modSpec ); 388 modSpec.valueLineList.add( valueLine ); 389 } 390 } 391 } 392 else 393 { 394 ValueLine valueLine = createValueLine( modSpec ); 395 modSpec.valueLineList.add( valueLine ); 396 } 397 398 shell.layout( true, true ); 399 } 400 401 402 409 private ValueLine createValueLine( final ModSpec modSpec ) 410 { 411 final ValueLine valueLine = new ValueLine(); 412 413 valueLine.valueComposite = BaseWidgetUtils.createColumnContainer( modSpec.modGroup, 3, 1 ); 415 valueLine.valueText = BaseWidgetUtils.createText( valueLine.valueComposite, "", 1 ); 416 valueLine.valueText.addModifyListener( this ); 417 418 valueLine.valueAddButton = new Button( valueLine.valueComposite, SWT.PUSH ); 420 valueLine.valueAddButton.setText( " + " ); 421 valueLine.valueAddButton.addSelectionListener( new SelectionAdapter() 422 { 423 public void widgetSelected( SelectionEvent e ) 424 { 425 int index = modSpec.valueLineList.size(); 426 for ( int i = 0; i < modSpec.valueLineList.size(); i++ ) 427 { 428 ValueLine valueLine = modSpec.valueLineList.get( i ); 429 if ( valueLine.valueAddButton == e.widget ) 430 { 431 index = i + 1; 432 } 433 } 434 435 addValueLine( modSpec, index ); 436 437 validate(); 438 } 439 } ); 440 441 valueLine.valueDeleteButton = new Button( valueLine.valueComposite, SWT.PUSH ); 443 valueLine.valueDeleteButton.setText( " \u2212 " ); valueLine.valueDeleteButton.addSelectionListener( new SelectionAdapter() 445 { 446 public void widgetSelected( SelectionEvent e ) 447 { 448 int index = 0; 449 for ( int i = 0; i < modSpec.valueLineList.size(); i++ ) 450 { 451 ValueLine valueLine = modSpec.valueLineList.get( i ); 452 if ( valueLine.valueDeleteButton == e.widget ) 453 { 454 index = i; 455 } 456 } 457 458 deleteValueLine( modSpec, index ); 459 460 validate(); 461 } 462 } ); 463 464 return valueLine; 465 } 466 467 468 474 private void deleteValueLine( ModSpec modSpec, int index ) 475 { 476 ValueLine valueLine = ( ValueLine ) modSpec.valueLineList.remove( index ); 477 if ( valueLine != null ) 478 { 479 valueLine.valueComposite.dispose(); 480 481 if ( !modComposite.isDisposed() ) 482 { 483 shell.layout( true, true ); 484 } 485 } 486 } 487 488 489 494 public String getLdifFragment() 495 { 496 497 StringBuffer sb = new StringBuffer (); 498 sb.append( "changetype: modify" ).append( BrowserCoreConstants.LINE_SEPARATOR ); 499 500 ModSpec[] modSpecs = ( ModSpec[] ) modSpecList.toArray( new ModSpec[modSpecList.size()] ); 501 502 if ( modSpecs.length > 0 ) 503 { 504 for ( int i = 0; i < modSpecs.length; i++ ) 505 { 506 ModSpec modSpec = modSpecs[i]; 507 508 String type = modSpec.modType.getText(); 510 String attribute = modSpec.modAttributeCombo.getText(); 511 String [] values = new String [modSpec.valueLineList.size()]; 512 for ( int k = 0; k < values.length; k++ ) 513 { 514 values[k] = ( ( ValueLine ) modSpec.valueLineList.get( k ) ).valueText.getText(); 515 } 516 517 sb.append( type ).append( ": " ).append( attribute ).append( BrowserCoreConstants.LINE_SEPARATOR ); 519 for ( int k = 0; k < values.length; k++ ) 520 { 521 if ( values[k].length() > 0 ) 522 { 523 sb.append( attribute ).append( ": " ).append( values[k] ).append( 524 BrowserCoreConstants.LINE_SEPARATOR ); 525 } 526 } 527 sb.append( "-" ).append( BrowserCoreConstants.LINE_SEPARATOR ); 528 } 530 } 531 532 return sb.toString(); 533 } 534 535 549 private class ModSpec 550 { 551 552 553 private Group modGroup; 554 555 556 private Combo modType; 557 558 559 private DecoratedField modAttributeComboField; 560 561 562 private Combo modAttributeCombo; 563 564 565 private ContentProposalAdapter modAttributeCPA; 566 567 568 private Button modAddButton; 569 570 571 private Button modDeleteButton; 572 573 574 private ArrayList <ValueLine> valueLineList = new ArrayList <ValueLine>();; 575 } 576 577 588 private class ValueLine 589 { 590 591 592 private Composite valueComposite; 593 594 595 private Text valueText; 596 597 598 private Button valueAddButton; 599 600 601 private Button valueDeleteButton; 602 } 603 604 } 605 | Popular Tags |