KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > wizards > CreateANewAttributeTypeWizardPage


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.wizards;
22
23
24 import org.apache.directory.ldapstudio.schemas.Activator;
25 import org.apache.directory.ldapstudio.schemas.Messages;
26 import org.apache.directory.ldapstudio.schemas.PluginConstants;
27 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
28 import org.apache.directory.ldapstudio.schemas.view.ViewUtils;
29 import org.apache.directory.shared.asn1.primitives.OID;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.wizard.WizardPage;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.events.ModifyEvent;
34 import org.eclipse.swt.events.ModifyListener;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.events.VerifyEvent;
38 import org.eclipse.swt.events.VerifyListener;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.plugin.AbstractUIPlugin;
46
47
48 /**
49  * Default Page for new attribute type wizard
50  */

51 public class CreateANewAttributeTypeWizardPage extends WizardPage
52 {
53     // UI Fields
54
private Text oidField;
55     private Text nameField;
56
57
58     /**
59      * Creates a new instance of CreateANewAttributeTypeWizardPage.
60      */

61     public CreateANewAttributeTypeWizardPage()
62     {
63         super( "CreateANewAttributeTypeWizardPage" ); //$NON-NLS-1$
64
setTitle( Messages.getString( "CreateANewAttributeTypeWizardPage.Page_Title" ) ); //$NON-NLS-1$
65
setDescription( Messages.getString( "CreateANewAttributeTypeWizardPage.Page_Description" ) ); //$NON-NLS-1$
66
setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
67             PluginConstants.IMG_ATTRIBUTE_TYPE_NEW_WIZARD ) );
68     }
69
70
71     /**
72      * OID field getter
73      *
74      * @return the value of the OID field
75      */

76     public String JavaDoc getOidField()
77     {
78         return this.oidField.getText();
79     }
80
81
82     /**
83      * Name field getter
84      *
85      * @return the value of the name field
86      */

87     public String JavaDoc getNameField()
88     {
89         return this.nameField.getText();
90     }
91
92
93     /*
94      * (non-Javadoc)
95      *
96      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
97      */

98     public void createControl( Composite parent )
99     {
100         Composite container = new Composite( parent, SWT.NULL );
101         GridLayout layout = new GridLayout();
102         container.setLayout( layout );
103         layout.numColumns = 2;
104         layout.verticalSpacing = 1;
105         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
106
107         // Setting up the OID section
108
new Label( container, SWT.NULL );
109
110         final Button autoOID = new Button( container, SWT.CHECK );
111         autoOID.setText( Messages.getString( "CreateANewAttributeTypeWizardPage.Prefix_with_the_default_OID" ) ); //$NON-NLS-1$
112

113         autoOID.addSelectionListener( new SelectionAdapter()
114         {
115             public void widgetSelected( SelectionEvent e )
116             {
117                 IPreferenceStore store = Activator.getDefault().getPreferenceStore();
118
119                 store.setValue( PluginConstants.PREFS_SCHEMAS_EDITOR_AUTO_OID, autoOID.getSelection() );
120                 if ( autoOID.getSelection() )
121                 {
122                     String JavaDoc temp = store.getString( PluginConstants.PREFS_SCHEMAS_EDITOR_COMPANY_OID );
123                     oidField.setText( temp + "." ); //$NON-NLS-1$
124
}
125                 else
126                 {
127                     oidField.setText( "" ); //$NON-NLS-1$
128
}
129             }
130         } );
131
132         IPreferenceStore store = Activator.getDefault().getPreferenceStore();
133         boolean auto_oid = store.getBoolean( PluginConstants.PREFS_SCHEMAS_EDITOR_AUTO_OID );
134         autoOID.setSelection( auto_oid );
135
136         Label label = new Label( container, SWT.NULL );
137         label.setText( Messages.getString( "CreateANewAttributeTypeWizardPage.OID" ) ); //$NON-NLS-1$
138
oidField = new Text( container, SWT.BORDER | SWT.SINGLE );
139         if ( auto_oid )
140         {
141             String JavaDoc temp = store.getString( PluginConstants.PREFS_SCHEMAS_EDITOR_COMPANY_OID );
142             oidField.setText( temp + "." ); //$NON-NLS-1$
143
}
144         oidField.setLayoutData( gd );
145         oidField.addModifyListener( new ModifyListener()
146         {
147             public void modifyText( ModifyEvent e )
148             {
149                 dialogChanged();
150             }
151         } );
152         oidField.addVerifyListener( new VerifyListener()
153         {
154             public void verifyText( VerifyEvent e )
155             {
156                 if ( !e.text.matches( "([0-9]*\\.?)*" ) ) //$NON-NLS-1$
157
{
158                     e.doit = false;
159                 }
160             }
161         } );
162
163         // Setting up the Name section
164
Label label2 = new Label( container, SWT.NULL );
165         label2.setText( Messages.getString( "CreateANewAttributeTypeWizardPage.Name" ) ); //$NON-NLS-1$
166
nameField = new Text( container, SWT.BORDER | SWT.SINGLE );
167         nameField.setLayoutData( gd );
168         nameField.addModifyListener( new ModifyListener()
169         {
170             public void modifyText( ModifyEvent e )
171             {
172                 dialogChanged();
173             }
174         } );
175         dialogChanged();
176         setControl( container );
177         setErrorMessage( null );
178         setPageComplete( false );
179     }
180
181
182     /**
183      * This method is called when the user modifies something in the UI.
184      */

185     private void dialogChanged()
186     {
187         if ( getOidField().length() == 0 )
188         {
189             updateStatus( Messages.getString( "CreateANewAttributeTypeWizardPage.An_OID_must_be_specified" ) ); //$NON-NLS-1$
190
return;
191         }
192
193         if ( !OID.isOID( getOidField() ) )
194         {
195             updateStatus( Messages.getString( "CreateANewAttributeTypeWizardPage.Malformed_OID" ) ); //$NON-NLS-1$
196
return;
197         }
198
199         if ( getNameField().length() == 0 )
200         {
201             updateStatus( Messages.getString( "CreateANewAttributeTypeWizardPage.A_name_must_be_specified" ) ); //$NON-NLS-1$
202
return;
203         }
204
205         if ( SchemaPool.getInstance().containsSchemaElement( getOidField() ) )
206         {
207             updateStatus( Messages
208                 .getString( "CreateANewAttributeTypeWizardPage.An_element_of_the_same_OID_already_exists" ) ); //$NON-NLS-1$
209
return;
210         }
211
212         if ( !ViewUtils.verifyName( getNameField() ) )
213         {
214             updateStatus( "Name is not valid." ); //$NON-NLS-1$
215
return;
216         }
217
218         if ( SchemaPool.getInstance().containsSchemaElement( getNameField() ) )
219         {
220             updateStatus( Messages
221                 .getString( "CreateANewAttributeTypeWizardPage.An_element_of_the_same_name_already_exists" ) ); //$NON-NLS-1$
222
return;
223         }
224
225         updateStatus( null );
226     }
227
228
229     /**
230      * Updates the status of the page.
231      *
232      * @param message
233      * the message to display
234      */

235     private void updateStatus( String JavaDoc message )
236     {
237         setErrorMessage( message );
238         setPageComplete( message == null );
239     }
240 }
241
Popular Tags