KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > entryeditor > EntryEditorWidgetQuickFilterWidget


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.entryeditor;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
26 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
27 import org.eclipse.jface.preference.PreferenceConverter;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.graphics.Font;
35 import org.eclipse.swt.graphics.FontData;
36 import org.eclipse.swt.graphics.RGB;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Text;
42
43
44 /**
45  * The EntryEditorWidgetQuickFilterWidget implements an instant search
46  * for the entry editor widget. It contains separate search fields for
47  * attribute type and/or value.
48  *
49  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
50  * @version $Rev$, $Date$
51  */

52 public class EntryEditorWidgetQuickFilterWidget
53 {
54
55     /** The filter to propagate the entered filter phrases. */
56     private EntryEditorWidgetFilter filter;
57
58     /** The entry editor widget. */
59     private EntryEditorWidget entryEditorWidget;
60
61     /** The parent, used to create the composite. */
62     private Composite parent;
63
64     /** The outer composite. */
65     private Composite composite;
66
67     /** The inner composite, it is created/destroyed when showing/hiding the quick filter. */
68     private Composite innerComposite;
69
70     /** The quick filter attribute text. */
71     private Text quickFilterAttributeText;
72
73     /** The quick filter value text. */
74     private Text quickFilterValueText;
75
76     /** The clear quick filter button. */
77     private Button clearQuickFilterButton;
78
79
80     /**
81      * Creates a new instance of EntryEditorWidgetQuickFilterWidget.
82      *
83      * @param filter the filter
84      * @param entryEditorWidget the entry editor widget
85      */

86     public EntryEditorWidgetQuickFilterWidget( EntryEditorWidgetFilter filter, EntryEditorWidget entryEditorWidget )
87     {
88         this.filter = filter;
89         this.entryEditorWidget = entryEditorWidget;
90     }
91
92
93     /**
94      * Creates the outer composite.
95      *
96      * @param parent the parent
97      */

98     public void createComposite( Composite parent )
99     {
100         this.parent = parent;
101
102         composite = BaseWidgetUtils.createColumnContainer( parent, 1, 1 );
103         GridLayout gl = new GridLayout();
104         gl.marginHeight = 2;
105         gl.marginWidth = 2;
106         composite.setLayout( gl );
107
108         innerComposite = null;
109     }
110
111
112     /**
113      * Creates the inner composite with its input fields.
114      */

115     private void create()
116     {
117         innerComposite = BaseWidgetUtils.createColumnContainer( composite, 3, 1 );
118
119         quickFilterAttributeText = new Text( innerComposite, SWT.BORDER );
120         quickFilterAttributeText.setLayoutData( new GridData( 200 - 14, SWT.DEFAULT ) );
121         quickFilterAttributeText.addModifyListener( new ModifyListener()
122         {
123             public void modifyText( ModifyEvent e )
124             {
125                 filter.setQuickFilterAttribute( quickFilterAttributeText.getText() );
126                 clearQuickFilterButton.setEnabled( !"".equals( quickFilterAttributeText.getText() )
127                     || !"".equals( quickFilterValueText.getText() ) );
128                 if ( !"".equals( quickFilterAttributeText.getText() ) )
129                 {
130                     RGB fgRgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
131                         BrowserCommonConstants.PREFERENCE_QUICKFILTER_FOREGROUND_COLOR );
132                     RGB bgRgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
133                         BrowserCommonConstants.PREFERENCE_QUICKFILTER_BACKGROUND_COLOR );
134                     Color fgColor = BrowserCommonActivator.getDefault().getColor( fgRgb );
135                     Color bgColor = BrowserCommonActivator.getDefault().getColor( bgRgb );
136                     quickFilterAttributeText.setForeground( fgColor );
137                     quickFilterAttributeText.setBackground( bgColor );
138                     FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
139                         .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_QUICKFILTER_FONT );
140                     Font font = BrowserCommonActivator.getDefault().getFont( fontData );
141                     quickFilterAttributeText.setFont( font );
142                 }
143                 else
144                 {
145                     quickFilterAttributeText.setBackground( null );
146                 }
147             }
148         } );
149
150         quickFilterValueText = new Text( innerComposite, SWT.BORDER );
151         quickFilterValueText.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
152         quickFilterValueText.addModifyListener( new ModifyListener()
153         {
154             public void modifyText( ModifyEvent e )
155             {
156                 filter.setQuickFilterValue( quickFilterValueText.getText() );
157                 clearQuickFilterButton.setEnabled( !"".equals( quickFilterAttributeText.getText() )
158                     || !"".equals( quickFilterValueText.getText() ) );
159                 if ( !"".equals( quickFilterValueText.getText() ) )
160                 {
161                     RGB fgRgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
162                         BrowserCommonConstants.PREFERENCE_QUICKFILTER_FOREGROUND_COLOR );
163                     RGB bgRgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
164                         BrowserCommonConstants.PREFERENCE_QUICKFILTER_BACKGROUND_COLOR );
165                     Color fgColor = BrowserCommonActivator.getDefault().getColor( fgRgb );
166                     Color bgColor = BrowserCommonActivator.getDefault().getColor( bgRgb );
167                     quickFilterValueText.setForeground( fgColor );
168                     quickFilterValueText.setBackground( bgColor );
169                     FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
170                         .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_QUICKFILTER_FONT );
171                     Font font = BrowserCommonActivator.getDefault().getFont( fontData );
172                     quickFilterValueText.setFont( font );
173                 }
174                 else
175                 {
176                     quickFilterValueText.setBackground( null );
177                 }
178             }
179         } );
180
181         clearQuickFilterButton = new Button( innerComposite, SWT.PUSH );
182         clearQuickFilterButton.setToolTipText( "Clear Quick Filter" );
183         clearQuickFilterButton.setImage( BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_CLEAR ) );
184         clearQuickFilterButton.setEnabled( false );
185         clearQuickFilterButton.addSelectionListener( new SelectionAdapter()
186         {
187             public void widgetSelected( SelectionEvent e )
188             {
189                 if ( !"".equals( quickFilterAttributeText.getText() ) )
190                 {
191                     quickFilterAttributeText.setText( "" );
192                 }
193                 if ( !"".equals( quickFilterValueText.getText() ) )
194                 {
195                     quickFilterValueText.setText( "" );
196                 }
197             }
198         } );
199
200         setEnabled( composite.isEnabled() );
201
202         composite.layout( true, true );
203         parent.layout( true, true );
204     }
205
206
207     /**
208      * Destroys the inner widget.
209      */

