|
From: Christopher W. <caw...@us...> - 2006-02-10 19:56:04
|
Update of /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11825/src/org/rubypeople/rdt/ui/actions Modified Files: RubyActionGroup.java Added Files: SurroundWithBeginRescueAction.java SelectionDispatchAction.java Log Message: new action to surround selected text with a begin...rescue block --- NEW FILE: SurroundWithBeginRescueAction.java --- package org.rubypeople.rdt.ui.actions; import java.util.Map; import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.ITextFileBufferManager; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.PlatformUI; import org.rubypeople.rdt.core.IRubyScript; import org.rubypeople.rdt.core.RubyModelException; import org.rubypeople.rdt.core.formatter.Indents; import org.rubypeople.rdt.internal.corext.util.RubyModelUtil; import org.rubypeople.rdt.internal.ui.IRubyHelpContextIds; import org.rubypeople.rdt.internal.ui.RubyPlugin; import org.rubypeople.rdt.internal.ui.actions.SelectionConverter; import org.rubypeople.rdt.internal.ui.rubyeditor.RubyEditor; import org.rubypeople.rdt.internal.ui.util.ExceptionHandler; public class SurroundWithBeginRescueAction extends SelectionDispatchAction { private RubyEditor fEditor; /** * Note: This constructor is for internal use only. Clients should not call * this constructor. * * @param editor * the compilation unit editor */ public SurroundWithBeginRescueAction(RubyEditor editor) { super(editor.getEditorSite()); setText("Surround with begin...rescue"); fEditor = editor; setEnabled((fEditor != null && SelectionConverter.getInputAsRubyScript(fEditor) != null)); PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IRubyHelpContextIds.SURROUND_WITH_TRY_CATCH_ACTION); } public void run(ITextSelection selection) { try { createChange(selection, new NullProgressMonitor()); } catch (CoreException e) { // FIXME Localize! Extract into ResourceBundle ExceptionHandler.handle(e, getDialogTitle(), "Error occurred while surrounding code with begin..rescue block"); } } private static String getDialogTitle() { // FIXME Localize! Extract into ResourceBundle return "Surround with begin...rescue"; } private IFile getFile() { IRubyScript cu = getRubyScript(); return (IFile) RubyModelUtil.toOriginal(cu).getResource(); } private IRubyScript getRubyScript() { return SelectionConverter.getInputAsRubyScript(fEditor); } /* * non Java-doc * * @see IRefactoring#createChange(IProgressMonitor) */ public void createChange(ITextSelection selection, IProgressMonitor pm) throws CoreException { final String NN = ""; //$NON-NLS-1$ if (pm == null) pm = new NullProgressMonitor(); pm.beginTask(NN, 2); // This is cheap since the compilation unit is already open in a editor. IPath path = getFile().getFullPath(); ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); try { bufferManager.connect(path, new SubProgressMonitor(pm, 1)); IDocument document = bufferManager.getTextFileBuffer(path).getDocument(); String text = createBeginRescueBlock(document, selection); document.replace(selection.getOffset(), selection.getLength(), text); } catch (BadLocationException e) { throw new CoreException(new Status(IStatus.ERROR, RubyPlugin.getPluginId(), IStatus.ERROR, e.getMessage(), e)); } finally { bufferManager.disconnect(path, new SubProgressMonitor(pm, 1)); pm.done(); } } private String createBeginRescueBlock(IDocument document, ITextSelection selection) throws BadLocationException, RubyModelException { String originalText = selection.getText(); String lineDelimiter = document.getLineDelimiter(0); Map options = getRubyScript().getRubyProject().getOptions(true); int lineNumber = selection.getStartLine(); String line = document.get(document.getLineOffset(lineNumber), document .getLineLength(lineNumber)); int indentationUnits = Indents.measureIndentUnits(line, Indents.getTabWidth(options), Indents.getIndentWidth(options)); StringBuffer text = new StringBuffer(); text.append("begin"); text.append(lineDelimiter); text.append(Indents.createIndentString(indentationUnits + 1, options)); text.append(originalText); text.append(lineDelimiter); text.append(Indents.createIndentString(indentationUnits, options)); text.append("rescue StandardError => e"); text.append(lineDelimiter); text.append(Indents.createIndentString(indentationUnits + 1, options)); text.append("puts e"); text.append(lineDelimiter); text.append(Indents.createIndentString(indentationUnits, options)); text.append("end"); text.append(lineDelimiter); text.append(Indents.createIndentString(indentationUnits, options)); return text.toString(); } public void selectionChanged(ITextSelection selection) { setEnabled(selection.getLength() > 0 && (fEditor != null && SelectionConverter.getInputAsRubyScript(fEditor) != null)); } } --- NEW FILE: SelectionDispatchAction.java --- /******************************************************************************* * Copyright (c) 2000, 2005 IBM Corporation and others. * 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 - initial API and implementation *******************************************************************************/ package org.rubypeople.rdt.ui.actions; import org.eclipse.jface.action.Action; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.util.Assert; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbenchSite; /** * Action that dispatches the <code>IAction#run()</code> and the * <code>ISelectionChangedListener#selectionChanged</code> * according to the type of the selection. * * <ul> * <li>if selection is of type <code>ITextSelection</code> then * <code>run(ITextSelection)</code> and <code>selectionChanged(ITextSelection)</code> * is called.</li> * <li>if selection is of type <code>IStructuredSelection</code> then * <code>run(IStructuredSelection)</code> and <code> * selectionChanged(IStructuredSelection)</code> is called.</li> * <li>default is to call <code>run(ISelection)</code> and <code> * selectionChanged(ISelection)</code>.</li> * </ul> * * <p> * Note: This class is not intended to be subclassed outside the JDT UI plug-in. * </p> * * @since 2.0 */ public abstract class SelectionDispatchAction extends Action implements ISelectionChangedListener { private IWorkbenchSite fSite; private ISelectionProvider fExtraSelectionProvider; /** * Creates a new action with no text and no image. * <p> * Configure the action later using the set methods. * </p> * * @param site the site this action is working on */ protected SelectionDispatchAction(IWorkbenchSite site) { Assert.isNotNull(site); fSite= site; fExtraSelectionProvider= null; } /** * Creates a new action with no text and no image * * <p> * Configure the action later using the set methods. * </p> * * @param site the site this action is working on * @param provider a special selection provider which is used * instead of the site's selection provider. * * @since 3.2 */ protected SelectionDispatchAction(IWorkbenchSite site, ISelectionProvider provider) { Assert.isNotNull(site); Assert.isNotNull(provider); fSite= site; fExtraSelectionProvider= provider; } /** * Returns the site owning this action. * * @return the site owning this action */ public IWorkbenchSite getSite() { return fSite; } /** * Returns the selection provided by the site owning this action. * * @return the site's selection */ public ISelection getSelection() { if (getSelectionProvider() != null) return getSelectionProvider().getSelection(); else return null; } /** * Returns the shell provided by the site owning this action. * * @return the site's shell */ public Shell getShell() { return fSite.getShell(); } /** * Returns the selection provider managed by the site owning this action or the selection provider explicitly set in * {@link #SelectionDispatchAction(IWorkbenchSite, ISelectionProvider)}. * * @return the site's selection provider */ public ISelectionProvider getSelectionProvider() { if (fExtraSelectionProvider != null) { return fExtraSelectionProvider; } return fSite.getSelectionProvider(); } /** * Updates the action's enablement state according to the given selection. This * default implementation calls one of the <code>selectionChanged</code> * methods depending on the type of the passed selection. * * @param selection the selection this action is working on */ public void update(ISelection selection) { dispatchSelectionChanged(selection); } /** * Notifies this action that the given structured selection has changed. This default * implementation calls <code>selectionChanged(ISelection selection)</code>. * * @param selection the new selection */ public void selectionChanged(IStructuredSelection selection) { selectionChanged((ISelection)selection); } /** * Executes this actions with the given structured selection. This default implementation * calls <code>run(ISelection selection)</code>. * * @param selection the selection */ public void run(IStructuredSelection selection) { run((ISelection)selection); } /** * Notifies this action that the given text selection has changed. This default * implementation calls <code>selectionChanged(ISelection selection)</code>. * * @param selection the new selection */ public void selectionChanged(ITextSelection selection) { selectionChanged((ISelection)selection); } /** * Executes this actions with the given text selection. This default implementation * calls <code>run(ISelection selection)</code>. * * @param selection the selection */ public void run(ITextSelection selection) { run((ISelection)selection); } /** * Notifies this action that the given selection has changed. This default * implementation sets the action's enablement state to <code>false</code>. * * @param selection the new selection */ public void selectionChanged(ISelection selection) { setEnabled(false); } /** * Executes this actions with the given selection. This default implementation * does nothing. * * @param selection the selection */ public void run(ISelection selection) { } /* (non-Javadoc) * Method declared on IAction. */ public void run() { dispatchRun(getSelection()); } /* (non-Javadoc) * Method declared on ISelectionChangedListener. */ public void selectionChanged(SelectionChangedEvent event) { dispatchSelectionChanged(event.getSelection()); } private void dispatchSelectionChanged(ISelection selection) { if (selection instanceof IStructuredSelection) { selectionChanged((IStructuredSelection)selection); } else if (selection instanceof ITextSelection) { selectionChanged((ITextSelection)selection); } else { selectionChanged(selection); } } private void dispatchRun(ISelection selection) { if (selection instanceof IStructuredSelection) { run((IStructuredSelection)selection); } else if (selection instanceof ITextSelection) { run((ITextSelection)selection); } else { run(selection); } } } Index: RubyActionGroup.java =================================================================== RCS file: /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/RubyActionGroup.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RubyActionGroup.java 12 Mar 2005 16:51:24 -0000 1.3 --- RubyActionGroup.java 10 Feb 2006 19:55:56 -0000 1.4 *************** *** 18,21 **** --- 18,22 ---- menu.add(editor.getAction("ToggleComment")); + menu.add(editor.getAction("SurroundWithBeginRescue")); menu.add(editor.getAction("Comment")); menu.add(editor.getAction("Uncomment")); |