KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > filtereditor > FilterAutoEditStrategy


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.browser.common.filtereditor;
22
23
24 import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilter;
25 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterParser;
26 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterToken;
27
28 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
29 import org.eclipse.jface.text.DocumentCommand;
30 import org.eclipse.jface.text.IAutoEditStrategy;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.TextUtilities;
33 import org.eclipse.jface.text.source.SourceViewer;
34
35
36 // TODO: Refactor Filter Editor
37
public class FilterAutoEditStrategy extends DefaultAutoIndentStrategy implements IAutoEditStrategy
38 {
39
40     public static final String JavaDoc INDENT_STRING = " ";
41
42     private LdapFilterParser parser;
43
44     private SourceViewer sourceViewer;
45
46     private int autoRightParenthesisFilterOffset;
47
48
49     public FilterAutoEditStrategy( SourceViewer sourceViewer, LdapFilterParser parser )
50     {
51         super();
52         this.sourceViewer = sourceViewer;
53         this.parser = parser;
54         this.autoRightParenthesisFilterOffset = -1;
55     }
56
57
58     public void customizeDocumentCommand( IDocument d, DocumentCommand c )
59     {
60
61         LdapFilter filter = this.parser.getModel().getFilter( c.offset );
62
63         // this.dumpDocumentCommand(c);
64

65         if ( c.length == 0 && c.text != null )
66         {
67             // new line
68
if ( TextUtilities.endsWith( d.getLegalLineDelimiters(), c.text ) != -1 )
69             {
70                 super.customizeDocumentCommand( d, c );
71                 if ( filter != null && filter.getFilterComponent() != null )
72                 {
73                     LdapFilterToken startToken = filter.getFilterComponent().getStartToken();
74                     if ( startToken != null
75                         && ( startToken.getType() == LdapFilterToken.AND || startToken.getType() == LdapFilterToken.OR ) )
76                     {
77
78                         if ( startToken.getOffset() == c.offset - 1 )
79                         {
80                             c.text += INDENT_STRING;
81                             if ( filter.getStopToken() != null && filter.getStopToken().getOffset() == c.offset )
82                             {
83                                 c.caretOffset = c.offset + c.text.length();
84                                 c.shiftsCaret = false;
85                                 c.text += "\n";
86                                 super.customizeDocumentCommand( d, c );
87                             }
88                         }
89                     }
90                 }
91             }
92
93             // filter start/stop
94
if ( c.text.equals( "(" ) )
95             {
96                 c.text = "()";
97                 c.caretOffset = c.offset + 1;
98                 c.shiftsCaret = false;
99                 this.autoRightParenthesisFilterOffset = c.offset;
100             }
101             else if ( c.text.equals( ")" ) )
102             {
103                 LdapFilter filter2 = this.parser.getModel().getFilter( this.autoRightParenthesisFilterOffset );
104                 if ( filter2 != null && filter2.getStopToken() != null
105                     && filter2.getStopToken().getOffset() == c.offset )
106                 {
107                     c.text = "";
108                     c.caretOffset = c.offset + 1;
109                     c.shiftsCaret = false;
110                 }
111                 this.autoRightParenthesisFilterOffset = -1;
112             }
113
114             // tab to IDENT_STRING
115
if ( c.text.equals( "\t" ) )
116             {
117                 c.text = INDENT_STRING;
118             }
119
120             // smart formatting
121
if ( filter != null && filter.getStartToken() != null && filter.getFilterComponent() == null )
122             {
123                 if ( c.text.equals( "&" ) || c.text.equals( "|" ) )
124                 {
125                     if ( filter.getStartToken().getOffset() == c.offset - 1 )
126                     {
127                         c.text += "\n";
128                         super.customizeDocumentCommand( d, c );
129                         c.text += INDENT_STRING + "()";
130                         c.caretOffset = c.offset + c.text.length() - 1;
131                         c.shiftsCaret = false;
132                         if ( filter.getStopToken() != null && filter.getStopToken().getOffset() == c.offset )
133                         {
134                             c.text += "\n";
135                             super.customizeDocumentCommand( d, c );
136                         }
137
138                     }
139                 }
140                 else if ( c.text.equals( "!" ) )
141                 {
142                     if ( filter.getStartToken().getOffset() == c.offset - 1 )
143                     {
144                         c.text += "()";
145                         c.caretOffset = c.offset + c.text.length() - 1;
146                         c.shiftsCaret = false;
147                     }
148                 }
149             }
150
151         }
152     }
153
154
155     private void autoIndentAfterNewLine()
156     {
157
158     }
159
160
161     private void dumpDocumentCommand( DocumentCommand command )
162     {
163         System.out.println( "----------------------------------" );
164         System.out.println( " offset : " + command.offset );
165         System.out.println( " length : " + command.length );
166         System.out.println( " text : " + command.text );
167         System.out.println( " caretoffset: " + command.caretOffset );
168         System.out.println( " shiftsCaret: " + command.shiftsCaret );
169         System.out.println( " doit : " + command.doit );
170
171     }
172 }
173
Popular Tags