210     private void destroy()
211     {
212         if ( !"".equals( quickFilterAttributeText.getText() ) )
213         {
214             quickFilterAttributeText.setText( "" );
215         }
216         if ( !"".equals( quickFilterValueText.getText() ) )
217         {
218             quickFilterValueText.setText( "" );
219         }
220         innerComposite.dispose();
221         innerComposite = null;
222
223         composite.layout( true, true );
224         parent.layout( true, true );
225     }
226
227
228     /**
229      * Disposes this widget.
230      */

231     public void dispose()
232     {
233         if ( filter != null )
234         {
235             quickFilterAttributeText = null;
236             quickFilterValueText = null;
237             clearQuickFilterButton = null;
238             innerComposite = null;
239             composite.dispose();
240             composite = null;
241             parent = null;
242             filter = null;
243         }
244     }
245
246
247     /**
248      * Enables or disables this quick filter widget.
249      *
250      * @param enabled true to enable this quick filter widget, false to disable it
251      */

252     public void setEnabled( boolean enabled )
253     {
254         if ( composite != null && !composite.isDisposed() )
255         {
256             composite.setEnabled( enabled );
257         }
258         if ( innerComposite != null && !innerComposite.isDisposed() )
259         {
260             innerComposite.setEnabled( enabled );
261             quickFilterAttributeText.setEnabled( enabled );
262             quickFilterValueText.setEnabled( enabled );
263             clearQuickFilterButton.setEnabled( enabled );
264         }
265     }
266
267
268     /**
269      * Activates or deactivates this quick filter widget.
270      *
271      * @param visible true to crate this quick filter widget, false to destroy it
272      */

273     public void setActive( boolean visible )
274     {
275         if ( visible && innerComposite == null && composite != null )
276         {
277             create();
278             quickFilterAttributeText.setFocus();
279         }
280         else if ( !visible && innerComposite != null && composite != null )
281         {
282             destroy();
283             entryEditorWidget.getViewer().getTree().setFocus();
284         }
285     }
286
287 }
288
Popular Tags