-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProtectedTextComponent.java
More file actions
92 lines (78 loc) · 2.38 KB
/
ProtectedTextComponent.java
File metadata and controls
92 lines (78 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
/*
* Class to manage the protection of text and to highlight the protected
* text within a text component. This class has two functions:
*
* a) notify the ProtectedDocument class of the text to be protected.
* b) notify the ProtectedHighlighter class of the text to be protected.
*/
public class ProtectedTextComponent
{
private JTextComponent component;
private ProtectedHighlighter highlighter;
private ProtectedDocument document;
/**
* Specify the component to be protected. The text will be highlighted
* using the default color
*/
public ProtectedTextComponent(JTextComponent component)
{
this(component, null);
}
/**
* Specify the component to be protected. The text will be highlighted
* using the specified color
*/
public ProtectedTextComponent(JTextComponent component, Color color)
{
this.component = component;
// Handles updates to the Document and caret movement
document = new ProtectedDocument(component);
// Handles highlighting of the protected text
highlighter = new ProtectedHighlighter(component, color);
}
/**
* Protect a range of characters
*
* @param start starting offset
* @param end ending offset
*/
public void protectText(int start, int end)
{
document.protect(start, end);
highlighter.addHighlight(start, end + 1);
}
/**
* Protect an entire line
*
* @param line the line to protect
*/
public void protectLine(int line)
{
protectLines(line, line);
}
/**
* Protect a range of lines
*
* @param firstLine first line in the range
* @param lastLine last line in the range
*/
public void protectLines(int firstLine, int lastLine)
{
Element root = component.getDocument().getDefaultRootElement();
firstLine = Math.max(firstLine, 0);
firstLine = Math.min(firstLine, root.getElementCount() - 1);
Element firstElement = root.getElement( firstLine );
lastLine = Math.max(lastLine, 0);
lastLine = Math.min(lastLine, root.getElementCount() - 1);
Element lastElement = root.getElement( lastLine );
int start = firstElement.getStartOffset();
int end = lastElement.getEndOffset();
document.protect(start - 1, end - 1);
highlighter.addHighlight(start, end);
}
}