Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / branches / org.gvsig.raster.wms_dataaccess_refactoring / org.gvsig.raster.wms.app.wmsclient / src / main / java / org / gvsig / raster / wms / app / wmsclient / infobypoint / MainInfoByPointDialog.java @ 2435

History | View | Annotate | Download (10.4 KB)

1 1944 nbrodin
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22
package org.gvsig.raster.wms.app.wmsclient.infobypoint;
23
24
import java.awt.BorderLayout;
25
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.util.HashMap;
28
29
import javax.swing.ImageIcon;
30
import javax.swing.JEditorPane;
31
import javax.swing.JPanel;
32
import javax.swing.JScrollPane;
33
import javax.swing.event.HyperlinkEvent;
34
import javax.swing.event.HyperlinkListener;
35
import javax.swing.text.Document;
36
import javax.swing.text.html.HTMLEditorKit;
37
import javax.swing.text.html.StyleSheet;
38
import javax.swing.tree.DefaultMutableTreeNode;
39
import javax.swing.tree.DefaultTreeModel;
40
import javax.swing.tree.TreePath;
41
42
import org.gvsig.andami.IconThemeHelper;
43
import org.gvsig.andami.PluginServices;
44
import org.gvsig.andami.ui.mdiManager.IWindow;
45
import org.gvsig.andami.ui.mdiManager.WindowInfo;
46
import org.gvsig.app.project.documents.view.info.gui.XMLItem;
47
import org.gvsig.fmap.mapcontext.layers.FLayer;
48
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior;
49
import org.gvsig.raster.swing.RasterSwingLocator;
50
import org.gvsig.raster.swing.infobypoint.InfoByPointDataModel;
51
import org.gvsig.raster.swing.infobypoint.MainInfoByPointPanel;
52
import org.gvsig.raster.swing.pixelinspector.PixelInspector;
53
import org.gvsig.utils.xmlViewer.TextXMLContent;
54
import org.gvsig.utils.xmlViewer.XMLContent;
55
import org.gvsig.utils.xmlViewer.XMLViewer;
56
import org.slf4j.LoggerFactory;
57
import org.xml.sax.Attributes;
58
import org.xml.sax.ContentHandler;
59
import org.xml.sax.Locator;
60
import org.xml.sax.SAXException;
61
62
/**
63
 * Dialog for the information by point of a raster layer
64
 * @author Nacho Brodin (nachobrodin@gmail.com)
65
 */
