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