eclipse插件开发学习笔记配套代码
时间: 2023-09-09 08:02:45 浏览: 220
eclipse插件开发学习笔记配套源码
5星 · 资源好评率100%
首先,感谢您对eclipse插件开发学习的关注!下面是一些学习笔记配套代码的例子。
1. 创建一个最简单的插件:
```java
public class HelloWorldPlugin implements IStartup {
@Override
public void earlyStartup() {
System.out.println("Hello, Eclipse Plugin Development!");
}
}
```
2. 在Eclipse菜单中添加一个新的命令:
```java
public class MyCommandHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), "My Command", "Hello, Eclipse Plugin Development!");
return null;
}
}
```
3. 在编辑器右键菜单中添加一个新的动作:
```java
public class MyEditorAction extends Action {
public MyEditorAction() {
setText("My Action");
}
@Override
public void run() {
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editor instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) editor;
IDocumentProvider documentProvider = textEditor.getDocumentProvider();
IDocument document = documentProvider.getDocument(textEditor.getEditorInput());
selectCurrentWord(textEditor, document);
}
}
private void selectCurrentWord(ITextEditor editor, IDocument document) {
ISelectionProvider selectionProvider = editor.getSelectionProvider();
ITextSelection selection = (ITextSelection) selectionProvider.getSelection();
int offset = selection.getOffset();
try {
int lineStartOffset = document.getLineOffset(selection.getStartLine());
int lineEndOffset = lineStartOffset + document.getLineLength(selection.getStartLine());
String lineText = document.get(lineStartOffset, lineEndOffset - lineStartOffset);
int wordStartOffset = lineText.lastIndexOf(' ', offset - lineStartOffset) + 1;
int wordEndOffset = lineText.indexOf(' ', offset - lineStartOffset);
if (wordEndOffset < wordStartOffset) { // The word is the last or only word on the line
wordEndOffset = lineEndOffset - lineStartOffset;
} else {
wordEndOffset += lineStartOffset;
}
selectionProvider.setSelection(new TextSelection(wordStartOffset, wordEndOffset - wordStartOffset));
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
```
此处仅提供了一些简单的示例代码,以帮助您开始使用eclipse插件开发。有关更详细的学习和开发指导,请参阅Eclipse官方文档和教程。祝您在插件开发的学习过程中取得成功!
阅读全文