KickJava   Java API By Example, From Geeks To Geeks.

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


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.Activator;
28 import org.apache.directory.ldapstudio.schemas.Messages;
29 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
30 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
31 import org.apache.directory.ldapstudio.schemas.view.views.TableDecoratingLabelProvider;
32 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
33 import org.eclipse.jface.dialogs.Dialog;
34 import org.eclipse.jface.dialogs.IDialogConstants;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.jface.viewers.TableViewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.KeyAdapter;
40 import org.eclipse.swt.events.KeyEvent;
41 import org.eclipse.swt.events.ModifyEvent;
42 import org.eclipse.swt.events.ModifyListener;
43 import org.eclipse.swt.events.MouseAdapter;
44 import org.eclipse.swt.events.MouseEvent;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Control;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Table;
52 import org.eclipse.swt.widgets.Text;
53 import org.eclipse.ui.PlatformUI;
54
55
56 /**
57  * This class is Attribute Type Selection Dialog, that allows user to select an attribute type.
58  *
59  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
60  * @version $Rev$, $Date$
61  */

62 public class AttributeTypeSelectionDialog extends Dialog
63 {
64     /** The selected Attribute Type */
65     private AttributeType selectedAttributeType;
66
67     /** The Schema Pool */
68     private SchemaPool schemaPool;
69
70     /** The hidden Attribute Types */
71     private List JavaDoc<AttributeType> hiddenAttributeTypes;
72
73     // UI Fields
74
private Text searchText;
75     private Table attributeTypesTable;
76     private TableViewer tableViewer;
77
78
79     /**
80      * Creates a new instance of AttributeTypeSelectionDialog.
81      */

82     public AttributeTypeSelectionDialog()
83     {
84         super( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() );
85         schemaPool = SchemaPool.getInstance();
86     }
87
88
89     /* (non-Javadoc)
90      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
91      */

92     protected void configureShell( Shell newShell )
93     {
94         super.configureShell( newShell );
95         newShell.setText( Messages.getString( "AttributeTypeSelectionDialog.Attribute_Type_Selection" ) ); //$NON-NLS-1$
96
}
97
98
99     /* (non-Javadoc)
100      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
101      */

102     protected Control createDialogArea( Composite parent )
103     {
104         Composite composite = new Composite( parent, SWT.NONE );
105         GridLayout layout = new GridLayout( 1, false );
106         composite.setLayout( layout );
107
108         Label chooseLabel = new Label( composite, SWT.NONE );
109         chooseLabel.setText( Messages.getString( "AttributeTypeSelectionDialog.Choose_an_attribute_type" ) ); //$NON-NLS-1$
110
chooseLabel.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, false ) );
111
112         searchText = new Text( composite, SWT.BORDER );
113         searchText.setLayoutData( new GridData( GridData.FILL, SWT.NONE, true, false ) );
114         searchText.addModifyListener( new ModifyListener()
115         {
116             public void modifyText( ModifyEvent e )
117             {
118                 tableViewer.setInput( searchText.getText() );
119                 attributeTypesTable.select( 0 );
120             }
121         } );
122         searchText.addKeyListener( new KeyAdapter()
123         {
124             public void keyPressed( KeyEvent e )
125             {
126                 if ( e.keyCode == SWT.ARROW_DOWN )
127                 {
128                     attributeTypesTable.setFocus();
129                 }
130             }
131         } );
132
133         Label matchingLabel = new Label( composite, SWT.NONE );
134         matchingLabel.setText( Messages.getString( "AttributeTypeSelectionDialog.Matching_attribute_type(s)" ) ); //$NON-NLS-1$
135
matchingLabel.setLayoutData( new GridData( GridData.FILL, SWT.None, true, false ) );
136
137         attributeTypesTable = new Table( composite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
138             | SWT.FULL_SELECTION | SWT.HIDE_SELECTION );
139         GridData gridData = new GridData( GridData.FILL, GridData.FILL, true, true );
140         gridData.heightHint = 148;
141         gridData.minimumHeight = 148;
142         gridData.widthHint = 350;
143         gridData.minimumWidth = 350;
144         attributeTypesTable.setLayoutData( gridData );
145         attributeTypesTable.addMouseListener( new MouseAdapter()
146         {
147             public void mouseDoubleClick( MouseEvent e )
148             {
149                 if ( attributeTypesTable.getSelectionIndex() != -1 )
150                 {
151                     okPressed();
152                 }
153             }
154         } );
155
156         tableViewer = new TableViewer( attributeTypesTable );
157         tableViewer.setUseHashlookup( true );
158
159         tableViewer.setContentProvider( new AttributeTypeSelectionDialogContentProvider( hiddenAttributeTypes ) );
160         tableViewer.setLabelProvider( new TableDecoratingLabelProvider(
161             new AttributeTypeSelectionDialogLabelProvider(), Activator.getDefault().getWorkbench()
162                 .getDecoratorManager().getLabelDecorator() ) );
163
164         // We need to force the input to load the complete list of attribute types
165
tableViewer.setInput( "" ); //$NON-NLS-1$
166
// We also need to force the selection of the first row
167
attributeTypesTable.select( 0 );
168
169         return composite;
170     }
171
172
173     /* (non-Javadoc)
174      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
175      */

