2016-11-22 120 views
0

我正在開發一個使用eclipse插件開發環境(PDE)推薦代碼的插件。現在我正在設計界面。事情是我想要在Eclipse編輯器中獲取光標位置並在該位置打開一個JFrame。我試圖在文檔和論壇的幫助下獲得位置,並且只能獲得直到現在的偏移量,或者可以說行和列偏移量。我想知道它代表一個位置的點(x,y)。那麼任何想法如何獲得光標位置?Eclipse文本編輯器獲取光標位置

+0

那你試試? –

+0

你有什麼對象可以訪問?只是編輯?觀衆?這是主要的文本小部件? – nitind

+0

我試圖從鏈接中的答案下面的代碼,但它只是讓我抵消。 http://stackoverflow.com/questions/1619623/eclipse-plugin-how-to-get-current-text-editor-corsor-position –

回答

1

假設你有StyledText控制在編輯器中使用getCaretOffset來獲取插入符偏移:

StyledText text = ... get editor styled text 

int caret = text.getCaretOffset(); 

然後調用getLocationAtOffset得到的x,偏移量相對於控制y座標:

Point point = text.getLocationAtOffset(caret); 

如有必要,您可以將其轉換爲相對於顯示器:

point = text.toDisplay(point); 

請注意,Eclipse插件通常使用SWT,而不是Swing。打開一個JFrame比SWT控件要困難得多。

可以使用得到StyledTextITextEditor

StyledText text = (StyledText)editor.getAdapter(Control.class); 
+0

我想從eclipse的編輯器中獲取插入點的位置,就像你上面做的那樣,而不是從一個風格的文字。 –

+0

Eclipse文本編輯器使用StyledText作爲文本 –

+0

IEditorPart editor = PlatformUI.getWorkbench()。getActiveWorkbenchWindow()。getActivePage()。getActiveEditor(); \t \t如果(編輯的instanceof ITextEditor){ \t \t ISelectionProvider selectionProvider =((ITextEditor)編輯).getSelectionProvider(); \t \t ISelection selection = selectionProvider.getSelection();如果(ITextSelection的選擇實例){ \t \t \t ITextSelection textSelection; =(ITextSelection)selection; \t \t offset = textSelection.getOffset(); \t \t} \t \t} –