View | Details | Raw Unified | Return to bug 93845 | Differences between
and this patch

Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitEditor.java (-2 / +7 lines)
Lines 1146-1152 Link Here
1146
	 * The remembered selection.
1146
	 * The remembered selection.
1147
	 * @since 3.0
1147
	 * @since 3.0
1148
	 */
1148
	 */
1149
	private RememberedSelection fRememberedSelection= new RememberedSelection();
1149
	private RememberedSelection fRememberedSelection;
1150
	/** The bracket inserter. */
1150
	/** The bracket inserter. */
1151
	private BracketInserter fBracketInserter= new BracketInserter();
1151
	private BracketInserter fBracketInserter= new BracketInserter();
1152
1152
Lines 1875-1880 Link Here
1875
	 * @see AbstractTextEditor#rememberSelection()
1875
	 * @see AbstractTextEditor#rememberSelection()
1876
	 */
1876
	 */
1877
	protected void rememberSelection() {
1877
	protected void rememberSelection() {
1878
		if (fRememberedSelection == null)
1879
			fRememberedSelection= new RememberedSelection();
1878
		fRememberedSelection.remember();
1880
		fRememberedSelection.remember();
1879
	}
1881
	}
1880
1882
Lines 1882-1888 Link Here
1882
	 * @see AbstractTextEditor#restoreSelection()
1884
	 * @see AbstractTextEditor#restoreSelection()
1883
	 */
1885
	 */
1884
	protected void restoreSelection() {
1886
	protected void restoreSelection() {
1885
		fRememberedSelection.restore();
1887
		if (fRememberedSelection != null)
1888
			fRememberedSelection.restore();
1889
		else
1890
			super.restoreSelection();
1886
	}
1891
	}
