Building a fully functional code editor based on the JFace Text framework: Part 3rd

Source: Internet
Author: User

Double Click and Triple click

Double click (double click) and Triple click (Triple click) are accessibility features that make it easy for users to select text (which, of course, does not necessarily have to be selected). This article describes how to customize the double-click behavior in JTF and how to add three-click Support for JTF.

Double Click

In Eclipse's Java editor, when the user double-clicks the editing area with the mouse, you can see that the word that is double-clicked is selected, which is the JTF double click (double click) feature. The actions that are triggered when you double-click are customizable, and you do not have to select a piece of text.

The interface associated with double-clicking is Itextdoubleclickstrategy, it has only one method called doubleclicked, as long as the interface is implemented, in the example, I added the Exprdoubleclickstrategy class:

Listing 1. Exprdoubleclickstrategy implements the Itextdoubleclickstrategy interface

public class ExprDoubleClickStrategy implements ITextDoubleClickStrategy {
public void doubleClicked(ITextViewer viewer) {
  // get doc
  IDocument doc = viewer.getDocument();

  // get token list
  TokenList tokenList = TokenManager.getTokenList(doc);

  // get double click position 
  int offset = viewer.getSelectedRange().x;

  // get token in that offset
  CommonToken token = tokenList.getToken(offset);

  // select whole token if token is not null
  if(token != null && token.getType() != Token.EOF)
  {
   // select double clicked token 
  viewer.setSelectedRange(
  token.getStartIndex(), token.getStopIndex() - token.getStartIndex() + 1);
  }
  }
}

This process is very straightforward, gets the location that is clicked, gets the corresponding symbol through the position, and then selects the entire symbol. We again took advantage of the symbol Tokenlist came to the specified character offset.

As with the second part of the series, with the implementation you have to let JTF know your implementation, we then modify the exprconfiguation, overwriting a Getdoubleclickstrategy method:

Listing 2. Let JTF know your Double Click implementation

public ITextDoubleClickStrategy getDoubleClickStrategy(
      ISourceViewer sourceViewer, String contentType)
{
   return new ExprDoubleClickStrategy();
}

Simply return to our implementation of the Itextdoubleclickstrategy only, so that JTF know our double-click Behavior, and note that the double-click Behavior is also bound to the text type, but we have only one type, so did not use this information.

The reader can try the example in this article, double-click a variable to see if the variable is all selected.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.