KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
25 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
26 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
27 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
28 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
29 import org.eclipse.jface.viewers.ILazyContentProvider;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.swt.widgets.Display;
33
34
35 public class SearchResultEditorContentProvider implements ILazyContentProvider
36 {
37
38     private SearchResultEditorWidget mainWidget;
39
40     private SearchResultEditorConfiguration configuration;
41
42     private Object JavaDoc input;
43
44     private Object JavaDoc[] elements;
45
46     private Object JavaDoc[] filteredAndSortedElements;
47
48
49     public SearchResultEditorContentProvider( SearchResultEditorWidget mainWidget,
50         SearchResultEditorConfiguration configuration )
51     {
52         this.mainWidget = mainWidget;
53         this.configuration = configuration;
54
55         this.configuration.getFilter().connect( this );
56         this.configuration.getSorter().connect( this );
57     }
58
59
60     public void dispose()
61     {
62         this.mainWidget = null;
63         this.configuration = null;
64         this.elements = null;
65         this.filteredAndSortedElements = null;
66     }
67
68
69     public void refresh()
70     {
71         this.filterAndSort();
72         this.mainWidget.getViewer().refresh();
73     }
74
75
76     private void filterAndSort()
77     {
78
79         this.filteredAndSortedElements = elements;
80
81         // filter and sort, use Job if too much elements
82
if ( this.configuration.getFilter().isFiltered() || this.configuration.getSorter().isSorted() )
83         {
84             if ( elements.length > 1000 && this.mainWidget.getViewer() != null
85                 && !this.mainWidget.getViewer().getTable().isDisposed() )
86             {
87                 FilterAndSortJob job = new FilterAndSortJob( this.configuration, this.mainWidget, this.elements );
88                 //RunnableContextJobAdapter.execute( job, new TimeTriggeredProgressMonitorDialog( Display.getCurrent()
89
// .getActiveShell(), 5000 ) );
90
RunnableContextJobAdapter.execute( job, new ProgressMonitorDialog( Display.getCurrent()
91                     .getActiveShell() ) );
92                 this.filteredAndSortedElements = job.getFilteredAndSortedElements();
93             }
94             else if ( elements.length > 0 && this.mainWidget.getViewer() != null
95                 && !this.mainWidget.getViewer().getTable().isDisposed() )
96             {
97                 this.filteredAndSortedElements = this.configuration.getFilter().filter( this.mainWidget.getViewer(),
98                     "", elements );
99                 this.configuration.getSorter().sort( this.mainWidget.getViewer(), this.filteredAndSortedElements );
100             }
101         }
102
103         // update virtual table
104
this.mainWidget.getViewer().setItemCount( this.filteredAndSortedElements.length );
105
106         // update state
107
String JavaDoc url = "";
108         boolean enabled = true;
109         if ( input != null && input instanceof ISearch )
110         {
111             ISearch search = ( ISearch ) input;
112
113             if ( filteredAndSortedElements.length < elements.length )
114             {
115                 url += filteredAndSortedElements.length + " of ";
116             }
117
118             if ( search.getSearchResults() == null )
119             {
120                 url += "Search not performed - ";
121                 enabled = false;
122             }
123             else if ( search.getSearchResults().length == 1 )
124             {
125                 url += search.getSearchResults().length + " Result - ";
126             }
127             else
128             {
129                 url += search.getSearchResults().length + " Results - ";
130             }
131
132             // url += search.getURL();
133
url += "Search Base: " + search.getSearchBase().toString() + " - ";
134             url += "Filter: " + search.getFilter();
135
136             boolean showDn = BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
137                 BrowserUIConstants.PREFERENCE_SEARCHRESULTEDITOR_SHOW_DN )
138                 || search.getReturningAttributes().length == 0;
139             this.configuration.getFilter().inputChanged( search, showDn );
140             this.configuration.getSorter().inputChanged( search, showDn );
141         }
142         else
143         {
144             url = "No search selected";;
145             enabled = false;
146         }
147
148         if ( this.mainWidget.getInfoText() != null && !this.mainWidget.getInfoText().isDisposed() )
149         {
150             this.mainWidget.getInfoText().setText( url );
151         }
152         if ( this.mainWidget.getQuickFilterWidget() != null )
153         {
154             this.mainWidget.getQuickFilterWidget().setEnabled( enabled );
155         }
156         if ( this.mainWidget.getViewer() != null && !this.mainWidget.getViewer().getTable().isDisposed() )
157         {
158             this.mainWidget.getViewer().getTable().setEnabled( enabled );
159         }
160
161     }
162
163
164     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
165     {
166         this.input = newInput;
167         this.elements = getElements( newInput );
168         // this.filterAndSort();
169
}
170
171
172     public Object JavaDoc[] getElements( Object JavaDoc inputElement )
173     {
174         if ( inputElement != null && inputElement instanceof ISearch )
175         {
176             ISearch search = ( ISearch ) inputElement;
177             return search.getSearchResults() != null ? search.getSearchResults() : new Object JavaDoc[0];
178         }
179         else
180         {
181             return new Object JavaDoc[]
182                 {};
183             // return new Object[]{inputElement};
184
}
185     }
186
187
188     public TableViewer getViewer()
189     {
190         return this.mainWidget.getViewer();
191     }
192
193
194     public void updateElement( int index )
195     {
196         // if(sortedAndSortedElements instanceof ISearchResult[]) {
197
if ( filteredAndSortedElements != null && filteredAndSortedElements.length > 0
198             && index < filteredAndSortedElements.length )
199         {
200             // System.out.println(index);
201
mainWidget.getViewer().replace( filteredAndSortedElements[index], index );
202         }
203     }
204
205 }
206
Popular Tags