01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.javaeditor.selectionactions;
11:
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: import org.eclipse.core.runtime.Assert;
16:
17: import org.eclipse.jface.viewers.ISelectionChangedListener;
18: import org.eclipse.jface.viewers.SelectionChangedEvent;
19:
20: import org.eclipse.jdt.core.ISourceRange;
21:
22: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
23:
24: public class SelectionHistory {
25:
26: private List fHistory;
27: private JavaEditor fEditor;
28: private ISelectionChangedListener fSelectionListener;
29: private int fSelectionChangeListenerCounter;
30: private StructureSelectHistoryAction fHistoryAction;
31:
32: public SelectionHistory(JavaEditor editor) {
33: Assert.isNotNull(editor);
34: fEditor = editor;
35: fHistory = new ArrayList(3);
36: fSelectionListener = new ISelectionChangedListener() {
37: public void selectionChanged(SelectionChangedEvent event) {
38: if (fSelectionChangeListenerCounter == 0)
39: flush();
40: }
41: };
42: fEditor.getSelectionProvider().addSelectionChangedListener(
43: 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(
86: fSelectionListener);
87: }
88: }
|