KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > dialogs > EditAliasesDialog


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.schemas.view.dialogs;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
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 /**
62  * This class implements the Manage Aliases Dialog.
63  *
64  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
65  * @version $Rev$, $Date$
66  */

67 public class EditAliasesDialog extends Dialog
68 {
69     /** The aliases List */
70     private List JavaDoc<String JavaDoc> aliases;
71     private List JavaDoc<String JavaDoc> aliasesLowerCased;
72
73     /** The dirty flag */
74     private boolean dirty = false;
75
76     // UI Fields
77
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     /**
86      * Creates a new instance of EditAliasesDialog.
87      *
88      * @param aliases
89      * the array containing the aliases
90      */

91     public EditAliasesDialog( String JavaDoc[] aliases )
92     {
93         super( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() );
94         this.aliases = new ArrayList JavaDoc<String JavaDoc>();
95         aliasesLowerCased = new ArrayList JavaDoc<String JavaDoc>();
96         for ( String JavaDoc alias : aliases )
97         {
98             this.aliases.add( alias );
99             aliasesLowerCased.add( alias.toLowerCase() );
100         }
101     }
102
103
104     /* (non-Javadoc)
105      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
106      */

107     protected void configureShell( Shell newShell )
108     {
109         super.configureShell( newShell );
110         newShell.setText( Messages.getString( "EditAliasesDialog.Edit_aliases" ) ); //$NON-NLS-1$
111
}
112
113
114     /* (non-Javadoc)
115      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
116      */

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         // ALIASES Label
124
Label aliases_label = new Label( composite, SWT.NONE );
125         aliases_label.setText( Messages.getString( "EditAliasesDialog.Aliases" ) ); //$NON-NLS-1$
126
aliases_label.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, true, 2, 1 ) );
127
128         // ALIASES Table
129
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         // ADD Label
139
Label add_label = new Label( composite, SWT.NONE );
140         add_label.setText( Messages.getString( "EditAliasesDialog.Add_an_alias" ) ); //$NON-NLS-1$
141
add_label.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, true, 2, 1 ) );
142
143         // NEW ALIAS Field
144
newAliasText = new Text( composite, SWT.BORDER );
145         newAliasText.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, false ) );
146
147         // Add Button
148
newAliasAddButton = new Button( composite, SWT.PUSH );
149         newAliasAddButton.setText( Messages.getString( "EditAliasesDialog.Add" ) ); //$NON-NLS-1$
150
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." ) ); //$NON-NLS-1$
165
errorLabel.setLayoutData( new GridData( GridData.FILL, GridData.FILL, true, true ) );
166
167         // Table initialization
168
fillAliasesTable();
169
170         // Listeners initialization
171
initListeners();
172
173         // Setting the focus to the text field
174
newAliasText.setFocus();
175
176         return composite;
177     }
178
179
180     /**
181      * Fills in the Aliases Table from the aliases list */

182     private void fillAliasesTable()
183     {
184         aliasesTable.removeAll();
185         aliasesTable.setItemCount( 0 );
186         for ( String JavaDoc alias : aliases )
187         {
188             TableItem newItem = new TableItem( aliasesTable, SWT.NONE );
189             newItem.setText( alias );
190         }
191     }
192
193
194     /**
195      * Initializes the Listeners.
196      */

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" ) ) ) { //$NON-NLS-1$
204
// NOTE: I couldn't find the corresponding Identificator in the SWT.SWT Class,
205
// so I Used JFace Action fineKeyCode method to get the Backspace keycode.
206

207                     removeAliases();
208                 }
209             }
210
211
212             public void keyReleased( KeyEvent e )
213             {
214             }
215         } );
216
217         // Aliases Table's Popup Menu
218
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" ) ); //$NON-NLS-1$
222
deleteMenuItem.setImage( PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_TOOL_DELETE ) );
223         // Adding the listener
224
deleteMenuItem.addListener( SWT.Selection, new Listener()
225         {
226             public void handleEvent( Event event )
227             {
228                 removeAliases();
229             }
230         } );
231
232         // NEW ALIAS Field
233
newAliasText.addTraverseListener( new TraverseListener()
234         {
235             public void keyTraversed( TraverseEvent e )
236             {
237                 if ( e.detail == SWT.TRAVERSE_RETURN )
238                 {
239                     String JavaDoc text = newAliasText.getText();
240
241                     if ( ( !"".equals( text ) ) && ( !aliasesLowerCased.contains( text.toLowerCase() ) ) //$NON-NLS-1$
242
&& ( !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 JavaDoc text = newAliasText.getText();
257
258                 if ( "".equals( text ) ) //$NON-NLS-1$
259
{
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." ) ); //$NON-NLS-1$
267
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." ) ); //$NON-NLS-1$
274
newAliasAddButton.setEnabled( false );
275                 }
276                 else if ( !ViewUtils.verifyName( text ) )
277                 {
278                     errorComposite.setVisible( true );
279                     errorLabel.setText( Messages.getString( "EditAliasesDialog.Invalid_Alias." ) ); //$NON-NLS-1$
280
newAliasAddButton.setEnabled( false );
281                 }
282             }
283         } );
284
285         // ADD Button
286
newAliasAddButton.addSelectionListener( new SelectionAdapter()
287         {
288             public void widgetSelected( SelectionEvent e )
289             {
290                 addANewAlias();
291             }
292         } );
293
294     }
295
296
297     /**
298      * Removes the selected aliases in the Aliases Table from the Aliases List.
299      */

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     /**
314      * Adds a new alias
315      */

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( "" ); //$NON-NLS-1$
324
newAliasText.setFocus();
325             this.dirty = true;
326         }
327     }
328
329
330     /* (non-Javadoc)
331      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
332      */

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     /**
341      * Returns the aliases.
342      *
343      * @return
344      * the aliases
345      */

346     public String JavaDoc[] getAliases()
347     {
348         return aliases.toArray( new String JavaDoc[0] );
349     }
350
351
352     /**
353      * Gets the Dirty flag of the dialog
354      *
355      * @return
356      * the dirty flag of the dialog
357      */

358     public boolean isDirty()
359     {
360         return dirty;
361     }
362 }
363
Popular Tags