66
public class MainInfoByPointDialog extends JPanel implements IWindow, HyperlinkListener {
67
        private static final long               serialVersionUID   = 1L;
68
        private MainInfoByPointPanel            mainPanel          = null;
69
        private JScrollPane                     scroll             = null;
70
        private JPanel                          wmsInfo            = null;
71
        private JEditorPane                     jeditor            = null;
72
        private XMLViewer                       dlgXML             = null;
73
        private XMLItem                         item               = null;
74
        private XMLContent                      xmlContent         = null;
75
76
        public class StringXMLItem extends TextXMLContent implements XMLItem {
77
                FLayer layer;
78
79
                public StringXMLItem(String text, FLayer lyr) {
80
                        super(text);
81
                        this.layer = lyr;
82
                }
83
84
                public FLayer getLayer(){
85
                        return this.layer;
86
                }
87
88
                public void parse(ContentHandler handler) throws SAXException {
89
                        setContentHandler(new FilterContentHandler(handler));
90
                        parse();
91
                }
92
93
                private class FilterContentHandler implements ContentHandler {
94
95
                        private ContentHandler handler;
96
97
                        public FilterContentHandler(ContentHandler handler) {
98
                                this.handler = handler;
99
                        }
100
101
                        public void setDocumentLocator(Locator arg0) {
102
                        }
103
104
                        public void startDocument() throws SAXException {
105
                        }
106
107
                        public void endDocument() throws SAXException {
108
                        }
109
110
                        public void startPrefixMapping(String arg0, String arg1)
111
                                        throws SAXException {
112
                        }
113
114
                        public void endPrefixMapping(String arg0) throws SAXException {
115
                        }
116
117
                        public void startElement(String arg0, String arg1, String arg2,
118
                                        Attributes arg3) throws SAXException {
119
                                handler.startElement(arg0, arg1, arg2, arg3);
120
                        }
121
122
                        public void endElement(String arg0, String arg1, String arg2)
123
                                        throws SAXException {
124
                                handler.endElement(arg0, arg1, arg2);
125
                        }
126
127
                        public void characters(char[] arg0, int arg1, int arg2)
128
                                        throws SAXException {
129
                                handler.characters(arg0, arg1, arg2);
130
                        }
131
132
                        public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
133
                                        throws SAXException {
134
                        }
135
136
                        public void processingInstruction(String arg0, String arg1)
137
                                        throws SAXException {
138
                        }
139
140
                        public void skippedEntity(String arg0) throws SAXException {
141
                        }
142
                }
143
        }
144
145
        public MainInfoByPointDialog() {
146
                setLayout(new BorderLayout());
147
                add((Component)getMainPanel(), BorderLayout.WEST);
148
                add((Component)getWMSInfoPanel(), BorderLayout.CENTER);
149
        }
150
151
        public MainInfoByPointPanel getMainPanel() {
152
                if(mainPanel == null) {
153
                        HashMap<String, String> translations = new HashMap<String, String>();
154
                        translations.put("info", PluginServices.getText(this, "info"));
155
                        translations.put("view", PluginServices.getText(this, "view"));
156
                        translations.put("layer_list", PluginServices.getText(this, "layer_list"));
157
                        translations.put("band_values", PluginServices.getText(this, "band_values"));
158
                        translations.put("pixel_point", PluginServices.getText(this, "pixel_point"));
159
                        translations.put("view_point", PluginServices.getText(this, "view_point"));
160
                        translations.put("world_point", PluginServices.getText(this, "world_point"));
161
                        translations.put("bands", PluginServices.getText(this, "bands"));
162
                        translations.put("colors", PluginServices.getText(this, "colors"));
163
                        translations.put("coords", PluginServices.getText(this, "coords"));
164
                        translations.put("lat", PluginServices.getText(this, "lat"));
165
                        translations.put("long", PluginServices.getText(this, "long"));
166
                        translations.put("red", PluginServices.getText(this, "red"));
167
                        translations.put("green", PluginServices.getText(this, "green"));
168
                        translations.put("blue", PluginServices.getText(this, "blue"));
169
                        HashMap<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
170
                        icons.put("forward-icon", IconThemeHelper.getImageIcon("forward-icon"));
171
                        icons.put("backward-icon", IconThemeHelper.getImageIcon("backward-icon"));
172
                        mainPanel = RasterSwingLocator.getSwingManager().createInfoByPointPanel(
173
                                        translations, icons, null);
174
                        ((JPanel)mainPanel).setPreferredSize(new Dimension(230, 0));
175
                }
176
                return mainPanel;
177
        }
178
179
        private JPanel getWMSInfoPanel() {
180
                if(wmsInfo == null) {
181
                        wmsInfo = new JPanel();
182
                        wmsInfo.setLayout(new BorderLayout());
183
                        jeditor = new JEditorPane();
184
                        scroll = new JScrollPane(jeditor);
185
                        jeditor.addHyperlinkListener(this);
186
                        jeditor.setEditable(false);
187
                        wmsInfo.add(scroll, BorderLayout.CENTER);
188
                }
189
                return wmsInfo;
190
        }
191
192
        private JPanel getWMSXMLInfoPanel(String text) throws SAXException {
193
                if(wmsInfo == null) {
194
                        wmsInfo = new JPanel();
195
                        wmsInfo.setLayout(new BorderLayout());
196
                        dlgXML = new XMLViewer();
197
                        scroll = new JScrollPane(dlgXML);
198
                        item = new StringXMLItem(text, null);
199
                        xmlContent = new XMLContent() {
200
                                private ContentHandler handler;
201
202
                                public void setContentHandler(ContentHandler arg0) {
203
                                        handler = arg0;
204
                                }
205
206
                                public void parse() throws SAXException {
207
                                        handler.startDocument();
208
                                        item.parse( handler);
209
                                        handler.endDocument();
210
                                }
211
                        };
212
                        dlgXML.setNamesColumn(new String[0]);
213
                        dlgXML.setModel(xmlContent);
214
                        dlgXML.getXmlTree().setRootVisible(false);
215
                        DefaultTreeModel treeModel = (DefaultTreeModel)dlgXML.getXmlTree().getModel();
216
                        DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML.getXmlTree().getModel().getRoot();
217
                        DefaultMutableTreeNode n = root.getFirstLeaf();
218
                        TreePath path = new TreePath(treeModel.getPathToRoot(n));
219
                        dlgXML.getXmlTree().expandPath(path);
220
                        //dlgXML.getXmlTree().setSelectionPath(path);
221
222
                        wmsInfo.add(scroll, BorderLayout.CENTER);
223
                }
224
                return wmsInfo;
225
        }
226
227
        public void setWMSInfoText(String text, String type) {
228
                if(type.equals("text/html")) {
229
                        if(jeditor == null) {
230
                                wmsInfo = null;
231
                                dlgXML = null;
232
                                remove(1);
233
                                add((Component)getWMSInfoPanel(), BorderLayout.CENTER);
234
                        }
235
                        HTMLEditorKit kit = new HTMLEditorKit();
236
                        jeditor.setEditorKit(kit);
237
238
                        text = removeLabelBlock(text, "style");
239
                        text = removeLabelBlock(text, "meta");
240
241
                        StyleSheet s = kit.getStyleSheet();
242
                        s.addRule("body {font-family:verdana; margin: 15px; }");
243
                        s.addRule("caption {background-color:#FBFFE1;font-style:normal;color:black}");
244
                        s.addRule("th {background-color:#D6D6D6;color:white;}");
245
                        s.addRule("td {background-color:#FEEDD6;color:black;}");
246
                        Document doc = kit.createDefaultDocument();
247
                        jeditor.setDocument(doc);
248
                        jeditor.setContentType(type);
249
                        jeditor.setText(text);
250
                } else if(type.equals("text/xml")) {
251
                        try {
252
                                if(dlgXML == null) {
253
                                        wmsInfo = null;
254
                                        jeditor = null;
255
                                        remove(1);
256
                                        add((Component)getWMSXMLInfoPanel(text), BorderLayout.CENTER);
257
                                } else {
258
                                        item = new StringXMLItem(text, null);
259
                                        dlgXML.setModel(xmlContent);
260
                                }
261
                        } catch (SAXException e) {
262
                                LoggerFactory.getLogger(MainInfoByPointDialog.class).debug("", e);
263
                        }
264
                }
265
        }
266
267
        /**
268
         * Removes a label from the text
269
         * @param text
270
         * @param label
271
         * @return
272
         */
273
        private String removeLabelBlock(String text, String label) {
274
                if(text.contains("<" + label) && text.contains("/" + label + ">")) {
275
                        int initIndex = text.indexOf("<" + label);
276
                        int endIndex = text.indexOf("/" + label + ">");
277
                        return text.substring(0, initIndex) + text.substring(endIndex + 7, text.length());
278
                }
279
                return text;
280
        }
281
282
        public WindowInfo getWindowInfo() {
283
                WindowInfo m_viewinfo = new WindowInfo(WindowInfo.MODELESSDIALOG | WindowInfo.RESIZABLE);
284
                m_viewinfo.setTitle(PluginServices.getText(this, "WMS InfoByPoint"));
285
                m_viewinfo.setHeight(380);
286
                m_viewinfo.setWidth(700);
287
                return m_viewinfo;
288
        }
289
290
        public void windowClosed() {
291
                CompoundBehavior.setAllControlsBehavior(InfoByWMSPointExtension.oldBehavior);
292
        }
293
294
        public Object getWindowProfile() {
295
                return WindowInfo.PROPERTIES_PROFILE;
296
        }
297
298
        public InfoByPointDataModel getInfoByPointDataModel() {
299
                return getMainPanel().getInfoByPointDataModel();
300
        }
301
302
        public PixelInspector getPixelInspector() {
303
                return getMainPanel().getPixelInspectorPanel();
304
        }
305
306
        public void hyperlinkUpdate(HyperlinkEvent e) {
307
                if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
308 2435 nbrodin
                        new BrowserControl().displayURL(e.getURL().toString());
309 1944 nbrodin
                }
310
        }
311
}