Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2030 / extensions / org.gvsig.mkmvnproject / src / main / java / org / gvsig / mkmvnproject / CreatePluginConsolePanel.java @ 35916

History | View | Annotate | Download (5.51 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;
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 LOG = 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
                consoleThread = new ConsoleThread(pipedis, console);
114
                consoleThread.start();
115
        }
116

    
117
        /**
118
         * Returns a {@link PrintStream} which allows to write messages to the
119
         * console.
120
         * 
121
         * @return a {@link PrintStream} which allows to write messages to the
122
         *         console
123
         */
124
        public PrintStream getPrintStream() {
125
                return printStream;
126
        }
127

    
128
        /**
129
         * Returns a {@link PrintStream} which allows to write error messages to the
130
         * console.
131
         * 
132
         * @return a {@link PrintStream} which allows to write error messages to the
133
         *         console
134
         */
135
        public PrintStream getErrorPrintStream() {
136
                return getPrintStream();
137
        }
138

    
139
        /**
140
         * Closes the console. Once this method is called, any more calls to the
141
         * console {@link PrintStream} will throw an exception.
142
         */
143
        public void closeConsole() {
144
                printStream.flush();
145
                printStream.close();
146
                try {
147
                        pipedos.close();
148
                        pipedis.close();
149
                        // Wait for the thread to finish
150
                        consoleThread.join(500);
151
                } catch (IOException e) {
152
                        LOG.warn("Error closing the internal piped streams", e);
153
                } catch (InterruptedException e) {
154
                        // Nothing special to do
155
                        LOG.debug("Console thread interrupted while waiting to finish", e);
156
                }
157
        }
158

    
159
        private static final class ConsoleThread extends Thread {
160

    
161
                private final PipedInputStream pipedis;
162
                private final JTextArea console;
163

    
164
                /**
165
                 * Constructor.
166
                 */
167
                public ConsoleThread(PipedInputStream pipedis, JTextArea console) {
168
                        super("Create plugin console update thread");
169
                        setDaemon(true);
170
                        this.pipedis = pipedis;
171
                        this.console = console;
172
                }
173

    
174
                @Override
175
                public void run() {
176
                        try {
177
                                BufferedReader reader = new BufferedReader(
178
                                                new InputStreamReader(pipedis));
179
                                String line;
180
                                while ((line = reader.readLine()) != null) {
181
                                        SwingUtilities.invokeLater(new ConsoleUpdateRunnable(
182
                                                        console, line));
183
                                }
184
                                LOG.debug("Console input stream end, finish reading");
185
                                reader.close();
186
                        } catch (IOException e) {
187
                LOG.warn("Error reading from the console string", e);
188
                        }
189
                }
190

    
191
        }
192

    
193
        /**
194
         * Runnable used to update the console text field.
195
         * 
196
         * @author gvSIG Team
197
         * @version $Id$
198
         */
199
        private static final class ConsoleUpdateRunnable implements Runnable {
200
                private final String newLine;
201
                private final JTextArea console;
202

    
203
                /**
204
                 * Constructor.
205
                 */
206
                public ConsoleUpdateRunnable(JTextArea console, String newLine) {
207
                        this.console = console;
208
                        this.newLine = newLine;
209
                }
210

    
211
                public void run() {
212
                        console.append(newLine);
213
                        console.append("\n");
214
                }
215
        }
216
}