KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > TextSelectionNavigationLocation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.texteditor;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.BadPositionCategoryException;
15 import org.eclipse.jface.text.DefaultPositionUpdater;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IPositionUpdater;
18 import org.eclipse.jface.text.ITextSelection;
19 import org.eclipse.jface.text.Position;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IMemento;
25 import org.eclipse.ui.INavigationLocation;
26 import org.eclipse.ui.NavigationLocation;
27
28
29 /**
30  * Represents the text selection context marked for the user in the navigation history.
31  *
32  * @since 2.1
33  */

34 public class TextSelectionNavigationLocation extends NavigationLocation {
35
36     // Memento tags and values
37
private static final String JavaDoc TAG_X= "x"; //$NON-NLS-1$
38
private static final String JavaDoc TAG_Y= "y"; //$NON-NLS-1$
39
private static final String JavaDoc TAG_INFO= "info"; //$NON-NLS-1$
40
private static final String JavaDoc INFO_DELETED= "deleted"; //$NON-NLS-1$
41
private static final String JavaDoc INFO_NOT_DELETED= "not_deleted"; //$NON-NLS-1$
42

43     private static final String JavaDoc CATEGORY= "__navigation_" + TextSelectionNavigationLocation.class.hashCode(); //$NON-NLS-1$
44
private static final IPositionUpdater fgPositionUpdater= new DefaultPositionUpdater(CATEGORY);
45
46
47     private Position fPosition;
48     private IDocument fDocument;
49     private Position fSavedPosition;
50
51
52     /**
53      * Creates a new text selection navigation location.
54      *
55      * @param part the text editor part
56      * @param initialize a boolean indicating whether to initialize the new instance from the current selection
57      */

58     public TextSelectionNavigationLocation(ITextEditor part, boolean initialize) {
59         super(part);
60
61         if (initialize) {
62
63             ISelection s= part.getSelectionProvider().getSelection();
64             if(s == null || s.isEmpty())
65                 return;
66
67             ITextSelection selection= (ITextSelection) s;
68             if(selection.getOffset() == 0 && selection.getLength() == 0)
69                 return;
70
71             IDocument document= getDocument(part);
72             Position position= new Position(selection.getOffset(), selection.getLength());
73             if (installOnDocument(document, position)) {
74                 fDocument= document;
75                 fPosition= position;
76                 if (!part.isDirty())
77                     fSavedPosition= new Position(fPosition.offset, fPosition.length);
78             }
79         }
80     }
81
82     /**
83      * Returns the text editor's document.
84      *
85      * @param part the text editor
86      * @return the document of the given text editor
87      */

88     private IDocument getDocument(ITextEditor part) {
89         IDocumentProvider provider= part.getDocumentProvider();
90         return provider.getDocument(part.getEditorInput());
91     }
92
93     /**
94      * Installs the given position on the given document.
95      *
96      * @param document the document
97      * @param position the position
98      * @return <code>true</code> if the position could be installed
99      */

100     private boolean installOnDocument(IDocument document, Position position) {
101
102         if (document != null && position != null) {
103
104             if (!document.containsPositionCategory(CATEGORY)) {
105                 document.addPositionCategory(CATEGORY);
106                 document.addPositionUpdater(fgPositionUpdater);
107             }
108
109             try {
110                 document.addPosition(CATEGORY, position);
111                 return true;
112             } catch (BadLocationException e) {
113             } catch (BadPositionCategoryException e) {
114             }
115         }
116
117         return false;
118     }
119
120     /**
121      * Uninstalls the given position from the given document.
122      *
123      * @param document the document
124      * @param position the position
125      * @return <code>true</code> if the position could be uninstalled
126      */

127     private boolean uninstallFromDocument(IDocument document, Position position) {
128
129         if (document != null && position != null) {
130             try {
131
132                 document.removePosition(CATEGORY, position);
133
134                 Position[] category= document.getPositions(CATEGORY);
135                 if (category == null || category.length == 0) {
136                     document.removePositionCategory(CATEGORY);
137                     document.removePositionUpdater(fgPositionUpdater);
138                 }
139                 return true;
140
141             } catch (BadPositionCategoryException e) {
142             }
143         }
144
145         return false;
146     }
147
148     /*
149      * @see Object#toString()
150      */

151     public String JavaDoc toString() {
152         return "Selection<" + fPosition + ">"; //$NON-NLS-1$ //$NON-NLS-2$
153
}
154
155     /**
156      * Tells whether this location is equal to the current
157      * location in the given text editor.
158      *
159      * @param part the text editor
160      * @return <code>true</code> if the locations are equal
161      */

162     private boolean equalsLocationOf(ITextEditor part) {
163
164         if (fPosition == null)
165             return true;
166
167         if (fPosition.isDeleted)
168             return false;
169
170         ISelectionProvider provider= part.getSite().getSelectionProvider();
171         ISelection selection= provider.getSelection();
172         if (selection instanceof ITextSelection) {
173             ITextSelection textSelection= (ITextSelection) selection;
174             if (textSelection.getOffset() == fPosition.offset && textSelection.getLength() == fPosition.length) {
175                 String JavaDoc text= textSelection.getText();
176                 if (text != null) {
177                     try {
178                         return text.equals(fDocument.get(fPosition.offset, fPosition.length));
179                     } catch (BadLocationException e) {
180                     }
181                 }
182             }
183         }
184
185         return false;
186     }
187
188     public void dispose() {
189         uninstallFromDocument(fDocument, fPosition);
190         fDocument= null;
191         fPosition= null;
192         fSavedPosition= null;
193         super.dispose();
194     }
195
196     /**
197      * Releases the state of this location.
198      */

199     public void releaseState() {
200         // deactivate
201
uninstallFromDocument(fDocument, fPosition);
202         fDocument= null;
203         fPosition= null;
204         fSavedPosition= null;
205         super.releaseState();
206     }
207
208     /**
209      * Merges the given location into this one.
210      *
211      * @param location the location to merge into this one
212      * @return <code>true<code> if merging was successful
213      */

214     public boolean mergeInto(INavigationLocation location) {
215
216         if (location == null)
217             return false;
218
219         if (getClass() != location.getClass())
220             return false;
221
222         if (fPosition == null || fPosition.isDeleted)
223             return true;
224
225         TextSelectionNavigationLocation s= (TextSelectionNavigationLocation) location;
226         if (s.fPosition == null || s.fPosition.isDeleted) {
227             uninstallFromDocument(fDocument, fPosition);
228             s.fDocument= fDocument;
229             s. fPosition= fPosition;
230             s.fSavedPosition= fSavedPosition;
231             return true;
232         }
233
234         if (s.fDocument == fDocument) {
235             if (s.fPosition.overlapsWith(fPosition.offset, fPosition.length) || fPosition.offset + fPosition.length == s.fPosition.offset || s.fPosition.offset + s.fPosition.length == fPosition.offset) {
236                 s.fPosition.offset= fPosition.offset;
237                 s.fPosition.length= fPosition.length;
238                 return true;
239             }
240         }
241
242         return false;
243     }
244
245     /**
246      * Restores this location.
247      */

248     public void restoreLocation() {
249         if (fPosition == null || fPosition.isDeleted)
250             return;
251
252         IEditorPart part= getEditorPart();
253         if (part instanceof ITextEditor) {
254             ITextEditor editor= (ITextEditor) getEditorPart();
255             editor.selectAndReveal(fPosition.offset, fPosition.length);
256         }
257     }
258
259     /**
260      * Restores the object state from the given memento.
261      *
262      * @param memento the memento
263      */

264     public void restoreState(IMemento memento) {
265
266         IEditorPart part= getEditorPart();
267         if (part instanceof ITextEditor) {
268
269             // restore
270
fDocument= getDocument((ITextEditor) part);
271
272             Integer JavaDoc offset= memento.getInteger(TAG_X);
273             Integer JavaDoc length= memento.getInteger(TAG_Y);
274             String JavaDoc deleted= memento.getString(TAG_INFO);
275
276             if (offset != null && length != null) {
277                 Position p= new Position(offset.intValue(), length.intValue());
278                 if (deleted != null)
279                     p.isDeleted= INFO_DELETED.equals(deleted) ? true : false;
280
281                 // activate
282
if (installOnDocument(fDocument, p)) {
283                     fPosition= p;
284                     if (!part.isDirty())
285                         fSavedPosition= new Position(fPosition.offset, fPosition.length);
286                 }
287             }
288         }
289     }
290
291     /**
292      * Stores the object state into the given memento.
293      *
294      * @param memento the memento
295      */

296     public void saveState(IMemento memento) {
297         if (fSavedPosition != null) {
298             memento.putInteger(TAG_X, fSavedPosition.offset);
299             memento.putInteger(TAG_Y, fSavedPosition.length);
300             memento.putString(TAG_INFO, (fSavedPosition.isDeleted ? INFO_DELETED : INFO_NOT_DELETED));
301         }
302     }
303
304     /**
305      * Hook method which is called when the given editor has been saved.
306      *
307      * @param part the editor part
308      */

309     public void partSaved(IEditorPart part) {
310         // http://dev.eclipse.org/bugs/show_bug.cgi?id=25440
311
if (fPosition == null || fPosition.isDeleted())
312             fSavedPosition= null;
313         else
314             fSavedPosition= new Position(fPosition.offset, fPosition.length);
315     }
316
317     /**
318      * Updates the this location.
319      */

320     public void update() {
321         IEditorPart part= getEditorPart();
322         if (part instanceof ITextEditor) {
323             ITextEditor textEditor= (ITextEditor) getEditorPart();
324
325             if(equalsLocationOf(textEditor))
326                 return;
327
328             ISelection s= textEditor.getSelectionProvider().getSelection();
329             if(s == null || s.isEmpty())
330                 return;
331
332             ITextSelection selection= (ITextSelection) s;
333             if(selection.getOffset() == 0 && selection.getLength() == 0)
334                 return;
335
336             fPosition.offset= selection.getOffset();
337             fPosition.length= selection.getLength();
338             fPosition.isDeleted= false;
339
340             if (!part.isDirty())
341                 fSavedPosition= new Position(fPosition.offset, fPosition.length);
342         }
343     }
344 }
345
Popular Tags