KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > search > FilterWidget


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.widgets.search;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
25 import org.apache.directory.ldapstudio.browser.common.dialogs.FilterDialog;
26 import org.apache.directory.ldapstudio.browser.common.filtereditor.FilterContentAssistProcessor;
27 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
28 import org.apache.directory.ldapstudio.browser.common.widgets.BrowserWidget;
29 import org.apache.directory.ldapstudio.browser.common.widgets.DialogContentAssistant;
30 import org.apache.directory.ldapstudio.browser.common.widgets.HistoryUtils;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterParser;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.swt.events.ModifyEvent;
35 import org.eclipse.swt.events.ModifyListener;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Combo;
41 import org.eclipse.swt.widgets.Composite;
42
43
44 /**
45  * The FileterWidget could be used to specify an LDAP filter.
46  * It is composed of a text with a content assit to enter
47  * a filter and a button to open a {@link FilterDialog}.
48  *
49  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
50  * @version $Rev$, $Date$
51  */

52 public class FilterWidget extends BrowserWidget
53 {
54
55     /** The filter combo. */
56     private Combo filterCombo;
57
58     /** The button to open the filter editor. */
59     private Button filterEditorButton;
60
61     /** The content assist processor. */
62     private FilterContentAssistProcessor contentAssistProcessor;
63
64     /** The connection. */
65     private IConnection connection;
66
67     /** The inital filter. */
68     private String JavaDoc initalFilter;
69
70
71     /**
72      * Creates a new instance of FilterWidget.
73      *
74      * @param connection the connection
75      * @param initalFilter the inital filter
76      */

77     public FilterWidget( IConnection connection, String JavaDoc initalFilter )
78     {
79         this.connection = connection;
80         this.initalFilter = initalFilter;
81     }
82
83
84     /**
85      * Creates a new instance of FilterWidget with no connection and
86      * no initial filter.
87      */

88     public FilterWidget()
89     {
90         this.connection = null;
91         this.initalFilter = null;
92     }
93
94
95     /**
96      * Creates the widget.
97      *
98      * @param parent the parent
99      */

100     public void createWidget( final Composite parent )
101     {
102         // Combo
103
filterCombo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
104         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
105         gd.horizontalSpan = 1;
106         gd.widthHint = 200;
107         filterCombo.setLayoutData( gd );
108
109         filterCombo.addModifyListener( new ModifyListener()
110         {
111             public void modifyText( ModifyEvent e )
112             {
113                 notifyListeners();
114             }
115         } );
116
117         // Content assist
118
LdapFilterParser parser = new LdapFilterParser();
119         contentAssistProcessor = new FilterContentAssistProcessor( parser );
120         DialogContentAssistant fca = new DialogContentAssistant();
121         fca.enableAutoInsert( true );
122         fca.enableAutoActivation( true );
123         fca.setAutoActivationDelay( 100 );
124         fca.setContentAssistProcessor( contentAssistProcessor, IDocument.DEFAULT_CONTENT_TYPE );
125         fca.install( filterCombo );
126
127         // Filter editor button
128
filterEditorButton = BaseWidgetUtils.createButton( parent, "F&ilter Editor...", 1 );
129         filterEditorButton.addSelectionListener( new SelectionAdapter()
130         {
131             public void widgetSelected( SelectionEvent e )
132             {
133                 if ( connection != null )
134                 {
135                     FilterDialog dialog = new FilterDialog( parent.getShell(), "Filter Editor", filterCombo.getText(),
136                         connection );
137                     dialog.open();
138                     String JavaDoc filter = dialog.getFilter();
139                     if ( filter != null )
140                     {
141                         filterCombo.setText( filter );
142                     }
143                 }
144             }
145         } );
146
147         // filter history
148
String JavaDoc[] history = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_SEARCH_FILTER_HISTORY );
149         filterCombo.setItems( history );
150
151         // initial values
152
setConnection( connection );
153         filterCombo.setText( initalFilter == null ? "(objectClass=*)" : initalFilter );
154     }
155
156
157     /**
158      * Gets the filter.
159      *
160      * @return the filter
161      */

162     public String JavaDoc getFilter()
163     {
164         return filterCombo.getText();
165     }
166
167
168     /**
169      * Sets the filter.
170      *
171      * @param filter the filter
172      */

173     public void setFilter( String JavaDoc filter )
174     {
175         filterCombo.setText( filter );
176     }
177
178
179     /**
180      * Sets the connection.
181      *
182      * @param connection the connection
183      */

184     public void setConnection( IConnection connection )
185     {
186         this.connection = connection;
187         contentAssistProcessor.setPossibleAttributeTypes( connection == null ? new String JavaDoc[0] : connection.getSchema()
188             .getAttributeTypeDescriptionNames() );
189     }
190
191
192     /**
193      * Saves dialog settings.
194      */

195     public void saveDialogSettings()
196     {
197         HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_SEARCH_FILTER_HISTORY, filterCombo.getText() );
198     }
199
200
201     /**
202      * Sets the focus.
203      */

204     public void setFocus()
205     {
206         // filterCombo.setFocus();
207
}
208
209
210     /**
211      * Sets the enabled state of the widget.
212      *
213      * @param b true to enable the widget, false to disable the widget
214      */

215     public void setEnabled( boolean b )
216     {
217         filterCombo.setEnabled( b );
218         filterEditorButton.setEnabled( b );
219     }
220
221 }
222
Popular Tags