176     protected void createButtonsForButtonBar( Composite parent )
177     {
178         createButton( parent, IDialogConstants.OK_ID, Messages.getString( "AttributeTypeSelectionDialog.Add" ), true ); //$NON-NLS-1$
179
createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false );
180
181     }
182
183
184     /* (non-Javadoc)
185      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
186      */

187     protected void okPressed()
188     {
189         StructuredSelection selection = ( StructuredSelection ) tableViewer.getSelection();
190
191         if ( selection.isEmpty() )
192         {
193             MessageDialog
194                 .openError(
195                     getShell(),
196                     Messages.getString( "AttributeTypeSelectionDialog.Invalid_Selection" ), Messages.getString( "AttributeTypeSelectionDialog.You_have_to_choose_an_attribute_type" ) ); //$NON-NLS-1$ //$NON-NLS-2$
197
return;
198         }
199
200         AttributeTypeWrapper atw = ( AttributeTypeWrapper ) selection.getFirstElement();
201         if ( atw != null )
202         {
203             selectedAttributeType = atw.getMyAttributeType();
204         }
205
206         super.okPressed();
207     }
208
209
210     /**
211      * Returns the selected Attribute Type.
212      *
213      * @return
214      * the selected Attribute Type
215      */

216     public AttributeType getSelectedAttributeType()
217     {
218         return selectedAttributeType;
219     }
220
221
222     /**
223      * Set the hidden Attribute Types.
224      *
225      * @param list
226      * a list of Attribute Types to hide
227      */

228     public void setHiddenAttributeTypes( List JavaDoc<AttributeType> list )
229     {
230         hiddenAttributeTypes = list;
231     }
232
233
234     /**
235      * Sets the hidden Attribute Types.
236      *
237      * @param attributeTypes
238      * an array of Attribute Types to hide
239      */

240     public void setHiddenAttributeTypes( AttributeType[] attributeTypes )
241     {
242         hiddenAttributeTypes = new ArrayList JavaDoc<AttributeType>();
243
244         for ( AttributeType objectClass : attributeTypes )
245         {
246             hiddenAttributeTypes.add( objectClass );
247         }
248     }
249
250
251     /**
252      * Sets the hidden Attribute Types.
253      *
254      * @param names
255      * an array of names of Attribute Types to hide
256      */

257     public void setHiddenAttributeTypes( String JavaDoc[] names )
258     {
259         hiddenAttributeTypes = new ArrayList JavaDoc<AttributeType>();
260
261         for ( String JavaDoc name : names )
262         {
263             AttributeType at = schemaPool.getAttributeType( name );
264             if ( at != null )
265             {
266                 hiddenAttributeTypes.add( at );
267             }
268         }
269     }
270 }
271
Popular Tags