1 11 package org.eclipse.ui.actions; 12 13 import org.eclipse.jface.action.Action; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.jface.viewers.ISelectionChangedListener; 16 import org.eclipse.jface.viewers.ISelectionProvider; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.jface.viewers.SelectionChangedEvent; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 21 40 public abstract class SelectionProviderAction extends Action implements 41 ISelectionChangedListener { 42 43 46 private ISelectionProvider provider; 47 48 57 protected SelectionProviderAction(ISelectionProvider provider, String text) { 58 super(text); 59 this.provider = provider; 60 provider.addSelectionChangedListener(this); 61 } 62 63 67 public void dispose() { 68 provider.removeSelectionChangedListener(this); 69 } 70 71 76 public ISelection getSelection() { 77 return provider.getSelection(); 78 } 79 80 85 public ISelectionProvider getSelectionProvider() { 86 return provider; 87 } 88 89 96 public IStructuredSelection getStructuredSelection() { 97 ISelection selection = provider.getSelection(); 98 if (selection instanceof IStructuredSelection) { 99 return (IStructuredSelection) selection; 100 } else { 101 return new StructuredSelection(); 102 } 103 } 104 105 115 public void selectionChanged(ISelection selection) { 116 } 117 118 128 public void selectionChanged(IStructuredSelection selection) { 129 } 131 132 140 public final void selectionChanged(SelectionChangedEvent event) { 141 ISelection selection = event.getSelection(); 142 if (selection instanceof IStructuredSelection) { 143 selectionChanged((IStructuredSelection) selection); 144 } else { 145 selectionChanged(selection); 146 } 147 } 148 } 149 | Popular Tags |