KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > searchresult > SearchResultEditorUniversalListener


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.ui.editors.searchresult;
22
23
24 /**
25  * The SearchResultEditorUniversalListener manages all events for the search result editor.
26  *
27  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
28  * @version $Rev$, $Date$
29  */

30 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
31 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils;
32 import org.apache.directory.ldapstudio.browser.core.events.EmptyValueAddedEvent;
33 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
34 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener;
35 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
36 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateEvent;
37 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateListener;
38 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
39 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
40 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
41 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
42 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
43 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
44 import org.apache.directory.ldapstudio.browser.ui.actions.OpenSearchResultAction;
45 import org.apache.directory.ldapstudio.browser.ui.views.browser.BrowserView;
46 import org.eclipse.jface.viewers.CellEditor;
47 import org.eclipse.jface.viewers.ISelection;
48 import org.eclipse.jface.viewers.TableViewer;
49 import org.eclipse.jface.viewers.TextCellEditor;
50 import org.eclipse.swt.SWT;
51 import org.eclipse.swt.custom.TableEditor;
52 import org.eclipse.swt.events.KeyEvent;
53 import org.eclipse.swt.events.KeyListener;
54 import org.eclipse.swt.events.MouseAdapter;
55 import org.eclipse.swt.events.MouseEvent;
56 import org.eclipse.swt.events.MouseListener;
57 import org.eclipse.swt.events.MouseMoveListener;
58 import org.eclipse.swt.events.MouseTrackListener;
59 import org.eclipse.swt.events.SelectionAdapter;
60 import org.eclipse.swt.events.SelectionEvent;
61 import org.eclipse.swt.events.SelectionListener;
62 import org.eclipse.swt.graphics.Point;
63 import org.eclipse.swt.layout.GridData;
64 import org.eclipse.swt.widgets.Display;
65 import org.eclipse.swt.widgets.Event;
66 import org.eclipse.swt.widgets.TableColumn;
67 import org.eclipse.swt.widgets.TableItem;
68 import org.eclipse.swt.widgets.Text;
69 import org.eclipse.ui.INullSelectionListener;
70 import org.eclipse.ui.IPartListener2;
71 import org.eclipse.ui.IWorkbenchPart;
72 import org.eclipse.ui.IWorkbenchPartReference;
73 import org.eclipse.ui.PlatformUI;
74 import org.eclipse.ui.contexts.IContextActivation;
75 import org.eclipse.ui.contexts.IContextService;
76 import org.eclipse.ui.forms.events.HyperlinkEvent;
77 import org.eclipse.ui.forms.events.IHyperlinkListener;
78 import org.eclipse.ui.forms.widgets.Hyperlink;
79
80
81 public class SearchResultEditorUniversalListener implements SearchUpdateListener, EntryUpdateListener
82 {
83
84     /** The search result editor */
85     private SearchResultEditor editor;
86
87     /** The table viewer */
88     private TableViewer viewer;
89
90     /** The cursor */
91     private SearchResultEditorCursor cursor;
92
93     /** The action used to start the default value editor */
94     private OpenBestEditorAction startEditAction;
95
96     /** The selected search that is displayed in the search result editor */
97     private ISearch selectedSearch;
98
99     /** The hyperlink used for DNs */
100     private Hyperlink dnLink;
101
102     /** The table editor, used to display the hyperlink */
103     private TableEditor tableEditor;
104
105     /** Token used to activate and deactivate shortcuts in the editor */
106     private IContextActivation contextActivation;
107
108     /** Listener that listens for selections of ISearch objects. */
109     private INullSelectionListener searchSelectionListener = new INullSelectionListener()
110     {
111         /**
112          * {@inheritDoc}
113          *
114          * This implementation sets the editor's input when a search is selected.
115          */

116         public void selectionChanged( IWorkbenchPart part, ISelection selection )
117         {
118             if ( editor != null && part != null )
119             {
120                 if ( editor.getSite().getWorkbenchWindow() == part.getSite().getWorkbenchWindow() )
121                 {
122                     ISearch[] searches = SelectionUtils.getSearches( selection );
123                     Object JavaDoc[] objects = SelectionUtils.getObjects( selection );
124                     if ( searches.length == 1 && objects.length == 1 )
125                     {
126                         editor.setInput( new SearchResultEditorInput( searches[0] ) );
127                     }
128                     else
129                     {
130                         editor.setInput( new SearchResultEditorInput( null ) );
131                     }
132                 }
133             }
134         }
135     };
136
137     /** The part listener used to activate and deactivate the shortcuts */
138     private IPartListener2 partListener = new IPartListener2()
139     {
140         /**
141          * {@inheritDoc}
142          *
143          * This implementation deactivates the shortcuts when the part is deactivated.
144          */

145         public void partDeactivated( IWorkbenchPartReference partRef )
146         {
147             if ( partRef.getPart( false ) == editor && contextActivation != null )
148             {
149
150                 editor.getActionGroup().deactivateGlobalActionHandlers();
151
152                 IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
153                     IContextService.class );
154                 contextService.deactivateContext( contextActivation );
155                 contextActivation = null;
156             }
157         }
158
159
160         /**
161          * {@inheritDoc}
162          *
163          * This implementation activates the shortcuts when the part is activated.
164          */

165         public void partActivated( IWorkbenchPartReference partRef )
166         {
167             if ( partRef.getPart( false ) == editor )
168             {
169
170                 IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
171                     IContextService.class );
172                 contextActivation = contextService
173                     .activateContext( "org.apache.directory.ldapstudio.browser.action.context" );
174                 // org.eclipse.ui.contexts.dialogAndWindow
175
// org.eclipse.ui.contexts.window
176
// org.eclipse.ui.text_editor_context
177

178                 editor.getActionGroup().activateGlobalActionHandlers();
179             }
180         }
181
182
183         /**
184          * {@inheritDoc}
185          */

