1 20 21 package org.apache.directory.ldapstudio.browser.ui.editors.searchresult; 22 23 24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 25 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils; 26 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent; 27 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener; 28 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 29 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 30 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 31 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 32 33 import org.eclipse.jface.action.Action; 34 import org.eclipse.jface.resource.ImageDescriptor; 35 import org.eclipse.jface.viewers.ISelection; 36 import org.eclipse.jface.viewers.ISelectionChangedListener; 37 import org.eclipse.jface.viewers.ISelectionProvider; 38 import org.eclipse.jface.viewers.SelectionChangedEvent; 39 40 41 public abstract class AbstractSearchResultListenerAction extends Action implements ISelectionChangedListener, 42 EntryUpdateListener 43 { 44 45 protected ISelectionProvider selectionProvider; 46 47 protected ISearch selectedSearch; 48 49 protected ISearchResult selectedSearchResult; 50 51 protected AttributeHierarchy selectedAttributeHierarchie; 52 53 protected String selectedProperty; 54 55 56 AbstractSearchResultListenerAction( ISelectionProvider selectionProvider, String title, ImageDescriptor image, 57 String command, int style ) 58 { 59 super( title, style ); 60 super.setText( title ); 61 super.setToolTipText( title ); 62 super.setImageDescriptor( image ); 63 super.setActionDefinitionId( command ); 64 super.setEnabled( false ); 65 66 this.selectionProvider = selectionProvider; 67 68 this.init(); 69 } 70 71 72 AbstractSearchResultListenerAction( ISelectionProvider selectionProvider, String title, ImageDescriptor image, 73 String command ) 74 { 75 this( selectionProvider, title, image, command, Action.AS_PUSH_BUTTON ); 76 } 77 78 79 private void init() 80 { 81 this.selectionProvider.addSelectionChangedListener( this ); 82 EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() ); 83 84 this.selectedSearch = null; 85 this.selectedSearchResult = null; 86 this.selectedAttributeHierarchie = null; 87 this.selectedProperty = null; 88 } 89 90 91 public void selectionChanged( SelectionChangedEvent event ) 92 { 93 94 ISelection selection = event.getSelection(); 95 96 ISearchResult[] searchResults = SelectionUtils.getSearchResults( selection ); 97 AttributeHierarchy[] ah = SelectionUtils.getAttributeHierarchie( selection ); 98 String [] selectedProperties = SelectionUtils.getProperties( selection ); 99 100 if ( searchResults.length == 1 ) 101 { 102 this.selectedSearchResult = searchResults[0]; 103 this.selectedSearch = this.selectedSearchResult.getSearch(); 104 } 105 else 106 { 107 this.selectedSearchResult = null; 108 this.selectedSearch = null; 109 } 110 111 if ( ah.length == 1 ) 112 { 113 this.selectedAttributeHierarchie = ah[0]; 114 } 115 else 116 { 117 this.selectedAttributeHierarchie = null; 118 } 119 120 if ( selectedProperties.length == 1 ) 121 { 122 this.selectedProperty = selectedProperties[0]; 123 } 124 else 125 { 126 this.selectedProperty = null; 127 } 128 129 this.updateEnabledState(); 130 } 131 132 133 public final void entryUpdated( EntryModificationEvent event ) 134 { 135 this.updateEnabledState(); 136 } 137 138 139 protected abstract void updateEnabledState(); 140 141 142 public void dispose() 143 { 144 EventRegistry.removeEntryUpdateListener( this ); 145 this.selectionProvider.removeSelectionChangedListener( this ); 146 147 this.selectedSearchResult = null; 148 this.selectedAttributeHierarchie = null; 149 this.selectedSearch = null; 150 this.selectedProperty = null; 151 } 152 153 } 154 | Popular Tags |