KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > text > LdifCompletionProcessor


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.editor.text;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
29 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
30 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifInvalidPart;
31 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord;
33 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModDnRecord;
34 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModifyRecord;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
36 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
37 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifInvalidContainer;
38 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec;
39 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
40 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifSepContainer;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifChangeTypeLine;
43 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine;
44 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
45 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator;
46 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
47 import org.apache.directory.ldapstudio.ldifeditor.editor.ILdifEditor;
48
49 import org.eclipse.jface.preference.IPreferenceStore;
50 import org.eclipse.jface.text.BadLocationException;
51 import org.eclipse.jface.text.IDocument;
52 import org.eclipse.jface.text.IRegion;
53 import org.eclipse.jface.text.ITextViewer;
54 import org.eclipse.jface.text.contentassist.CompletionProposal;
55 import org.eclipse.jface.text.contentassist.ContentAssistant;
56 import org.eclipse.jface.text.contentassist.ICompletionProposal;
57 import org.eclipse.jface.text.contentassist.IContextInformation;
58 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
59 import org.eclipse.jface.text.templates.Template;
60 import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
61 import org.eclipse.jface.text.templates.TemplateContextType;
62 import org.eclipse.swt.graphics.Image;
63
64
65 public class LdifCompletionProcessor extends TemplateCompletionProcessor
66 {
67
68     // private final static String DN = "dn: ";
69
private final static String JavaDoc CT_ADD = "changetype: add" + BrowserCoreConstants.LINE_SEPARATOR;
70
71     private final static String JavaDoc CT_MODIFY = "changetype: modify" + BrowserCoreConstants.LINE_SEPARATOR;
72
73     private final static String JavaDoc CT_DELETE = "changetype: delete" + BrowserCoreConstants.LINE_SEPARATOR;
74
75     private final static String JavaDoc CT_MODDN = "changetype: moddn" + BrowserCoreConstants.LINE_SEPARATOR;
76
77     private final static String JavaDoc MD_NEWRDN = "newrdn: ";
78
79     private final static String JavaDoc MD_DELETEOLDRDN_TRUE = "deleteoldrdn: 1";
80
81     // private final static String MD_DELETEOLDRDN_FALSE = "deleteoldrdn:
82
// 0";
83
private final static String JavaDoc MD_NEWSUPERIOR = "newsuperior: ";
84
85     private final ILdifEditor editor;
86
87     private final ContentAssistant contentAssistant;
88
89
90     public LdifCompletionProcessor( ILdifEditor editor, ContentAssistant contentAssistant )
91     {
92         this.editor = editor;
93         this.contentAssistant = contentAssistant;
94     }
95
96
97     public ICompletionProposal[] computeCompletionProposals( ITextViewer viewer, int offset )
98     {
99
100         IPreferenceStore store = LdifEditorActivator.getDefault().getPreferenceStore();
101         contentAssistant.enableAutoInsert( store
102             .getBoolean( LdifEditorConstants.PREFERENCE_LDIFEDITOR_CONTENTASSIST_INSERTSINGLEPROPOSALAUTO ) );
103         contentAssistant.enableAutoActivation( store
104             .getBoolean( LdifEditorConstants.PREFERENCE_LDIFEDITOR_CONTENTASSIST_ENABLEAUTOACTIVATION ) );
105         contentAssistant.setAutoActivationDelay( store
106             .getInt( LdifEditorConstants.PREFERENCE_LDIFEDITOR_CONTENTASSIST_AUTOACTIVATIONDELAY ) );
107
108         List JavaDoc proposalList = new ArrayList JavaDoc();
109
110         LdifFile model = editor.getLdifModel();
111         LdifContainer container = LdifFile.getContainer( model, offset );
112         LdifContainer innerContainer = container != null ? LdifFile.getInnerContainer( container, offset ) : null;
113         LdifPart part = container != null ? LdifFile.getContainerContent( container, offset ) : null;
114         int documentLine = -1;
115         int documentLineOffset = -1;
116         String JavaDoc prefix = "";
117         try
118         {
119             documentLine = viewer.getDocument().getLineOfOffset( offset );
120             documentLineOffset = viewer.getDocument().getLineOffset( documentLine );
121             prefix = viewer.getDocument().get( documentLineOffset, offset - documentLineOffset );
122         }
123         catch ( BadLocationException e )
124         {
125         }
126         // TemplateContextType contextType = getContextType(viewer, new
127
// Region(offset, 0));
128

129         // Add context dependend template proposals
130
ICompletionProposal[] templateProposals = super.computeCompletionProposals( viewer, offset );
131         if ( templateProposals != null )
132         {
133             proposalList.addAll( Arrays.asList( templateProposals ) );
134         }
135
136         // changetype: xxx
137
if ( container instanceof LdifRecord )
138         {
139             LdifRecord record = ( LdifRecord ) container;
140             LdifPart[] parts = record.getParts();
141             if ( parts.length > 1 && ( !( parts[1] instanceof LdifChangeTypeLine ) || !parts[1].isValid() ) )
142             {
143                 if ( CT_ADD.startsWith( prefix ) )
144                     proposalList.add( new CompletionProposal( CT_ADD, offset - prefix.length(), prefix.length(), CT_ADD
145                         .length(), LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_ADD ), CT_ADD
146                         .substring( 0, CT_ADD.length() - BrowserCoreConstants.LINE_SEPARATOR.length() ), null, null ) );
147                 if ( CT_MODIFY.startsWith( prefix ) )
148                     proposalList.add( new CompletionProposal( CT_MODIFY, offset - prefix.length(), prefix.length(),
149                         CT_MODIFY.length(),
150                         LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MODIFY ), CT_MODIFY
151                             .substring( 0, CT_MODIFY.length() - BrowserCoreConstants.LINE_SEPARATOR.length() ), null,
152                         null ) );
153                 if ( CT_DELETE.startsWith( prefix ) )
154                     proposalList.add( new CompletionProposal( CT_DELETE, offset - prefix.length(), prefix.length(),
155                         CT_DELETE.length(),
156                         LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_DELETE ), CT_DELETE
157                             .substring( 0, CT_DELETE.length() - BrowserCoreConstants.LINE_SEPARATOR.length() ), null,
158                         null ) );
159                 if ( CT_MODDN.startsWith( prefix ) )
160                     proposalList.add( new CompletionProposal( CT_MODDN, offset - prefix.length(), prefix.length(),
161                         CT_MODDN.length(), LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_RENAME ),
162                         CT_MODDN.substring( 0, CT_MODDN.length() - BrowserCoreConstants.LINE_SEPARATOR.length() ),
163                         null, null ) );
164             }
165
166         }
167
168         // changetype: modify
169
if ( container instanceof LdifChangeModDnRecord )
170         {
171             LdifChangeModDnRecord record = ( LdifChangeModDnRecord ) container;
172             if ( ( record.getNewrdnLine() == null || !record.getNewrdnLine().isValid() )
173                 && MD_NEWRDN.startsWith( prefix ) )
174             {
175                 proposalList.add( new CompletionProposal( MD_NEWRDN, offset - prefix.length(), prefix.length(),
176                     MD_NEWRDN.length(), null, null, null, null ) );
177             }
178             if ( ( record.getDeloldrdnLine() == null || !record.getDeloldrdnLine().isValid() )
179                 && MD_DELETEOLDRDN_TRUE.startsWith( prefix ) )
180             {
181                 proposalList.add( new CompletionProposal( MD_DELETEOLDRDN_TRUE, offset - prefix.length(), prefix
182                     .length(), MD_DELETEOLDRDN_TRUE.length(), null, null, null, null ) );
183             }
184             if ( ( record.getNewsuperiorLine() == null || !record.getNewsuperiorLine().isValid() )
185                 && MD_NEWSUPERIOR.startsWith( prefix ) )
186             {
187                 proposalList.add( new CompletionProposal( MD_NEWSUPERIOR, offset - prefix.length(), prefix.length(),
188                     MD_NEWSUPERIOR.length(), null, null, null, null ) );
189             }
190         }
191
192         // modspecs
193
if ( innerContainer instanceof LdifModSpec )
194         {
195             LdifModSpec modSpec = ( LdifModSpec ) innerContainer;
196             String JavaDoc att = modSpec.getModSpecType().getRawAttributeDescription();
197             if ( att != null && att.startsWith( prefix ) )
198             {
199                 proposalList.add( new CompletionProposal( att, offset - prefix.length(), prefix.length(), att.length(),
200                     null, null, null, null ) );
201             }
202         }
203
204         // attribute descriptions
205
if ( container instanceof LdifContentRecord || container instanceof LdifChangeAddRecord )
206         {
207
208             if ( part instanceof LdifInvalidPart
209                 || part instanceof LdifAttrValLine
210                 || ( part instanceof LdifSepLine && ( container instanceof LdifContentRecord || container instanceof LdifChangeAddRecord ) ) )
211             {
212
213                 String JavaDoc rawAttributeDescription = prefix;
214                 String JavaDoc rawValueType = "";
215
216                 if ( part instanceof LdifAttrValLine )
217                 {
218                     LdifAttrValLine line = ( LdifAttrValLine ) part;
219                     rawAttributeDescription = line.getRawAttributeDescription();
220                     rawValueType = line.getRawValueType();
221                 }
222
223                 if ( offset <= part.getOffset() + rawAttributeDescription.length() )
224                 {
225                     Schema schema = editor.getConnection() != null ? editor.getConnection().getSchema()
226                         : Schema.DEFAULT_SCHEMA;
227                     String JavaDoc[] attributeNames = schema.getAttributeTypeDescriptionNames();
228                     Arrays.sort( attributeNames );
229                     for ( int a = 0; a < attributeNames.length; a++ )
230                     {
231                         if ( rawAttributeDescription.length() == 0
232                             || attributeNames[a].startsWith( rawAttributeDescription ) )
233                         {
234
235                             String JavaDoc proposal = attributeNames[a];
236
237                             if ( rawValueType.length() == 0 )
238                             {
239                                 if ( schema.getAttributeTypeDescription( proposal ).isBinary() )
240                                 {
241                                     proposal += ":: ";
242                                 }
243                                 else
244                                 {
245                                     proposal += ": ";
246                                 }
247                             }
248
249                             proposalList
250                                 .add( new CompletionProposal( proposal, offset - rawAttributeDescription.length(),
251                                     rawAttributeDescription.length(), proposal.length() ) );
252                         }
253                     }
254                 }
255             }
256         }
257
258         // comment
259
boolean commentOnly = false;
260         if ( documentLineOffset == offset )
261         {
262             commentOnly = proposalList.isEmpty();
263             proposalList.add( new CompletionProposal( "# ", offset, 0, 2, LdifEditorActivator.getDefault().getImage(
264                 LdifEditorConstants.IMG_LDIF_COMMENT ), "# - Comment", null, null ) );
265         }
266
267         // adjust auto-insert
268
this.contentAssistant.enableAutoInsert( !commentOnly );
269
270         ICompletionProposal[] proposals = ( ICompletionProposal[] ) proposalList.toArray( new ICompletionProposal[0] );
271         return proposals;
272
273     }
274
275
276     protected String JavaDoc extractPrefix( ITextViewer viewer, int offset )
277     {
278
279         IDocument document = viewer.getDocument();
280         if ( offset > document.getLength() )
281             return ""; //$NON-NLS-1$
282

283         try
284         {
285             int documentLine = viewer.getDocument().getLineOfOffset( offset );
286             int documentLineOffset = viewer.getDocument().getLineOffset( documentLine );
287             String JavaDoc prefix = viewer.getDocument().get( documentLineOffset, offset - documentLineOffset );
288             return prefix;
289         }
290         catch ( BadLocationException e )
291         {
292             return ""; //$NON-NLS-1$
293
}
294     }
295
296
297     public IContextInformation[] computeContextInformation( ITextViewer viewer, int offset )
298     {
299         return null;
300     }
301
302
303     public char[] getCompletionProposalAutoActivationCharacters()
304     {
305
306         char[] chars = new char[53];
307         for ( int i = 0; i < 26; i++ )
308             chars[i] = ( char ) ( 'a' + i );
309         for ( int i = 0; i < 26; i++ )
310             chars[i + 26] = ( char ) ( 'A' + i );
311         chars[52] = ':';
312
313         return chars;
314     }
315
316
317     public char[] getContextInformationAutoActivationCharacters()
318     {
319         return null;
320     }
321
322
323     public String JavaDoc getErrorMessage()
324     {
325         return null;
326     }
327
328
329     public IContextInformationValidator getContextInformationValidator()
330     {
331         return null;
332     }
333
334
335     protected Template[] getTemplates( String JavaDoc contextTypeId )
336     {
337         Template[] templates = LdifEditorActivator.getDefault().getLdifTemplateStore().getTemplates( contextTypeId );
338         return templates;
339     }
340
341
342     protected TemplateContextType getContextType( ITextViewer viewer, IRegion region )
343     {
344
345         int offset = region.getOffset();
346
347         LdifFile model = editor.getLdifModel();
348         LdifContainer container = LdifFile.getContainer( model, offset );
349         LdifContainer innerContainer = container != null ? LdifFile.getInnerContainer( container, offset ) : null;
350         LdifPart part = container != null ? LdifFile.getContainerContent( container, offset ) : null;
351         int documentLine = -1;
352         int documentLineOffset = -1;
353         String JavaDoc prefix = "";
354         try
355         {
356             documentLine = viewer.getDocument().getLineOfOffset( offset );
357             documentLineOffset = viewer.getDocument().getLineOffset( documentLine );
358             prefix = viewer.getDocument().get( documentLineOffset, offset - documentLineOffset );
359         }
360         catch ( BadLocationException e )
361         {
362         }
363
364         // FILE
365
if ( container == null && innerContainer == null && part == null )
366         {
367             return LdifEditorActivator.getDefault().getLdifTemplateContextTypeRegistry().getContextType(
368                 LdifEditorConstants.LDIF_FILE_TEMPLATE_ID );
369         }
370         if ( container instanceof LdifSepContainer && innerContainer == null && part instanceof LdifSepLine )
371         {
372             return LdifEditorActivator.getDefault().getLdifTemplateContextTypeRegistry().getContextType(
373                 LdifEditorConstants.LDIF_FILE_TEMPLATE_ID );
374         }
375         if ( ( container instanceof LdifInvalidContainer && part instanceof LdifInvalidPart && "d".equals( prefix ) )
376             || ( container instanceof LdifContentRecord && part instanceof LdifInvalidPart && "dn".equals( prefix ) )
377             || ( container instanceof LdifContentRecord && part instanceof LdifInvalidPart && "dn:".equals( prefix ) ) )
378         {
379             return LdifEditorActivator.getDefault().getLdifTemplateContextTypeRegistry().getContextType(
380                 LdifEditorConstants.LDIF_FILE_TEMPLATE_ID );
381         }
382
383         // MODIFICATION RECORD
384
if ( container instanceof LdifChangeModifyRecord && innerContainer == null
385             && ( part instanceof LdifSepLine || part instanceof LdifInvalidPart ) )
386         {
387             return LdifEditorActivator.getDefault().getLdifTemplateContextTypeRegistry().getContextType(
388                 LdifEditorConstants.LDIF_MODIFICATION_RECORD_TEMPLATE_ID );
389         }
390
391         // MODIFICATION ITEM
392
if ( container instanceof LdifChangeModifyRecord && innerContainer instanceof LdifModSpec )
393         {
394             return LdifEditorActivator.getDefault().getLdifTemplateContextTypeRegistry().getContextType(
395                 LdifEditorConstants.LDIF_MODIFICATION_ITEM_TEMPLATE_ID );
396         }
397
398         // MODDN RECORD
399
if ( container instanceof LdifChangeModDnRecord && innerContainer == null
400             && ( part instanceof LdifSepLine || part instanceof LdifInvalidPart ) )
401         {
402             return LdifEditorActivator.getDefault().getLdifTemplateContextTypeRegistry().getContextType(
403                 LdifEditorConstants.LDIF_MODDN_RECORD_TEMPLATE_ID );
404         }
405
406         // TemplateContextType contextType =
407
// Activator.getDefault().getContextTypeRegistry().getContextType(LdifEditorConstants.LDIF_FILE_TEMPLATE_ID);
408
// TemplateContextType contextType =
409
// Activator.getDefault().getContextTypeRegistry().getContextType(LdifEditorConstants.LDIF_MODIFICATION_RECORD_TEMPLATE_ID);
410

411         return null;
412
413     }
414
415
416     protected Image getImage( Template template )
417     {
418
419         if ( template.getPattern().indexOf( "add: " ) > -1 )
420         {
421             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_ADD );
422         }
423         else if ( template.getPattern().indexOf( "replace: " ) > -1 )
424         {
425             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_REPLACE );
426         }
427         else if ( template.getPattern().indexOf( "delete: " ) > -1 )
428         {
429             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_DELETE );
430         }
431
432         else if ( template.getPattern().indexOf( "changetype: add" ) > -1 )
433         {
434             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_ADD );
435         }
436         else if ( template.getPattern().indexOf( "changetype: modify" ) > -1 )
437         {
438             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MODIFY );
439         }
440         else if ( template.getPattern().indexOf( "changetype: delete" ) > -1 )
441         {
442             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_DELETE );
443         }
444         else if ( template.getPattern().indexOf( "changetype: moddn" ) > -1 )
445         {
446             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_RENAME );
447         }
448         else if ( template.getPattern().indexOf( "dn: " ) > -1 )
449         {
450             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_ENTRY );
451         }
452
453         else
454         {
455             return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_TEMPLATE );
456         }
457
458     }
459
460 }
461
Popular Tags