186         public void partBroughtToTop( IWorkbenchPartReference partRef )
187         {
188         }
189
190
191         /**
192          * {@inheritDoc}
193          */

194         public void partClosed( IWorkbenchPartReference partRef )
195         {
196         }
197
198
199         /**
200          * {@inheritDoc}
201          */

202         public void partOpened( IWorkbenchPartReference partRef )
203         {
204         }
205
206
207         /**
208          * {@inheritDoc}
209          */

210         public void partHidden( IWorkbenchPartReference partRef )
211         {
212         }
213
214
215         /**
216          * {@inheritDoc}
217          */

218         public void partVisible( IWorkbenchPartReference partRef )
219         {
220         }
221
222
223         /**
224          * {@inheritDoc}
225          */

226         public void partInputChanged( IWorkbenchPartReference partRef )
227         {
228         }
229     };
230
231     /** The listener used to handle clicks to the DN hyper link */
232     private IHyperlinkListener dnHyperlinkListener = new IHyperlinkListener()
233     {
234         /**
235          * {@inheritDoc}
236          *
237          * This implementation opens the search result when clicking thd DN link.
238          */

239         public void linkActivated( HyperlinkEvent e )
240         {
241             ISearchResult sr = ( ISearchResult ) e.widget.getData();
242             OpenSearchResultAction action = new OpenSearchResultAction();
243             action.setSelectedSearchResults( new ISearchResult[]
244                 { sr } );
245             action.run();
246         }
247
248
249         /**
250          * {@inheritDoc}
251          */

252         public void linkEntered( HyperlinkEvent e )
253         {
254         }
255
256
257         /**
258          * {@inheritDoc}
259          */

260         public void linkExited( HyperlinkEvent e )
261         {
262         }
263     };
264
265     /** This listener removes the DN link when then mouse exits the hyperlink control */
266     private MouseTrackListener dnMouseTrackListener = new MouseTrackListener()
267     {
268         /**
269          * {@inheritDoc}
270          */

271         public void mouseEnter( MouseEvent e )
272         {
273         }
274
275
276         /**
277          * {@inheritDoc}
278          *
279          * This implementation removed the DN link.
280          */

281         public void mouseExit( MouseEvent e )
282         {
283             if ( !dnLink.isDisposed() )
284             {
285                 dnLink.setVisible( false );
286                 tableEditor.setEditor( null );
287             }
288         }
289
290
291         public void mouseHover( MouseEvent e )
292         {
293         }
294     };
295
296     /** This listener renders the DN hyperlink when the mouse cursor moves over the DN */
297     private MouseMoveListener cursorMouseMoveListener = new MouseMoveListener()
298     {
299         /**
300          * {@inheritDoc}
301          *
302          * This implementation renders the DN link.
303          */

304         public void mouseMove( MouseEvent e )
305         {
306             if ( !cursor.isDisposed() )
307             {
308                 TableItem item = cursor.getRow();
309                 if ( cursor.getColumn() == 0
310                     && "DN".equalsIgnoreCase( cursor.getRow().getParent().getColumns()[0].getText() ) )
311                 {
312                     checkDnLink( item );
313                 }
314             }
315         }
316     };
317
318     /** This listener renders the DN link when the mouse cursor moves over the DN */
319     private MouseMoveListener viewerMouseMoveListener = new MouseMoveListener()
320     {
321         /**
322          * {@inheritDoc}
323          *
324          * This implementation renders the DN link.
325          */

326         public void mouseMove( MouseEvent e )
327         {
328             if ( !viewer.getTable().isDisposed() )
329             {
330                 TableItem item = viewer.getTable().getItem( new Point( e.x, e.y ) );
331                 viewer.getTable().getColumns()[0].getWidth();
332                 if ( e.x > 0 && e.x < viewer.getTable().getColumns()[0].getWidth()
333                     && "DN".equalsIgnoreCase( viewer.getTable().getColumns()[0].getText() ) )
334                 {
335                     checkDnLink( item );
336                 }
337             }
338         }
339     };
340
341     /** This listener starts the value editor and toggles the cursor's background color */
342     private SelectionListener cursorSelectionListener = new SelectionAdapter()
343     {
344         /**
345          * {@inheritDoc}
346          *
347          * This implementation sets the cursor's background color.
348          */

349         public void widgetSelected( SelectionEvent e )
350         {
351             // viewer.setSelection(new StructuredSelection(getRow()), true);
352
// viewer.getTable().setSelection(new TableItem[]{getRow()});
353
viewer.setSelection( null, true );
354             viewer.getTable().setSelection( new TableItem[0] );
355
356             ISearchResult result = cursor.getSelectedSearchResult();
357             String JavaDoc property = cursor.getSelectedProperty();
358             if ( property != null && result != null && viewer.getCellModifier().canModify( result, property ) )
359             {
360                 cursor.setBackground( Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION ) );
361             }
362             else
363             {
364                 cursor.setBackground( Display.getDefault().getSystemColor( SWT.COLOR_TITLE_INACTIVE_FOREGROUND ) );
365             }
366
367             // cursor.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
368
}
369
370
371         /**
372          * {@inheritDoc}
373          *
374          * This implementation starts the value editor when pressing enter.
375          */

