KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > selectionactions > SelectionHistory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.javaeditor.selectionactions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc(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