1887
1892
1888
	/*
1893
	/*
(-)ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java (-15 / +124 lines)
Lines 14-21 Link Here
14
14
15
import java.lang.reflect.InvocationTargetException;
15
import java.lang.reflect.InvocationTargetException;
16
16
17
import org.eclipse.swt.SWT;
18
19
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.IProgressMonitor;
21
19
Lines 25-36 Link Here
25
import org.eclipse.core.resources.IResource;
23
import org.eclipse.core.resources.IResource;
26
import org.eclipse.core.resources.IStorage;
24
import org.eclipse.core.resources.IStorage;
27
25
26
import org.eclipse.swt.SWT;
27
28
import org.eclipse.jface.action.Action;
28
import org.eclipse.jface.action.Action;
29
import org.eclipse.jface.action.IAction;
29
import org.eclipse.jface.action.IAction;
30
import org.eclipse.jface.viewers.ISelectionProvider;
30
import org.eclipse.jface.viewers.ISelectionProvider;
31
31
32
import org.eclipse.jface.text.Assert;
32
import org.eclipse.jface.text.Assert;
33
import org.eclipse.jface.text.IRegion;
33
import org.eclipse.jface.text.IRegion;
34
import org.eclipse.jface.text.ITextSelection;
34
import org.eclipse.jface.text.TextSelection;
35
import org.eclipse.jface.text.TextSelection;
35
36
36
import org.eclipse.ui.IEditorDescriptor;
37
import org.eclipse.ui.IEditorDescriptor;
Lines 41-59 Link Here
41
import org.eclipse.ui.IWorkbenchPage;
42
import org.eclipse.ui.IWorkbenchPage;
42
import org.eclipse.ui.PartInitException;
43
import org.eclipse.ui.PartInitException;
43
import org.eclipse.ui.actions.WorkspaceModifyOperation;
44
import org.eclipse.ui.actions.WorkspaceModifyOperation;
44
import org.eclipse.ui.ide.IDE;
45
import org.eclipse.ui.ide.IGotoMarker;
46
import org.eclipse.ui.part.FileEditorInput;
45
import org.eclipse.ui.part.FileEditorInput;
47
import org.eclipse.ui.texteditor.ITextEditor;
46
import org.eclipse.ui.texteditor.ITextEditor;
48
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
47
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
49
import org.eclipse.ui.texteditor.TextEditorAction;
48
import org.eclipse.ui.texteditor.TextEditorAction;
50
49
50
import org.eclipse.ui.ide.IDE;
51
import org.eclipse.ui.ide.IGotoMarker;
52
51
import org.eclipse.jdt.core.IClassFile;
53
import org.eclipse.jdt.core.IClassFile;
52
import org.eclipse.jdt.core.ICompilationUnit;
54
import org.eclipse.jdt.core.ICompilationUnit;
55
import org.eclipse.jdt.core.IImportDeclaration;
53
import org.eclipse.jdt.core.IJavaElement;
56
import org.eclipse.jdt.core.IJavaElement;
54
import org.eclipse.jdt.core.IJavaProject;
57
import org.eclipse.jdt.core.IJavaProject;
55
import org.eclipse.jdt.core.ILocalVariable;
58
import org.eclipse.jdt.core.ILocalVariable;
56
import org.eclipse.jdt.core.IMember;
59
import org.eclipse.jdt.core.IMember;
60
import org.eclipse.jdt.core.IPackageDeclaration;
57
import org.eclipse.jdt.core.ISourceRange;
61
import org.eclipse.jdt.core.ISourceRange;
58
import org.eclipse.jdt.core.ISourceReference;
62
import org.eclipse.jdt.core.ISourceReference;
59
import org.eclipse.jdt.core.ITypeParameter;
63
import org.eclipse.jdt.core.ITypeParameter;
Lines 63-68 Link Here
63
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
67
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
64
import org.eclipse.jdt.internal.corext.util.Messages;
68
import org.eclipse.jdt.internal.corext.util.Messages;
65
69
70
import org.eclipse.jdt.ui.PreferenceConstants;
71
66
import org.eclipse.jdt.internal.ui.JavaPlugin;
72
import org.eclipse.jdt.internal.ui.JavaPlugin;
67
73
68
/**
74
/**
Lines 128-138 Link Here
128
			return openInEditor((IFile) inputElement, activate);
134
			return openInEditor((IFile) inputElement, activate);
129
135
130
		IEditorInput input= getEditorInput(inputElement);
136
		IEditorInput input= getEditorInput(inputElement);
131
		if (input instanceof IFileEditorInput) {
132
			IFileEditorInput fileInput= (IFileEditorInput) input;
133
			return openInEditor(fileInput.getFile(), activate);
134
		}
135
136
		if (input != null)
137
		if (input != null)
137
			return openInEditor(input, getEditorID(input, inputElement), activate);
138
			return openInEditor(input, getEditorID(input, inputElement), activate);
138
139
Lines 268-274 Link Here
268
	private static void initializeHighlightRange(IEditorPart editorPart) {
269
	private static void initializeHighlightRange(IEditorPart editorPart) {
269
		if (editorPart instanceof ITextEditor) {
270
		if (editorPart instanceof ITextEditor) {
270
			IAction toggleAction= editorPart.getEditorSite().getActionBars().getGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY);
271
			IAction toggleAction= editorPart.getEditorSite().getActionBars().getGlobalActionHandler(ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY);
271
			if (toggleAction != null && toggleAction.isEnabled() && toggleAction.isChecked()) {
272
			boolean enable= toggleAction != null; 
273
			if (enable && editorPart instanceof JavaEditor)
274
				enable= JavaPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
275
			else
276
				enable= enable && toggleAction.isEnabled() && toggleAction.isChecked();
277
			if (enable) {
272
				if (toggleAction instanceof TextEditorAction) {
278
				if (toggleAction instanceof TextEditorAction) {
273
					// Reset the action
279
					// Reset the action
274
					((TextEditorAction)toggleAction).setEditor(null);
280
					((TextEditorAction)toggleAction).setEditor(null);
Lines 301-317 Link Here
301
		return null;
307
		return null;
302
	}
308
	}
303
309
304
	private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException {
310
	private static IEditorInput getEditorInput(IJavaElement je) throws JavaModelException {
311
		final ITextSelection textSelection= getTextSelection(je);
312
		IJavaElement element= je;
305
		while (element != null) {
313
		while (element != null) {
306
			if (element instanceof ICompilationUnit) {
314
			if (element instanceof ICompilationUnit) {
307
				ICompilationUnit unit= JavaModelUtil.toOriginal((ICompilationUnit) element);
315
				ICompilationUnit unit= JavaModelUtil.toOriginal((ICompilationUnit) element);
308
					IResource resource= unit.getResource();
316
					IResource resource= unit.getResource();
309
					if (resource instanceof IFile)
317
					if (resource instanceof IFile) {
310
						return new FileEditorInput((IFile) resource);
318
						return new FileEditorInput((IFile) resource) {
319
							public Object getAdapter(Class adapter) {
320
								if (adapter == ITextSelection.class)
321
									return textSelection;
322
								return super.getAdapter(adapter);
323
							}
324
						};
325
					}
311
			}
326
			}
312
327
313
			if (element instanceof IClassFile)
328
			if (element instanceof IClassFile) {
314
				return new InternalClassFileEditorInput((IClassFile) element);
329
				if (textSelection != null)
330
					return new InternalClassFileEditorInput((IClassFile) element, textSelection);
331
				else
332
					return new InternalClassFileEditorInput((IClassFile) element);
333
			}
315
334
316
			element= element.getParent();
335
			element= element.getParent();
317
		}
336
		}
Lines 319-324 Link Here
319
		return null;
338
		return null;
320
	}
339
	}
321
340
341
	/**
342
	 * Returns the text selection for the given Java element.
343
	 * 
344
	 * @param je the Java element
345
	 * @return the text selection or <code>null</code>
346
	 */