376         public void widgetDefaultSelected( SelectionEvent e )
377         {
378             viewer.setSelection( null, true );
379             viewer.getTable().setSelection( new TableItem[0] );
380             if ( startEditAction.isEnabled() )
381                 startEditAction.run();
382         }
383     };
384
385     /** This listener starts the value editor when double-clicking a cell */
386     private MouseListener cursorMouseListener = new MouseAdapter()
387     {
388         /**
389          * {@inheritDoc}
390          *
391          * This implementation starts the value editor when double-clicking a cell.
392          */

393         public void mouseDoubleClick( MouseEvent e )
394         {
395             viewer.setSelection( null, true );
396             viewer.getTable().setSelection( new TableItem[0] );
397             if ( startEditAction.isEnabled() )
398                 startEditAction.run();
399         }
400
401
402         /**
403          * {@inheritDoc}
404          */

405         public void mouseDown( MouseEvent e )
406         {
407         }
408
409
410         /**
411          * {@inheritDoc}
412          */

413         public void mouseUp( MouseEvent e )
414         {
415         }
416     };
417
418     /** This listener starts the value editor when typing */
419     private KeyListener cursorKeyListener = new KeyListener()
420     {
421         /**
422          * {@inheritDoc}
423          *
424          * This implementation starts the value editor when a non-control key is pressed.
425          */

426         public void keyPressed( KeyEvent e )
427         {
428             if ( e.character != '\0' && e.character != SWT.CR && e.character != SWT.LF && e.character != SWT.BS
429                 && e.character != SWT.DEL && e.character != SWT.TAB && e.character != SWT.ESC
430                 && ( e.stateMask == 0 || e.stateMask == SWT.SHIFT ) )
431             {
432
433                 if ( startEditAction.isEnabled()
434                     && startEditAction.getBestValueEditor().getCellEditor() instanceof TextCellEditor )
435                 {
436                     startEditAction.run();
437                     CellEditor editor = viewer.getCellEditors()[cursor.getColumn()];
438                     if ( editor instanceof TextCellEditor )
439                     {
440                         editor.setValue( String.valueOf( e.character ) );
441                         ( ( Text ) editor.getControl() ).setSelection( 1 );
442                     }
443                 }
444
445             }
446         }
447
448
449         /**
450          * {@inheritDoc}
451          */

452         public void keyReleased( KeyEvent e )
453         {
454         }
455     };
456
457
458     /**
459      * Creates a new instance of SearchResultEditorUniversalListener.
460      *
461      * @param editor the search result editor
462      */

