PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.debug.ui.source_3.7.101.v20110817_r371/org/eclipse/debug/ui/actions/ToggleBreakpointAction.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 227 lines | 143 code | 18 blank | 66 comment | 33 complexity | 6e0cd645674121f546e9f5700908ef91 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2005, 2009 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. * Wind River Systems - added support for IToggleBreakpointsTargetFactory
  11. *******************************************************************************/
  12. package org.eclipse.debug.ui.actions;
  13. import org.eclipse.core.runtime.CoreException;
  14. import org.eclipse.debug.internal.ui.DebugUIPlugin;
  15. import org.eclipse.debug.internal.ui.actions.ActionMessages;
  16. import org.eclipse.debug.internal.ui.actions.IToggleBreakpointsTargetManagerListener;
  17. import org.eclipse.debug.internal.ui.actions.ToggleBreakpointsTargetManager;
  18. import org.eclipse.jface.action.Action;
  19. import org.eclipse.jface.text.BadLocationException;
  20. import org.eclipse.jface.text.IDocument;
  21. import org.eclipse.jface.text.IRegion;
  22. import org.eclipse.jface.text.ITextSelection;
  23. import org.eclipse.jface.text.TextSelection;
  24. import org.eclipse.jface.text.source.IVerticalRulerInfo;
  25. import org.eclipse.jface.viewers.ISelection;
  26. import org.eclipse.jface.viewers.ISelectionProvider;
  27. import org.eclipse.ui.IWorkbenchPart;
  28. import org.eclipse.ui.texteditor.IDocumentProvider;
  29. import org.eclipse.ui.texteditor.ITextEditor;
  30. import org.eclipse.ui.texteditor.IUpdate;
  31. /**
  32. * Action to toggle a breakpoint in a vertical ruler of a workbench part
  33. * containing a document. The part must provide an <code>IToggleBreakpointsTarget</code>
  34. * adapter which may optionally be an instance of an
  35. * <code>IToggleBreakpointsTargetExtension</code>.
  36. * <p>
  37. * Clients may instantiate this class.
  38. * </p>
  39. * @since 3.1
  40. * @see org.eclipse.debug.ui.actions.RulerToggleBreakpointActionDelegate
  41. * @noextend This class is not intended to be subclassed by clients.
  42. */
  43. public class ToggleBreakpointAction extends Action implements IUpdate {
  44. private IWorkbenchPart fPart;
  45. private IDocument fDocument;
  46. private IVerticalRulerInfo fRulerInfo;
  47. private IToggleBreakpointsTargetManagerListener fListener = new IToggleBreakpointsTargetManagerListener() {
  48. public void preferredTargetsChanged() {
  49. update();
  50. }
  51. };
  52. /**
  53. * Constructs a new action to toggle a breakpoint in the given
  54. * part containing the given document and ruler.
  55. *
  56. * @param part the part in which to toggle the breakpoint - provides
  57. * an <code>IToggleBreakpointsTarget</code> adapter
  58. * @param document the document breakpoints are being set in or
  59. * <code>null</code> when the document should be derived from the
  60. * given part
  61. * @param rulerInfo specifies location the user has double-clicked
  62. */
  63. public ToggleBreakpointAction(IWorkbenchPart part, IDocument document, IVerticalRulerInfo rulerInfo) {
  64. super(ActionMessages.ToggleBreakpointAction_0);
  65. fPart = part;
  66. fDocument = document;
  67. fRulerInfo = rulerInfo;
  68. ToggleBreakpointsTargetManager.getDefault().addChangedListener(fListener);
  69. }
  70. /*
  71. * (non-Javadoc)
  72. * @see org.eclipse.jface.action.IAction#run()
  73. */
  74. public void run() {
  75. IDocument document= getDocument();
  76. if (document == null) {
  77. return;
  78. }
  79. int line = fRulerInfo.getLineOfLastMouseButtonActivity();
  80. // Test if line is valid
  81. if (line == -1)
  82. return;
  83. try {
  84. ITextSelection selection = getTextSelection(document, line);
  85. IToggleBreakpointsTarget toggleTarget =
  86. ToggleBreakpointsTargetManager.getDefault().getToggleBreakpointsTarget(fPart, selection);
  87. if (toggleTarget == null) {
  88. return;
  89. }
  90. if (toggleTarget instanceof IToggleBreakpointsTargetExtension) {
  91. IToggleBreakpointsTargetExtension extension = (IToggleBreakpointsTargetExtension) toggleTarget;
  92. if (extension.canToggleBreakpoints(fPart, selection)) {
  93. extension.toggleBreakpoints(fPart, selection);
  94. return;
  95. }
  96. }
  97. if (toggleTarget.canToggleLineBreakpoints(fPart, selection)) {
  98. toggleTarget.toggleLineBreakpoints(fPart, selection);
  99. } else if (toggleTarget.canToggleWatchpoints(fPart, selection)) {
  100. toggleTarget.toggleWatchpoints(fPart, selection);
  101. } else if (toggleTarget.canToggleMethodBreakpoints(fPart, selection)) {
  102. toggleTarget.toggleMethodBreakpoints(fPart, selection);
  103. }
  104. } catch (BadLocationException e) {
  105. reportException(e);
  106. } catch (CoreException e) {
  107. reportException(e);
  108. }
  109. }
  110. /**
  111. * Report an error to the user.
  112. *
  113. * @param e underlying exception
  114. */
  115. private void reportException(Exception e) {
  116. DebugUIPlugin.errorDialog(fPart.getSite().getShell(), ActionMessages.ToggleBreakpointAction_1, ActionMessages.ToggleBreakpointAction_2, e); //
  117. }
  118. /**
  119. * Disposes this action. Clients must call this method when
  120. * this action is no longer needed.
  121. */
  122. public void dispose() {
  123. fDocument = null;
  124. fPart = null;
  125. fRulerInfo = null;
  126. ToggleBreakpointsTargetManager.getDefault().removeChangedListener(fListener);
  127. }
  128. /**
  129. * Returns the document on which this action operates.
  130. *
  131. * @return the document or <code>null</code> if none
  132. */
  133. private IDocument getDocument() {
  134. if (fDocument != null)
  135. return fDocument;
  136. if (fPart instanceof ITextEditor) {
  137. ITextEditor editor= (ITextEditor)fPart;
  138. IDocumentProvider provider = editor.getDocumentProvider();
  139. if (provider != null)
  140. return provider.getDocument(editor.getEditorInput());
  141. }
  142. IDocument doc = (IDocument) fPart.getAdapter(IDocument.class);
  143. if (doc != null) {
  144. return doc;
  145. }
  146. return null;
  147. }
  148. /* (non-Javadoc)
  149. * @see org.eclipse.ui.texteditor.IUpdate#update()
  150. */
  151. public void update() {
  152. IDocument document= getDocument();
  153. if (document != null) {
  154. int line = fRulerInfo.getLineOfLastMouseButtonActivity();
  155. if (line > -1) {
  156. try {
  157. ITextSelection selection = getTextSelection(document, line);
  158. IToggleBreakpointsTarget adapter =
  159. ToggleBreakpointsTargetManager.getDefault().getToggleBreakpointsTarget(fPart, selection);
  160. if (adapter == null) {
  161. setEnabled(false);
  162. return;
  163. }
  164. if (adapter instanceof IToggleBreakpointsTargetExtension) {
  165. IToggleBreakpointsTargetExtension extension = (IToggleBreakpointsTargetExtension) adapter;
  166. if (extension.canToggleBreakpoints(fPart, selection)) {
  167. setEnabled(true);
  168. return;
  169. }
  170. }
  171. if (adapter.canToggleLineBreakpoints(fPart, selection) ||
  172. adapter.canToggleWatchpoints(fPart, selection) ||
  173. adapter.canToggleMethodBreakpoints(fPart, selection))
  174. {
  175. setEnabled(true);
  176. return;
  177. }
  178. } catch (BadLocationException e) {
  179. reportException(e);
  180. }
  181. }
  182. }
  183. setEnabled(false);
  184. }
  185. /**
  186. * Determines the text selection for the breakpoint action. If clicking on the ruler inside
  187. * the highlighted text, return the text selection for the highlighted text. Otherwise,
  188. * return a text selection representing the start of the line.
  189. *
  190. * @param document The IDocument backing the Editor.
  191. * @param line The line clicked on in the ruler.
  192. * @return An ITextSelection as described.
  193. * @throws BadLocationException If underlying operations throw.
  194. */
  195. private ITextSelection getTextSelection(IDocument document, int line) throws BadLocationException {
  196. IRegion region = document.getLineInformation(line);
  197. ITextSelection textSelection = new TextSelection(document, region.getOffset(), 0);
  198. ISelectionProvider provider = fPart.getSite().getSelectionProvider();
  199. if (provider != null){
  200. ISelection selection = provider.getSelection();
  201. if (selection instanceof ITextSelection
  202. && ((ITextSelection) selection).getStartLine() <= line
  203. && ((ITextSelection) selection).getEndLine() >= line) {
  204. textSelection = (ITextSelection) selection;
  205. }
  206. }
  207. return textSelection;
  208. }
  209. }