KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
29 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener;
31 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
32 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
33 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
34 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
35 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
36 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
37 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
38
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.ISelectionChangedListener;
41 import org.eclipse.jface.viewers.ISelectionProvider;
42 import org.eclipse.jface.viewers.SelectionChangedEvent;
43 import org.eclipse.jface.viewers.StructuredSelection;
44 import org.eclipse.jface.viewers.TableViewer;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.custom.TableCursor;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.events.SelectionListener;
50 import org.eclipse.swt.widgets.Display;
51
52
53 public class SearchResultEditorCursor extends TableCursor implements ISelectionProvider, EntryUpdateListener
54 {
55
56     private TableViewer viewer;
57
58
59     public SearchResultEditorCursor( TableViewer viewer )
60     {
61         super( viewer.getTable(), SWT.NONE );
62         this.viewer = viewer;
63
64         setBackground( Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION ) );
65         setForeground( Display.getDefault().getSystemColor( SWT.COLOR_LIST_SELECTION_TEXT ) );
66
67         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
68
69         initSelectionChecker();
70         initSelectionProvider();
71     }
72
73
74     private void initSelectionChecker()
75     {
76         this.addSelectionListener( new SelectionListener()
77         {
78             public void widgetSelected( SelectionEvent e )
79             {
80                 checkSelection();
81             }
82
83
84             public void widgetDefaultSelected( SelectionEvent e )
85             {
86                 checkSelection();
87             }
88
89
90             private void checkSelection()
91             {
92                 if ( viewer != null && viewer.getColumnProperties() != null
93                     && viewer.getColumnProperties().length - 1 < getColumn() )
94                 {
95                     setSelection( getRow(), viewer.getColumnProperties().length - 1 );
96                 }
97             }
98         } );
99     }
100
101
102     public boolean setFocus()
103     {
104         return super.setFocus();
105     }
106
107
108     private void initSelectionProvider()
109     {
110         this.addSelectionListener( new SelectionAdapter()
111         {
112             public void widgetSelected( SelectionEvent e )
113             {
114                 for ( Iterator JavaDoc it = selectionChangesListenerList.iterator(); it.hasNext(); )
115                 {
116                     ( ( ISelectionChangedListener ) it.next() ).selectionChanged( new SelectionChangedEvent(
117                         SearchResultEditorCursor.this, getSelection() ) );
118                 }
119             }
120         } );
121     }
122
123
124     public void dispose()
125     {
126         EventRegistry.removeEntryUpdateListener( this );
127         this.viewer = null;
128         super.dispose();
129     }
130
131
132     public void entryUpdated( EntryModificationEvent event )
133     {
134         this.viewer.refresh();
135         this.redraw();
136     }
137
138
139     public String JavaDoc getSelectedProperty()
140     {
141         if ( !this.isDisposed() && this.getRow() != null && this.viewer != null
142             && this.viewer.getColumnProperties() != null
143             && this.viewer.getColumnProperties().length >= this.getColumn() + 1 )
144         {
145             String JavaDoc property = ( String JavaDoc ) this.viewer.getColumnProperties()[this.getColumn()];
146             return property;
147         }
148         return null;
149     }
150
151
152     public AttributeHierarchy getSelectedAttributeHierarchie()
153     {
154         if ( !this.isDisposed() && this.getRow() != null && this.viewer != null
155             && this.viewer.getColumnProperties() != null
156             && this.viewer.getColumnProperties().length >= this.getColumn() + 1 )
157         {
158             Object JavaDoc o = this.getRow().getData();
159             String JavaDoc property = ( String JavaDoc ) this.viewer.getColumnProperties()[this.getColumn()];
160             if ( o instanceof ISearchResult && !BrowserUIConstants.DN.equals( property ) )
161             {
162                 ISearchResult sr = ( ISearchResult ) o;
163                 AttributeHierarchy ah = sr.getAttributeWithSubtypes( property );
164
165                 if ( ah == null )
166                 {
167                     try
168                     {
169                         ah = new AttributeHierarchy( sr.getEntry(), property, new IAttribute[]
170                             { new Attribute( sr.getEntry(), property ) } );
171                     }
172                     catch ( ModelModificationException e )
173                     {
174                         e.printStackTrace();
175                     }
176                 }
177
178                 return ah;
179             }
180         }
181         return null;
182     }
183
184
185     public ISearchResult getSelectedSearchResult()
186     {
187         if ( !this.isDisposed() && this.getRow() != null )
188         {
189             Object JavaDoc o = this.getRow().getData();
190             if ( o instanceof ISearchResult )
191             {
192                 return ( ISearchResult ) o;
193             }
194         }
195         return null;
196     }
197
198     private List JavaDoc selectionChangesListenerList = new ArrayList JavaDoc();
199
200
201     public void addSelectionChangedListener( ISelectionChangedListener listener )
202     {
203         if ( !this.selectionChangesListenerList.contains( listener ) )
204         {
205             this.selectionChangesListenerList.add( listener );
206         }
207     }
208
209
210     public ISelection getSelection()
211     {
212         ISearchResult searchResult = this.getSelectedSearchResult();
213         AttributeHierarchy ah = this.getSelectedAttributeHierarchie();
214         String JavaDoc property = this.getSelectedProperty();
215         // String attributeName = this.getSelectedAttributeName();
216

217         // System.out.println(attributes.length);
218

219         List JavaDoc list = new ArrayList JavaDoc();
220         if ( searchResult != null )
221             list.add( searchResult );
222         if ( ah != null )
223             list.add( ah );
224         if ( property != null )
225         {
226             list.add( property );
227         }
228         // if(attributeName != null) list.add(attributeName);
229

230         return new StructuredSelection( list );
231     }
232
233
234     public void removeSelectionChangedListener( ISelectionChangedListener listener )
235     {
236         if ( this.selectionChangesListenerList.contains( listener ) )
237         {
238             this.selectionChangesListenerList.remove( listener );
239         }
240     }
241
242
243     public void setSelection( ISelection selection )
244     {
245     }
246
247 }
248
Popular Tags