1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.view.std; |
24 |
|
25 |
import java.awt.Color; |
26 |
|
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
|
30 |
import java.util.logging.Level; |
31 |
|
32 |
import javax.swing.BorderFactory; |
33 |
import javax.swing.ImageIcon; |
34 |
import javax.swing.JScrollPane; |
35 |
import javax.swing.JTextPane; |
36 |
import javax.swing.Timer; |
37 |
|
38 |
import javax.swing.text.Style; |
39 |
import javax.swing.text.StyleConstants; |
40 |
import javax.swing.text.StyleContext; |
41 |
import javax.swing.text.StyledDocument; |
42 |
|
43 |
import org.jsampler.CC; |
44 |
import org.jsampler.HF; |
45 |
|
46 |
import static org.jsampler.view.std.StdI18n.i18n; |
47 |
|
48 |
/** |
49 |
* |
50 |
* @author Grigor Iliev |
51 |
*/ |
52 |
public class JSBackendLogFrame extends JSFrame { |
53 |
private final BackendLogPane backendLogPane = new BackendLogPane(); |
54 |
private final Timer timer; |
55 |
|
56 |
public |
57 |
JSBackendLogFrame() { |
58 |
super(i18n.getLabel("JSBackendLogFrame.title"), "JSBackendLogFrame"); |
59 |
ImageIcon i = CC.getViewConfig().getBasicIconSet().getApplicationIcon(); |
60 |
if(i != null) setIconImage(i.getImage()); |
61 |
|
62 |
add(new JScrollPane(backendLogPane)); |
63 |
|
64 |
ActionListener l = new ActionListener() { |
65 |
public void |
66 |
actionPerformed(ActionEvent e) { |
67 |
processInput(); |
68 |
} |
69 |
}; |
70 |
|
71 |
timer = new Timer(500, l); |
72 |
timer.start(); |
73 |
} |
74 |
|
75 |
public void |
76 |
stopTimer() { timer.stop(); } |
77 |
|
78 |
private void |
79 |
processInput() { |
80 |
Process p = CC.getBackendProcess(); |
81 |
if(p == null) return; |
82 |
|
83 |
try { |
84 |
StringBuffer sb = new StringBuffer(); |
85 |
while(p.getInputStream().available() > 0) { |
86 |
sb.append((char) p.getInputStream().read()); |
87 |
} |
88 |
String s = sb.toString(); |
89 |
if(s.length() > 0) backendLogPane.appendText(s); |
90 |
|
91 |
int i = s.indexOf("Starting LSCP network server"); |
92 |
if(i != -1 && s.indexOf("OK", i + 27) != -1) { |
93 |
synchronized(CC.getBackendMonitor()) { |
94 |
// Notify that the LSCP server is started |
95 |
CC.getBackendMonitor().notifyAll(); |
96 |
} |
97 |
} |
98 |
|
99 |
sb = new StringBuffer(); |
100 |
while(p.getErrorStream().available() > 0) { |
101 |
sb.append((char) p.getErrorStream().read()); |
102 |
} |
103 |
s = sb.toString(); |
104 |
if(s.length() > 0) backendLogPane.appendError(s); |
105 |
} catch(Exception x) { |
106 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
107 |
} |
108 |
} |
109 |
|
110 |
public static class BackendLogPane extends JTextPane { |
111 |
private final String STYLE_ROOT = "root"; |
112 |
private final String STYLE_REGULAR = "regular"; |
113 |
private final String STYLE_ERROR = "errorMessage"; |
114 |
private final String STYLE_ERROR_0 = "errorMessage0"; |
115 |
|
116 |
public |
117 |
BackendLogPane() { |
118 |
Style def; |
119 |
def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); |
120 |
StyledDocument doc = getStyledDocument(); |
121 |
Style root = doc.addStyle(STYLE_ROOT, def); |
122 |
Style regular = doc.addStyle(STYLE_REGULAR, root); |
123 |
Style style = doc.addStyle(STYLE_ERROR_0, regular); |
124 |
StyleConstants.setForeground(style, Color.RED); |
125 |
doc.addStyle(STYLE_ERROR, style); |
126 |
|
127 |
setEditable(false); |
128 |
setBorder(BorderFactory.createEmptyBorder()); |
129 |
} |
130 |
|
131 |
public void |
132 |
appendText(String s) { |
133 |
StyledDocument doc = getStyledDocument(); |
134 |
try { |
135 |
doc.insertString(doc.getLength(), s, doc.getStyle(STYLE_REGULAR)); |
136 |
} catch(Exception x) { |
137 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
138 |
} |
139 |
} |
140 |
|
141 |
public void |
142 |
appendError(String s) { |
143 |
StyledDocument doc = getStyledDocument(); |
144 |
try { |
145 |
doc.insertString(doc.getLength(), s, doc.getStyle(STYLE_ERROR)); |
146 |
} catch(Exception x) { |
147 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
148 |
} |
149 |
} |
150 |
} |
151 |
} |