KickJava   Java API By Example, From Geeks To Geeks.

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


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.LdapAndFilterComponent;
25 import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilter;
26 import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilterComponent;
27 import org.apache.directory.ldapstudio.browser.core.model.filter.LdapFilterItemComponent;
28 import org.apache.directory.ldapstudio.browser.core.model.filter.LdapNotFilterComponent;
29 import org.apache.directory.ldapstudio.browser.core.model.filter.LdapOrFilterComponent;
30 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterParser;
31
32 import org.eclipse.jface.text.formatter.IFormattingStrategy;
33 import org.eclipse.jface.text.source.SourceViewer;
34
35
36 public class FilterFormattingStrategy implements IFormattingStrategy
37 {
38
39     private LdapFilterParser parser;
40
41     private SourceViewer sourceViewer;
42
43
44     public FilterFormattingStrategy( SourceViewer sourceViewer, LdapFilterParser parser )
45     {
46         super();
47         this.parser = parser;
48         this.sourceViewer = sourceViewer;
49     }
50
51
52     public void formatterStarts( String JavaDoc initialIndentation )
53     {
54     }
55
56
57     public String JavaDoc format( String JavaDoc content, boolean isLineStart, String JavaDoc indentation, int[] positions )
58     {
59         // this.parser.parse(content);
60
LdapFilter model = this.parser.getModel();
61         if ( model != null && model.isValid() )
62         {
63             this.sourceViewer.getDocument().set( get( model, 0 ) );
64         }
65
66         return null;
67     }
68
69
70     private String JavaDoc get( LdapFilter filter, int indent )
71     {
72         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
73
74         for ( int i = 0; i < indent; i++ )
75             sb.append( FilterAutoEditStrategy.INDENT_STRING );
76
77         LdapFilterComponent fc = filter.getFilterComponent();
78         if ( fc instanceof LdapFilterItemComponent )
79         {
80             sb.append( '(' ).append( ( ( LdapFilterItemComponent ) fc ).toString() ).append( ')' );
81         }
82         else if ( fc instanceof LdapNotFilterComponent )
83         {
84             sb.append( "(!" );
85             LdapNotFilterComponent lnfc = ( LdapNotFilterComponent ) fc;
86             if ( lnfc.getFilters().length > 0
87                 && lnfc.getFilters()[0].getFilterComponent() instanceof LdapFilterItemComponent )
88             {
89                 sb.append( get( ( lnfc ).getFilters()[0], 0 ) );
90             }
91             else
92             {
93                 sb.append( '\n' );
94                 sb.append( get( ( lnfc ).getFilters()[0], indent + 1 ) );
95                 sb.append( '\n' );
96                 for ( int i = 0; i < indent; i++ )
97                     sb.append( FilterAutoEditStrategy.INDENT_STRING );
98             }
99             sb.append( ')' );
100         }
101         else if ( fc instanceof LdapAndFilterComponent )
102         {
103             sb.append( "(&" );
104             sb.append( '\n' );
105             LdapFilter[] filters = ( ( LdapAndFilterComponent ) fc ).getFilters();
106             for ( int i = 0; i < filters.length; i++ )
107             {
108                 sb.append( get( filters[i], indent + 1 ) );
109                 sb.append( '\n' );
110             }
111             for ( int i = 0; i < indent; i++ )
112                 sb.append( FilterAutoEditStrategy.INDENT_STRING );
113             sb.append( ')' );
114         }
115         else if ( fc instanceof LdapOrFilterComponent )
116         {
117             sb.append( "(|" );
118             sb.append( '\n' );
119             LdapFilter[] filters = ( ( LdapOrFilterComponent ) fc ).getFilters();
120             for ( int i = 0; i < filters.length; i++ )
121             {
122                 sb.append( get( filters[i], indent + 1 ) );
123                 sb.append( '\n' );
124             }
125             for ( int i = 0; i < indent; i++ )
126                 sb.append( FilterAutoEditStrategy.INDENT_STRING );
127             sb.append( ')' );
128         }
129
130         return sb.toString();
131     }
132
133
134     public void formatterStops()
135     {
136     }
137
138 }
139
Popular Tags