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 @ 43634
History | View | Annotate | Download (6.5 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 41488 | jjdelcerro | * 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 | 40558 | jjdelcerro | *
|
11 | 41488 | jjdelcerro | * 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 | 40558 | jjdelcerro | *
|
16 | 41488 | jjdelcerro | * 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 | 40558 | jjdelcerro | *
|
20 | 41488 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40558 | jjdelcerro | */
|
23 | 41603 | jjdelcerro | package org.gvsig.app.gui; |
24 | 40435 | jjdelcerro | |
25 | import java.awt.BorderLayout; |
||
26 | import java.awt.Dimension; |
||
27 | import java.awt.event.ActionEvent; |
||
28 | import java.awt.event.ActionListener; |
||
29 | 41488 | jjdelcerro | import java.net.UnknownHostException; |
30 | import java.util.Iterator; |
||
31 | 40435 | jjdelcerro | |
32 | import javax.swing.BorderFactory; |
||
33 | import javax.swing.Box; |
||
34 | import javax.swing.BoxLayout; |
||
35 | import javax.swing.JButton; |
||
36 | 41438 | jjdelcerro | import javax.swing.JLabel; |
37 | 40435 | jjdelcerro | import javax.swing.JPanel; |
38 | import javax.swing.JScrollPane; |
||
39 | 41438 | jjdelcerro | import javax.swing.JTabbedPane; |
40 | 40435 | jjdelcerro | import javax.swing.JTextPane; |
41 | 41438 | jjdelcerro | 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 | 41488 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
47 | import org.gvsig.tools.exception.BaseRuntimeException; |
||
48 | 41438 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
49 | import org.gvsig.tools.service.ServiceException; |
||
50 | 40435 | jjdelcerro | |
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 | 41438 | jjdelcerro | public class MessagePanel extends JPanel { |
57 | 40435 | jjdelcerro | |
58 | 41438 | jjdelcerro | private static Logger logger = LoggerFactory.getLogger(MessagePanel.class); |
59 | 40435 | jjdelcerro | |
60 | 41438 | jjdelcerro | private static final long serialVersionUID = 2579894035021915221L; |
61 | 40435 | jjdelcerro | |
62 | 41438 | jjdelcerro | private JButton accept = null; |
63 | private JTextPane text = null; |
||
64 | 40435 | jjdelcerro | |
65 | 41438 | jjdelcerro | public static void showMessage(String title, |
66 | 41603 | jjdelcerro | String header, String html) { |
67 | JPanel panel = new MessagePanel(header, html, null); |
||
68 | 41438 | jjdelcerro | WindowManager wm = ToolsSwingLocator.getWindowManager(); |
69 | wm.showWindow(panel, title, WindowManager.MODE.DIALOG); |
||
70 | } |
||
71 | 40435 | jjdelcerro | |
72 | 41488 | jjdelcerro | public static void showMessage(String title, |
73 | 41603 | jjdelcerro | String header, Exception ex) { |
74 | showMessage(title, header, getMessageAsHTML(ex)); |
||
75 | 41488 | jjdelcerro | } |
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 | 41603 | jjdelcerro | if (message != null && !(message.startsWith("Exception ") |
114 | || message.startsWith("Error ")) ) {
|
||
115 | 41488 | jjdelcerro | 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 | 41438 | jjdelcerro | public MessagePanel(final String header, final String html, Feature feature) { |
132 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
133 | 40435 | jjdelcerro | |
134 | 41438 | jjdelcerro | this.setLayout(new BorderLayout()); |
135 | this.setPreferredSize(new Dimension(500, 300)); |
||
136 | this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); |
||
137 | 40435 | jjdelcerro | |
138 | 41438 | jjdelcerro | JLabel headerlabel = new JLabel(); |
139 | headerlabel.setText(header); |
||
140 | 40435 | jjdelcerro | |
141 | 41438 | jjdelcerro | JTabbedPane tabs = new JTabbedPane(); |
142 | 40435 | jjdelcerro | |
143 | 41438 | jjdelcerro | text = new JTextPane(); |
144 | text.setContentType("text/html");
|
||
145 | text.setEditable(false);
|
||
146 | text.setText(html); |
||
147 | text.setCaretPosition(0);
|
||
148 | 40435 | jjdelcerro | |
149 | 41438 | jjdelcerro | JScrollPane scrollPane = new JScrollPane(text); |
150 | scrollPane.setPreferredSize(new Dimension(500, 220)); |
||
151 | 40435 | jjdelcerro | |
152 | 41438 | jjdelcerro | tabs.addTab(i18nManager.getTranslation("_Problem"), scrollPane);
|
153 | 41488 | jjdelcerro | if (feature != null) { |
154 | 41438 | jjdelcerro | 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 | 40435 | jjdelcerro | |
164 | 41438 | jjdelcerro | 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 | 40435 | jjdelcerro | } |