1 20 21 package org.apache.directory.ldapstudio.schemas.view.dialogs; 22 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import org.apache.directory.ldapstudio.schemas.Messages; 28 import org.apache.directory.ldapstudio.schemas.model.SchemaPool; 29 import org.apache.directory.ldapstudio.schemas.view.ViewUtils; 30 import org.eclipse.jface.action.Action; 31 import org.eclipse.jface.dialogs.Dialog; 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.KeyEvent; 35 import org.eclipse.swt.events.KeyListener; 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.events.TraverseEvent; 41 import org.eclipse.swt.events.TraverseListener; 42 import org.eclipse.swt.graphics.Image; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.layout.GridLayout; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.swt.widgets.Control; 48 import org.eclipse.swt.widgets.Event; 49 import org.eclipse.swt.widgets.Label; 50 import org.eclipse.swt.widgets.Listener; 51 import org.eclipse.swt.widgets.Menu; 52 import org.eclipse.swt.widgets.MenuItem; 53 import org.eclipse.swt.widgets.Shell; 54 import org.eclipse.swt.widgets.Table; 55 import org.eclipse.swt.widgets.TableItem; 56 import org.eclipse.swt.widgets.Text; 57 import org.eclipse.ui.ISharedImages; 58 import org.eclipse.ui.PlatformUI; 59 60 61 67 public class EditAliasesDialog extends Dialog 68 { 69 70 private List <String > aliases; 71 private List <String > aliasesLowerCased; 72 73 74 private boolean dirty = false; 75 76 private Table aliasesTable; 78 private Text newAliasText; 79 private Button newAliasAddButton; 80 private Composite errorComposite; 81 private Image errorImage; 82 private Label errorLabel; 83 84 85 91 public EditAliasesDialog( String [] aliases ) 92 { 93 super( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() ); 94 this.aliases = new ArrayList <String >(); 95 aliasesLowerCased = new ArrayList <String >(); 96 for ( String alias : aliases ) 97 { 98 this.aliases.add( alias ); 99 aliasesLowerCased.add( alias.toLowerCase() ); 100 } 101 } 102 103 104 107 protected void configureShell( Shell newShell ) 108 { 109 super.configureShell( newShell ); 110 newShell.setText( Messages.getString( "EditAliasesDialog.Edit_aliases" ) ); } 112 113 114 117 protected Control createDialogArea( Composite parent ) 118 { 119 Composite composite = new Composite( parent, SWT.NONE ); 120 composite.setLayout( new GridLayout( 2, false ) ); 121 composite.setLayoutData( new GridData( GridData.FILL, GridData.FILL, true, true ) ); 122 123 Label aliases_label = new Label( composite, SWT.NONE ); 125 aliases_label.setText( Messages.getString( "EditAliasesDialog.Aliases" ) ); aliases_label.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, true, 2, 1 ) ); 127 128 aliasesTable = new Table( composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION 130 | SWT.HIDE_SELECTION ); 131 GridData gridData = new GridData( GridData.FILL, GridData.FILL, true, true, 2, 1 ); 132 gridData.heightHint = 100; 133 gridData.minimumHeight = 100; 134 gridData.widthHint = 200; 135 gridData.minimumWidth = 200; 136 aliasesTable.setLayoutData( gridData ); 137 138 Label add_label = new Label( composite, SWT.NONE ); 140 add_label.setText( Messages.getString( "EditAliasesDialog.Add_an_alias" ) ); add_label.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, true, 2, 1 ) ); 142 143 newAliasText = new Text( composite, SWT.BORDER ); 145 newAliasText.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, false ) ); 146 147 newAliasAddButton = new Button( composite, SWT.PUSH ); 149 newAliasAddButton.setText( Messages.getString( "EditAliasesDialog.Add" ) ); newAliasAddButton.setLayoutData( new GridData( SWT.NONE, SWT.NONE, false, false ) ); 151 newAliasAddButton.setEnabled( false ); 152 153 errorComposite = new Composite( composite, SWT.NONE ); 154 errorComposite.setLayout( new GridLayout( 2, false ) ); 155 errorComposite.setLayoutData( new GridData( GridData.FILL, GridData.FILL, true, true ) ); 156 errorComposite.setVisible( false ); 157 158 errorImage = PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_OBJS_ERROR_TSK ); 159 Label label = new Label( errorComposite, SWT.NONE ); 160 label.setImage( errorImage ); 161 label.setSize( 16, 16 ); 162 163 errorLabel = new Label( errorComposite, SWT.NONE ); 164 errorLabel.setText( Messages.getString( "EditAliasesDialog.An_element_with_same_alias_already_exists." ) ); errorLabel.setLayoutData( new GridData( GridData.FILL, GridData.FILL, true, true ) ); 166 167 fillAliasesTable(); 169 170 initListeners(); 172 173 newAliasText.setFocus(); 175 176 return composite; 177 } 178 179 180 182 private void fillAliasesTable() 183 { 184 aliasesTable.removeAll(); 185 aliasesTable.setItemCount( 0 ); 186 for ( String alias : aliases ) 187 { 188 TableItem newItem = new TableItem( aliasesTable, SWT.NONE ); 189 newItem.setText( alias ); 190 } 191 } 192 193 194 197 private void initListeners() 198 { 199 aliasesTable.addKeyListener( new KeyListener() 200 { 201 public void keyPressed( KeyEvent e ) 202 { 203 if ( ( e.keyCode == SWT.DEL ) || ( e.keyCode == Action.findKeyCode( "BACKSPACE" ) ) ) { 207 removeAliases(); 208 } 209 } 210 211 212 public void keyReleased( KeyEvent e ) 213 { 214 } 215 } ); 216 217 Menu menu = new Menu( getShell(), SWT.POP_UP ); 219 aliasesTable.setMenu( menu ); 220 MenuItem deleteMenuItem = new MenuItem( menu, SWT.PUSH ); 221 deleteMenuItem.setText( Messages.getString( "EditAliasesDialog.Delete" ) ); deleteMenuItem.setImage( PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_TOOL_DELETE ) ); 223 deleteMenuItem.addListener( SWT.Selection, new Listener() 225 { 226 public void handleEvent( Event event ) 227 { 228 removeAliases(); 229 } 230 } ); 231 232 newAliasText.addTraverseListener( new TraverseListener() 234 { 235 public void keyTraversed( TraverseEvent e ) 236 { 237 if ( e.detail == SWT.TRAVERSE_RETURN ) 238 { 239 String text = newAliasText.getText(); 240 241 if ( ( !"".equals( text ) ) && ( !aliasesLowerCased.contains( text.toLowerCase() ) ) && ( !SchemaPool.getInstance().containsSchemaElement( text ) ) ) 243 { 244 addANewAlias(); 245 } 246 } 247 } 248 } ); 249 250 newAliasText.addModifyListener( new ModifyListener() 251 { 252 public void modifyText( ModifyEvent e ) 253 { 254 errorComposite.setVisible( false ); 255 newAliasAddButton.setEnabled( true ); 256 String text = newAliasText.getText(); 257 258 if ( "".equals( text ) ) { 260 newAliasAddButton.setEnabled( false ); 261 } 262 else if ( aliasesLowerCased.contains( text.toLowerCase() ) ) 263 { 264 errorComposite.setVisible( true ); 265 errorLabel 266 .setText( Messages.getString( "EditAliasesDialog.This_alias_already_exists_in_the_list." ) ); newAliasAddButton.setEnabled( false ); 268 } 269 else if ( SchemaPool.getInstance().containsSchemaElement( text ) ) 270 { 271 errorComposite.setVisible( true ); 272 errorLabel.setText( Messages 273 .getString( "EditAliasesDialog.An_element_with_same_alias_already_exists." ) ); newAliasAddButton.setEnabled( false ); 275 } 276 else if ( !ViewUtils.verifyName( text ) ) 277 { 278 errorComposite.setVisible( true ); 279 errorLabel.setText( Messages.getString( "EditAliasesDialog.Invalid_Alias." ) ); newAliasAddButton.setEnabled( false ); 281 } 282 } 283 } ); 284 285 newAliasAddButton.addSelectionListener( new SelectionAdapter() 287 { 288 public void widgetSelected( SelectionEvent e ) 289 { 290 addANewAlias(); 291 } 292 } ); 293 294 } 295 296 297 300 private void removeAliases() 301 { 302 TableItem[] selectedItems = aliasesTable.getSelection(); 303 for ( TableItem item : selectedItems ) 304 { 305 aliases.remove( item.getText() ); 306 aliasesLowerCased.remove( item.getText().toLowerCase() ); 307 } 308 dirty = true; 309 fillAliasesTable(); 310 } 311 312 313 316 private void addANewAlias() 317 { 318 if ( newAliasText.getText().length() != 0 ) 319 { 320 aliases.add( newAliasText.getText() ); 321 aliasesLowerCased.add( newAliasText.getText().toLowerCase() ); 322 fillAliasesTable(); 323 newAliasText.setText( "" ); newAliasText.setFocus(); 325 this.dirty = true; 326 } 327 } 328 329 330 333 protected void createButtonsForButtonBar( Composite parent ) 334 { 335 createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false ); 336 createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false ); 337 } 338 339 340 346 public String [] getAliases() 347 { 348 return aliases.toArray( new String [0] ); 349 } 350 351 352 358 public boolean isDirty() 359 { 360 return dirty; 361 } 362 } 363 | Popular Tags |