1 11 12 package org.eclipse.ui.actions; 13 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.viewers.ISelection; 16 import org.eclipse.jface.viewers.ISelectionChangedListener; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.jface.viewers.SelectionChangedEvent; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 import org.eclipse.swt.widgets.Event; 21 22 49 public abstract class BaseSelectionListenerAction extends Action implements 50 ISelectionChangedListener { 51 54 private IStructuredSelection selection = new StructuredSelection(); 55 56 59 private boolean running = false; 60 61 65 private IStructuredSelection deferredSelection = null; 66 67 73 protected BaseSelectionListenerAction(String text) { 74 super(text); 75 } 76 77 85 protected void clearCache() { 86 } 88 89 96 public IStructuredSelection getStructuredSelection() { 97 return selection; 98 } 99 100 112 public final void selectionChanged(IStructuredSelection selection) { 113 if (running) { 119 deferredSelection = selection; 120 return; 121 } 122 this.selection = selection; 123 clearCache(); 124 setEnabled(updateSelection(selection)); 125 } 126 127 134 public final void selectionChanged(SelectionChangedEvent event) { 135 ISelection selection = event.getSelection(); 136 if (selection instanceof IStructuredSelection) { 137 selectionChanged((IStructuredSelection) selection); 138 } else { 139 selectionChanged(StructuredSelection.EMPTY); 140 } 141 } 142 143 156 protected boolean updateSelection(IStructuredSelection selection) { 157 return true; 158 } 159 160 163 public void runWithEvent(Event event) { 164 running = true; 167 try { 168 run(); 169 } finally { 170 running = false; 171 IStructuredSelection s = deferredSelection; 172 deferredSelection = null; 173 if (s != null) { 174 selectionChanged(s); 175 } 176 } 177 } 178 } 179 | Popular Tags |