KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > views > SearchView


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.schemas.view.views;
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.schemas.Activator;
29 import org.apache.directory.ldapstudio.schemas.Messages;
30 import org.apache.directory.ldapstudio.schemas.PluginConstants;
31 import org.apache.directory.ldapstudio.schemas.controller.SchemasViewController;
32 import org.apache.directory.ldapstudio.schemas.controller.actions.EraseSearchAction;
33 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
34 import org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent;
35 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
36 import org.apache.directory.ldapstudio.schemas.model.PoolListener;
37 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
38 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditor;
39 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditorInput;
40 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditor;
41 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditorInput;
42 import org.apache.log4j.Logger;
43 import org.eclipse.jface.action.IToolBarManager;
44 import org.eclipse.jface.viewers.TableViewer;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.FocusAdapter;
47 import org.eclipse.swt.events.FocusEvent;
48 import org.eclipse.swt.events.KeyAdapter;
49 import org.eclipse.swt.events.KeyEvent;
50 import org.eclipse.swt.events.ModifyEvent;
51 import org.eclipse.swt.events.ModifyListener;
52 import org.eclipse.swt.events.MouseAdapter;
53 import org.eclipse.swt.events.MouseEvent;
54 import org.eclipse.swt.events.SelectionAdapter;
55 import org.eclipse.swt.events.SelectionEvent;
56 import org.eclipse.swt.layout.GridData;
57 import org.eclipse.swt.layout.GridLayout;
58 import org.eclipse.swt.widgets.Combo;
59 import org.eclipse.swt.widgets.Composite;
60 import org.eclipse.swt.widgets.Label;
61 import org.eclipse.swt.widgets.Table;
62 import org.eclipse.swt.widgets.TableColumn;
63 import org.eclipse.ui.IEditorInput;
64 import org.eclipse.ui.IWorkbenchPage;
65 import org.eclipse.ui.PartInitException;
66 import org.eclipse.ui.PlatformUI;
67 import org.eclipse.ui.part.ViewPart;
68
69
70 /**
71  * This class represents the Search View.
72  *
73  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
74  * @version $Rev$, $Date$
75  */

