| 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 | */ |
| 29 | package jexer.teditor; |
| 30 | |
| 31 | import java.util.SortedMap; |
| 32 | import java.util.TreeMap; |
| 33 | |
| 34 | import jexer.bits.CellAttributes; |
| 35 | import jexer.bits.Color; |
| 36 | |
| 37 | /** |
| 38 | * Highlighter provides color choices for certain text strings. |
| 39 | */ |
| 40 | public 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 | // NOP |
| 60 | } |
| 61 | |
| 62 | // ------------------------------------------------------------------------ |
| 63 | // Highlighter ------------------------------------------------------------ |
| 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 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 | |
| 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 | */ |
| 95 | public boolean shouldSplit(final int ch) { |
| 96 | // For now, split on punctuation |
| 97 | String punctuation = "'\"\\<>{}[]!@#$%^&*();:.,-+/*?"; |
| 98 | if (ch < 0x100) { |
| 99 | if (punctuation.indexOf((char) ch) != -1) { |
| 100 | return true; |
| 101 | } |
| 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) { |
| 113 | if (colors == null) { |
| 114 | return null; |
| 115 | } |
| 116 | CellAttributes attr = colors.get(name); |
| 117 | return attr; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Sets to defaults that resemble the Borland IDE colors. |
| 122 | */ |
| 123 | public void setJavaColors() { |
| 124 | colors = new TreeMap<String, CellAttributes>(); |
| 125 | |
| 126 | CellAttributes color; |
| 127 | |
| 128 | String [] types = { |
| 129 | "boolean", "byte", "short", "int", "long", "char", "float", |
| 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", |
| 154 | "if", "else", "do", "while", "for", "break", "continue", |
| 155 | "switch", "case", "default", |
| 156 | }; |
| 157 | color = new CellAttributes(); |
| 158 | color.setForeColor(Color.YELLOW); |
| 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 | |
| 194 | } |