KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > widgets > LdifEditorWidget


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.ldifeditor.widgets;
22
23
24 import org.apache.directory.ldapstudio.browser.common.widgets.BrowserWidget;
25 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
26 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
27 import org.apache.directory.ldapstudio.ldifeditor.editor.ILdifEditor;
28 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifDocumentProvider;
29 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifSourceViewerConfiguration;
30 import org.apache.directory.ldapstudio.ldifeditor.editor.NonExistingLdifEditorInput;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.jface.resource.JFaceResources;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.jface.text.ITextListener;
35 import org.eclipse.jface.text.TextEvent;
36 import org.eclipse.jface.text.source.SourceViewer;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.graphics.Font;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Composite;
42
43
44 /**
45  * The LdifEditorWidget provides basic LDIF editor functionality like
46  * syntax highlighting and content assistent.
47  *
48  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
49  * @version $Rev$, $Date$
50  */

51 public class LdifEditorWidget extends BrowserWidget implements ILdifEditor, ITextListener
52 {
53
54     /** The connection. */
55     private IConnection connection;
56
57     /** The initial LDIF. */
58     private String JavaDoc initialLdif;
59
60     /** The content assist enabled. */
61     private boolean contentAssistEnabled;
62
63     /** The editor input. */
64     private NonExistingLdifEditorInput editorInput;
65
66     /** The document provider. */
67     private LdifDocumentProvider documentProvider;
68
69     /** The source viewer. */
70     private SourceViewer sourceViewer;
71
72     /** The source viewer configuration. */
73     private LdifSourceViewerConfiguration sourceViewerConfiguration;
74
75
76     /**
77      * Creates a new instance of LdifEditorWidget.
78      *
79      * @param contentAssistEnabled the content assist enabled
80      * @param initialLdif the initial ldif
81      * @param connection the connection
82      */

83     public LdifEditorWidget( IConnection connection, String JavaDoc initialLdif, boolean contentAssistEnabled )
84     {
85         this.connection = connection;
86         this.initialLdif = initialLdif;
87         this.contentAssistEnabled = contentAssistEnabled;
88     }
89
90
91     /**
92      * Disposes this widget.
93      */

94     public void dispose()
95     {
96         if ( editorInput != null )
97         {
98             sourceViewer.removeTextListener( this );
99             documentProvider.disconnect( editorInput );
100             // documentProvider = null;
101
editorInput = null;
102         }
103     }
104
105
106     /**
107      * Creates the widget.
108      *
109      * @param parent the parent
110      */

111     public void createWidget( Composite parent )
112     {
113         Composite composite = new Composite( parent, SWT.NONE );
114         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
115         GridLayout layout = new GridLayout( 1, false );
116         layout.marginWidth = 0;
117         layout.marginHeight = 0;
118         composite.setLayout( layout );
119
120         // create source viewer
121
// sourceViewer = new ProjectionViewer(parent, ruler,
122
// getOverviewRuler(), true, styles);
123
sourceViewer = new SourceViewer( composite, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL );
124         sourceViewer.getControl().setLayoutData( new GridData( GridData.FILL_BOTH ) );
125
126         // configure
127
sourceViewerConfiguration = new LdifSourceViewerConfiguration( this, this.contentAssistEnabled );
128         sourceViewer.configure( sourceViewerConfiguration );
129
130         // set font
131
Font font = JFaceResources.getFont( JFaceResources.TEXT_FONT );
132         sourceViewer.getTextWidget().setFont( font );
133
134         // setup document
135
try
136         {
137             editorInput = new NonExistingLdifEditorInput();
138             documentProvider = new LdifDocumentProvider();
139             documentProvider.connect( editorInput );
140
141             IDocument document = documentProvider.getDocument( editorInput );
142             document.set( initialLdif );
143             sourceViewer.setDocument( document );
144         }
145         catch ( CoreException e )
146         {
147             e.printStackTrace();
148         }
149
150         // listener
151
sourceViewer.addTextListener( this );
152
153         // focus
154
sourceViewer.getControl().setFocus();
155     }
156
157
158     /**
159      * {@inheritDoc}
160      */

161     public IConnection getConnection()
162     {
163         return connection;
164     }
165
166
167     /**
168      * {@inheritDoc}
169      */

170     public LdifFile getLdifModel()
171     {
172         return documentProvider.getLdifModel();
173     }
174
175
176     /**
177      * {@inheritDoc}
178      */

179     public Object JavaDoc getAdapter( Class JavaDoc adapter )
180     {
181         return null;
182     }
183
184
185     /**
186      * {@inheritDoc}
187      */

188     public void textChanged( TextEvent event )
189     {
190         super.notifyListeners();
191     }
192
193
194     /**
195      * Gets the source viewer.
196      *
197      * @return the source viewer
198      */

199     public SourceViewer getSourceViewer()
200     {
201         return sourceViewer;
202     }
203
204
205     /**
206      * Gets the source viewer configuration.
207      *
208      * @return the source viewer configuration
209      */

210     public LdifSourceViewerConfiguration getSourceViewerConfiguration()
211     {
212         return sourceViewerConfiguration;
213     }
214
215 }
216
Popular Tags