String location within RSTA

Questions on using RSyntaxTextArea should go here.

Moderator: robert

String location within RSTA

Postby sandeep » Mon May 03, 2010 4:31 pm

How can we get location/point of any text(say small string from index 2 - 5) in RSTA...
sandeep
 

Re: String location within RSTA

Postby robert » Mon May 03, 2010 10:18 pm

I'm not sure what you're asking. Are you asking for search/replace functionality? If so then check out the SearchEngine class, particularly the find() and replace() methods.
User avatar
robert
 
Posts: 639
Joined: Sat May 10, 2008 5:16 pm

Re: String location within RSTA

Postby sandeep » Tue May 04, 2010 6:42 am

No i was not asking about search replace functionality i have already incorporated that....... Suppose i have underline(squiggle) a
wrong word, now i want point/location of that particular word so that when mouse hovers over that word i can display some tooltip.
sandeep
 

Re: String location within RSTA

Postby robert » Tue May 04, 2010 12:34 pm

How did you add your squiggle underline?

Did you create your own Parser using the org.fife.ui.rsyntaxtextarea.parser package? If you did then this task would be easy. You would create a Parser that returned a ParseResult containing 1 or more ParserNotices (which would be what get squiggle-underlined). A ParseResult has a "getToolTipText()" method that will get automatically called for the text area when a tool tip is requested, so you don't have to manually set the tool tip to display.

For an example of a Parser implementation written by someone other than myself, check out the pg3b project's source code for its GUI front-end, particularly the ScriptEditor class. It creates a Parser to parse the contents of an RSTA with the Pnuts compiler. Note that it's been a couple of months since the author's fiddled with that code, and it may be a tad out of date, but it's a good implementation and excellent example of using this API. Alternatively, you can check out the SpellingParser class I wrote for the SpellChecker add-on library to RSTA. In particular, see its parse() method (called whenver the editor is modified, creates the ParseResult mentioned above) and the spellingError() method (callback called by the spell checking library, and called indirectly from parse(), it is what actually creates the concrete ParserNotices to return to the user).

There should probably be better documentation on creating custom Parsers. :) Those two are probably the best ones because of their simplicity. Actually, another very simple Parser is the TaskTagParser, included with RSTA, which searches for things like "TODO:" and "FIXME:" in comments of source code. That one's probably the easiest to undestand because it's so short.

But, assuming you didn't do that...

Did you simply created your own SquiggleUnderlineHighlightPainter, and called textArea.getHighlighter().addHighlight(p0, p1, painter)? If so, then I think you'll need to do a little legwork. You'll have to create your own org.fife.ui.rtextarea.ToolTipSupplier. This may not be quite right, but it should put you on the right path:

Code: Select all
ToolTipSupplier supplier = new ToolTipSupplier() {

   private Highlighter.Highlight getHighlightAt(Highlighter.Highlight[] highlights, int offs) {
      for (int i=0; i<highlights.length; i++) {
         if (highlights[i].getStartOffset()<=offs && highlights[i].getEndOffset()>=offs) {
            return highlights[i];
         }
      }
      return null;
   }

   public String getToolTipText(RTextArea textArea, java.awt.event.MouseEvent e) {
      int offs = textArea.viewToModel(e.getPoint());
      if (offs==-1) {
         return null;
      }
      Highlighter h = textArea.getHighlighter();
      Highlighter.Highlight[] highlights = h.getHighlights();
      Highlighter.Highlight highlight = getHighlightAt(offs);
      if (highlight!=null) {
         try {
            int start = highlight.getStartOffset();
            int len = highlight.getEndOffset() - start;
            String word = textArea.getText(start, len);
            String tooltip = // Now that you have the word underlined, generate whatever tool tip you want
            return tooltip;
         } catch (BadLocationException ble) {
            ble.printStackTrace();
         }
      }
      return null;
   }

}


Then, you attach this supplier to your RTextArea. This will intercept calls to getToolTipText(), allowing your custom tool tip to take the place of the default one (usually null).

Code: Select all
textArea.setToolTipSupplier(supplier);
ToolTipManager.sharedInstance().registerComponent(textArea);


I hope this was helpful. Using the Parser API, while seemingly more complex, is actually probably a cleaner route to go as it allows you to focus on parsing the text in the document and identifying things to be underlined, rather than writing "low-level" code like having to iterate through highlighted text just to find what text the mouse is over.
User avatar
robert
 
Posts: 639
Joined: Sat May 10, 2008 5:16 pm

Re: String location within RSTA

Postby sandeep » Tue May 04, 2010 2:15 pm

Ya its working fine now, i am able to display tooltip on underlined words, thanx a lot..........
sandeep
 


Return to Help

Who is online

Users browsing this forum: No registered users and 0 guests

cron