1 20 package org.apache.directory.ldapstudio.aciitemeditor.dialogs; 21 22 23 import java.util.List ; 24 25 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext; 26 import org.apache.directory.ldapstudio.aciitemeditor.Activator; 27 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 28 import org.apache.directory.ldapstudio.browser.core.internal.model.Value; 29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 30 import org.apache.directory.ldapstudio.browser.core.model.IValue; 31 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 32 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor; 33 import org.eclipse.jface.dialogs.Dialog; 34 import org.eclipse.jface.dialogs.IDialogConstants; 35 import org.eclipse.jface.viewers.ArrayContentProvider; 36 import org.eclipse.jface.viewers.CellEditor; 37 import org.eclipse.jface.viewers.DoubleClickEvent; 38 import org.eclipse.jface.viewers.IDoubleClickListener; 39 import org.eclipse.jface.viewers.ISelectionChangedListener; 40 import org.eclipse.jface.viewers.IStructuredSelection; 41 import org.eclipse.jface.viewers.LabelProvider; 42 import org.eclipse.jface.viewers.SelectionChangedEvent; 43 import org.eclipse.jface.viewers.TableViewer; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.events.SelectionAdapter; 46 import org.eclipse.swt.events.SelectionEvent; 47 import org.eclipse.swt.layout.GridData; 48 import org.eclipse.swt.layout.GridLayout; 49 import org.eclipse.swt.widgets.Button; 50 import org.eclipse.swt.widgets.Composite; 51 import org.eclipse.swt.widgets.Control; 52 import org.eclipse.swt.widgets.Shell; 53 import org.eclipse.swt.widgets.Table; 54 55 56 62 public class MultiValuedDialog extends Dialog 63 { 64 65 private String displayName; 66 67 68 private AbstractDialogStringValueEditor valueEditor; 69 70 71 private List <String > values; 72 73 74 private ACIItemValueWithContext context; 75 76 77 private Composite composite = null; 78 79 80 private Table table = null; 81 82 83 private TableViewer tableViewer = null; 84 85 86 private Composite buttonComposite = null; 87 88 89 private Button addButton = null; 90 91 92 private Button editButton = null; 93 94 95 private Button deleteButton = null; 96 97 98 107 public MultiValuedDialog( Shell parentShell, String displayName, List <String > values, 108 ACIItemValueWithContext context, AbstractDialogStringValueEditor valueEditor ) 109 { 110 super( parentShell ); 111 super.setShellStyle( super.getShellStyle() | SWT.RESIZE ); 112 113 this.displayName = displayName; 114 this.values = values; 115 this.context = context; 116 this.valueEditor = valueEditor; 117 } 118 119 120 125 protected void configureShell( Shell shell ) 126 { 127 super.configureShell( shell ); 128 shell.setText( Messages.getString("MultiValuedDialog.dialog.titlePrefix") + displayName ); shell.setImage( Activator.getDefault().getImage( Messages.getString("MultiValuedDialog.dialog.icon") ) ); } 131 132 133 138 protected void createButtonsForButtonBar( Composite parent ) 139 { 140 createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false ); 141 } 142 143 144 147 protected Control createDialogArea( Composite parent ) 148 { 149 composite = ( Composite ) super.createDialogArea( parent ); 151 GridData gd = new GridData( GridData.FILL_BOTH ); 152 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 153 gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH / 2 ); 154 composite.setLayoutData( gd ); 155 GridLayout layout = ( GridLayout ) composite.getLayout(); 156 layout.makeColumnsEqualWidth = false; 157 layout.numColumns = 2; 158 159 createTable(); 160 161 createButtonComposite(); 162 163 applyDialogFont( composite ); 164 return composite; 165 } 166 167 168 171 private void createTable() 172 { 173 GridData tableGridData = new GridData( GridData.FILL_BOTH ); 174 tableGridData.grabExcessHorizontalSpace = true; 175 tableGridData.verticalAlignment = GridData.FILL; 176 tableGridData.horizontalAlignment = GridData.FILL; 177 179 table = new Table( composite, SWT.BORDER ); 180 table.setHeaderVisible( false ); 181 table.setLayoutData( tableGridData ); 182 table.setLinesVisible( false ); 183 tableViewer = new TableViewer( table ); 184 tableViewer.setContentProvider( new ArrayContentProvider() ); 185 tableViewer.setLabelProvider( new LabelProvider() ); 186 tableViewer.setInput( values ); 187 188 tableViewer.addSelectionChangedListener( new ISelectionChangedListener() 189 { 190 public void selectionChanged( SelectionChangedEvent event ) 191 { 192 valueSelected(); 193 } 194 } ); 195 196 tableViewer.addDoubleClickListener( new IDoubleClickListener() 197 { 198 public void doubleClick( DoubleClickEvent event ) 199 { 200 editValue(); 201 } 202 } ); 203 } 204 205 206 209 private void createButtonComposite() 210 { 211 GridData deleteButtonGridData = new GridData(); 212 deleteButtonGridData.horizontalAlignment = GridData.FILL; 213 deleteButtonGridData.grabExcessHorizontalSpace = false; 214 deleteButtonGridData.verticalAlignment = GridData.BEGINNING; 215 deleteButtonGridData.widthHint = Activator.getButtonWidth( composite ); 216 217 GridData editButtonGridData = new GridData(); 218 editButtonGridData.horizontalAlignment = GridData.FILL; 219 editButtonGridData.grabExcessHorizontalSpace = false; 220 editButtonGridData.verticalAlignment = GridData.BEGINNING; 221 editButtonGridData.widthHint = Activator.getButtonWidth( composite ); 222 223 GridData addButtonGridData = new GridData(); 224 addButtonGridData.horizontalAlignment = GridData.FILL; 225 addButtonGridData.grabExcessHorizontalSpace = false; 226 addButtonGridData.verticalAlignment = GridData.BEGINNING; 227 addButtonGridData.widthHint = Activator.getButtonWidth( composite ); 228 229 GridLayout gridLayout = new GridLayout(); 230 gridLayout.marginWidth = 0; 231 gridLayout.marginHeight = 0; 232 GridData gridData = new GridData(); 233 gridData.horizontalAlignment = GridData.CENTER; 234 gridData.grabExcessHorizontalSpace = false; 235 gridData.grabExcessVerticalSpace = false; 236 gridData.verticalAlignment = GridData.FILL; 237 238 buttonComposite = new Composite( composite, SWT.NONE ); 239 buttonComposite.setLayoutData( gridData ); 240 buttonComposite.setLayout( gridLayout ); 241 242 addButton = new Button( buttonComposite, SWT.NONE ); 243 addButton.setText( Messages.getString( "MultiValuedDialog.button.add" ) ); addButton.setLayoutData( addButtonGridData ); 245 addButton.addSelectionListener( new SelectionAdapter() 246 { 247 public void widgetSelected( SelectionEvent e ) 248 { 249 addValue(); 250 } 251 } ); 252 253 editButton = new Button( buttonComposite, SWT.NONE ); 254 editButton.setText( Messages.getString( "MultiValuedDialog.button.edit" ) ); editButton.setLayoutData( editButtonGridData ); 256 editButton.addSelectionListener( new SelectionAdapter() 257 { 258 public void widgetSelected( SelectionEvent e ) 259 { 260 editValue(); 261 } 262 } ); 263 editButton.setEnabled( false ); 264 265 deleteButton = new Button( buttonComposite, SWT.NONE ); 266 deleteButton.setText( Messages.getString( "MultiValuedDialog.button.delete" ) ); deleteButton.setLayoutData( deleteButtonGridData ); 268 deleteButton.addSelectionListener( new SelectionAdapter() 269 { 270 public void widgetSelected( SelectionEvent e ) 271 { 272 deleteValue(); 273 } 274 } ); 275 deleteButton.setEnabled( false ); 276 277 } 278 279 280 283 private void addValue() 284 { 285 286 Object oldRawValue; 287 try 288 { 289 IAttribute attribute = new Attribute( context.getEntry(), "" ); IValue value = new Value( attribute, "" ); oldRawValue = valueEditor.getRawValue( value ); } 293 catch ( ModelModificationException e ) 294 { 295 oldRawValue = valueEditor.getRawValue( context.getConnection(), "" ); } 297 298 299 CellEditor cellEditor = valueEditor.getCellEditor(); 300 cellEditor.setValue( oldRawValue ); 301 cellEditor.activate(); 302 Object newRawValue = cellEditor.getValue(); 303 304 if ( newRawValue != null ) 305 { 306 String newValue = ( String ) valueEditor.getStringOrBinaryValue( newRawValue ); 307 308 values.add( newValue ); 309 tableViewer.refresh(); 310 } 311 } 312 313 314 318 private void editValue() 319 { 320 String oldValue = getSelectedValue(); 321 if ( oldValue != null ) 322 { 323 Object oldRawValue; 324 try 325 { 326 IAttribute attribute = new Attribute( context.getEntry(), "" ); IValue value = new Value( attribute, oldValue ); oldRawValue = valueEditor.getRawValue( value ); } 330 catch ( ModelModificationException e ) 331 { 332 oldRawValue = valueEditor.getRawValue( context.getConnection(), oldValue ); 333 } 334 335 CellEditor cellEditor = valueEditor.getCellEditor(); 336 cellEditor.setValue( oldRawValue ); 337 cellEditor.activate(); 338 Object newRawValue = cellEditor.getValue(); 339 340 if ( newRawValue != null ) 341 { 342 String newValue = ( String ) valueEditor.getStringOrBinaryValue( newRawValue ); 343 344 values.remove( oldValue ); 345 values.add( newValue ); 346 tableViewer.refresh(); 347 } 348 } 349 } 350 351 352 355 private void deleteValue() 356 { 357 String value = getSelectedValue(); 358 if ( value != null ) 359 { 360 values.remove( value ); 361 tableViewer.refresh(); 362 } 363 } 364 365 366 370 private void valueSelected() 371 { 372 String value = getSelectedValue(); 373 374 if ( value == null ) 375 { 376 editButton.setEnabled( false ); 377 deleteButton.setEnabled( false ); 378 } 379 else 380 { 381 editButton.setEnabled( true ); 382 deleteButton.setEnabled( true ); 383 } 384 } 385 386 387 390 private String getSelectedValue() 391 { 392 String value = null; 393 394 IStructuredSelection selection = ( IStructuredSelection ) tableViewer.getSelection(); 395 if ( !selection.isEmpty() ) 396 { 397 Object element = selection.getFirstElement(); 398 if ( element instanceof String ) 399 { 400 value = ( String ) element; 401 } 402 } 403 404 return value; 405 } 406 407 } 408 | Popular Tags |