KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > editors > objectClass > ObjectClassEditorSourceCodePage


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.objectClass;
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.ObjectClass;
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.ObjectClassLiteral;
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 Object Class Editor
52  */

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

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

61     /** The modified object class */
62     private ObjectClass modifiedObjectClass;
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 Viewer Widget */
71     private ModifyListener schemaSourceViewerListener = new ModifyListener()
72     {
73         public void modifyText( ModifyEvent e )
74         {
75             canLeaveThePage = true;
76             try
77             {
78                 ( ( ObjectClassEditor ) getEditor() ).setDirty( true );
79                 OpenLdapSchemaParser parser = new OpenLdapSchemaParser();
80                 parser.parse( schemaSourceViewer.getTextWidget().getText() );
81
82                 List JavaDoc objectclasses = parser.getObjectClassTypes();
83                 if ( objectclasses.size() != 1 )
84                 {
85                     // TODO Throw an exception and return
86
}
87                 else
88                 {
89                     updateObjectClass( ( ObjectClassLiteral ) objectclasses.get( 0 ) );
90                 }
91             }
92             catch ( IOException JavaDoc e1 )
93             {
94                 canLeaveThePage = false;
95             }
96             catch ( ParseException JavaDoc exception )
97             {
98                 canLeaveThePage = false;
99             }
100         }
101     };
102
103
104     /**
105      * Default constructor
106      * @param editor
107      * the associated editor
108      */

109     public ObjectClassEditorSourceCodePage( 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         modifiedObjectClass = ( ( ObjectClassEditor ) getEditor() ).getModifiedObjectClass();
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 ( modifiedObjectClass.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" object class
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( modifiedObjectClass.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 Object Class from the given Object Class Literal.
176      *
177      * @param ocl
178      * the Object Class Literal
179      */

180     private void updateObjectClass( ObjectClassLiteral ocl )
181     {
182         modifiedObjectClass.setClassType( ocl.getClassType() );
183         modifiedObjectClass.setDescription( ocl.getDescription() );
184         modifiedObjectClass.setMay( ocl.getMay() );
185         modifiedObjectClass.setMust( ocl.getMust() );
186         modifiedObjectClass.setNames( ocl.getNames() );
187         modifiedObjectClass.setObsolete( ocl.isObsolete() );
188         modifiedObjectClass.setOid( ocl.getOid() );
189         modifiedObjectClass.setSuperiors( ocl.getSuperiors() );
190     }
191
192
193     /**
194      * Refreshes the UI.
195      */

196     public void refreshUI()
197     {
198         fillInUiFields();
199     }
200 }
201
Popular Tags