347
	public static ITextSelection getTextSelection(IJavaElement je) {
348
		if (je instanceof ISourceReference)
349
			return getTextSelection((ISourceReference)je);
350
		return null;
351
	}
352
	
353
	/**
354
	 * Returns the text selection for the given source reference.
355
	 * 
356
	 * @param reference	the source reference
357
	 * @return the text selection or <code>null</code>
358
	 */
359
	public static ITextSelection getTextSelection(ISourceReference reference) {
360
		if (reference == null)
361
			return null;
362
		
363
		try {
364
			ISourceRange range= null;
365
			if (reference instanceof ILocalVariable) {
366
				IJavaElement je2= ((ILocalVariable)reference).getParent();
367
				if (je2 instanceof ISourceReference)
368
					range= ((ISourceReference)je2).getSourceRange();
369
			} else
370
				range= reference.getSourceRange();
371
			
372
			if (range == null)
373
				return null;
374
			
375
			int offset= range.getOffset();
376
			int length= range.getLength();
377
			
378
			if (offset < 0 || length < 0)
379
				return null;
380
			
381
			offset= -1;
382
			length= -1;
383
			
384
			if (reference instanceof IMember) {
385
				range= ((IMember) reference).getNameRange();
386
				if (range != null) {
387
					offset= range.getOffset();
388
					length= range.getLength();
389
				}
390
			} else if (reference instanceof ITypeParameter) {
391
				range= ((ITypeParameter) reference).getNameRange();
392
				if (range != null) {
393
					offset= range.getOffset();
394
					length= range.getLength();
395
				}
396
			} else if (reference instanceof ILocalVariable) {
397
				range= ((ILocalVariable)reference).getNameRange();
398
				if (range != null) {
399
					offset= range.getOffset();
400
					length= range.getLength();
401
				}
402
			} else if (reference instanceof IImportDeclaration) {
403
				String name= ((IImportDeclaration) reference).getElementName();
404
				if (name != null && name.length() > 0) {
405
					String content= reference.getSource();
406
					if (content != null) {
407
						offset= range.getOffset() + content.indexOf(name);
408
						length= name.length();
409
					}
410
				}
411
			} else if (reference instanceof IPackageDeclaration) {
412
				String name= ((IPackageDeclaration) reference).getElementName();
413
				if (name != null && name.length() > 0) {
414
					String content= reference.getSource();
415
					if (content != null) {
416
						offset= range.getOffset() + content.indexOf(name);
417
						length= name.length();
418
					}
419
				}
420
			}
421
			if (offset >= 0 && length >= 0)
422
				return new TextSelection(offset, length);
423
			
424
		} catch (JavaModelException x) {
425
		} catch (IllegalArgumentException x) {
426
		}
427
		
428
		return null;
429
	}
