|
From: Christopher W. <caw...@us...> - 2006-03-28 23:15:50
|
Update of /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18162/src/org/rubypeople/rdt/internal/ui/actions Modified Files: SelectionConverter.java ActionMessages.java Added Files: ActionUtil.java OpenActionUtil.java Log Message: start laying groundwork for the F3/Open action where users can easily navigate from text selections and menus with IRubyElements to the source. --- NEW FILE: ActionUtil.java --- package org.rubypeople.rdt.internal.ui.actions; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectNature; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.widgets.Shell; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.IRubyProject; import org.rubypeople.rdt.core.RubyCore; import org.rubypeople.rdt.internal.ui.rubyeditor.RubyEditor; public class ActionUtil { private ActionUtil() {} public static boolean isProcessable(Shell shell, RubyEditor editor) { if (editor == null) return true; IRubyElement input= SelectionConverter.getInput(editor); // if a Ruby editor doesn't have an input of type Ruby element // then it is for sure not on the build path if (input == null) { MessageDialog.openInformation(shell, ActionMessages.ActionUtil_notOnBuildPath_title, ActionMessages.ActionUtil_notOnBuildPath_message); return false; } return isProcessable(shell, input); } public static boolean isProcessable(Shell shell, Object element) { if (!(element instanceof IRubyElement)) return true; if (isOnBuildPath((IRubyElement)element)) return true; MessageDialog.openInformation(shell, ActionMessages.ActionUtil_notOnBuildPath_title, ActionMessages.ActionUtil_notOnBuildPath_message); return false; } public static boolean isOnBuildPath(IRubyElement element) { //fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051 if (element.getElementType() == IRubyElement.RUBY_PROJECT) return true; IRubyProject project= element.getRubyProject(); try { // TODO When we handle loadpaths correctly, uncomment // if (!project.isOnLoadpath(element)) // return false; IProject resourceProject= project.getProject(); if (resourceProject == null) return false; IProjectNature nature= resourceProject.getNature(RubyCore.NATURE_ID); // We have a Ruby project if (nature != null) return true; } catch (CoreException e) { } return false; } } --- NEW FILE: OpenActionUtil.java --- package org.rubypeople.rdt.internal.ui.actions; import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PartInitException; import org.eclipse.ui.dialogs.ElementListSelectionDialog; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.RubyModelException; import org.rubypeople.rdt.internal.ui.rubyeditor.EditorUtility; import org.rubypeople.rdt.ui.RubyElementLabelProvider; public class OpenActionUtil { /** * Opens the editor on the given element and subsequently selects it. */ public static void open(Object element, boolean activate) throws RubyModelException, PartInitException { IEditorPart part= EditorUtility.openInEditor(element, activate); if (element instanceof IRubyElement) EditorUtility.revealInEditor(part, (IRubyElement)element); } /** * Shows a dialog for resolving an ambiguous ruby element. * Utility method that can be called by subclasses. */ public static IRubyElement selectRubyElement(IRubyElement[] elements, Shell shell, String title, String message) { int nResults= elements.length; if (nResults == 0) return null; if (nResults == 1) return elements[0]; int flags= RubyElementLabelProvider.SHOW_DEFAULT | RubyElementLabelProvider.SHOW_QUALIFIED | RubyElementLabelProvider.SHOW_ROOT; ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new RubyElementLabelProvider(flags)); dialog.setTitle(title); dialog.setMessage(message); dialog.setElements(elements); if (dialog.open() == Window.OK) { Object[] selection= dialog.getResult(); if (selection != null && selection.length > 0) { nResults= selection.length; for (int i= 0; i < nResults; i++) { Object current= selection[i]; if (current instanceof IRubyElement) return (IRubyElement) current; } } } return null; } } Index: ActionMessages.java =================================================================== RCS file: /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/actions/ActionMessages.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ActionMessages.java 10 Feb 2006 19:58:29 -0000 1.1 --- ActionMessages.java 28 Mar 2006 23:15:47 -0000 1.2 *************** *** 36,39 **** --- 36,51 ---- public static String MemberFilterActionGroup_hide_localtypes_tooltip; public static String MemberFilterActionGroup_hide_localtypes_description; + public static String OpenAction_label; + public static String OpenAction_tooltip; + public static String OpenAction_description; + public static String OpenAction_declaration_label; + public static String OpenAction_select_element; + public static String OpenAction_error_messageBadSelection; + public static String OpenAction_error_message; + public static String OpenAction_error_messageProblems; + public static String OpenAction_error_messageArgs; + public static String OpenAction_error_title; + public static String ActionUtil_notOnBuildPath_title; + public static String ActionUtil_notOnBuildPath_message; static { Index: SelectionConverter.java =================================================================== RCS file: /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/actions/SelectionConverter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SelectionConverter.java 10 Feb 2006 19:58:29 -0000 1.1 --- SelectionConverter.java 28 Mar 2006 23:15:47 -0000 1.2 *************** *** 1,7 **** --- 1,12 ---- package org.rubypeople.rdt.internal.ui.actions; + import org.eclipse.jface.text.ITextSelection; + import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorInput; + import org.rubypeople.rdt.core.ICodeAssist; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.IRubyScript; + import org.rubypeople.rdt.core.RubyModelException; + import org.rubypeople.rdt.internal.corext.util.RubyModelUtil; import org.rubypeople.rdt.internal.ui.RubyPlugin; import org.rubypeople.rdt.internal.ui.rubyeditor.RubyEditor; *************** *** 26,28 **** --- 31,73 ---- } + public static boolean canOperateOn(RubyEditor editor) { + if (editor == null) + return false; + return getInput(editor) != null; + } + + private static final IRubyElement[] EMPTY_RESULT= new IRubyElement[0]; + + /** + * Converts the text selection provided by the given editor a Ruby element by + * asking the user if code reolve returned more than one result. If the selection + * doesn't cover a Ruby element <code>null</code> is returned. + */ + public static IRubyElement codeResolve(RubyEditor editor, Shell shell, String title, String message) throws RubyModelException { + IRubyElement[] elements= codeResolve(editor); + if (elements == null || elements.length == 0) + return null; + IRubyElement candidate= elements[0]; + if (elements.length > 1) { + candidate= OpenActionUtil.selectRubyElement(elements, shell, title, message); + } + return candidate; + } + + public static IRubyElement[] codeResolve(RubyEditor editor) throws RubyModelException { + return codeResolve(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection()); + } + + public static IRubyElement[] codeResolve(IRubyElement input, ITextSelection selection) throws RubyModelException { + if (input instanceof ICodeAssist) { + if (input instanceof IRubyScript) { + RubyModelUtil.reconcile((IRubyScript) input); + } + IRubyElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength()); + if (elements != null && elements.length > 0) + return elements; + } + return EMPTY_RESULT; + } + } |