Merge branch 'subtree'
[fanfix.git] / src / jexer / teditor / Highlighter.java
CommitLineData
e8a11f98
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
e8a11f98
KL
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.teditor;
30
31import java.util.SortedMap;
32import java.util.TreeMap;
33
34import jexer.bits.CellAttributes;
35import jexer.bits.Color;
36
37/**
38 * Highlighter provides color choices for certain text strings.
39 */
40public class Highlighter {
41
615a0d99
KL
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
45
e8a11f98
KL
46 /**
47 * The highlighter colors.
48 */
49 private SortedMap<String, CellAttributes> colors;
50
615a0d99
KL
51 // ------------------------------------------------------------------------
52 // Constructors -----------------------------------------------------------
53 // ------------------------------------------------------------------------
54
e8a11f98
KL
55 /**
56 * Public constructor sets the theme to the default.
57 */
58 public Highlighter() {
21460f44 59 // NOP
e8a11f98
KL
60 }
61
615a0d99
KL
62 // ------------------------------------------------------------------------
63 // Highlighter ------------------------------------------------------------
64 // ------------------------------------------------------------------------
65
21460f44
KL
66 /**
67 * Set keyword highlighting.
68 *
69 * @param enabled if true, enable keyword highlighting
70 */
71 public void setEnabled(final boolean enabled) {
72 if (enabled) {
73 setJavaColors();
74 } else {
75 colors = null;
76 }
77 }
78
79 /**
80 * Set my field values to that's field.
81 *
82 * @param rhs an instance of Highlighter
83 */
84 public void setTo(final Highlighter rhs) {
85 colors = new TreeMap<String, CellAttributes>();
86 colors.putAll(rhs.colors);
87 }
88
e8a11f98
KL
89 /**
90 * See if this is a character that should split a word.
91 *
92 * @param ch the character
93 * @return true if the word should be split
94 */
2d3f60d8 95 public boolean shouldSplit(final int ch) {
e8a11f98
KL
96 // For now, split on punctuation
97 String punctuation = "'\"\\<>{}[]!@#$%^&*();:.,-+/*?";
2d3f60d8
KL
98 if (ch < 0x100) {
99 if (punctuation.indexOf((char) ch) != -1) {
100 return true;
101 }
e8a11f98
KL
102 }
103 return false;
104 }
105
106 /**
107 * Retrieve the CellAttributes for a named theme color.
108 *
109 * @param name theme color name, e.g. "twindow.border"
110 * @return color associated with name, e.g. bold yellow on blue
111 */
112 public CellAttributes getColor(final String name) {
21460f44
KL
113 if (colors == null) {
114 return null;
115 }
5ca5f8e5 116 CellAttributes attr = colors.get(name);
e8a11f98
KL
117 return attr;
118 }
119
120 /**
121 * Sets to defaults that resemble the Borland IDE colors.
122 */
123 public void setJavaColors() {
21460f44
KL
124 colors = new TreeMap<String, CellAttributes>();
125
e8a11f98
KL
126 CellAttributes color;
127
21460f44 128 String [] types = {
e8a11f98 129 "boolean", "byte", "short", "int", "long", "char", "float",
21460f44
KL
130 "double", "void",
131 };
132 color = new CellAttributes();
133 color.setForeColor(Color.GREEN);
134 color.setBackColor(Color.BLUE);
135 color.setBold(true);
136 for (String str: types) {
137 colors.put(str, color);
138 }
139
140 String [] modifiers = {
141 "abstract", "final", "native", "private", "protected", "public",
142 "static", "strictfp", "synchronized", "transient", "volatile",
143 };
144 color = new CellAttributes();
145 color.setForeColor(Color.WHITE);
146 color.setBackColor(Color.BLUE);
147 color.setBold(true);
148 for (String str: modifiers) {
149 colors.put(str, color);
150 }
151
152 String [] keywords = {
153 "new", "class", "interface", "extends", "implements",
e8a11f98
KL
154 "if", "else", "do", "while", "for", "break", "continue",
155 "switch", "case", "default",
156 };
157 color = new CellAttributes();
21460f44 158 color.setForeColor(Color.YELLOW);
e8a11f98
KL
159 color.setBackColor(Color.BLUE);
160 color.setBold(true);
161 for (String str: keywords) {
162 colors.put(str, color);
163 }
164
165 String [] operators = {
166 "[", "]", "(", ")", "{", "}",
167 "*", "-", "+", "/", "=", "%",
168 "^", "&", "!", "<<", ">>", "<<<", ">>>",
169 "&&", "||",
170 ">", "<", ">=", "<=", "!=", "==",
171 ",", ";", ".", "?", ":",
172 };
173 color = new CellAttributes();
174 color.setForeColor(Color.CYAN);
175 color.setBackColor(Color.BLUE);
176 color.setBold(true);
177 for (String str: operators) {
178 colors.put(str, color);
179 }
180
181 String [] packageKeywords = {
182 "package", "import",
183 };
184 color = new CellAttributes();
185 color.setForeColor(Color.GREEN);
186 color.setBackColor(Color.BLUE);
187 color.setBold(true);
188 for (String str: packageKeywords) {
189 colors.put(str, color);
190 }
191
192 }
193
e8a11f98 194}