1 11 package org.eclipse.jdt.internal.ui.javaeditor.selectionactions; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 import org.eclipse.jface.viewers.ISelectionChangedListener; 19 import org.eclipse.jface.viewers.SelectionChangedEvent; 20 21 import org.eclipse.jdt.core.ISourceRange; 22 23 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 24 25 public class SelectionHistory { 26 27 private List fHistory; 28 private JavaEditor fEditor; 29 private ISelectionChangedListener fSelectionListener; 30 private int fSelectionChangeListenerCounter; 31 private StructureSelectHistoryAction fHistoryAction; 32 33 public SelectionHistory(JavaEditor editor) { 34 Assert.isNotNull(editor); 35 fEditor= editor; 36 fHistory= new ArrayList (3); 37 fSelectionListener= new ISelectionChangedListener() { 38 public void selectionChanged(SelectionChangedEvent event) { 39 if (fSelectionChangeListenerCounter == 0) 40 flush(); 41 } 42 }; 43 fEditor.getSelectionProvider().addSelectionChangedListener(fSelectionListener); 44 } 45 46 public void setHistoryAction(StructureSelectHistoryAction action) { 47 Assert.isNotNull(action); 48 fHistoryAction= action; 49 } 50 51 public boolean isEmpty() { 52 return fHistory.isEmpty(); 53 } 54 55 public void remember(ISourceRange range) { 56 fHistory.add(range); 57 fHistoryAction.update(); 58 } 59 60 public ISourceRange getLast() { 61 if (isEmpty()) 62 return null; 63 int size= fHistory.size(); 64 ISourceRange result= (ISourceRange)fHistory.remove(size - 1); 65 fHistoryAction.update(); 66 return result; 67 } 68 69 public void flush() { 70 if (fHistory.isEmpty()) 71 return; 72 fHistory.clear(); 73 fHistoryAction.update(); 74 } 75 76 public void ignoreSelectionChanges() { 77 fSelectionChangeListenerCounter++; 78 } 79 80 public void listenToSelectionChanges() { 81 fSelectionChangeListenerCounter--; 82 } 83 84 public void dispose() { 85 fEditor.getSelectionProvider().removeSelectionChangedListener(fSelectionListener); 86 } 87 } 88 | Popular Tags |