463     public SearchResultEditorUniversalListener( SearchResultEditor editor )
464     {
465         this.editor = editor;
466
467         startEditAction = editor.getActionGroup().getOpenBestEditorAction();
468         viewer = editor.getMainWidget().getViewer();
469         cursor = editor.getConfiguration().getCursor( viewer );
470
471         // create dn link control
472
dnLink = new Hyperlink( viewer.getTable(), SWT.NONE );
473         dnLink.setLayoutData( new GridData( SWT.BOTTOM, SWT.LEFT, true, true ) );
474         dnLink.setText( "" );
475         dnLink.setMenu( viewer.getTable().getMenu() );
476         tableEditor = new TableEditor( viewer.getTable() );
477         tableEditor.horizontalAlignment = SWT.LEFT;
478         tableEditor.verticalAlignment = SWT.BOTTOM;
479         tableEditor.grabHorizontal = true;
480         tableEditor.grabVertical = true;
481
482         // init listeners
483
dnLink.addHyperlinkListener( dnHyperlinkListener );
484         dnLink.addMouseTrackListener( dnMouseTrackListener );
485
486         cursor.addMouseMoveListener( cursorMouseMoveListener );
487         cursor.addSelectionListener( cursorSelectionListener );
488         cursor.addMouseListener( cursorMouseListener );
489         cursor.addKeyListener( cursorKeyListener );
490
491         viewer.getTable().addMouseMoveListener( viewerMouseMoveListener );
492
493         editor.getSite().getPage().addPartListener( partListener );
494         editor.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener( BrowserView.getId(),
495             searchSelectionListener );
496
497         EventRegistry.addSearchUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
498         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
499     }
500
501
502     /**
503      * Disposes the listener.
504      */

505     public void dispose()
506     {
507         if ( editor != null )
508         {
509             editor.getSite().getPage().removePartListener( partListener );
510             editor.getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(
511                 BrowserView.getId(), searchSelectionListener );
512
513             EventRegistry.removeSearchUpdateListener( this );
514             EventRegistry.removeEntryUpdateListener( this );
515
516             selectedSearch = null;
517             startEditAction = null;
518             cursor = null;
519             viewer = null;
520             editor = null;
521         }
522     }
523
524
525     /**
526      * {@inheritDoc}
527      *
528      * This implementation refreshes the search result editor.
529      */

530     public void searchUpdated( SearchUpdateEvent searchUpdateEvent )
531     {
532         if ( selectedSearch == searchUpdateEvent.getSearch() )
533         {
534             refreshInput();
535         }
536     }
537
538
539     /**
540      * {@inheritDoc}
541      *
542      * This implementation refreshes the search result editor
543      * or starts the value editor if an empty value was added.
544      */

545     public void entryUpdated( EntryModificationEvent event )
546     {
547
548         if ( event instanceof EmptyValueAddedEvent && !editor.getActionGroup().isEditorActive() )
549         {
550             EmptyValueAddedEvent evae = ( EmptyValueAddedEvent ) event;
551             IAttribute att = evae.getAddedValue().getAttribute();
552             AttributeHierarchy ah = cursor.getSelectedAttributeHierarchie();
553             if ( ah != null && ah.contains( att ) )
554             {
555                 viewer.setSelection( null, true );
556                 viewer.getTable().setSelection( new TableItem[0] );
557                 if ( startEditAction.isEnabled() )
558                 {
559                     startEditAction.run();
560                 }
561             }
562         }
563         else
564         {
565             viewer.refresh( true );
566             cursor.notifyListeners( SWT.Selection, new Event() );
567         }
568     }
569
570
571     /**
572      * Sets the input.
573      *
574      * @param search the search
575      */

576     void setInput( ISearch search )
577     {
578         selectedSearch = search;
579         refreshInput();
580         editor.getActionGroup().setInput( search );
581     }
582
583
584     /**
585      * Refreshes the input, makes columns visible or hides columns depending on the number
586      * of returning attributes.
587      */

