KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > widgets > ACIItemSourceEditorComposite


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 package org.apache.directory.ldapstudio.aciitemeditor.widgets;
21
22
23 import java.text.ParseException JavaDoc;
24
25 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
26 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
27 import org.apache.directory.ldapstudio.aciitemeditor.sourceeditor.ACISourceViewerConfiguration;
28 import org.apache.directory.shared.ldap.aci.ACIItem;
29 import org.apache.directory.shared.ldap.aci.ACIItemParser;
30 import org.eclipse.jface.resource.JFaceResources;
31 import org.eclipse.jface.text.Document;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.IRegion;
34 import org.eclipse.jface.text.Region;
35 import org.eclipse.jface.text.source.SourceViewer;
36 import org.eclipse.jface.text.source.SourceViewerConfiguration;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.graphics.Font;
39 import org.eclipse.swt.layout.FillLayout;
40 import org.eclipse.swt.widgets.Composite;
41
42
43 /**
44  * This composite contains the source editor.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class ACIItemSourceEditorComposite extends Composite
50 {
51
52     /** The source editor */
53     private SourceViewer sourceEditor;
54
55     /** The source editor configuration. */
56     private SourceViewerConfiguration configuration;
57
58
59     /**
60      * Creates a new instance of ACIItemSourceEditorComposite.
61      *
62      * @param parent
63      * @param style
64      */

65     public ACIItemSourceEditorComposite( Composite parent, int style )
66     {
67         super( parent, style );
68         setLayout( new FillLayout() );
69
70         createSourceEditor();
71     }
72
73
74     /**
75      * Creates and configures the source editor.
76      *
77      */

78     private void createSourceEditor()
79     {
80         // create source editor
81
sourceEditor = new SourceViewer( this, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL );
82
83         // setup basic configuration
84
configuration = new ACISourceViewerConfiguration();
85         sourceEditor.configure( configuration );
86
87         // set text font
88
Font font = JFaceResources.getFont( JFaceResources.TEXT_FONT );
89         sourceEditor.getTextWidget().setFont( font );
90
91         // setup document
92
IDocument document = new Document();
93         sourceEditor.setDocument( document );
94     }
95
96
97     /**
98      * Sets the input to the source editor.
99      * A syntax check is performed before setting the input, an
100      * invalid syntax causes a ParseException.
101      *
102      * @param input the valid string representation of the ACI item
103      * @throws ParseException it the syntax check fails.
104      */

105     public void setInput( String JavaDoc input ) throws ParseException JavaDoc
106     {
107         ACIItemParser parser = Activator.getDefault().getACIItemParser();
108         parser.parse( input );
109
110         forceSetInput( input );
111     }
112
113
114     /**
115      * Set the input to the source editor without a syntax check.
116      *
117      * @param input The string representation of the ACI item, may be invalid
118      */

119     public void forceSetInput( String JavaDoc input )
120     {
121         sourceEditor.getDocument().set( input );
122
123         // format
124
IRegion region = new Region( 0, sourceEditor.getDocument().getLength() );
125         configuration.getContentFormatter( sourceEditor ).format( sourceEditor.getDocument(), region );
126
127     }
128
129
130     /**
131      * Returns the string representation of the ACI item.
132      * A syntax check is performed before returning the input, an
133      * invalid syntax causes a ParseException.
134      *
135      * @return the valid string representation of the ACI item
136      * @throws ParseException it the syntax check fails.
137      */

138     public String JavaDoc getInput() throws ParseException JavaDoc
139     {
140         String JavaDoc input = forceGetInput();
141
142         // strip new lines
143
input = input.replaceAll( "\\n", " " ); //$NON-NLS-1$ //$NON-NLS-2$
144
input = input.replaceAll( "\\r", " " ); //$NON-NLS-1$ //$NON-NLS-2$
145

146         ACIItemParser parser = Activator.getDefault().getACIItemParser();
147         ACIItem aciItem = parser.parse( input );
148
149         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
150         if ( aciItem != null )
151         {
152             aciItem.printToBuffer( buffer );
153         }
154         return buffer.toString();
155     }
156
157
158     /**
159      * Returns the string representation of the ACI item without syntax check.
160      * In other words only the text in the source editor is returned.
161      *
162      * @return the string representation of the ACI item, may be invalid
163      */

164     public String JavaDoc forceGetInput()
165     {
166         return sourceEditor.getDocument().get();
167     }
168
169
170     /**
171      * Sets the context.
172      *
173      * @param context the context
174      */

175     public void setContext( ACIItemValueWithContext context )
176     {
177
178     }
179
180
181     /**
182      * Formats the content.
183      */

184     public void format()
185     {
186         IRegion region = new Region( 0, sourceEditor.getDocument().getLength() );
187         configuration.getContentFormatter( sourceEditor ).format( sourceEditor.getDocument(), region );
188     }
189
190 }
191
Popular Tags