diff options
author | Alexander Kurtakov | 2013-01-21 21:46:43 +0000 |
---|---|---|
committer | Camilo Bernal | 2013-01-21 22:45:55 +0000 |
commit | 1817168a7099c637370efb7f46325e37d21e4c01 (patch) | |
tree | 9cb8422b12a8096b65b9e94348e04896876fa29f | |
parent | c660d42d3d962f69f720b2f53bf74c867dd8373a (diff) | |
download | org.eclipse.linuxtools-1817168a7099c637370efb7f46325e37d21e4c01.tar.gz org.eclipse.linuxtools-1817168a7099c637370efb7f46325e37d21e4c01.tar.xz org.eclipse.linuxtools-1817168a7099c637370efb7f46325e37d21e4c01.zip |
Decouple the double click listener from the editor internals.
Pre-req for dropping the systemtap internal CEditor in favor of CDT's
one.
Change-Id: Ieefe8829348be9bed16116822d3016b10f04f5e5
Reviewed-on: https://git.eclipse.org/r/9818
Tested-by: Hudson CI
Reviewed-by: Camilo Bernal <cabernal@redhat.com>
IP-Clean: Camilo Bernal <cabernal@redhat.com>
Tested-by: Camilo Bernal <cabernal@redhat.com>
3 files changed, 177 insertions, 150 deletions
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/hidden/KernelSourceAction.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/hidden/KernelSourceAction.java index 5b19cd46f6..303b3e6d1b 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/hidden/KernelSourceAction.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/hidden/KernelSourceAction.java @@ -70,7 +70,7 @@ public class KernelSourceAction extends Action implements ISelectionListener, IW expandAction = new TreeExpandCollapseAction(KernelBrowserView.class); LogManager.logDebug("End KernelSourceAction:", this); //$NON-NLS-1$ } - + /** * Updates <code>selection</code> with the current selection whenever the user changes * the current selection. @@ -126,7 +126,7 @@ public class KernelSourceAction extends Action implements ISelectionListener, IW if(o instanceof TreeNode) { TreeNode t = (TreeNode)o; if(t.isClickable()) { - + IFileStore fs = (IFileStore)t.getData(); if (fs != null) { IEditorInput input= createEditorInput(fs); @@ -135,16 +135,17 @@ public class KernelSourceAction extends Action implements ISelectionListener, IW if(editor instanceof STPEditor) IDESessionSettings.setActiveSTPEditor((STPEditor)editor); wb.getActiveWorkbenchWindow().getActivePage().openEditor(input, CEditor.ID); +// wb.getActiveWorkbenchWindow().getActivePage().openEditor(input, "org.eclipse.cdt.ui.editor.CEditor"); LogManager.logDebug("Editor opened", this); //$NON-NLS-1$ } catch (PartInitException e) { LogManager.logCritical("PartInitException run: " + e.getMessage(), this); //$NON-NLS-1$ } - + } } else { - + expandAction.run(); } } diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/hidden/RulerDoubleClickHandler.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/hidden/RulerDoubleClickHandler.java new file mode 100644 index 0000000000..039151364e --- /dev/null +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/hidden/RulerDoubleClickHandler.java @@ -0,0 +1,169 @@ +/******************************************************************************* + * Copyright (c) 2006 IBM Corporation. + * Copyright (c) 2013 Red Hat Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse + * Red Hat Inc. - extract it as separate class and decouple from the editor internals + *******************************************************************************/ +package org.eclipse.linuxtools.internal.systemtap.ui.ide.actions.hidden; + +import java.util.ArrayList; + +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.source.IVerticalRulerInfo; +import org.eclipse.linuxtools.internal.systemtap.ui.ide.IDEPlugin; +import org.eclipse.linuxtools.internal.systemtap.ui.ide.Localization; +import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor; +import org.eclipse.linuxtools.internal.systemtap.ui.ide.preferences.IDEPreferenceConstants; +import org.eclipse.linuxtools.systemtap.ui.editor.actions.file.NewFileAction; +import org.eclipse.linuxtools.systemtap.ui.ide.IDESessionSettings; +import org.eclipse.linuxtools.systemtap.ui.logging.LogManager; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.ide.FileStoreEditorInput; +import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor; + +/** + * The <code>RulerDoubleClickHandler</code> handles double click events on the + * ruler for this text editor. It first checks to see if the user clicked on a + * comment line, then if they clicked on a line that SystemTap can use as a probe + * point, and if the line of code passes both checks, it dispatches an event to the + * active STPEditor to insert a code block describing the line of code that the user + * clicked on. + * + * The block of code is sent to the STPEditor only under the following circumstances: + * <ul> + * <li>The line of code is not blank</li> + * <li>If the line of code contains a single-line comment, it must not be the only text on that line</li> + * <li>The line of code must not fall within a multiline comment</li> + * <li>The line of code must be a line that can be used by SystemTap, + * determined by running the following:<br/> + * <code>stap -p2 -e 'probe kernel.statement("*@filename:linenumber")'</code><br/> + * If <code>stap</code> does not generate errors while running the test command, the + * line is assumed valid.</li> + * </ul> + * + * If all of the above are met, the active STPEditor listed in <code>IDESessionSettings</code> + * is told to insert a template probe for this line of code using the <code>SimpleEditor.insertText</code> method. + * If no the returned STPEditor reference is null, the code opens a new editor. + * + * @see org.eclipse.linuxtools.systemtap.ui.editor.SimpleEditor#insertText + * @see org.eclipse.linuxtools.systemtap.ui.structures.runnable.Command + * @see org.eclipse.swt.events.MouseListener + */ +public class RulerDoubleClickHandler extends MouseAdapter { + + private AbstractDecoratedTextEditor editor; + public RulerDoubleClickHandler(AbstractDecoratedTextEditor editor) { + this.editor = editor; + } + /** + * The doubleclick event handler method. + * @param e The <code>MouseEvent</code> that represents this doubleclick event. + */ + @Override + public void mouseDoubleClick(MouseEvent e) + { + editor.getSite().getShell().setCursor(editor.getSite().getShell().getDisplay().getSystemCursor(SWT.CURSOR_WAIT)); + int lineno = ((IVerticalRulerInfo)editor.getAdapter(IVerticalRulerInfo.class)).getLineOfLastMouseButtonActivity(); + IDocument document = editor.getDocumentProvider().getDocument( + editor.getEditorInput()); + + String s = document.get(); + String[] lines = s.split("\n"); //$NON-NLS-1$ + String line = lines[lineno].trim(); + boolean die = false; + if("".equals(line)) //eat blank lines //$NON-NLS-1$ + die = true; + if(line.startsWith("#")) //eat preprocessor directives //$NON-NLS-1$ + die = true; + if(line.startsWith("//")) //eat C99 comments //$NON-NLS-1$ + die = true; + if(line.startsWith("/*") && !line.contains("*/") && !line.endsWith("*/")) //try to eat single-line C comments //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + die = true; + + //gogo find comment segments + try + { + ArrayList<Integer> commentChunks = new ArrayList<Integer>(); + char[] chars = s.toCharArray(); + int needle = 1; + int offset = document.getLineOffset(lineno); + while (needle < chars.length) + { + if(chars[needle-1] == '/' && chars[needle] == '*') + { + commentChunks.add(needle); + while(needle < chars.length) + { + if(chars[needle-1] == '*' && chars[needle] == '/') + { + commentChunks.add(needle); + needle++; + break; + } + needle++; + } + } + needle++; + } + for(int i=0, pair, start, end; i < commentChunks.size(); i++) + { + if(!(commentChunks.get(i).intValue() < offset)) + { + pair = i - i%2; + start = commentChunks.get(pair).intValue(); + end = commentChunks.get(pair+1).intValue(); + if(offset >= start && offset <= end) + die=true; + } + } + } catch (BadLocationException excp) { + LogManager.logCritical("Exception mouseDoubleClick: " + excp.getMessage(), this); //$NON-NLS-1$ + } + if(die) { + MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), + Localization.getString("CEditor.ProbeInsertFailed"),Localization.getString("CEditor.CanNotProbeLine")); //$NON-NLS-1$ //$NON-NLS-2$ + } else { + IEditorInput in = editor.getEditorInput(); + if(in instanceof FileStoreEditorInput) { + FileStoreEditorInput input = (FileStoreEditorInput)in; + + IPreferenceStore p = IDEPlugin.getDefault().getPreferenceStore(); + String kernroot = p.getString(IDEPreferenceConstants.P_KERNEL_SOURCE); + + String filepath = input.getURI().getPath(); + String kernrelative = filepath.substring(kernroot.length()+1, filepath.length()); + StringBuffer sb = new StringBuffer(); + + sb.append("probe kernel.statement(\"*@"+ kernrelative + ":" + (lineno+1) + "\")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + + sb.append("\n{\n\t\n}\n"); //$NON-NLS-1$ + STPEditor activeSTPEditor = IDESessionSettings.getActiveSTPEditor(); + if(null == activeSTPEditor) { + NewFileAction action = new NewFileAction(); + action.run(); + IEditorPart ed = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); + if(ed instanceof STPEditor) + IDESessionSettings.setActiveSTPEditor((STPEditor)ed); + } + + if(null != activeSTPEditor) + activeSTPEditor.insertText(sb.toString()); + } + } + editor.getSite().getShell().setCursor(null); //Return the cursor to normal + } +} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/c/CEditor.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/c/CEditor.java index 200c7bc99f..f0b16afc0a 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/c/CEditor.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/c/CEditor.java @@ -11,11 +11,6 @@ package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.c; -import java.util.ArrayList; - -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.jface.preference.IPreferenceStore; -import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.source.AnnotationRulerColumn; import org.eclipse.jface.text.source.CompositeRuler; import org.eclipse.jface.text.source.IVerticalRuler; @@ -24,23 +19,11 @@ import org.eclipse.jface.text.source.SourceViewerConfiguration; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.linuxtools.internal.systemtap.ui.ide.IDEPlugin; -import org.eclipse.linuxtools.internal.systemtap.ui.ide.Localization; -import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPEditor; +import org.eclipse.linuxtools.internal.systemtap.ui.ide.actions.hidden.RulerDoubleClickHandler; import org.eclipse.linuxtools.internal.systemtap.ui.ide.preferences.IDEPreferenceConstants; import org.eclipse.linuxtools.systemtap.ui.editor.ColorManager; -import org.eclipse.linuxtools.systemtap.ui.editor.actions.file.NewFileAction; -import org.eclipse.linuxtools.systemtap.ui.ide.IDESessionSettings; -import org.eclipse.linuxtools.systemtap.ui.logging.LogManager; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.MouseAdapter; -import org.eclipse.swt.events.MouseEvent; -import org.eclipse.swt.graphics.Cursor; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.ide.FileStoreEditorInput; import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor; @@ -55,137 +38,10 @@ public class CEditor extends AbstractDecoratedTextEditor { /** * The handler for doubleclick events on the ruler for this text editor. */ - private RulerDoubleClickHandler handler = new RulerDoubleClickHandler(); + private RulerDoubleClickHandler handler; public static final String ID = "org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.c.CEditor"; //$NON-NLS-1$ - /** - * The <code>RulerDoubleClickHandler</code> handles double click events on the - * ruler for this text editor. It first checks to see if the user clicked on a - * comment line, then if they clicked on a line that SystemTap can use as a probe - * point, and if the line of code passes both checks, it dispatches an event to the - * active STPEditor to insert a code block describing the line of code that the user - * clicked on. - * - * The block of code is sent to the STPEditor only under the following circumstances: - * <ul> - * <li>The line of code is not blank</li> - * <li>If the line of code contains a single-line comment, it must not be the only text on that line</li> - * <li>The line of code must not fall within a multiline comment</li> - * <li>The line of code must be a line that can be used by SystemTap, - * determined by running the following:<br/> - * <code>stap -p2 -e 'probe kernel.statement("*@filename:linenumber")'</code><br/> - * If <code>stap</code> does not generate errors while running the test command, the - * line is assumed valid.</li> - * </ul> - * - * If all of the above are met, the active STPEditor listed in <code>IDESessionSettings</code> - * is told to insert a template probe for this line of code using the <code>SimpleEditor.insertText</code> method. - * If no the returned STPEditor reference is null, the code opens a new editor. - * - * @author Henry Hughes - * @author Ryan Morse - * @see org.eclipse.linuxtools.systemtap.ui.editor.SimpleEditor#insertText - * @see org.eclipse.linuxtools.systemtap.ui.structures.runnable.Command - * @see org.eclipse.swt.events.MouseListener - */ - private class RulerDoubleClickHandler extends MouseAdapter - { - /** - * The doubleclick event handler method. - * @param e The <code>MouseEvent</code> that represents this doubleclick event. - */ - @Override - public void mouseDoubleClick(MouseEvent e) - { - getSite().getShell().setCursor(new Cursor(getSite().getShell().getDisplay(), SWT.CURSOR_WAIT)); - int lineno = getVerticalRuler().getLineOfLastMouseButtonActivity(); - - String s = getSourceViewer().getDocument().get(); - String[] lines = s.split("\n"); //$NON-NLS-1$ - String line = lines[lineno].trim(); - boolean die = false; - if("".equals(line)) //eat blank lines //$NON-NLS-1$ - die = true; - if(line.startsWith("#")) //eat preprocessor directives //$NON-NLS-1$ - die = true; - if(line.startsWith("//")) //eat C99 comments //$NON-NLS-1$ - die = true; - if(line.startsWith("/*") && !line.contains("*/") && !line.endsWith("*/")) //try to eat single-line C comments //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - die = true; - - //gogo find comment segments - try - { - ArrayList<Integer> commentChunks = new ArrayList<Integer>(); - char[] chars = s.toCharArray(); - int needle = 1; - int offset = getSourceViewer().getDocument().getLineOffset(lineno); - while (needle < chars.length) - { - if(chars[needle-1] == '/' && chars[needle] == '*') - { - commentChunks.add(needle); - while(needle < chars.length) - { - if(chars[needle-1] == '*' && chars[needle] == '/') - { - commentChunks.add(needle); - needle++; - break; - } - needle++; - } - } - needle++; - } - for(int i=0, pair, start, end; i < commentChunks.size(); i++) - { - if(!(commentChunks.get(i).intValue() < offset)) - { - pair = i - i%2; - start = commentChunks.get(pair).intValue(); - end = commentChunks.get(pair+1).intValue(); - if(offset >= start && offset <= end) - die=true; - } - } - } catch (BadLocationException excp) { - LogManager.logCritical("Exception mouseDoubleClick: " + excp.getMessage(), this); //$NON-NLS-1$ - } - if(die) { - MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), - Localization.getString("CEditor.ProbeInsertFailed"),Localization.getString("CEditor.CanNotProbeLine")); //$NON-NLS-1$ //$NON-NLS-2$ - } else { - IEditorInput in = getEditorInput(); - if(in instanceof FileStoreEditorInput) { - FileStoreEditorInput input = (FileStoreEditorInput)in; - - IPreferenceStore p = IDEPlugin.getDefault().getPreferenceStore(); - String kernroot = p.getString(IDEPreferenceConstants.P_KERNEL_SOURCE); - - String filepath = input.getURI().getPath(); - String kernrelative = filepath.substring(kernroot.length()+1, filepath.length()); - StringBuffer sb = new StringBuffer(); - - sb.append("probe kernel.statement(\"*@"+ kernrelative + ":" + (lineno+1) + "\")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - - sb.append("\n{\n\t\n}\n"); //$NON-NLS-1$ - STPEditor activeSTPEditor = IDESessionSettings.getActiveSTPEditor(); - if(null == activeSTPEditor) { - NewFileAction action = new NewFileAction(); - action.run(); - IEditorPart ed = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); - if(ed instanceof STPEditor) - IDESessionSettings.setActiveSTPEditor((STPEditor)ed); - } - if(null != activeSTPEditor) - activeSTPEditor.insertText(sb.toString()); - } - } - getSite().getShell().setCursor(null); //Return the cursor to normal - } - } /** * Default Constructor for the <code>CEditor</code> class. Creates an instance of the editor which @@ -193,6 +49,7 @@ public class CEditor extends AbstractDecoratedTextEditor { */ public CEditor() { super(); + handler = new RulerDoubleClickHandler(this); internal_init(); IDEPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(cColorPropertyChangeListener); } |