svn-gvsig-desktop / tags / v2_0_0_Build_2056 / extensions / org.gvsig.mkmvnproject / src / main / java / org / gvsig / mkmvnproject / gui / CreatePluginConsolePanel.java @ 39038
History | View | Annotate | Download (6.03 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.mkmvnproject.gui; |
23 |
|
24 |
import java.awt.BorderLayout; |
25 |
import java.awt.Color; |
26 |
import java.awt.Dimension; |
27 |
import java.io.BufferedReader; |
28 |
import java.io.IOException; |
29 |
import java.io.InputStreamReader; |
30 |
import java.io.PipedInputStream; |
31 |
import java.io.PipedOutputStream; |
32 |
import java.io.PrintStream; |
33 |
|
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.JScrollPane; |
36 |
import javax.swing.JTextArea; |
37 |
import javax.swing.SwingUtilities; |
38 |
|
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
/**
|
43 |
* A panel which shows the execution log messages of the create plugin process.
|
44 |
*
|
45 |
* @author gvSIG Team
|
46 |
* @version $Id$
|
47 |
*/
|
48 |
public class CreatePluginConsolePanel extends JPanel { |
49 |
|
50 |
private static final long serialVersionUID = -7213048219847956909L; |
51 |
|
52 |
private static final Logger logger = LoggerFactory |
53 |
.getLogger(CreatePluginConsolePanel.class); |
54 |
|
55 |
private JTextArea console; |
56 |
|
57 |
private PrintStream printStream; |
58 |
private PipedOutputStream pipedos; |
59 |
private PipedInputStream pipedis; |
60 |
private Thread consoleThread; |
61 |
|
62 |
/**
|
63 |
* Constructor.
|
64 |
*
|
65 |
* @throws IOException
|
66 |
* if there is an error creating the console streams
|
67 |
*/
|
68 |
public CreatePluginConsolePanel() throws IOException { |
69 |
super();
|
70 |
initialize(); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Constructor.
|
75 |
*
|
76 |
* @param isDoubleBuffered
|
77 |
* if the panel must be double buffered.
|
78 |
* @throws IOException
|
79 |
* if there is an error creating the console streams
|
80 |
*/
|
81 |
public CreatePluginConsolePanel(boolean isDoubleBuffered) |
82 |
throws IOException { |
83 |
super(isDoubleBuffered);
|
84 |
initialize(); |
85 |
} |
86 |
|
87 |
/**
|
88 |
* Initializes the panel GUI.
|
89 |
*
|
90 |
* @throws IOException
|
91 |
*/
|
92 |
private void initialize() throws IOException { |
93 |
Dimension dimension = new Dimension(600, 200); |
94 |
setSize(dimension); |
95 |
setLayout(new BorderLayout()); |
96 |
|
97 |
console = new JTextArea(); |
98 |
console.setEditable(false);
|
99 |
// console.setColumns(20);
|
100 |
// console.setRows(5);
|
101 |
console.setBackground(Color.WHITE);
|
102 |
console.setLineWrap(true);
|
103 |
console.setWrapStyleWord(true);
|
104 |
|
105 |
JScrollPane consoleScrollPane = new JScrollPane(console); |
106 |
add(consoleScrollPane, BorderLayout.CENTER);
|
107 |
|
108 |
pipedis = new PipedInputStream(); |
109 |
|
110 |
pipedos = new PipedOutputStream(pipedis); |
111 |
|
112 |
printStream = new PrintStream(pipedos); |
113 |
|
114 |
logger.info("===================================");
|
115 |
logger.info("= Opening plugin creation console =");
|
116 |
logger.info("===================================");
|
117 |
|
118 |
consoleThread = new ConsoleThread(pipedis, console);
|
119 |
consoleThread.start(); |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Returns a {@link PrintStream} which allows to write messages to the
|
124 |
* console.
|
125 |
*
|
126 |
* @return a {@link PrintStream} which allows to write messages to the
|
127 |
* console
|
128 |
*/
|
129 |
public PrintStream getPrintStream() { |
130 |
return printStream;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Returns a {@link PrintStream} which allows to write error messages to the
|
135 |
* console.
|
136 |
*
|
137 |
* @return a {@link PrintStream} which allows to write error messages to the
|
138 |
* console
|
139 |
*/
|
140 |
public PrintStream getErrorPrintStream() { |
141 |
return getPrintStream();
|
142 |
} |
143 |
|
144 |
/**
|
145 |
* Closes the console. Once this method is called, any more calls to the
|
146 |
* console {@link PrintStream} will throw an exception.
|
147 |
*/
|
148 |
public void closeConsole() { |
149 |
|
150 |
logger.info("===================================");
|
151 |
logger.info("= Closing plugin creation console =");
|
152 |
logger.info("===================================");
|
153 |
|
154 |
printStream.flush(); |
155 |
printStream.close(); |
156 |
try {
|
157 |
pipedos.close(); |
158 |
pipedis.close(); |
159 |
// Wait for the thread to finish
|
160 |
consoleThread.join(500);
|
161 |
} catch (IOException e) { |
162 |
logger.warn("Error closing the internal piped streams", e);
|
163 |
} catch (InterruptedException e) { |
164 |
// Nothing special to do
|
165 |
logger.debug("Console thread interrupted while waiting to finish", e);
|
166 |
} |
167 |
} |
168 |
|
169 |
private static final class ConsoleThread extends Thread { |
170 |
|
171 |
private final PipedInputStream pipedis; |
172 |
private final JTextArea console; |
173 |
|
174 |
/**
|
175 |
* Constructor.
|
176 |
*/
|
177 |
public ConsoleThread(PipedInputStream pipedis, JTextArea console) { |
178 |
super("Create plugin console update thread"); |
179 |
setDaemon(true);
|
180 |
this.pipedis = pipedis;
|
181 |
this.console = console;
|
182 |
} |
183 |
|
184 |
@Override
|
185 |
public void run() { |
186 |
try {
|
187 |
BufferedReader reader = new BufferedReader( |
188 |
new InputStreamReader(pipedis)); |
189 |
String line;
|
190 |
while ((line = reader.readLine()) != null) { |
191 |
// =========== Writing lines to log too
|
192 |
logger.info(line); |
193 |
// =====================================
|
194 |
SwingUtilities.invokeLater(new ConsoleUpdateRunnable( |
195 |
console, line)); |
196 |
} |
197 |
logger.debug("Console input stream end, finish reading");
|
198 |
reader.close(); |
199 |
} catch (IOException e) { |
200 |
logger.warn("Error reading from the console string", e);
|
201 |
} |
202 |
} |
203 |
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* Runnable used to update the console text field.
|
208 |
*
|
209 |
* @author gvSIG Team
|
210 |
* @version $Id$
|
211 |
*/
|
212 |
private static final class ConsoleUpdateRunnable implements Runnable { |
213 |
private final String newLine; |
214 |
private final JTextArea console; |
215 |
|
216 |
/**
|
217 |
* Constructor.
|
218 |
*/
|
219 |
public ConsoleUpdateRunnable(JTextArea console, String newLine) { |
220 |
this.console = console;
|
221 |
this.newLine = newLine;
|
222 |
} |
223 |
|
224 |
public void run() { |
225 |
console.append(newLine); |
226 |
console.append("\n");
|
227 |
} |
228 |
} |
229 |
} |