Java Tips Weblog

  • Blog Stats

    • 2,614,706 hits
  • Categories

  • Archives

Protected Text Component

Posted by Rob Camick on December 21, 2008

Text components allow you to freely change any of the text displayed in the component. There may be times when you wish to prevent the user from changing specific pieces of text. This is a relatively complex task as you will need to prevent the Document from being updated. As well it would probably be a good idea to highlight the protected text so the user has some visual feedback.

The ProtectedTextComponent class tries to accomplish the above functionality within a single class. This class will coordinate the behaviour of two related classes. The ProtectedDocument class will provide the functionality for protecting the text in the Document and the ProtectedHighlighter class will provide the functionality for highlighting the protected text.

The ProtectedDocument class is not actually a Document, but it will maintain the information necessary to prevent protected text from being updated or deleted. It provides two separate, but related pieces of functionality. A custom:

  • DocumentFilter – is used to control which areas of the text can be updated. Both insertion of new text in a protected region of text and the removal of protected text is prevented.
  • NavigationFilter – is used to control the caret movement. The caret will skip over the protected text and position itself at the next editable text character.

Similiarly, the ProtectedHighlighter class will provide two related pieces of highlighting functionality. It will manage:

  • highlight size – which must remain fixed, since the protected text can’t change. However, default processing in text components is to merge attributes as a character is typed with the previous character. This behaviour causes the highlight size to grow. Therefore, we must manage the highlight size internally so that it doesn’t grow. (Note, if anybody knows a way to prevent the highlight from growing I would love to hear it).
  • painting highlights – we can use the default functionality of the painter, but we need to know when an entire line should be highlighted so that we can force the highlighting to the right edge of the component, not just to the right edge of the text.

Protecting text with code something like this:

ProtectedTextComponent ptc = new ProtectedTextComponent(textComponent);
ptc.protectText(0, 0);
ptc.protectText(9, 10);
ptc.protectText(18, 19);
ptc.protectLine(2);
ptc.protectLines(4, 5);

would result in an image like this:

protected-text-component

Note, although the ProtectedDocument class was designed to be used with the ProtectedTextComponent class it can be used as a stand alone class if you don’t need the protected text highlighted.

Get The Code

ProtectedTextComponent.java
ProtectedDocument.java
ProtectedHighlighter.java

Related Reading

Implementing a Document Filter
Java API: javax.swing.text.NavigationFilter

One Response to “Protected Text Component”

  1. Jörg said

    Hi Rob,

    thanks again for this solution.

    For those who know only at runtime whether their TextComponent needs protection or not,
    I suggest the following method in ProtectedTextComponent, unless you have a better solution.

    public void removeProtection()
    {
    	highlighter.removeAllHighlights();
    	//document.component.setNavigationFilter(new NavigationFilter()); // Doesn't work.
    	document.component.setNavigationFilter(null);
    	document.component.setDocument(new PlainDocument());
    }
    

    Switching off highlighting was no problem, but I couldn’t tame the NavigationFilter
    as JTextComponent has no removeNavigationFilter(). Even setting it to null still
    blocked caret movement. So I ended up in giving the component a new document.
    But still, setting the filter to null is necessary.

    For the above method to work, “component” in ProtectedDocument has to be made
    accessible either in making it a class variable or implementing a getter.

    All the best

    Jörg

Leave a comment

 
Design a site like this with WordPress.com
Get started