Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.mkmvnproject.app / org.gvsig.mkmvnproject.app.mainplugin / src / main / java / org / gvsig / mkmvnproject / gui / CreatePluginConsolePanel.java @ 41074

History | View | Annotate | Download (6.05 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.mkmvnproject.gui;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.io.BufferedReader;
30
import java.io.IOException;
31
import java.io.InputStreamReader;
32
import java.io.PipedInputStream;
33
import java.io.PipedOutputStream;
34
import java.io.PrintStream;
35

    
36
import javax.swing.JPanel;
37
import javax.swing.JScrollPane;
38
import javax.swing.JTextArea;
39
import javax.swing.SwingUtilities;
40

    
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
/**
45
 * A panel which shows the execution log messages of the create plugin process.
46
 * 
47
 * @author gvSIG Team
48
 * @version $Id$
49
 */
50
public class CreatePluginConsolePanel extends JPanel {
51

    
52
        private static final long serialVersionUID = -7213048219847956909L;
53

    
54
        private static final Logger logger = LoggerFactory
55
                        .getLogger(CreatePluginConsolePanel.class);
56

    
57
        private JTextArea console;
58

    
59
        private PrintStream printStream;
60
        private PipedOutputStream pipedos;
61
        private PipedInputStream pipedis;
62
        private Thread consoleThread;
63

    
64
        /**
65
         * Constructor.
66
         * 
67
         * @throws IOException
68
         *             if there is an error creating the console streams
69
         */
70
        public CreatePluginConsolePanel() throws IOException {
71
                super();
72
                initialize();
73
        }
74

    
75
        /**
76
         * Constructor.
77
         * 
78
         * @param isDoubleBuffered
79
         *            if the panel must be double buffered.
80
         * @throws IOException
81
         *             if there is an error creating the console streams
82
         */
83
        public CreatePluginConsolePanel(boolean isDoubleBuffered)
84
                        throws IOException {
85
                super(isDoubleBuffered);
86
                initialize();
87
        }
88

    
89
        /**
90
         * Initializes the panel GUI.
91
         * 
92
         * @throws IOException
93
         */
94
        private void initialize() throws IOException {
95
                Dimension dimension = new Dimension(600, 200);
96
                setSize(dimension);
97
                setLayout(new BorderLayout());
98

    
99
                console = new JTextArea();
100
                console.setEditable(false);
101
                // console.setColumns(20);
102
                // console.setRows(5);
103
                console.setBackground(Color.WHITE);
104
                console.setLineWrap(true);
105
                console.setWrapStyleWord(true);
106

    
107
                JScrollPane consoleScrollPane = new JScrollPane(console);
108
                add(consoleScrollPane, BorderLayout.CENTER);
109

    
110
                pipedis = new PipedInputStream();
111

    
112
                pipedos = new PipedOutputStream(pipedis);
113

    
114
                printStream = new PrintStream(pipedos);
115
                
116
        logger.info("===================================");
117
        logger.info("= Opening plugin creation console =");
118
        logger.info("===================================");
119

    
120
                consoleThread = new ConsoleThread(pipedis, console);
121
                consoleThread.start();
122
        }
123

    
124
        /**
125
         * Returns a {@link PrintStream} which allows to write messages to the
126
         * console.
127
         * 
128
         * @return a {@link PrintStream} which allows to write messages to the
129
         *         console
130
         */
131
        public PrintStream getPrintStream() {
132
                return printStream;
133
        }
134

    
135
        /**
136
         * Returns a {@link PrintStream} which allows to write error messages to the
137
         * console.
138
         * 
139
         * @return a {@link PrintStream} which allows to write error messages to the
140
         *         console
141
         */
142
        public PrintStream getErrorPrintStream() {
143
                return getPrintStream();
144
        }
145

    
146
        /**
147
         * Closes the console. Once this method is called, any more calls to the
148
         * console {@link PrintStream} will throw an exception.
149
         */
150
        public void closeConsole() {
151
            
152
        logger.info("===================================");
153
        logger.info("= Closing plugin creation console =");
154
        logger.info("===================================");
155
            
156
                printStream.flush();
157
                printStream.close();
158
                try {
159
                        pipedos.close();
160
                        pipedis.close();
161
                        // Wait for the thread to finish
162
                        consoleThread.join(500);
163
                } catch (IOException e) {
164
                    logger.warn("Error closing the internal piped streams", e);
165
                } catch (InterruptedException e) {
166
                        // Nothing special to do
167
                    logger.debug("Console thread interrupted while waiting to finish", e);
168
                }
169
        }
170

    
171
        private static final class ConsoleThread extends Thread {
172

    
173
                private final PipedInputStream pipedis;
174
                private final JTextArea console;
175

    
176
                /**
177
                 * Constructor.
178
                 */
179
                public ConsoleThread(PipedInputStream pipedis, JTextArea console) {
180
                        super("Create plugin console update thread");
181
                        setDaemon(true);
182
                        this.pipedis = pipedis;
183
                        this.console = console;
184
                }
185

    
186
                @Override
187
                public void run() {
188
                        try {
189
                                BufferedReader reader = new BufferedReader(
190
                                                new InputStreamReader(pipedis));
191
                                String line;
192
                                while ((line = reader.readLine()) != null) {
193
                                    // =========== Writing lines to log too 
194
                            logger.info(line);
195
                    // =====================================
196
                                        SwingUtilities.invokeLater(new ConsoleUpdateRunnable(
197
                                                        console, line));
198
                                }
199
                                logger.debug("Console input stream end, finish reading");
200
                                reader.close();
201
                        } catch (IOException e) {
202
                            logger.info("Write end dead.");
203
                        }
204
                }
205

    
206
        }
207

    
208
        /**
209
         * Runnable used to update the console text field.
210
         * 
211
         * @author gvSIG Team
212
         * @version $Id$
213
         */
214
        private static final class ConsoleUpdateRunnable implements Runnable {
215
                private final String newLine;
216
                private final JTextArea console;
217

    
218
                /**
219
                 * Constructor.
220
                 */
221
                public ConsoleUpdateRunnable(JTextArea console, String newLine) {
222
                        this.console = console;
223
                        this.newLine = newLine;
224
                }
225

    
226
                public void run() {
227
                        console.append(newLine);
228
                        console.append("\n");
229
                }
230
        }
231
}