2010-03-07 13 views
6

मैं एक ग्रहण प्लग-इन विकसित कर रहा हूं जहां बटन दबाकर प्लग-इन जावा संपादक में चयनित टेक्स्ट लेता है और दिखाई देने वाले टेक्स्ट बॉक्स में डालता है।एक्लिप्स जावा संपादक से चयनित टेक्स्ट प्राप्त करें

मेरे कोड इस तरह दिखता है: मैं इसे यहाँ से मिला: http://dev.eclipse.org/newslists/news.eclipse.newcomer/msg02200.html

private ITextSelection getSelection(ITextEditor editor) { 
    ISelection selection = editor.getSelectionProvider() 
      .getSelection(); 
    return (ITextSelection) selection; 
} 

private String getSelectedText(ITextEditor editor) { 
    return getSelection(editor).getText(); 
} 

समस्या मैं जावा संपादक की ITextEditor प्रदर्शित किया जा रहा है कि कैसे मिल जाएगा है। संयोग से यह लिंक मैं पोस्ट में धागा में अगले सवाल है, लेकिन यह :(अनुत्तरित

+0

जब आप कहते हैं, "चयनित पाठ," आप वास्तव में प्रकाश डाला मतलब क्या है, या केवल वर्तमान कीवर्ड कि कर्सर पर है? – StockB

उत्तर

7

आप ActiveEditor के लिए पूछ सकते this thread में के रूप में बताया गया है:

IEditorPart part; 

part = 
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().get 
ActiveEditor(); 

if(part instanceof ITextEditor){ 
    ITextEditor editor = (ITextEditor)part; 
    IDocumentProvider provider = editor.getDocumentProvider(); 
    IDocument document = provider.getDocument(editor.getEditorInput()); 

ओपी Krt_Malta इस blog entry "Programmatically query current text selection" का उल्लेख है, जो इस अन्य SO उत्तर के समान है ( ब्लॉग प्रविष्टि से पहले) "Replace selected code from eclipse editor through plugin command"

+0

एसओ सवाल भी देखें http://stackoverflow.com/questions/1694748/adding-item-to-eclipse-text-viewer-context-menu एक समान समस्या के साथ – VonC

+0

धन्यवाद :) प्रश्न सबमिट करने के बाद मैंने फैसला करने का फैसला किया जिस तरीके से मैंने पहले पोस्ट किया था, यह देखने के लिए कि क्या कोई मैचों में आया है। हिट्स में से एक था http://usayadis.wordpress.com/2009/10/20/programmatically-query-current-text-selection/ जो मैंने वही किया था जो मैंने खोजा था। धन्यवाद और सम्मान, Krt_Malta –

1

मैं ओ जोड़ना चाहता हूं VonCs उत्तर के लिए बात है। चयन प्राप्त करने के लिए वह जिस तकनीक का वर्णन करता है वह सभी प्रकार के टेक्स्ट संपादकों के लिए उपयोगी है, न केवल जावा संपादकों के रूप में, बल्कि इन प्रश्नों के बारे में है। लेकिन उनका समाधान इस मामले में काम नहीं करता है कि वर्कस्पेस भाग MultiPageEditorPart है, क्योंकि यह ITextEditor नहीं है।

लेकिन कई मामलों में (उदाहरण के लिए मानक एक्सएमएल संपादक के साथ) MultiPageEditorPart में ऐसे पृष्ठ हैं जो ITextEditor एस हैं। उन मामलों में आप MultiPageEditorPart से सक्रिय पृष्ठ प्राप्त कर सकते हैं और उस से चयन प्राप्त कर सकते हैं।

यह निम्न कोड के साथ किया जा सकता है:

ITextEditor editor = null; 

if (part instanceof ITextEditor) { 
    editor = (ITextEditor) part; 
} else if (part instanceof MultiPageEditorPart) { 
    Object page = ((MultiPageEditorPart) part).getSelectedPage(); 
    if (page instanceof ITextEditor) editor = (ITextEditor) page; 
} 

if (editor != null) { 
    IDocumentProvider provider = editor.getDocumentProvider(); 
    IDocument document = provider.getDocument(editor.getEditorInput()); 
} 
+0

अच्छा बिंदु, मेरे (पुराने) उत्तर के अतिरिक्त। +1 – VonC

संबंधित मुद्दे