[97] | 1 | package com.framsticks.gui.console; |
---|
| 2 | |
---|
| 3 | import com.framsticks.communication.File; |
---|
| 4 | import com.framsticks.util.FramsticksException; |
---|
[100] | 5 | // import org.apache.logging.log4j.Logger; |
---|
[97] | 6 | |
---|
| 7 | // import java.awt.Color; |
---|
| 8 | |
---|
| 9 | import javax.swing.JTextPane; |
---|
| 10 | import javax.swing.text.BadLocationException; |
---|
| 11 | import javax.swing.text.Document; |
---|
| 12 | import javax.swing.text.SimpleAttributeSet; |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * Class paints messages. |
---|
| 16 | */ |
---|
| 17 | public class ConsolePainter { |
---|
[100] | 18 | // private static final Logger log = LogManager.getLogger(ConsolePainter.class.getName()); |
---|
[97] | 19 | |
---|
| 20 | private final Document doc; |
---|
| 21 | private final JTextPane textPane; |
---|
| 22 | // private Color c1 = new Color(192, 0, 0); |
---|
| 23 | // private Color c2 = new Color(84, 141, 212); |
---|
| 24 | // private Color c3 = new Color(118, 146, 60); |
---|
| 25 | // private Color c4 = new Color(0, 0, 128); |
---|
| 26 | // private final SimpleLinePainter linePainter1; |
---|
| 27 | // private final SimpleLinePainter linePainter2; |
---|
| 28 | private final SimpleAttributeSet set = new SimpleAttributeSet(); |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Constructor sets reference to pane with text. |
---|
| 33 | * @param pane Pane where text is displaying. |
---|
| 34 | */ |
---|
| 35 | public ConsolePainter(JTextPane pane) { |
---|
| 36 | this.textPane = pane; |
---|
| 37 | doc = textPane.getStyledDocument(); |
---|
| 38 | // linePainter1 = new SimpleLinePainter(textPane, new Color(224, 224, 255)); |
---|
| 39 | // linePainter2 = new SimpleLinePainter(textPane, new Color(240, 240, 255)); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Paints message. |
---|
| 44 | */ |
---|
| 45 | public void paintMessage(File file) { |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | String line; |
---|
| 49 | // Boolean boldNextId = false; |
---|
| 50 | // Boolean doNotPaint = false; |
---|
| 51 | |
---|
| 52 | while ((line = file.getContent().readLine()) != null) { |
---|
| 53 | paintLine(line + "\n"); |
---|
| 54 | /* |
---|
| 55 | if (doNotPaint) { |
---|
| 56 | plainText(line); |
---|
| 57 | if (line.endsWith("~\n")) { |
---|
| 58 | doNotPaint = false; |
---|
| 59 | } |
---|
| 60 | continue; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | int indColon = line.indexOf(':'); |
---|
| 64 | |
---|
| 65 | if (indColon == -1) { |
---|
| 66 | String word = firstWord(line); |
---|
| 67 | String rest = line.substring(word.length()); |
---|
| 68 | Color col = c1; |
---|
| 69 | |
---|
| 70 | if (word.startsWith("ok")) { |
---|
| 71 | col = Color.BLACK; |
---|
| 72 | } else if (word.startsWith("error")) { |
---|
| 73 | col = Color.RED; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | paintLine(word, col, "b"); |
---|
| 77 | paintLine(rest, Color.BLACK, "b"); |
---|
| 78 | continue; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | if (indColon == line.length() - 2) { |
---|
| 82 | String before = line.substring(0, indColon); |
---|
| 83 | String type = "b"; |
---|
| 84 | |
---|
| 85 | if (before.equals("class")) { |
---|
| 86 | boldNextId = true; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | paintLine(before + ":", c2, type); |
---|
| 90 | paintLine("\n", Color.BLACK, ""); |
---|
| 91 | continue; |
---|
| 92 | } |
---|
| 93 | String before = line.substring(0, indColon); |
---|
| 94 | String after = line.substring(indColon + 1); |
---|
| 95 | |
---|
| 96 | String type; |
---|
| 97 | |
---|
| 98 | if (before.equals("id") && boldNextId) { |
---|
| 99 | type = "b"; |
---|
| 100 | boldNextId = false; |
---|
| 101 | } else { |
---|
| 102 | type = ""; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | paintLine(before + ":", c3, "b"); |
---|
| 106 | paintLine(after, Color.BLACK, type); |
---|
| 107 | |
---|
| 108 | if (after.startsWith("~")) { |
---|
| 109 | doNotPaint = true; |
---|
| 110 | }*/ |
---|
| 111 | |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | scrollDown(); |
---|
| 115 | |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | private void paintLine(String text) { |
---|
| 119 | //TODO colouring output, maybe using parsers? |
---|
| 120 | try { |
---|
| 121 | doc.insertString(doc.getLength(), text, set); |
---|
| 122 | } catch (BadLocationException e) { |
---|
| 123 | throw new FramsticksException().msg("failed document insertion").cause(e); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | /** |
---|
| 127 | * Paints line of text. |
---|
| 128 | * @param text Line of text to be painted. |
---|
| 129 | * @param color Font color. |
---|
| 130 | * @param type Font attributes [b|i|u] (bold | italic | underline) |
---|
| 131 | */ |
---|
| 132 | /* |
---|
| 133 | private void paintLine(String text, Color color, String type) { |
---|
| 134 | SimpleAttributeSet set = new SimpleAttributeSet(); |
---|
| 135 | |
---|
| 136 | if (type.contains("b")) { |
---|
| 137 | StyleConstants.setBold(set, true); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if (type.contains("i")) { |
---|
| 141 | StyleConstants.setItalic(set, true); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | if (type.contains("u")) { |
---|
| 145 | StyleConstants.setUnderline(set, true); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | StyleConstants.setFontFamily(set, "Monospaced"); |
---|
| 149 | StyleConstants.setForeground(set, color); |
---|
| 150 | textPane.setCharacterAttributes(set, true); |
---|
| 151 | |
---|
| 152 | try { |
---|
| 153 | doc.insertString(doc.getLength(), text, set); |
---|
| 154 | } catch (BadLocationException e) { |
---|
| 155 | e.printStackTrace(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | } |
---|
| 159 | */ |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Returns first word in text line. |
---|
| 165 | * @param line Line of text. |
---|
| 166 | * @return First word in text line (with space char). |
---|
| 167 | */ |
---|
| 168 | // private String firstWord(String line) { |
---|
| 169 | // String result; |
---|
| 170 | // int spaceIndex = line.indexOf(' '); |
---|
| 171 | |
---|
| 172 | // if (spaceIndex == -1) { |
---|
| 173 | // result = line; |
---|
| 174 | // } else { |
---|
| 175 | // result = line.substring(0, spaceIndex + 1); |
---|
| 176 | // } |
---|
| 177 | |
---|
| 178 | // return result; |
---|
| 179 | // } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Paint line as plain text. |
---|
| 183 | * @param msg |
---|
| 184 | */ |
---|
| 185 | /* |
---|
| 186 | private void plainText(String msg) { |
---|
| 187 | paintLine(msg, Color.BLACK, ""); |
---|
| 188 | } |
---|
| 189 | */ |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Adds message typed by user. |
---|
| 193 | * @param msg Message typed by user. |
---|
| 194 | */ |
---|
| 195 | public void userLine(String line) { |
---|
| 196 | // paintLine("[USER]> "/*, c4, "b"*/); |
---|
| 197 | scrollDown(); |
---|
| 198 | // linePainter1.resetHighlight(); |
---|
| 199 | paintLine(line); |
---|
| 200 | paintLine("\n"); |
---|
| 201 | scrollDown(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * Scrolling text pane. |
---|
| 206 | */ |
---|
| 207 | private void scrollDown() { |
---|
| 208 | // textPane.setCaretPosition(doc.getLength() - 1); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | } |
---|