Statistics
| Revision:

gvsig-projects-pool / org.gvsig.winmgr / trunk / org.gvsig.winmgr.app / org.gvsig.winmgr.app.mainplugin / src / main / java / org / gvsig / coreplugin / ConsolaFrame.java @ 682

History | View | Annotate | Download (10.1 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.coreplugin;
25

    
26
import java.awt.BorderLayout;
27

    
28
import javax.swing.JEditorPane;
29
import javax.swing.JPanel;
30
import javax.swing.JScrollPane;
31
import javax.swing.JTabbedPane;
32

    
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.messages.MessageEvent;
35
import org.gvsig.andami.messages.NotificationListener;
36
import org.gvsig.andami.ui.mdiManager.IWindow;
37
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
38
import org.gvsig.andami.ui.mdiManager.WindowInfo;
39

    
40

    
41
/**
42
 * Frame que escucha los eventos del sistema de mensajes de la aplicaci?n y los
43
 * muestra.
44
 */
45
public class ConsolaFrame extends JPanel implements IWindow, SingletonWindow,NotificationListener {
46
        private StringBuffer info = new StringBuffer();
47
        private StringBuffer warn = new StringBuffer();
48
        private StringBuffer error = new StringBuffer();
49
        private StringBuffer all = new StringBuffer();
50
        private JEditorPane textoInfos;
51
        private JEditorPane textoWarnings;
52
        private JEditorPane textoErrores;
53
        private JEditorPane textoAll;
54

    
55
        private final static String marcaInfo ="font size='3' color='#000000'";
56
        private final static String marcaWarning ="font size='3' color='#800000'";
57
        private final static String marcaError ="font size='3' color='#FF0000'";
58
        private JTabbedPane tabs;
59
        private JPanel panelErrores;
60

    
61
    /**
62
     * Crea la consola con los mensajes producidos hasta el momento de la
63
     * creaci?n de este objeto.
64
     *
65
     * @param todo Array de todos los mensajes
66
     * @param infos Array de los mensajes de informaci?n
67
     * @param warns Array de los mensajes de warning
68
     * @param errors Array de los mensajes de error
69
     */
70
    public ConsolaFrame() {
71

    
72
                this.setSize(400, 325);
73

    
74
        //Las cajas de texto donde van los mensajes
75
        textoInfos = new JEditorPane();
76
        textoInfos.setEditable(false);
77
                textoInfos.setContentType("text/html");
78
        textoWarnings = new JEditorPane();
79
        textoWarnings.setEditable(false);
80
                textoWarnings.setContentType("text/html");
81
        textoErrores = new JEditorPane();
82
        textoErrores.setEditable(false);
83
                textoErrores.setContentType("text/html");
84
        textoAll = new JEditorPane();
85
        textoAll.setEditable(false);
86
                textoAll.setContentType("text/html");
87

    
88
                JScrollPane scroll = new JScrollPane(textoAll);
89
                scroll.setVerticalScrollBarPolicy(
90
                                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
91
                scroll.setHorizontalScrollBarPolicy(
92
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
93
//                scroll.setPreferredSize(new Dimension(500, 500));
94
//                scroll.setMinimumSize(new Dimension(10, 10));
95
                JPanel panelAll = new JPanel(new BorderLayout());
96
                panelAll.add(scroll, BorderLayout.CENTER);
97

    
98
                scroll = new JScrollPane(textoInfos);
99
                scroll.setVerticalScrollBarPolicy(
100
                                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
101
                scroll.setHorizontalScrollBarPolicy(
102
                        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
103
//                scroll.setPreferredSize(new Dimension(250, 145));
104
//                scroll.setMinimumSize(new Dimension(10, 10));
105
                JPanel panelInfos = new JPanel(new BorderLayout());
106
                panelInfos.add(scroll, BorderLayout.CENTER);
107

    
108
                scroll = new JScrollPane(textoWarnings);
109
                scroll.setVerticalScrollBarPolicy(
110
                                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
111
                scroll.setHorizontalScrollBarPolicy(
112
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
113
//                scroll.setPreferredSize(new Dimension(250, 145));
114
//                scroll.setMinimumSize(new Dimension(10, 10));
115
                JPanel panelWarnings = new JPanel(new BorderLayout());
116
                panelWarnings.add(scroll, BorderLayout.CENTER);
117

    
118
                scroll = new JScrollPane(textoErrores);
119
                scroll.setVerticalScrollBarPolicy(
120
                                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
121
                scroll.setHorizontalScrollBarPolicy(
122
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
123
//                scroll.setPreferredSize(new Dimension(250, 145));
124
//                scroll.setMinimumSize(new Dimension(10, 10));
125
                panelErrores = new JPanel(new BorderLayout());
126
                panelErrores.add(scroll, BorderLayout.CENTER);
127

    
128
//        rellenar(infos, textoInfos, info, marcaInfo);
129
  //      rellenar(warns, textoWarnings, warn, marcaWarning);
130
    //    rellenar(errors, textoErrores, error, marcaError);
131
      //  rellenarTodo(todo, textoAll);
132

    
133
        //Las pesta?as
134
        tabs = new JTabbedPane();
135
        tabs.addTab(PluginServices.getText( this ,"todos"), panelAll);
136
        tabs.addTab(PluginServices.getText( this ,"info"), panelInfos);
137
        tabs.addTab(PluginServices.getText( this ,"warnings"), panelWarnings);
138
        tabs.addTab(PluginServices.getText( this ,"errores"), panelErrores);
139
                this.setLayout(new BorderLayout());
140
        this.add(tabs, BorderLayout.CENTER);
141

    
142
    }
143

    
144
        /**
145
         * M?todo de utilidad para rellenar los cuadros de texto con un array de
146
         * cadenas
147
         *
148
         * @param contenido array de cadenas que se mostrar?n en el cuadro de texto
149
         *        separadas por un retorno de carro
150
         * @param control Control en el que se pondr?n las cadenas
151
         */
152
        private void rellenar(String[] contenido, JEditorPane control, StringBuffer buffer, String marca) {
153
                String content = "";
154
                for (int i = 0; i < contenido.length; i++) {
155
                        content = content + "<"+marca+">"+contenido[i] + "</"+marca+"><br/";
156
                }
157

    
158
                buffer.append(content);
159

    
160
                control.setContentType("text/html");
161
                control.setText(content);
162
        }
163

    
164
        /*
165
         * M?todo de utilidad para rellenar los cuadros de texto con un array de
166
         * cadenas
167
         *
168
         * @param contenido array de cadenas que se mostrar?n en el cuadro de texto
169
         *        separadas por un retorno de carro
170
         * @param control Control en el que se pondr?n las cadenas
171
         *
172
        private void rellenarTodo(Message[] contenido, JEditorPane control) {
173
                HashMap marcasTipoMsg = new HashMap();
174
                marcasTipoMsg.put(new Integer(Message.INFO), marcaInfo);
175
                marcasTipoMsg.put(new Integer(Message.WARNING), marcaWarning);
176
                marcasTipoMsg.put(new Integer(Message.ERROR), marcaError);
177

178
                String content = "";
179
                for (int i = 0; i < contenido.length; i++) {
180
                        String marca = (String) marcasTipoMsg.get(new Integer(contenido[i].tipo));
181
                        content = content + "<"+marca+">"+contenido[i].texto + "</"+marca+"><br/";
182
                }
183

184
                all.append(content);
185

186
                control.setContentType("text/html");
187
                control.setText(content);
188
        }*/
189

    
190

    
191
        /**
192
         * Obtiene un string con la traza de una excepci?n a la consola
193
         *
194
         * @param t Elemento throwable
195
         */
196
        private static String dumpStackTrace(Throwable t) {
197
                if ( t == null ) {
198
                        return "";
199
                }
200
                StackTraceElement[] stes = t.getStackTrace();
201
                String todo = "<"+marcaWarning+">" + t.getClass().getName() + ": " + t.getLocalizedMessage() +
202
                        "<"+marcaWarning+">" + "<br/";
203

    
204
                for (int i = 0; i < stes.length; i++) {
205
                        todo += ("<"+marcaWarning+">&nbsp;&nbsp;&nbsp;&nbsp;" + stes[i].toString() + "<br/");
206
                }
207

    
208
                if (t.getCause() != null) {
209
                        todo = todo + dumpStackTrace(t.getCause());
210
                }
211

    
212
                return todo;
213
        }
214

    
215
    private void recortarbuffers() {
216
        if( error.length()>60*1024 ) {
217
            error.delete(0, 30*1024);
218
        }
219
        if( warn.length()>60*1024 ) {
220
            warn.delete(0, 30*1024);
221
        }
222
        if( info.length()>60*1024 ) {
223
            info.delete(0, 30*1024);
224
        }
225
        if( all.length()>60*1024 ) {
226
            all.delete(0, 30*1024);
227
        }
228
    }
229
    /**
230
     * @see com.iver.mdiApp.NotificationListener#errorEvent(java.lang.String)
231
     */
232
    public void errorEvent(MessageEvent e) {
233
            for (int i = 0; i < e.getMessages().length; i++){
234
                    String traza = dumpStackTrace(e.getExceptions()[i]);
235

    
236
                        error.append("<"+marcaError+">" + e.getMessages()[i] + "</"+marcaError+"><br/" + traza);
237
                        all.append("<"+marcaError+">" + e.getMessages()[i] + "</"+marcaError+"><br/" + traza);
238
            }
239
        recortarbuffers();
240
        textoErrores.setText(error.toString());
241
        textoAll.setText(all.toString());
242
        tabs.setSelectedComponent(panelErrores);
243
    }
244

    
245
    /**
246
     * @see com.iver.mdiApp.NotificationListener#warningEvent(java.lang.String)
247
     */
248
    public void warningEvent(MessageEvent e) {
249
                for (int i = 0; i < e.getMessages().length; i++){
250
                        String traza = dumpStackTrace(e.getExceptions()[i]);
251

    
252
                        warn.append("<"+marcaWarning+">" + e.getMessages()[i] + "</"+marcaWarning+"><br/" + traza);
253
                        all.append("<"+marcaWarning+">" + e.getMessages()[i] + "</"+marcaWarning+"><br/" + traza);
254
                }
255
        recortarbuffers();
256
        textoWarnings.setText(warn.toString());
257
        textoAll.setText(all.toString());
258
    }
259

    
260
    /**
261
     * @see com.iver.mdiApp.NotificationListener#infoEvent(java.lang.String)
262
     */
263
    public void infoEvent(MessageEvent e) {
264
                for (int i = 0; i < e.getMessages().length; i++){
265
                        String traza = dumpStackTrace(e.getExceptions()[i]);
266

    
267
                        info.append("<"+marcaInfo+">" + e.getMessages()[i] + "</"+marcaInfo+"><br/" + traza);
268
                        all.append("<"+marcaInfo+">" + e.getMessages()[i] + "</"+marcaInfo+"><br/" + traza);
269
                }
270
                recortarbuffers();
271
                textoInfos.setText(info.toString() );
272
                textoAll.setText(all.toString());
273

    
274
    }
275

    
276
        /**
277
         * @see com.iver.mdiApp.ui.MDIManager.IWindow#getModel()
278
         */
279
        public Object getWindowModel() {
280
                return "consola";
281
        }
282

    
283
        /**
284
         * @see org.gvsig.andami.ui.mdiManager.IWindow#getWindowInfo()
285
         */
286
        public WindowInfo getWindowInfo() {
287
                WindowInfo info = new WindowInfo(WindowInfo.MODELESSDIALOG|WindowInfo.RESIZABLE|WindowInfo.MAXIMIZABLE|WindowInfo.ICONIFIABLE);
288
                info.setTitle(PluginServices.getText(this, "titulo_consola"));
289
                return info;
290
        }
291

    
292
        public Object getWindowProfile() {
293
                return WindowInfo.PROPERTIES_PROFILE;
294
        }
295
}