#55 restore default attributes on exit
[nikiroo-utils.git] / src / jexer / teditor / Highlighter.java
... / ...
CommitLineData
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
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
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
45
46 /**
47 * The highlighter colors.
48 */
49 private SortedMap<String, CellAttributes> colors;
50
51 // ------------------------------------------------------------------------
52 // Constructors -----------------------------------------------------------
53 // ------------------------------------------------------------------------
54
55 /**
56 * Public constructor sets the theme to the default.
57 */
58 public Highlighter() {
59 colors = new TreeMap<String, CellAttributes>();
60 }
61
62 // ------------------------------------------------------------------------
63 // Highlighter ------------------------------------------------------------
64 // ------------------------------------------------------------------------
65
66 /**
67 * See if this is a character that should split a word.
68 *
69 * @param ch the character
70 * @return true if the word should be split
71 */
72 public boolean shouldSplit(final int ch) {
73 // For now, split on punctuation
74 String punctuation = "'\"\\<>{}[]!@#$%^&*();:.,-+/*?";
75 if (ch < 0x100) {
76 if (punctuation.indexOf((char) ch) != -1) {
77 return true;
78 }
79 }
80 return false;
81 }
82
83 /**
84 * Retrieve the CellAttributes for a named theme color.
85 *
86 * @param name theme color name, e.g. "twindow.border"
87 * @return color associated with name, e.g. bold yellow on blue
88 */
89 public CellAttributes getColor(final String name) {
90 CellAttributes attr = (CellAttributes) colors.get(name);
91 return attr;
92 }
93
94 /**
95 * Sets to defaults that resemble the Borland IDE colors.
96 */
97 public void setJavaColors() {
98 CellAttributes color;
99
100 String [] keywords = {
101 "boolean", "byte", "short", "int", "long", "char", "float",
102 "double", "void", "new",
103 "static", "final", "volatile", "synchronized", "abstract",
104 "public", "private", "protected",
105 "class", "interface", "extends", "implements",
106 "if", "else", "do", "while", "for", "break", "continue",
107 "switch", "case", "default",
108 };
109 color = new CellAttributes();
110 color.setForeColor(Color.WHITE);
111 color.setBackColor(Color.BLUE);
112 color.setBold(true);
113 for (String str: keywords) {
114 colors.put(str, color);
115 }
116
117 String [] operators = {
118 "[", "]", "(", ")", "{", "}",
119 "*", "-", "+", "/", "=", "%",
120 "^", "&", "!", "<<", ">>", "<<<", ">>>",
121 "&&", "||",
122 ">", "<", ">=", "<=", "!=", "==",
123 ",", ";", ".", "?", ":",
124 };
125 color = new CellAttributes();
126 color.setForeColor(Color.CYAN);
127 color.setBackColor(Color.BLUE);
128 color.setBold(true);
129 for (String str: operators) {
130 colors.put(str, color);
131 }
132
133 String [] packageKeywords = {
134 "package", "import",
135 };
136 color = new CellAttributes();
137 color.setForeColor(Color.GREEN);
138 color.setBackColor(Color.BLUE);
139 color.setBold(true);
140 for (String str: packageKeywords) {
141 colors.put(str, color);
142 }
143
144 }
145
146}