Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / MessagePanel.java @ 41603

History | View | Annotate | Download (6.5 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.gui;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.Dimension;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.net.UnknownHostException;
30
import java.util.Iterator;
31

    
32
import javax.swing.BorderFactory;
33
import javax.swing.Box;
34
import javax.swing.BoxLayout;
35
import javax.swing.JButton;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38
import javax.swing.JScrollPane;
39
import javax.swing.JTabbedPane;
40
import javax.swing.JTextPane;
41
import org.gvsig.fmap.dal.feature.Feature;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dynform.DynFormLocator;
44
import org.gvsig.tools.dynform.JDynForm;
45
import org.gvsig.tools.dynobject.DynObject;
46
import org.gvsig.tools.exception.BaseException;
47
import org.gvsig.tools.exception.BaseRuntimeException;
48
import org.gvsig.tools.i18n.I18nManager;
49
import org.gvsig.tools.service.ServiceException;
50

    
51
import org.gvsig.tools.swing.api.ToolsSwingLocator;
52
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56
public class MessagePanel extends JPanel {
57

    
58
    private static Logger logger = LoggerFactory.getLogger(MessagePanel.class);
59

    
60
    private static final long serialVersionUID = 2579894035021915221L;
61

    
62
    private JButton accept = null;
63
    private JTextPane text = null;
64

    
65
    public static void showMessage(String title,
66
            String header, String html) {
67
        JPanel panel = new MessagePanel(header, html, null);
68
        WindowManager wm = ToolsSwingLocator.getWindowManager();
69
        wm.showWindow(panel, title, WindowManager.MODE.DIALOG);
70
    }
71

    
72
    public static void showMessage(String title,
73
            String header, Exception ex) {
74
        showMessage(title, header, getMessageAsHTML(ex));
75
    }
76

    
77
    private static class ExceptionIterator implements Iterator {
78

    
79
        Throwable exception;
80

    
81
        ExceptionIterator(Throwable exception) {
82
            this.exception = exception;
83
        }
84

    
85
        public boolean hasNext() {
86
            return this.exception != null;
87
        }
88

    
89
        public Object next() {
90
            Throwable exception = this.exception;
91
            this.exception = exception.getCause();
92
            return exception;
93
        }
94

    
95
        public void remove() {
96
            throw new UnsupportedOperationException();
97
        }
98
    }
99

    
100
    private static String getMessageAsHTML(Exception ex) {
101
        Iterator exceptions = new ExceptionIterator(ex);
102
        StringBuilder html = new StringBuilder();
103

    
104
        html.append("<ul>\n");
105
        while (exceptions.hasNext()) {
106
            Throwable ex1 = ((Throwable) exceptions.next());
107
            String message = null;
108
            if (ex1 instanceof UnknownHostException) {
109
                message = "Unknown Host " + ex1.getMessage();
110
            } else {
111
                message = ex1.getMessage();
112
            }
113
            if (message != null && !(message.startsWith("Exception ") 
114
                    || message.startsWith("Error ")) ) {
115
                html.append("<li>");
116
                if (message.toLowerCase().contains(" create ")) {
117
                    message = message.replaceFirst(" create ", "<br>\nCREATE ");
118
                    message = message.replaceAll(",", ",<br>\n");
119
                } else if (message.toLowerCase().contains(" insert ")) {
120
                    message = message.replaceFirst(" insert ", "<br>\nINSERT ");
121
                    message = message.replaceAll(",", ",<br>\n");
122
                }
123
                html.append(message);
124
                html.append("</li>\n");
125
            }
126
        }
127
        html.append("</ul>\n");
128
        return html.toString();
129
    }
130

    
131
    public MessagePanel(final String header, final String html, Feature feature) {
132
        I18nManager i18nManager = ToolsLocator.getI18nManager();
133

    
134
        this.setLayout(new BorderLayout());
135
        this.setPreferredSize(new Dimension(500, 300));
136
        this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
137

    
138
        JLabel headerlabel = new JLabel();
139
        headerlabel.setText(header);
140

    
141
        JTabbedPane tabs = new JTabbedPane();
142

    
143
        text = new JTextPane();
144
        text.setContentType("text/html");
145
        text.setEditable(false);
146
        text.setText(html);
147
        text.setCaretPosition(0);
148

    
149
        JScrollPane scrollPane = new JScrollPane(text);
150
        scrollPane.setPreferredSize(new Dimension(500, 220));
151

    
152
        tabs.addTab(i18nManager.getTranslation("_Problem"), scrollPane);
153
        if (feature != null) {
154
            try {
155
                DynObject data = feature.getAsDynObject();
156
                JDynForm form = DynFormLocator.getDynFormManager().createJDynForm(data);
157
                form.setLayoutMode(form.USE_TABS);
158
                tabs.addTab(i18nManager.getTranslation("_Row_values"), form.asJComponent());
159
            } catch (ServiceException ex) {
160
                logger.warn("Can't show feature associated with the problem.", ex);
161
            }
162
        }
163

    
164
        accept = new JButton(i18nManager.getTranslation("_Accept"));
165
        accept.addActionListener(new ActionListener() {
166
            public void actionPerformed(ActionEvent arg0) {
167
                setVisible(false);
168
            }
169
        });
170

    
171
        JPanel buttonsPanel = new JPanel();
172
        buttonsPanel.setLayout(new BoxLayout(buttonsPanel,
173
                BoxLayout.LINE_AXIS));
174
        buttonsPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
175

    
176
        buttonsPanel.add(Box.createHorizontalGlue());
177
        buttonsPanel.add(accept);
178

    
179
        this.add(headerlabel, BorderLayout.NORTH);
180
        this.add(tabs, BorderLayout.CENTER);
181
        this.add(buttonsPanel, BorderLayout.SOUTH);
182
        this.setVisible(true);
183
    }
184

    
185
}