1 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 input; 43 44 private Object [] elements; 45 46 private Object [] 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 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 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 this.mainWidget.getViewer().setItemCount( this.filteredAndSortedElements.length ); 105 106 String 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 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 oldInput, Object newInput ) 165 { 166 this.input = newInput; 167 this.elements = getElements( newInput ); 168 } 170 171 172 public Object [] getElements( Object inputElement ) 173 { 174 if ( inputElement != null && inputElement instanceof ISearch ) 175 { 176 ISearch search = ( ISearch ) inputElement; 177 return search.getSearchResults() != null ? search.getSearchResults() : new Object [0]; 178 } 179 else 180 { 181 return new Object [] 182 {}; 183 } 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 ( filteredAndSortedElements != null && filteredAndSortedElements.length > 0 198 && index < filteredAndSortedElements.length ) 199 { 200 mainWidget.getViewer().replace( filteredAndSortedElements[index], index ); 202 } 203 } 204 205 } 206 | Popular Tags |