588     void refreshInput()
589     {
590
591         // create at least on column
592
ensureColumnCount( 1 );
593
594         // get all columns
595
TableColumn[] columns = viewer.getTable().getColumns();
596
597         // number of used columns
598
int usedColumns;
599
600         if ( selectedSearch != null )
601         {
602
603             // get displayed attributes
604
boolean showDn = BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
605                 BrowserUIConstants.PREFERENCE_SEARCHRESULTEDITOR_SHOW_DN )
606                 || selectedSearch.getReturningAttributes().length == 0;
607             String JavaDoc[] attributes;
608             if ( showDn )
609             {
610                 attributes = new String JavaDoc[selectedSearch.getReturningAttributes().length + 1];
611                 attributes[0] = "DN";
612                 System.arraycopy( selectedSearch.getReturningAttributes(), 0, attributes, 1, attributes.length - 1 );
613             }
614             else
615             {
616                 attributes = selectedSearch.getReturningAttributes();
617             }
618
619             // create missing columns
620
if ( attributes.length > columns.length )
621             {
622                 ensureColumnCount( attributes.length );
623                 columns = viewer.getTable().getColumns();
624             }
625
626             // set column headers
627
for ( int i = 0; i < attributes.length; i++ )
628             {
629                 columns[i].setText( attributes[i] );
630             }
631             viewer.setColumnProperties( attributes );
632
633             // set input
634
( ( SearchResultEditorLabelProvider ) viewer.getLabelProvider() ).inputChanged( selectedSearch, showDn );
635
636             viewer.setInput( selectedSearch );
637             // this.viewer.refresh();
638

639             // update cell editors
640
CellEditor[] editors = new CellEditor[attributes.length];
641             viewer.setCellEditors( editors );
642
643             if ( attributes.length > 0 )
644             {
645                 int width = viewer.getTable().getClientArea().width / attributes.length;
646                 for ( int i = 0; i < attributes.length; i++ )
647                 {
648                     columns[i].setWidth( width );
649                 }
650             }
651
652             // layout columns
653
// for(int i=0; i<attributes.length; i++) {
654
// columns[i].pack();
655
// }
656
usedColumns = attributes.length;
657         }
658         else
659         {
660             viewer.setInput( null );
661             columns[0].setText( "DN" );
662             columns[0].pack();
663             usedColumns = 1;
664         }
665
666         // make unused columns invisible
667
for ( int i = usedColumns; i < columns.length; i++ )
668         {
669             columns[i].setWidth( 0 );
670             columns[i].setText( " " );
671         }
672
673         // refresh content provider (sorter and filter)
674
editor.getConfiguration().getContentProvider( editor.getMainWidget() ).refresh();
675
676         // this.cursor.setFocus();
677
}
678
679
680     /**
681      * Ensures that the table contains at least the number of
682      * the requested columns.
683      *
684      * @param count the requested number of columns
685      */

686     private void ensureColumnCount( int count )
687     {
688         TableColumn[] columns = viewer.getTable().getColumns();
689         if ( columns.length < count )
690         {
691             for ( int i = columns.length; i < count; i++ )
692             {
693                 TableColumn column = new TableColumn( viewer.getTable(), SWT.LEFT );
694                 column.setText( "" );
695                 column.setWidth( 0 );
696                 column.setResizable( true );
697                 column.setMoveable( true );
698             }
699         }
700     }
701
702
703     /**
704      * Renders the DN link.
705      *
706      * @param item the table item
707      */

708     private void checkDnLink( TableItem item )
709     {
710
711         if ( dnLink == null || dnLink.isDisposed() || tableEditor == null || viewer.getTable().isDisposed()
712             || cursor.isDisposed() )
713         {
714             return;
715         }
716
717         boolean showLinks = BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
718             BrowserUIConstants.PREFERENCE_SEARCHRESULTEDITOR_SHOW_LINKS );
719         if ( showLinks )
720         {
721
722             boolean linkVisible = false;
723
724             if ( item != null )
725             {
726                 Object JavaDoc data = item.getData();
727
728                 if ( data instanceof ISearchResult )
729                 {
730                     ISearchResult sr = ( ISearchResult ) data;
731
732                     item.getFont();
733                     viewer.getTable().getColumn( 0 ).getWidth();
734                     viewer.getTable().getItemHeight();
735
736                     // dnLink.setText("<a>"+sr.getDn().toString()+"</a>");
737
dnLink.setData( sr );
738                     dnLink.setText( sr.getDn().toString() );
739                     dnLink.setUnderlined( true );
740                     dnLink.setFont( item.getFont() );
741                     dnLink.setForeground( item.getForeground() );
742                     dnLink.setBackground( item.getBackground() );
743                     dnLink.setBounds( item.getBounds( 0 ) );
744                     tableEditor.setEditor( dnLink, item, 0 );
745
746                     linkVisible = true;
747                 }
748
749             }
750
751             if ( !linkVisible )
752             {
753                 dnLink.setVisible( false );
754                 tableEditor.setEditor( null );
755             }
756         }
757     }
758
759 }
760
Popular Tags