Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.impl / src / main / java / org / gvsig / export / swing / impl / MessagePanel.java @ 43925

History | View | Annotate | Download (6.69 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.export.swing.impl;
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.i18n.I18nManager;
47
import org.gvsig.tools.service.ServiceException;
48

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

    
54
public class MessagePanel extends JPanel {
55

    
56
    private static final Logger logger = LoggerFactory.getLogger(MessagePanel.class);
57

    
58
    private static final long serialVersionUID = 2579894035021915221L;
59

    
60
    private JButton accept = null;
61
    private JTextPane text = null;
62

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

    
70
    public static void showMessage(String title,
71
            String header, Exception ex, Feature feature) {
72
        showMessage(title, header, getMessageAsHTML(ex), feature);
73
    }
74

    
75
    private static class ExceptionIterator implements Iterator {
76

    
77
        Throwable exception;
78

    
79
        ExceptionIterator(Throwable exception) {
80
            this.exception = exception;
81
        }
82

    
83
        @Override
84
        public boolean hasNext() {
85
            return this.exception != null;
86
        }
87

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

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

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

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

    
135
    @SuppressWarnings("OverridableMethodCallInConstructor")
136
    public MessagePanel(final String header, final String html, Feature feature) {
137
        I18nManager i18nManager = ToolsLocator.getI18nManager();
138

    
139
        this.setLayout(new BorderLayout());
140
        this.setPreferredSize(new Dimension(500, 300));
141
        this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
142

    
143
        JLabel headerlabel = new JLabel();
144
        headerlabel.setText(header);
145

    
146
        JTabbedPane tabs = new JTabbedPane();
147

    
148
        text = new JTextPane();
149
        text.setContentType("text/html");
150
        text.setEditable(false);
151
        text.setText(html);
152
        text.setCaretPosition(0);
153

    
154
        JScrollPane scrollPane = new JScrollPane(text);
155
        scrollPane.setPreferredSize(new Dimension(500, 220));
156

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

    
169
        accept = new JButton(i18nManager.getTranslation("_Accept"));
170
        accept.addActionListener(new ActionListener() {
171
            @Override
172
            public void actionPerformed(ActionEvent arg0) {
173
                setVisible(false);
174
            }
175
        });
176

    
177
        JPanel buttonsPanel = new JPanel();
178
        buttonsPanel.setLayout(new BoxLayout(buttonsPanel,
179
                BoxLayout.LINE_AXIS));
180
        buttonsPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
181

    
182
        buttonsPanel.add(Box.createHorizontalGlue());
183
        buttonsPanel.add(accept);
184

    
185
        this.add(headerlabel, BorderLayout.NORTH);
186
        this.add(tabs, BorderLayout.CENTER);
187
        this.add(buttonsPanel, BorderLayout.SOUTH);
188
        this.setVisible(true);
189
    }
190

    
191
}