1 20 21 package org.apache.directory.ldapstudio.schemas.view.views; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 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 76 public class SearchView extends ViewPart implements PoolListener 77 { 78 79 public static final String ID = Activator.PLUGIN_ID + ".view.SearchView"; 81 82 private SchemaPool pool; 83 84 private Table resultsTable; 86 private TableViewer resultsTableViewer; 87 private Combo searchField; 88 private Combo scopeCombo; 89 private SearchViewContentProvider searchContentProvider; 90 91 92 private final String TYPE_COLUMN = Messages.getString( "SearchView.Type_Column" ); 94 95 private final String NAME_COLUMN = Messages.getString( "SearchView.Name_Column" ); 97 98 private final String SCHEMA_COLUMN = Messages.getString( "SearchView.Schema_Column" ); 100 101 private String [] columnNames = new String [] 102 { TYPE_COLUMN, NAME_COLUMN, SCHEMA_COLUMN, }; 103 104 105 public static final String SEARCH_ALL = Messages.getString( "SearchView.Search_All_metadata" ); 107 108 public static final String SEARCH_NAME = Messages.getString( "SearchView.Search_Name" ); 110 111 public static final String SEARCH_OID = Messages.getString( "SearchView.Search_OID" ); 113 114 public static final String SEARCH_DESC = Messages.getString( "SearchView.Search_Description" ); 116 117 public static String currentSearchScope = SEARCH_ALL; 118 119 120 123 @Override 124 public void createPartControl( Composite parent ) 125 { 126 this.pool = SchemaPool.getInstance(); 127 pool.addListener( this ); 129 130 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" ) ); 139 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" ) ); 148 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 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 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 TableColumn column = new TableColumn( resultsTable, SWT.CENTER, 0 ); 205 column.setText( columnNames[0] ); 206 column.setWidth( 40 ); 207 208 column = new TableColumn( resultsTable, SWT.LEFT, 1 ); 210 column.setText( columnNames[1] ); 211 column.setWidth( 400 ); 212 213 column = new TableColumn( resultsTable, SWT.LEFT, 2 ); 215 column.setText( columnNames[2] ); 216 column.setWidth( 100 ); 217 } 218 219 220 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() ) ) { 250 String 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 ) { 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 309 private void openEditor( Table table ) 310 { 311 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 312 313 IEditorInput input = null; 314 String editorId = null; 315 316 Object 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 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" ); } 340 } 341 } 342 343 344 347 private void createTableViewer() 348 { 349 resultsTableViewer = new TableViewer( resultsTable ); 350 resultsTableViewer.setUseHashlookup( true ); 351 resultsTableViewer.setColumnProperties( columnNames ); 352 } 353 354 355 358 @Override 359 public void setFocus() 360 { 361 if ( searchField != null && !searchField.isDisposed() ) 362 { 363 searchField.setFocus(); 364 } 365 } 366 367 368 371 public void poolChanged( SchemaPool p, LDAPModelEvent e ) 372 { 373 resultsTableViewer.refresh(); 374 } 375 376 377 380 public void setPartName( String partName ) 381 { 382 super.setPartName( partName ); 383 } 384 385 386 389 private void initSearchHistory() 390 { 391 searchField.setItems( loadHistory( PluginConstants.PREFS_SEARCH_VIEW_SEARCH_HISTORY ) ); 392 } 393 394 395 403 public static void saveHistory( String key, String value ) 404 { 405 String [] history = loadHistory( key ); 407 List <String > list = new ArrayList <String >( Arrays.asList( history ) ); 408 409 if ( list.contains( value ) ) 411 { 412 list.remove( value ); 413 } 414 list.add( 0, value ); 415 416 while ( list.size() > 15 ) 418 { 419 list.remove( list.size() - 1 ); 420 } 421 422 history = ( String [] ) list.toArray( new String [list.size()] ); 424 Activator.getDefault().getDialogSettings().put( key, history ); 425 426 } 427 428 429 436 public static String [] loadHistory( String key ) 437 { 438 String [] history = Activator.getDefault().getDialogSettings().getArray( key ); 439 if ( history == null ) 440 { 441 history = new String [0]; 442 } 443 return history; 444 } 445 446 447 public void setSearch( String searchString, String 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 |