23ee90014e863bc0568a3526f4e086a793b6465c
2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.teditor
;
31 import java
.util
.SortedMap
;
32 import java
.util
.TreeMap
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.bits
.Color
;
38 * Highlighter provides color choices for certain text strings.
40 public class Highlighter
{
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * The highlighter colors.
49 private SortedMap
<String
, CellAttributes
> colors
;
51 // ------------------------------------------------------------------------
52 // Constructors -----------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * Public constructor sets the theme to the default.
58 public Highlighter() {
62 // ------------------------------------------------------------------------
63 // Highlighter ------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Set keyword highlighting.
69 * @param enabled if true, enable keyword highlighting
71 public void setEnabled(final boolean enabled
) {
80 * Set my field values to that's field.
82 * @param rhs an instance of Highlighter
84 public void setTo(final Highlighter rhs
) {
85 colors
= new TreeMap
<String
, CellAttributes
>();
86 colors
.putAll(rhs
.colors
);
90 * See if this is a character that should split a word.
92 * @param ch the character
93 * @return true if the word should be split
95 public boolean shouldSplit(final int ch
) {
96 // For now, split on punctuation
97 String punctuation
= "'\"\\<>{}[]!@#$%^&*();:.,-+/*?";
99 if (punctuation
.indexOf((char) ch
) != -1) {
107 * Retrieve the CellAttributes for a named theme color.
109 * @param name theme color name, e.g. "twindow.border"
110 * @return color associated with name, e.g. bold yellow on blue
112 public CellAttributes
getColor(final String name
) {
113 if (colors
== null) {
116 CellAttributes attr
= colors
.get(name
);
121 * Sets to defaults that resemble the Borland IDE colors.
123 public void setJavaColors() {
124 colors
= new TreeMap
<String
, CellAttributes
>();
126 CellAttributes color
;
129 "boolean", "byte", "short", "int", "long", "char", "float",
132 color
= new CellAttributes();
133 color
.setForeColor(Color
.GREEN
);
134 color
.setBackColor(Color
.BLUE
);
136 for (String str
: types
) {
137 colors
.put(str
, color
);
140 String
[] modifiers
= {
141 "abstract", "final", "native", "private", "protected", "public",
142 "static", "strictfp", "synchronized", "transient", "volatile",
144 color
= new CellAttributes();
145 color
.setForeColor(Color
.WHITE
);
146 color
.setBackColor(Color
.BLUE
);
148 for (String str
: modifiers
) {
149 colors
.put(str
, color
);
152 String
[] keywords
= {
153 "new", "class", "interface", "extends", "implements",
154 "if", "else", "do", "while", "for", "break", "continue",
155 "switch", "case", "default",
157 color
= new CellAttributes();
158 color
.setForeColor(Color
.YELLOW
);
159 color
.setBackColor(Color
.BLUE
);
161 for (String str
: keywords
) {
162 colors
.put(str
, color
);
165 String
[] operators
= {
166 "[", "]", "(", ")", "{", "}",
167 "*", "-", "+", "/", "=", "%",
168 "^", "&", "!", "<<", ">>", "<<<", ">>>",
170 ">", "<", ">=", "<=", "!=", "==",
171 ",", ";", ".", "?", ":",
173 color
= new CellAttributes();
174 color
.setForeColor(Color
.CYAN
);
175 color
.setBackColor(Color
.BLUE
);
177 for (String str
: operators
) {
178 colors
.put(str
, color
);
181 String
[] packageKeywords
= {
184 color
= new CellAttributes();
185 color
.setForeColor(Color
.GREEN
);
186 color
.setBackColor(Color
.BLUE
);
188 for (String str
: packageKeywords
) {
189 colors
.put(str
, color
);