0 0

【Eclipse-plugin】在Eclipse的编辑器上,右键插件Action中如何获得选中文本的颜色?30

<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>

 以上是plugin.xml

 

以下是Action文件:

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) {
	}

}

 在run方法中,如何获得选中文本的颜色?

目前还没有答案

相关推荐

Global site tag (gtag.js) - Google Analytics