| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * Version: $Id$ |
| 5 | * |
| 6 | * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a> |
| 7 | * |
| 8 | * License: LGPLv3 or later |
| 9 | * |
| 10 | * Copyright: This module is licensed under the GNU Lesser General |
| 11 | * Public License Version 3. Please see the file "COPYING" in this |
| 12 | * directory for more information about the GNU Lesser General Public |
| 13 | * License Version 3. |
| 14 | * |
| 15 | * Copyright (C) 2015 Kevin Lamonte |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU Lesser General Public License |
| 19 | * as published by the Free Software Foundation; either version 3 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, but |
| 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU Lesser General Public |
| 28 | * License along with this program; if not, see |
| 29 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 30 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 31 | * 02110-1301 USA |
| 32 | */ |
| 33 | package jexer.bits; |
| 34 | |
| 35 | /** |
| 36 | * The attributes used by a Cell: color, bold, blink, etc. |
| 37 | */ |
| 38 | public class CellAttributes { |
| 39 | |
| 40 | /** |
| 41 | * Bold |
| 42 | */ |
| 43 | public boolean bold; |
| 44 | |
| 45 | /** |
| 46 | * Blink |
| 47 | */ |
| 48 | public boolean blink; |
| 49 | |
| 50 | /** |
| 51 | * Reverse |
| 52 | */ |
| 53 | public boolean reverse; |
| 54 | |
| 55 | /** |
| 56 | * Underline |
| 57 | */ |
| 58 | public boolean underline; |
| 59 | |
| 60 | /** |
| 61 | * Protected |
| 62 | */ |
| 63 | public boolean protect; |
| 64 | |
| 65 | /** |
| 66 | * Foreground color. Color.WHITE, Color.RED, etc. |
| 67 | */ |
| 68 | public Color foreColor; |
| 69 | |
| 70 | /** |
| 71 | * Background color. Color.WHITE, Color.RED, etc. |
| 72 | */ |
| 73 | public Color backColor; |
| 74 | |
| 75 | /** |
| 76 | * Set to default not-bold, white foreground on black background. |
| 77 | */ |
| 78 | public void reset() { |
| 79 | bold = false; |
| 80 | blink = false; |
| 81 | reverse = false; |
| 82 | protect = false; |
| 83 | underline = false; |
| 84 | foreColor = Color.WHITE; |
| 85 | backColor = Color.BLACK; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Public constructor |
| 90 | */ |
| 91 | public CellAttributes() { |
| 92 | reset(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Comparison. All fields must match to return true. |
| 97 | */ |
| 98 | @Override |
| 99 | public boolean equals(Object rhs) { |
| 100 | if (!(rhs instanceof CellAttributes)) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | CellAttributes that = (CellAttributes)rhs; |
| 105 | return ((bold == that.bold) && |
| 106 | (blink == that.blink) && |
| 107 | (reverse == that.reverse) && |
| 108 | (underline == that.underline) && |
| 109 | (protect == that.protect) && |
| 110 | (foreColor == that.foreColor) && |
| 111 | (backColor == that.backColor)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set my field values to that's field |
| 116 | */ |
| 117 | public void setTo(Object rhs) { |
| 118 | CellAttributes that = (CellAttributes)rhs; |
| 119 | |
| 120 | this.bold = that.bold; |
| 121 | this.blink = that.blink; |
| 122 | this.reverse = that.reverse; |
| 123 | this.underline = that.underline; |
| 124 | this.protect = that.protect; |
| 125 | this.foreColor = that.foreColor; |
| 126 | this.backColor = that.backColor; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Convert enum to string |
| 131 | * |
| 132 | * @param color Color.RED, Color.BLUE, etc. |
| 133 | * @return "red", "blue", etc. |
| 134 | */ |
| 135 | static public String stringFromColor(Color color) { |
| 136 | if (color.equals(Color.BLACK)) { |
| 137 | return "black"; |
| 138 | } else if (color.equals(Color.WHITE)) { |
| 139 | return "white"; |
| 140 | } else if (color.equals(Color.RED)) { |
| 141 | return "red"; |
| 142 | } else if (color.equals(Color.CYAN)) { |
| 143 | return "cyan"; |
| 144 | } else if (color.equals(Color.GREEN)) { |
| 145 | return "green"; |
| 146 | } else if (color.equals(Color.MAGENTA)) { |
| 147 | return "magenta"; |
| 148 | } else if (color.equals(Color.BLUE)) { |
| 149 | return "blue"; |
| 150 | } else if (color.equals(Color.YELLOW)) { |
| 151 | return "yellow"; |
| 152 | } |
| 153 | throw new IllegalArgumentException("Invalid Color value: " + |
| 154 | color.value); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Convert string to enum |
| 159 | * |
| 160 | * @param color "red", "blue", etc. |
| 161 | * @return Color.RED, Color.BLUE, etc. |
| 162 | */ |
| 163 | static public Color colorFromString(String color) { |
| 164 | switch (color.toLowerCase()) { |
| 165 | case "black": |
| 166 | return Color.BLACK; |
| 167 | case "white": |
| 168 | return Color.WHITE; |
| 169 | case "red": |
| 170 | return Color.RED; |
| 171 | case "cyan": |
| 172 | return Color.CYAN; |
| 173 | case "green": |
| 174 | return Color.GREEN; |
| 175 | case "magenta": |
| 176 | return Color.MAGENTA; |
| 177 | case "blue": |
| 178 | return Color.BLUE; |
| 179 | case "yellow": |
| 180 | return Color.YELLOW; |
| 181 | case "brown": |
| 182 | return Color.YELLOW; |
| 183 | default: |
| 184 | // Let unknown strings become white |
| 185 | return Color.WHITE; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Make human-readable description of this CellAttributes |
| 191 | */ |
| 192 | @Override |
| 193 | public String toString() { |
| 194 | return String.format("%s%s on %s", |
| 195 | bold ? "bold " : "", |
| 196 | stringFromColor(foreColor), |
| 197 | stringFromColor(backColor)); |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |