KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > revisions > RevisionSelectionProvider


1 /*******************************************************************************
2  * Copyright (c) 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.jface.internal.text.revisions;
12
13 import org.eclipse.core.runtime.ListenerList;
14
15 import org.eclipse.jface.text.ITextSelection;
16 import org.eclipse.jface.text.ITextViewer;
17 import org.eclipse.jface.text.revisions.Revision;
18
19 import org.eclipse.jface.viewers.IPostSelectionProvider;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.StructuredSelection;
26
27 /**
28  * A selection provider for annotate revisions. Selections of a revision can currently happen in
29  * following ways - note that this list may be changed in the future:
30  * <ul>
31  * <li>when the user clicks the revision ruler with the mouse</li>
32  * <li>when the caret is moved to a revision's line (only on post-selection)</li>
33  * </ul>
34  * <p>
35  * Calling {@link #setSelection(ISelection)} will set the current sticky revision on the ruler.
36  * </p>
37  *
38  * @since 3.2
39  */

40 public final class RevisionSelectionProvider implements ISelectionProvider {
41
42     /**
43      * Post selection listener on the viewer that remembers the selection provider it is registered
44      * with.
45      */

46     private final class PostSelectionListener implements ISelectionChangedListener {
47         private final IPostSelectionProvider fPostProvider;
48
49         public PostSelectionListener(IPostSelectionProvider postProvider) {
50             postProvider.addPostSelectionChangedListener(this);
51             fPostProvider= postProvider;
52         }
53
54         public void selectionChanged(SelectionChangedEvent event) {
55             ISelection selection= event.getSelection();
56             if (selection instanceof ITextSelection) {
57                 ITextSelection ts= (ITextSelection) selection;
58                 int offset= ts.getOffset();
59                 setSelectedRevision(fPainter.getRevision(offset));
60             }
61             
62         }
63         
64         public void dispose() {
65             fPostProvider.removePostSelectionChangedListener(this);
66         }
67     }
68
69     private final RevisionPainter fPainter;
70     private final ListenerList fListeners= new ListenerList();
71     
72     /**
73      * The text viewer once we are installed, <code>null</code> if not installed.
74      */

75     private ITextViewer fViewer;
76     /**
77      * The selection listener on the viewer, or <code>null</code>.
78      */

79     private PostSelectionListener fSelectionListener;
80     /**
81      * The last selection, or <code>null</code>.
82      */

83     private Revision fSelection;
84     /**
85      * Incoming selection changes are ignored while sending out events.
86      *
87      * @since 3.3
88      */

89     private boolean fIgnoreEvents= false;
90
91     /**
92      * Creates a new selection provider.
93      *
94      * @param painter the painter that the created provider interacts with
95      */

96     RevisionSelectionProvider(RevisionPainter painter) {
97         fPainter= painter;
98     }
99
100     /*
101      * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
102      */

103     public void addSelectionChangedListener(ISelectionChangedListener listener) {
104         fListeners.add(listener);
105     }
106
107     /*
108      * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
109      */

110     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
111         fListeners.remove(listener);
112     }
113     
114     /*
115      * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
116      */

117     public ISelection getSelection() {
118         if (fSelection == null)
119             return StructuredSelection.EMPTY;
120         return new StructuredSelection(fSelection);
121     }
122
123     /*
124      * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
125      */

126     public void setSelection(ISelection selection) {
127         if (fIgnoreEvents)
128             return;
129         if (selection instanceof IStructuredSelection) {
130             Object JavaDoc first= ((IStructuredSelection) selection).getFirstElement();
131             if (first instanceof Revision)
132                 fPainter.handleRevisionSelected((Revision) first);
133             else if (first instanceof String JavaDoc)
134                 fPainter.handleRevisionSelected((String JavaDoc) first);
135             else if (selection.isEmpty())
136                 fPainter.handleRevisionSelected((Revision) null);
137         }
138     }
139
140     /**
141      * Installs the selection provider on the viewer.
142      *
143      * @param viewer the viewer on which we listen to for post selection events
144      */

145     void install(ITextViewer viewer) {
146         uninstall();
147         fViewer= viewer;
148         if (fViewer != null) {
149             ISelectionProvider provider= fViewer.getSelectionProvider();
150             if (provider instanceof IPostSelectionProvider) {
151                 IPostSelectionProvider postProvider= (IPostSelectionProvider) provider;
152                 fSelectionListener= new PostSelectionListener(postProvider);
153             }
154         }
155     }
156     
157     /**
158      * Uninstalls the selection provider.
159      */

160     void uninstall() {
161         fViewer= null;
162         if (fSelectionListener != null) {
163             fSelectionListener.dispose();
164             fSelectionListener= null;
165         }
166     }
167
168     /**
169      * Private protocol used by {@link RevisionPainter} to signal selection of a revision.
170      *
171      * @param revision the selected revision, or <code>null</code> for none
172      */

173     void revisionSelected(Revision revision) {
174         setSelectedRevision(revision);
175     }
176
177     /**
178      * Updates the currently selected revision and sends out an event if it changed.
179      *
180      * @param revision the newly selected revision or <code>null</code> for none
181      */

182     private void setSelectedRevision(Revision revision) {
183         if (revision != fSelection) {
184             fSelection= revision;
185             fireSelectionEvent();
186         }
187     }
188
189     private void fireSelectionEvent() {
190         fIgnoreEvents= true;
191         try {
192             ISelection selection= getSelection();
193             SelectionChangedEvent event= new SelectionChangedEvent(this, selection);
194
195             Object JavaDoc[] listeners= fListeners.getListeners();
196             for (int i= 0; i < listeners.length; i++)
197                 ((ISelectionChangedListener) listeners[i]).selectionChanged(event);
198         } finally {
199             fIgnoreEvents= false;
200         }
201     }
202 }
203
Popular Tags