KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > editors > attributeType > AttributeTypeEditorSourceCodePage


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.editors.attributeType;
22
23
24 import java.io.IOException JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.util.List JavaDoc;
27
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.Schema.SchemaType;
31 import org.apache.directory.ldapstudio.schemas.view.views.SchemaSourceViewer;
32 import org.apache.directory.server.core.tools.schema.AttributeTypeLiteral;
33 import org.apache.directory.server.core.tools.schema.OpenLdapSchemaParser;
34 import org.eclipse.jface.resource.JFaceResources;
35 import org.eclipse.jface.text.Document;
36 import org.eclipse.jface.text.IDocument;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.graphics.Font;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.ui.forms.IManagedForm;
44 import org.eclipse.ui.forms.editor.FormEditor;
45 import org.eclipse.ui.forms.editor.FormPage;
46 import org.eclipse.ui.forms.widgets.FormToolkit;
47 import org.eclipse.ui.forms.widgets.ScrolledForm;
48
49
50 /**
51  * This class is the Source Code Page of the Attribute Type Editor
52  */

53 public class AttributeTypeEditorSourceCodePage extends FormPage
54 {
55     /** The page ID*/
56     public static final String JavaDoc ID = AttributeTypeEditor.ID + "sourceCodePage"; //$NON-NLS-1$
57

58     /** The page title */
59     public static String JavaDoc TITLE = Messages.getString( "AttributeTypeEditorSourceCodePage.Source_Code" ); //$NON-NLS-1$
60

61     /** The modified attribute type */
62     private AttributeType modifiedAttributeType;
63
64     /** The Schema Source Viewer */
65     private SchemaSourceViewer schemaSourceViewer;
66
67     /** The flag to indicate if the user can leave the Source Code page */
68     private boolean canLeaveThePage = true;
69
70     /** The listener of the Schema Source Editor Widget */
71     private ModifyListener schemaSourceViewerListener = new ModifyListener()
72     {
73         public void modifyText( ModifyEvent e )
74         {
75             canLeaveThePage = true;
76             try
77             {
78                 ( ( AttributeTypeEditor ) getEditor() ).setDirty( true );
79                 OpenLdapSchemaParser parser = new OpenLdapSchemaParser();
80                 parser.parse( schemaSourceViewer.getTextWidget().getText() );
81                 List JavaDoc attributeTypes = parser.getAttributeTypes();
82                 if ( attributeTypes.size() != 1 )
83                 {
84                     // Throw an exception and return
85
}
86                 else
87                 {
88                     updateAttributeType( ( AttributeTypeLiteral ) attributeTypes.get( 0 ) );
89                 }
90             }
91             catch ( IOException JavaDoc e1 )
92             {
93                 canLeaveThePage = false;
94             }
95             catch ( ParseException JavaDoc exception )
96             {
97                 canLeaveThePage = false;
98             }
99         }
100     };
101
102
103     /**
104      * Default constructor
105      *
106      * @param editor
107      * the associated editor
108      */

109     public AttributeTypeEditorSourceCodePage( FormEditor editor )
110     {
111         super( editor, ID, TITLE );
112     }
113
114
115     /* (non-Javadoc)
116      * @see org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
117      */

118     protected void createFormContent( IManagedForm managedForm )
119     {
120         ScrolledForm form = managedForm.getForm();
121         FormToolkit toolkit = managedForm.getToolkit();
122         GridLayout layout = new GridLayout();
123         layout.marginWidth = 0;
124         layout.marginHeight = 0;
125         form.getBody().setLayout( layout );
126         toolkit.paintBordersFor( form.getBody() );
127
128         modifiedAttributeType = ( ( AttributeTypeEditor ) getEditor() ).getModifiedAttributeType();
129
130         // SOURCE CODE Field
131
schemaSourceViewer = new SchemaSourceViewer( form.getBody(), null, null, false, SWT.BORDER | SWT.H_SCROLL
132             | SWT.V_SCROLL );
133         GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true );
134         gd.heightHint = 10;
135         schemaSourceViewer.getTextWidget().setLayoutData( gd );
136         if ( modifiedAttributeType.getOriginatingSchema().type == SchemaType.coreSchema )
137         {
138             schemaSourceViewer.setEditable( false );
139         }
140
141         // set text font
142
Font font = JFaceResources.getFont( JFaceResources.TEXT_FONT );
143         schemaSourceViewer.getTextWidget().setFont( font );
144
145         IDocument document = new Document();
146         schemaSourceViewer.setDocument( document );
147
148         // Initialization from the "input" attribute type
149
fillInUiFields();
150
151         schemaSourceViewer.getTextWidget().addModifyListener( schemaSourceViewerListener );
152     }
153
154
155     /**
156      * Fills in the User Interface.
157      */

158     private void fillInUiFields()
159     {
160         // SOURCE CODE Field
161
schemaSourceViewer.getDocument().set( modifiedAttributeType.write() );
162     }
163
164
165     /* (non-Javadoc)
166      * @see org.eclipse.ui.forms.editor.FormPage#canLeaveThePage()
167      */

168     public boolean canLeaveThePage()
169     {
170         return canLeaveThePage;
171     }
172
173
174     /**
175      * Updates the Modified Attribute Type from the given Attribute Type Literal.
176      *
177      * @param atl
178      * the Attribute Type Literal
179      */

180     private void updateAttributeType( AttributeTypeLiteral atl )
181     {
182         modifiedAttributeType.setCollective( atl.isCollective() );
183         modifiedAttributeType.setDescription( atl.getDescription() );
184         modifiedAttributeType.setEquality( atl.getEquality() );
185         modifiedAttributeType.setLength( atl.getLength() );
186         modifiedAttributeType.setNames( atl.getNames() );
187         modifiedAttributeType.setNoUserModification( atl.isNoUserModification() );
188         modifiedAttributeType.setObsolete( atl.isObsolete() );
189         modifiedAttributeType.setOid( atl.getOid() );
190         modifiedAttributeType.setOrdering( atl.getOrdering() );
191         modifiedAttributeType.setSingleValue( atl.isSingleValue() );
192         modifiedAttributeType.setSubstr( atl.getSubstr() );
193         modifiedAttributeType.setSuperior( atl.getSuperior() );
194         modifiedAttributeType.setSyntax( atl.getSyntax() );
195         modifiedAttributeType.setUsage( atl.getUsage() );
196     }
197
198
199     /**
200      * Refreshes the UI.
201      */

202     public void refreshUI()
203     {
204         fillInUiFields();
205     }
206 }
207
Popular Tags