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