430
322
	public static IEditorInput getEditorInput(Object input) throws JavaModelException {
431
	public static IEditorInput getEditorInput(Object input) throws JavaModelException {
323
		if (input instanceof IJavaElement)
432
		if (input instanceof IJavaElement)
324
			return getEditorInput((IJavaElement) input);
433
			return getEditorInput((IJavaElement) input);
(-)ui/org/eclipse/jdt/internal/ui/javaeditor/InternalClassFileEditorInput.java (-1 / +16 lines)
Lines 12-19 Link Here
12
package org.eclipse.jdt.internal.ui.javaeditor;
12
package org.eclipse.jdt.internal.ui.javaeditor;
13
13
14
14
15
import org.eclipse.core.runtime.IAdaptable;
16
15
import org.eclipse.jface.resource.ImageDescriptor;
17
import org.eclipse.jface.resource.ImageDescriptor;
16
18
19
import org.eclipse.jface.text.ITextSelection;
20
17
import org.eclipse.ui.IMemento;
21
import org.eclipse.ui.IMemento;
18
import org.eclipse.ui.IPersistableElement;
22
import org.eclipse.ui.IPersistableElement;
19
23
Lines 26-38 Link Here
26
/**
30
/**
27
 * Class file considered as editor input.
31
 * Class file considered as editor input.
28
 */
32
 */
29
public class InternalClassFileEditorInput implements IClassFileEditorInput, IPersistableElement {
33
public class InternalClassFileEditorInput implements IClassFileEditorInput, IPersistableElement, IAdaptable {
30
34
31
	private IClassFile fClassFile;
35
	private IClassFile fClassFile;
36
	private ITextSelection fInitialSelection;
32
37
33
	public InternalClassFileEditorInput(IClassFile classFile) {
38
	public InternalClassFileEditorInput(IClassFile classFile) {
34
		fClassFile= classFile;
39
		fClassFile= classFile;
35
	}
40
	}
41
	
42
	public InternalClassFileEditorInput(IClassFile classFile, ITextSelection initialSelection) {
43
		fClassFile= classFile;
44
		fInitialSelection= initialSelection;
45
	}
36
46
37
	/*
47
	/*
38
	 * @see Object#equals(Object)
48
	 * @see Object#equals(Object)
Lines 112-117 Link Here
112
	public Object getAdapter(Class adapter) {
122
	public Object getAdapter(Class adapter) {
113
		if (adapter == IClassFile.class)
123
		if (adapter == IClassFile.class)
114
			return fClassFile;
124
			return fClassFile;
125
		
126
		if (adapter == ITextSelection.class)
127
			return fInitialSelection;
128
		
115
		return fClassFile.getAdapter(adapter);
129
		return fClassFile.getAdapter(adapter);
116
	}
130
	}
117
131
Lines 128-133 Link Here
128
	public String getFactoryId() {
142
	public String getFactoryId() {
129
		return ClassFileEditorInputFactory.ID;
143
		return ClassFileEditorInputFactory.ID;
130
	}
144
	}
145
	
131
}
146
}
132
147
133
148
(-)ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java (-50 / +16 lines)
Lines 166-176 Link Here
166
import org.eclipse.jdt.core.IJavaElement;
166
import org.eclipse.jdt.core.IJavaElement;
167
import org.eclipse.jdt.core.IJavaProject;
167
import org.eclipse.jdt.core.IJavaProject;
168
import org.eclipse.jdt.core.ILocalVariable;
168
import org.eclipse.jdt.core.ILocalVariable;
169
import org.eclipse.jdt.core.IMember;
170
import org.eclipse.jdt.core.IPackageDeclaration;
171
import org.eclipse.jdt.core.ISourceRange;
169
import org.eclipse.jdt.core.ISourceRange;
172
import org.eclipse.jdt.core.ISourceReference;
170
import org.eclipse.jdt.core.ISourceReference;
173
import org.eclipse.jdt.core.ITypeParameter;
174
import org.eclipse.jdt.core.JavaCore;
171
import org.eclipse.jdt.core.JavaCore;
175
import org.eclipse.jdt.core.JavaModelException;
172
import org.eclipse.jdt.core.JavaModelException;
176
import org.eclipse.jdt.core.dom.ASTNode;
173
import org.eclipse.jdt.core.dom.ASTNode;
Lines 1927-1939 Link Here
1927
		if (getSelectionProvider() == null)
1924
		if (getSelectionProvider() == null)
1928
			return;
1925
			return;
1929
1926
1927
		ITextSelection newSelection= EditorUtility.getTextSelection(reference);
1930
		ISelection selection= getSelectionProvider().getSelection();
1928
		ISelection selection= getSelectionProvider().getSelection();
1931
		if (selection instanceof TextSelection) {
1929
		if (selection instanceof TextSelection) {
1932
			TextSelection textSelection= (TextSelection) selection;
1930
			TextSelection currentSelection= (TextSelection) selection;
1931
			if (newSelection != null && newSelection.getOffset() == currentSelection.getOffset() && newSelection.getLength() == currentSelection.getLength())
1932
				return;
1933
			// PR 39995: [navigation] Forward history cleared after going back in navigation history:
1933
			// PR 39995: [navigation] Forward history cleared after going back in navigation history:
1934
			// mark only in navigation history if the cursor is being moved (which it isn't if
1934
			// mark only in navigation history if the cursor is being moved (which it isn't if
1935
			// this is called from a PostSelectionEvent that should only update the magnet)
1935
			// this is called from a PostSelectionEvent that should only update the magnet)
1936
			if (moveCursor && (textSelection.getOffset() != 0 || textSelection.getLength() != 0))
1936
			if (moveCursor && (currentSelection.getOffset() != 0 || currentSelection.getLength() != 0))
1937
				markInNavigationHistory();
1937
				markInNavigationHistory();
1938
		}
1938
		}
1939
1939
Lines 1971-2023 Link Here
1971
				if (!moveCursor)
1971
				if (!moveCursor)
1972
					return;
1972
					return;
1973
1973
1974
				offset= -1;
1974
				if (newSelection != null && newSelection.getOffset() > -1 && newSelection.getLength() > 0) {
1975
				length= -1;
1976
1977
				if (reference instanceof IMember) {
1978
					range= ((IMember) reference).getNameRange();
1979
					if (range != null) {
1980
						offset= range.getOffset();
1981
						length= range.getLength();
1982
					}
1983
				} else if (reference instanceof ITypeParameter) {
1984
					range= ((ITypeParameter) reference).getNameRange();
1985
					if (range != null) {
1986
						offset= range.getOffset();
1987
						length= range.getLength();
1988
					}
1989
				} else if (reference instanceof ILocalVariable) {
1990
					range= ((ILocalVariable)reference).getNameRange();
1991
					if (range != null) {
1992
						offset= range.getOffset();
1993
						length= range.getLength();
1994
					}
1995
				} else if (reference instanceof IImportDeclaration) {
1996
					String name= ((IImportDeclaration) reference).getElementName();
1997
					if (name != null && name.length() > 0) {
1998
						String content= reference.getSource();
1999
						if (content != null) {
2000
							offset= range.getOffset() + content.indexOf(name);
2001
							length= name.length();
2002
						}
2003
					}
2004
				} else if (reference instanceof IPackageDeclaration) {
2005
					String name= ((IPackageDeclaration) reference).getElementName();
2006
					if (name != null && name.length() > 0) {
2007
						String content= reference.getSource();
2008
						if (content != null) {
2009
							offset= range.getOffset() + content.indexOf(name);
2010
							length= name.length();
2011
						}
2012
					}
2013
				}
2014
2015
				if (offset > -1 && length > 0) {
2016
1975
2017
					try  {
1976
					try  {
2018
						textWidget.setRedraw(false);
1977
						textWidget.setRedraw(false);
2019
						sourceViewer.revealRange(offset, length);
1978
						sourceViewer.revealRange(newSelection.getOffset(), newSelection.getLength());
2020
						sourceViewer.setSelectedRange(offset, length);
1979
						sourceViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());
2021
					} finally {
1980
					} finally {
2022
						textWidget.setRedraw(true);
1981
						textWidget.setRedraw(true);
2023
					}
1982
					}
Lines 2045-2051 Link Here
2045
			 */
2004
			 */
2046
			return;
2005
			return;
2047
		}
2006
		}
2048
2007
		
2008
		if (getSelectionProvider().getSelection() instanceof ITextSelection) {
2009
			ITextSelection currentSelection= (ITextSelection)getSelectionProvider().getSelection();
2010
			ITextSelection newSelection= EditorUtility.getTextSelection(element);
2011
			if (newSelection != null && newSelection.getOffset() == currentSelection.getOffset() && newSelection.getLength() == currentSelection.getLength())
2012
				return;
2013
		}
2014
		
2049
		IJavaElement corresponding= getCorrespondingElement(element);
2015
		IJavaElement corresponding= getCorrespondingElement(element);
2050
		if (corresponding instanceof ISourceReference) {
2016
		if (corresponding instanceof ISourceReference) {
2051
			ISourceReference reference= (ISourceReference) corresponding;
2017
			ISourceReference reference= (ISourceReference) corresponding;

Return to bug 93845