Bug 118104 - ToggleBreakpointAdapter class cast exception for ITextEditor
diff --git a/org.eclipse.jdt.debug.ui/META-INF/MANIFEST.MF b/org.eclipse.jdt.debug.ui/META-INF/MANIFEST.MF
index 7b63ff4..065750a 100644
--- a/org.eclipse.jdt.debug.ui/META-INF/MANIFEST.MF
+++ b/org.eclipse.jdt.debug.ui/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.jdt.debug.ui; singleton:=true
-Bundle-Version: 3.1.1
+Bundle-Version: 3.1.2
 Bundle-Activator: org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/RunToLineAdapter.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/RunToLineAdapter.java
index 25996e2..f4faf11 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/RunToLineAdapter.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/RunToLineAdapter.java
@@ -38,7 +38,6 @@
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.swt.custom.BusyIndicator;
 import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.texteditor.ITextEditor;
 
@@ -51,56 +50,59 @@
 	 * @see org.eclipse.debug.ui.actions.IRunToLineTarget#runToLine(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection, org.eclipse.debug.core.model.ISuspendResume)
 	 */
 	public void runToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) throws CoreException {
-		IEditorPart editorPart = (IEditorPart)part;
-		IEditorInput input = editorPart.getEditorInput();
+		ITextEditor textEditor = getTextEditor(part);
 		String errorMessage = null;
-		if (input == null) {
-			errorMessage = ActionMessages.RunToLineAdapter_0; //$NON-NLS-1$
+		if (textEditor == null) {
+			errorMessage = ActionMessages.RunToLineAdapter_1;
 		} else {
-			final ITextEditor textEditor = (ITextEditor)editorPart;
-			final IDocument document= textEditor.getDocumentProvider().getDocument(input);
-			if (document == null) {
-				errorMessage = ActionMessages.RunToLineAdapter_1; //$NON-NLS-1$
+			IEditorInput input = textEditor.getEditorInput();
+			if (input == null) {
+				errorMessage = ActionMessages.RunToLineAdapter_0; //$NON-NLS-1$
 			} else {
-				final int[] validLine = new int[1];
-				final String[] typeName = new String[1];
-				final int[] lineNumber = new int[1];
-				final ITextSelection textSelection = (ITextSelection) selection;
-				Runnable r = new Runnable() {
-					public void run() {
-						lineNumber[0] = textSelection.getStartLine() + 1;
-						ASTParser parser = ASTParser.newParser(AST.JLS3);
-						parser.setSource(document.get().toCharArray());
-						CompilationUnit compilationUnit= (CompilationUnit)parser.createAST(null);
-						ValidBreakpointLocationLocator locator= new ValidBreakpointLocationLocator(compilationUnit, lineNumber[0], false, false);
-						compilationUnit.accept(locator);
-						validLine[0]= locator.getLineLocation();		
-						typeName[0]= locator.getFullyQualifiedTypeName();
-					}
-				};
-				BusyIndicator.showWhile(JDIDebugUIPlugin.getStandardDisplay(), r);
-				if (validLine[0] == lineNumber[0]) {
-					IBreakpoint breakpoint= null;
-					Map attributes = new HashMap(4);
-					BreakpointUtils.addRunToLineAttributes(attributes);
-					breakpoint= JDIDebugModel.createLineBreakpoint(ResourcesPlugin.getWorkspace().getRoot(), typeName[0], lineNumber[0], -1, -1, 1, false, attributes);
-					errorMessage = ActionMessages.RunToLineAdapter_2; //$NON-NLS-1$
-					if (target instanceof IAdaptable) {
-						IDebugTarget debugTarget = (IDebugTarget) ((IAdaptable)target).getAdapter(IDebugTarget.class);
-						if (debugTarget != null) {
-                            RunToLineHandler handler = new RunToLineHandler(debugTarget, target, breakpoint);
-                            handler.run(new NullProgressMonitor());
-							return;
-						}
-					}
+				final IDocument document= textEditor.getDocumentProvider().getDocument(input);
+				if (document == null) {
+					errorMessage = ActionMessages.RunToLineAdapter_1; //$NON-NLS-1$
 				} else {
-					// invalid line
-					if (textSelection.getLength() > 0) {
-						errorMessage = ActionMessages.RunToLineAdapter_3; //$NON-NLS-1$
+					final int[] validLine = new int[1];
+					final String[] typeName = new String[1];
+					final int[] lineNumber = new int[1];
+					final ITextSelection textSelection = (ITextSelection) selection;
+					Runnable r = new Runnable() {
+						public void run() {
+							lineNumber[0] = textSelection.getStartLine() + 1;
+							ASTParser parser = ASTParser.newParser(AST.JLS3);
+							parser.setSource(document.get().toCharArray());
+							CompilationUnit compilationUnit= (CompilationUnit)parser.createAST(null);
+							ValidBreakpointLocationLocator locator= new ValidBreakpointLocationLocator(compilationUnit, lineNumber[0], false, false);
+							compilationUnit.accept(locator);
+							validLine[0]= locator.getLineLocation();		
+							typeName[0]= locator.getFullyQualifiedTypeName();
+						}
+					};
+					BusyIndicator.showWhile(JDIDebugUIPlugin.getStandardDisplay(), r);
+					if (validLine[0] == lineNumber[0]) {
+						IBreakpoint breakpoint= null;
+						Map attributes = new HashMap(4);
+						BreakpointUtils.addRunToLineAttributes(attributes);
+						breakpoint= JDIDebugModel.createLineBreakpoint(ResourcesPlugin.getWorkspace().getRoot(), typeName[0], lineNumber[0], -1, -1, 1, false, attributes);
+						errorMessage = ActionMessages.RunToLineAdapter_2; //$NON-NLS-1$
+						if (target instanceof IAdaptable) {
+							IDebugTarget debugTarget = (IDebugTarget) ((IAdaptable)target).getAdapter(IDebugTarget.class);
+							if (debugTarget != null) {
+	                            RunToLineHandler handler = new RunToLineHandler(debugTarget, target, breakpoint);
+	                            handler.run(new NullProgressMonitor());
+								return;
+							}
+						}
 					} else {
-						errorMessage = ActionMessages.RunToLineAdapter_4; //$NON-NLS-1$
+						// invalid line
+						if (textSelection.getLength() > 0) {
+							errorMessage = ActionMessages.RunToLineAdapter_3; //$NON-NLS-1$
+						} else {
+							errorMessage = ActionMessages.RunToLineAdapter_4; //$NON-NLS-1$
+						}
+	
 					}
-
 				}
 			}
 		}
@@ -119,4 +121,19 @@
         }
 		return false;
 	}
+	
+    /**
+     * Returns the text editor associated with the given part or <code>null</code>
+     * if none. In case of a multi-page editor, this method should be used to retrieve
+     * the correct editor to perform the breakpoint operation on.
+     * 
+     * @param part workbench part
+     * @return text editor part or <code>null</code>
+     */
+    protected ITextEditor getTextEditor(IWorkbenchPart part) {
+    	if (part instanceof ITextEditor) {
+    		return (ITextEditor) part;
+    	}
+    	return (ITextEditor) part.getAdapter(ITextEditor.class);
+    }	
 }
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/ToggleBreakpointAdapter.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/ToggleBreakpointAdapter.java
index 9c9b241..63fc455 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/ToggleBreakpointAdapter.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/ToggleBreakpointAdapter.java
@@ -148,16 +148,16 @@
     public void toggleLineBreakpoints(final IWorkbenchPart part, final ISelection selection, final boolean bestMatch) {
         Job job = new Job("Toggle Line Breakpoint") { //$NON-NLS-1$
             protected IStatus run(IProgressMonitor monitor) {
-                if (selection instanceof ITextSelection) {
+            	ITextEditor textEditor = getTextEditor(part);
+                if (textEditor != null && selection instanceof ITextSelection) {
                     if (monitor.isCanceled()) {
                         return Status.CANCEL_STATUS;
                     }
                     report(null, part);
-                    IEditorPart editorPart = (IEditorPart) part;
                     ITextSelection textSelection = (ITextSelection) selection;
                     IType type = getType(textSelection);
-                    IEditorInput editorInput = editorPart.getEditorInput();
-                    IDocumentProvider documentProvider = ((ITextEditor) editorPart).getDocumentProvider();
+                    IEditorInput editorInput = textEditor.getEditorInput();
+                    IDocumentProvider documentProvider = textEditor.getDocumentProvider();
                     if (documentProvider == null) {
                         return Status.CANCEL_STATUS;
                     }
@@ -178,7 +178,7 @@
                                     int end = start + sourceRange.getLength();
                                     if (offset < start || offset > end) {
                                         // not in the inner type
-                                        IStatusLineManager statusLine = editorPart.getEditorSite().getActionBars().getStatusLineManager();
+                                        IStatusLineManager statusLine = textEditor.getEditorSite().getActionBars().getStatusLineManager();
                                         statusLine.setErrorMessage(MessageFormat.format(ActionMessages.ManageBreakpointRulerAction_Breakpoints_can_only_be_created_within_the_type_associated_with_the_editor___0___1, new String[] { type.getTypeQualifiedName() })); //$NON-NLS-1$
                                         Display.getCurrent().beep();
                                         return Status.OK_STATUS;
@@ -191,18 +191,16 @@
                         IResource resource = null;
                         Map attributes = new HashMap(10);
                         if (type == null) {
-                            resource = getResource(editorPart);
-                            if (editorPart instanceof ITextEditor) {
-                                CompilationUnit unit = parseCompilationUnit((ITextEditor) editorPart);
-                                Iterator types = unit.types().iterator();
-                                while (types.hasNext()) {
-                                    TypeDeclaration declaration = (TypeDeclaration) types.next();
-                                    int begin = declaration.getStartPosition();
-                                    int end = begin + declaration.getLength();
-                                    if (offset >= begin && offset <= end && !declaration.isInterface()) {
-                                        typeName = ValidBreakpointLocationLocator.computeTypeName(declaration);
-                                        break;
-                                    }
+                            resource = getResource(textEditor);
+                            CompilationUnit unit = parseCompilationUnit(textEditor);
+                            Iterator types = unit.types().iterator();
+                            while (types.hasNext()) {
+                                TypeDeclaration declaration = (TypeDeclaration) types.next();
+                                int begin = declaration.getStartPosition();
+                                int end = begin + declaration.getLength();
+                                if (offset >= begin && offset <= end && !declaration.isInterface()) {
+                                    typeName = ValidBreakpointLocationLocator.computeTypeName(declaration);
+                                    break;
                                 }
                             }
                         } else {
@@ -227,7 +225,7 @@
                                 removeBreakpoint(existingBreakpoint, true);
                                 return Status.OK_STATUS;
                             }
-                            createLineBreakpoint(resource, typeName, lineNumber, -1, -1, 0, true, attributes, document, bestMatch, type, editorPart);
+                            createLineBreakpoint(resource, typeName, lineNumber, -1, -1, 0, true, attributes, document, bestMatch, type, textEditor);
                         }
                     } catch (CoreException ce) {
                         return ce.getStatus();
@@ -271,10 +269,11 @@
                     report(null, part);
                     ISelection selection = finalSelection;
                     selection = translateToMembers(part, selection);
-                    if (selection instanceof ITextSelection) {
+                    ITextEditor textEditor = getTextEditor(part);
+                    if (textEditor != null && selection instanceof ITextSelection) {
                         ITextSelection textSelection = (ITextSelection) selection;
                         if (selection != null) {
-                            CompilationUnit compilationUnit = parseCompilationUnit((ITextEditor) part);
+                            CompilationUnit compilationUnit = parseCompilationUnit(textEditor);
                             if (compilationUnit != null) {
                                 BreakpointMethodLocator locator = new BreakpointMethodLocator(textSelection.getOffset());
                                 compilationUnit.accept(locator);
@@ -381,6 +380,21 @@
         }
         return selection instanceof ITextSelection;
     }
+    
+    /**
+     * Returns the text editor associated with the given part or <code>null</code>
+     * if none. In case of a multi-page editor, this method should be used to retrieve
+     * the correct editor to perform the breakpoint operation on.
+     * 
+     * @param part workbench part
+     * @return text editor part or <code>null</code>
+     */
+    protected ITextEditor getTextEditor(IWorkbenchPart part) {
+    	if (part instanceof ITextEditor) {
+    		return (ITextEditor) part;
+    	}
+    	return (ITextEditor) part.getAdapter(ITextEditor.class);
+    }
 
     protected IMethod[] getMethods(IStructuredSelection selection) {
         if (selection.isEmpty()) {
@@ -450,9 +464,10 @@
                     report(null, part);
                     ISelection selection = finalSelection;
                     selection = translateToMembers(part, selection);
-                    if (selection instanceof ITextSelection) {
+                    ITextEditor textEditor = getTextEditor(part);
+                    if (textEditor != null && selection instanceof ITextSelection) {
                         ITextSelection textSelection = (ITextSelection) selection;
-                        CompilationUnit compilationUnit = parseCompilationUnit((ITextEditor) part);
+                        CompilationUnit compilationUnit = parseCompilationUnit(textEditor);
                         if (compilationUnit != null) {
                             BreakpointFieldLocator locator = new BreakpointFieldLocator(textSelection.getOffset());
                             compilationUnit.accept(locator);
@@ -695,11 +710,11 @@
      *                if an exceptoin occurrs
      */
     protected ISelection translateToMembers(IWorkbenchPart part, ISelection selection) throws CoreException {
-        if (selection instanceof ITextSelection && part instanceof ITextEditor) {
+    	ITextEditor textEditor = getTextEditor(part);
+        if (selection instanceof ITextSelection && textEditor != null) {
             ITextSelection textSelection = (ITextSelection) selection;
-            ITextEditor editorPart = (ITextEditor) part;
-            IEditorInput editorInput = editorPart.getEditorInput();
-            IDocumentProvider documentProvider = editorPart.getDocumentProvider();
+            IEditorInput editorInput = textEditor.getEditorInput();
+            IDocumentProvider documentProvider = textEditor.getDocumentProvider();
             if (documentProvider == null) {
                 throw new CoreException(Status.CANCEL_STATUS);
             }