在run方法中,如何获得选中文本的颜色?
- Java code
package cat.popup.actions; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IActionDelegate; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; public class NewAction implements IObjectActionDelegate { private Shell shell; private IWorkbenchPart targetPart; /** * Constructor for Action1. */ public NewAction() { super(); } /** * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) */ public void setActivePart(IAction action, IWorkbenchPart targetPart) { shell = targetPart.getSite().getShell(); this.targetPart = targetPart; } /** * @see IActionDelegate#run(IAction) */ public void run(IAction action) { String title = targetPart.getTitle(); ISelection selection = targetPart.getSite().getSelectionProvider() .getSelection(); if (!(selection instanceof ITextSelection)) { return; } ITextSelection textSelection = (ITextSelection) selection; String text = textSelection.getText(); if (text == null || text.length() == 0) { return; } int endLine = textSelection.getEndLine(); int startLine = textSelection.getStartLine(); int offset = textSelection.getOffset(); System.out.println(text + "(title"+title+":" + startLine + ":" + endLine + ":" + offset + ")"); //这里怎么获得选中文本的颜色?如果选中了多个段,如何循环得到每段的颜色? MessageDialog.openInformation(shell, "Cat", "New Action was executed."); } /** * @see IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) { } }
配置文件:
- XML code
<plugin> <extension point="org.eclipse.ui.popupMenus"> <objectContribution objectClass="org.eclipse.core.resources.IResource" adaptable="true" id="cat.contribution1"> <action label="test" class="cat.popup.actions.NewAction" menubarPath="additions" enablesFor="+" id="cat.newAction"> </action> </objectContribution> </extension> </plugin>
------解决方案--------------------
代码没有哦,SourceViewer中应该可以根据光标start和offset的信息,获取当前选中的那部分的文字的颜色,字体什么的