76 public class SearchView extends ViewPart implements PoolListener
77 {
78     /** The view's ID */
79     public static final String JavaDoc ID = Activator.PLUGIN_ID + ".view.SearchView"; //$NON-NLS-1$
80

81     /** The Schema Pool */
82     private SchemaPool pool;
83
84     // UI fields
85
private Table resultsTable;
86     private TableViewer resultsTableViewer;
87     private Combo searchField;
88     private Combo scopeCombo;
89     private SearchViewContentProvider searchContentProvider;
90
91     /** The Type column */
92     private final String JavaDoc TYPE_COLUMN = Messages.getString( "SearchView.Type_Column" ); //$NON-NLS-1$
93

94     /** The Name column*/
95     private final String JavaDoc NAME_COLUMN = Messages.getString( "SearchView.Name_Column" ); //$NON-NLS-1$
96

97     /** The Schema column */
98     private final String JavaDoc SCHEMA_COLUMN = Messages.getString( "SearchView.Schema_Column" ); //$NON-NLS-1$
99

100     /** The Columns names Array */
101     private String JavaDoc[] columnNames = new String JavaDoc[]
102         { TYPE_COLUMN, NAME_COLUMN, SCHEMA_COLUMN, };
103
104     /** The Search All scope */
105     public static final String JavaDoc SEARCH_ALL = Messages.getString( "SearchView.Search_All_metadata" ); //$NON-NLS-1$
106

107     /** The Search Name scope */
108     public static final String JavaDoc SEARCH_NAME = Messages.getString( "SearchView.Search_Name" ); //$NON-NLS-1$
109

110     /** The Search OID scope */
111     public static final String JavaDoc SEARCH_OID = Messages.getString( "SearchView.Search_OID" ); //$NON-NLS-1$
112

113     /** The Search Description scope */
114     public static final String JavaDoc SEARCH_DESC = Messages.getString( "SearchView.Search_Description" ); //$NON-NLS-1$
115

116     /** The current Search type */
117     public static String JavaDoc currentSearchScope = SEARCH_ALL;
118
119
120     /* (non-Javadoc)
121      * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
122      */

123     @Override JavaDoc
124     public void createPartControl( Composite parent )
125     {
126         this.pool = SchemaPool.getInstance();
127         //we want to be notified if the pool has been modified
128
pool.addListener( this );
129
130         //top container
131
Composite top = new Composite( parent, SWT.NONE );
132
133         GridLayout layout = new GridLayout( 4, false );
134         top.setLayout( layout );
135
136         Label searchLabel = new Label( top, SWT.NONE );
137         searchLabel.setText( Messages.getString( "SearchView.Search" ) ); //$NON-NLS-1$
138

139         //search field
140
searchField = new Combo( top, SWT.DROP_DOWN | SWT.BORDER );
141         GridData gridData = new GridData( GridData.FILL, 0, true, false );
142         gridData.verticalAlignment = SWT.CENTER;
143         searchField.setLayoutData( gridData );
144
145         Label inLabel = new Label( top, SWT.NONE );
146         inLabel.setText( Messages.getString( "SearchView.in" ) ); //$NON-NLS-1$
147

148         //search scope combo
149
scopeCombo = new Combo( top, SWT.READ_ONLY | SWT.SINGLE );
150
151         gridData = new GridData( SWT.FILL, 0, false, false );
152         gridData.verticalAlignment = SWT.CENTER;
153         scopeCombo.setLayoutData( gridData );
154         scopeCombo.addSelectionListener( new SelectionAdapter()
155         {
156             public void widgetSelected( SelectionEvent e )
157             {
158                 currentSearchScope = scopeCombo.getText();
159                 resultsTableViewer.refresh();
160             }
161         } );
162         scopeCombo.add( SEARCH_ALL, 0 );
163         scopeCombo.add( SEARCH_NAME, 1 );
164         scopeCombo.add( SEARCH_OID, 2 );
165         scopeCombo.add( SEARCH_DESC, 3 );
166         scopeCombo.select( 0 );
167
168         // Create the table
169
createTable( top );
170         createTableViewer();
171         this.searchContentProvider = new SearchViewContentProvider();
172         resultsTableViewer.setContentProvider( searchContentProvider );
173         resultsTableViewer.setLabelProvider( new TableDecoratingLabelProvider( new SearchViewLabelProvider(), Activator
174             .getDefault().getWorkbench().getDecoratorManager().getLabelDecorator() ) );
175
176         initSearchHistory();
177         initListeners();
178         initToolbar();
179     }
180
181
182     private void initToolbar()
183     {
184         IToolBarManager toolbar = getViewSite().getActionBars().getToolBarManager();
185         toolbar.add( new EraseSearchAction( this ) );
186     }
187
188
189     /**
190      * Creates the Table
191      */

192     private void createTable( Composite parent )
193     {
194         resultsTable = new Table( parent, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION
195             | SWT.HIDE_SELECTION );
196
197         GridData gridData = new GridData( GridData.FILL, GridData.FILL, true, true, 4, 1 );
198         resultsTable.setLayoutData( gridData );
199
200         resultsTable.setLinesVisible( false );
201         resultsTable.setHeaderVisible( true );
202
203         // 1st column with image - NOTE: The SWT.CENTER has no effect!!
204
TableColumn column = new TableColumn( resultsTable, SWT.CENTER, 0 );
205         column.setText( columnNames[0] );
206         column.setWidth( 40 );
207
208         // 2nd column with name
209
column = new TableColumn( resultsTable, SWT.LEFT, 1 );
210         column.setText( columnNames[1] );
211         column.setWidth( 400 );
212
213         //3rd column with element defining schema
214
column = new TableColumn( resultsTable, SWT.LEFT, 2 );
215         column.setText( columnNames[2] );
216         column.setWidth( 100 );
217     }
218
219
220     /**
221      * Initializes the Listeners
222      */

223     private void initListeners()
224     {
225         searchField.addModifyListener( new ModifyListener()
226         {
227             public void modifyText( ModifyEvent e )
228             {
229                 resultsTableViewer.setInput( searchField.getText() );
230             }
231         } );
232
233         searchField.addKeyListener( new KeyAdapter()
234         {
235             public void keyReleased( KeyEvent e )
236             {
237                 if ( e.keyCode == 13 )
238                 {
239                     resultsTable.setFocus();
240                 }
241             }
242         } );
243
244         searchField.addFocusListener( new FocusAdapter()
245         {
246             public void focusLost( FocusEvent e )
247             {
248                 if ( !"".equals( searchField.getText() ) ) //$NON-NLS-1$
249
{
250                     String JavaDoc searchString = searchField.getText();
251                     saveHistory( PluginConstants.PREFS_SEARCH_VIEW_SEARCH_HISTORY, searchString );
252                     initSearchHistory();
253                     searchField.setText( searchString );
254                 }
255             }
256         } );
257
258         scopeCombo.addFocusListener( new FocusAdapter()
259         {
260             public void focusGained( FocusEvent arg0 )
261             {
262                 resultsTable.setFocus();
263             }
264         } );
265
266         resultsTable.addMouseListener( new MouseAdapter()
267         {
268             public void mouseDoubleClick( MouseEvent e )
269             {
270                 openEditor( ( Table ) e.getSource() );
271             }
272         } );
273
274         resultsTable.addKeyListener( new KeyAdapter()
275         {
276             public void keyPressed( KeyEvent e )
277             {
278                 if ( e.keyCode == SWT.ARROW_UP )
279                 {
280                     searchField.setFocus();
281                 }
282
283                 if ( e.keyCode == 13 ) // return key
284
{
285                     openEditor( ( Table ) e.getSource() );
286                 }
287             }
288         } );
289
290         resultsTable.addFocusListener( new FocusAdapter()
291         {
292             public void focusGained( FocusEvent e )
293             {
294                 if ( ( resultsTable.getSelectionCount() == 0 ) && ( resultsTable.getItemCount() != 0 ) )
295                 {
296                     resultsTable.select( 0 );
297                 }
298             }
299         } );
300     }
301
302
303     /**
304      * Open the editor associated with the current selection in the table
305      *
306      * @param table
307      * the associated table
308      */

309     private void openEditor( Table table )
310     {
311         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
312
313         IEditorInput input = null;
314         String JavaDoc editorId = null;
315
316         // Here is the double clicked item
317
Object JavaDoc item = table.getSelection()[0].getData();
318         if ( item instanceof AttributeType )
319         {
320             input = new AttributeTypeEditorInput( ( AttributeType ) item );
321             editorId = AttributeTypeEditor.ID;
322         }
323         else if ( item instanceof ObjectClass )
324         {
325             input = new ObjectClassEditorInput( ( ObjectClass ) item );
326             editorId = ObjectClassEditor.ID;
327         }
328
329         // Let's open the editor
330
if ( input != null )
331         {
332             try
333             {
334                 page.openEditor( input, editorId );
335             }
336             catch ( PartInitException exception )
337             {
338                 Logger.getLogger( SchemasViewController.class ).debug( "error when opening the editor" ); //$NON-NLS-1$
339
}
340         }
341     }
342
343
344     /**
345      * Creates the TableViewer
346      */

347     private void createTableViewer()
348     {
349         resultsTableViewer = new TableViewer( resultsTable );
350         resultsTableViewer.setUseHashlookup( true );
351         resultsTableViewer.setColumnProperties( columnNames );
352     }
353
354
355     /* (non-Javadoc)
356      * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
357      */

358     @Override JavaDoc
359     public void setFocus()
360     {
361         if ( searchField != null && !searchField.isDisposed() )
362         {
363             searchField.setFocus();
364         }
365     }
366
367
368     /* (non-Javadoc)
369      * @see org.safhaus.ldapstudio.model.PoolListener#poolChanged(org.safhaus.ldapstudio.model.SchemaPool, org.safhaus.ldapstudio.model.LDAPModelEvent)
370      */

371     public void poolChanged( SchemaPool p, LDAPModelEvent e )
372     {
373         resultsTableViewer.refresh();
374     }
375
376
377     /* (non-Javadoc)
378      * @see org.eclipse.ui.part.ViewPart#setPartName(java.lang.String)
379      */

380     public void setPartName( String JavaDoc partName )
381     {
382         super.setPartName( partName );
383     }
384
385
386     /**
387      * Initializes the Search History.
388      */

389     private void initSearchHistory()
390     {
391         searchField.setItems( loadHistory( PluginConstants.PREFS_SEARCH_VIEW_SEARCH_HISTORY ) );
392     }
393
394
395     /**
396      * Saves to the History.
397      *
398      * @param key
399      * the key to save to
400      * @param value
401      * the value to save
402      */

403     public static void saveHistory( String JavaDoc key, String JavaDoc value )
404     {
405         // get current history
406
String JavaDoc[] history = loadHistory( key );
407         List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>( Arrays.asList( history ) );
408
409         // add new value or move to first position
410
if ( list.contains( value ) )
411         {
412             list.remove( value );
413         }
414         list.add( 0, value );
415
416         // check history size
417
while ( list.size() > 15 )
418         {
419             list.remove( list.size() - 1 );
420         }
421
422         // save
423
history = ( String JavaDoc[] ) list.toArray( new String JavaDoc[list.size()] );
424         Activator.getDefault().getDialogSettings().put( key, history );
425
426     }
427
428
429     /**
430      * Loads History
431      *
432      * @param key
433      * the preference key
434      * @return
435      */

436     public static String JavaDoc[] loadHistory( String JavaDoc key )
437     {
438         String JavaDoc[] history = Activator.getDefault().getDialogSettings().getArray( key );
439         if ( history == null )
440         {
441             history = new String JavaDoc[0];
442         }
443         return history;
444     }
445
446
447     public void setSearch( String JavaDoc searchString, String JavaDoc scope )
448     {
449         scopeCombo.setText( scope );
450         currentSearchScope = scopeCombo.getText();
451         searchField.setText( searchString );
452         resultsTableViewer.setInput( searchString );
453         resultsTable.setFocus();
454     }
455 }
456
Popular Tags