KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectClass;
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.ObjectClassWrapper;
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 the Object Class Selection Dialog, that allows user to select an object class.
58  *
59  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
60  * @version $Rev$, $Date$
61  */

62 public class ObjectClassSelectionDialog extends Dialog
63 {
64     /** The selected Object Class */
65     private ObjectClass selectedObjectClass;
66
67     /** The Schema Pool */
68     private SchemaPool schemaPool;
69
70     /** The hidden Object Classes */
71     private List JavaDoc<ObjectClass> hiddenObjectClasses;
72
73     // UI Fields
74
private Text searchText;
75     private Table objectClassesTable;
76     private TableViewer tableViewer;
77
78
79     /**
80      * Creates a new instance of ObjectClassSelectionDialog.
81      */

82     public ObjectClassSelectionDialog()
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( "ObjectClassSelectionDialog.Object_Class_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( "ObjectClassSelectionDialog.Choose_an_object_class" ) ); //$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                 objectClassesTable.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                     objectClassesTable.setFocus();
129                 }
130             }
131         } );
132
133         Label matchingLabel = new Label( composite, SWT.NONE );
134         matchingLabel.setText( Messages.getString( "ObjectClassSelectionDialog.Matching_object_class(es)" ) ); //$NON-NLS-1$
135
matchingLabel.setLayoutData( new GridData( GridData.FILL, SWT.None, true, false ) );
136
137         objectClassesTable = 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         objectClassesTable.setLayoutData( gridData );
145         objectClassesTable.addMouseListener( new MouseAdapter()
146         {
147             public void mouseDoubleClick( MouseEvent e )
148             {
149                 if ( objectClassesTable.getSelectionIndex() != -1 )
150                 {
151                     okPressed();
152                 }
153             }
154         } );
155
156         tableViewer = new TableViewer( objectClassesTable );
157         tableViewer.setUseHashlookup( true );
158
159         tableViewer.setContentProvider( new ObjectClassSelectionDialogContentProvider( hiddenObjectClasses ) );
160         tableViewer.setLabelProvider( new TableDecoratingLabelProvider( new ObjectClassSelectionDialogLabelProvider(),
161             Activator.getDefault().getWorkbench().getDecoratorManager().getLabelDecorator() ) );
162
163         // We need to force the input to load the complete list of attribute types
164
tableViewer.setInput( "" ); //$NON-NLS-1$
165
// We also need to force the selection of the first row
166
objectClassesTable.select( 0 );
167
168         return composite;
169     }
170
171
172     /* (non-Javadoc)
173      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
174      */

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

185     protected void okPressed()
186     {
187         StructuredSelection selection = ( StructuredSelection ) tableViewer.getSelection();
188
189         if ( selection.isEmpty() )
190         {
191             MessageDialog
192                 .openError(
193                     getShell(),
194                     Messages.getString( "ObjectClassSelectionDialog.Invalid_Selection" ), Messages.getString( "ObjectClassSelectionDialog.You_have_to_choose_an_object_class" ) ); //$NON-NLS-1$ //$NON-NLS-2$
195
return;
196         }
197
198         ObjectClassWrapper ocw = ( ObjectClassWrapper ) selection.getFirstElement();
199         if ( ocw != null )
200         {
201             selectedObjectClass = ocw.getMyObjectClass();
202         }
203
204         super.okPressed();
205     }
206
207
208     /**
209      * Returns the selected Object Class.
210      *
211      * @return
212      * the selected Object Class
213      */

214     public ObjectClass getSelectedObjectClass()
215     {
216         return selectedObjectClass;
217     }
218
219
220     /**
221      * Set the hidden Object Classes.
222      *
223      * @param list
224      * a list of Object Classes to hide
225      */

226     public void setHiddenObjectClasses( List JavaDoc<ObjectClass> list )
227     {
228         hiddenObjectClasses = list;
229     }
230
231
232     /**
233      * Sets the hidden Object Classes.
234      *
235      * @param objectClasses
236      * an array of Object Classes to hide
237      */

238     public void setHiddenObjectClasses( ObjectClass[] objectClasses )
239     {
240         hiddenObjectClasses = new ArrayList JavaDoc<ObjectClass>();
241
242         for ( ObjectClass objectClass : objectClasses )
243         {
244             hiddenObjectClasses.add( objectClass );
245         }
246     }
247
248
249     /**
250      * Sets the hidden Object Classes.
251      *
252      * @param names
253      * an array of names of Object Classes to hide
254      */

255     public void setHiddenObjectClasses( String JavaDoc[] names )
256     {
257         hiddenObjectClasses = new ArrayList JavaDoc<ObjectClass>();
258
259         for ( String JavaDoc name : names )
260         {
261             ObjectClass oc = schemaPool.getObjectClass( name );
262             if ( oc != null )
263             {
264                 hiddenObjectClasses.add( oc );
265             }
266         }
267     }
268 }
269
Popular Tags