Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / dialogs / InfoToolViewer.java @ 6797

History | View | Annotate | Download (8.34 KB)

1 6117 jaume
package com.iver.cit.gvsig.gui.dialogs;
2
3
import java.awt.Dimension;
4 6797 ldiaz
import java.awt.event.ComponentEvent;
5
import java.awt.event.ComponentListener;
6 6117 jaume
import java.util.Vector;
7
import javax.swing.JList;
8
import javax.swing.JPanel;
9
import javax.swing.event.ListSelectionListener;
10
import javax.swing.tree.DefaultMutableTreeNode;
11
import javax.swing.tree.DefaultTreeModel;
12
import javax.swing.tree.TreePath;
13
import org.xml.sax.ContentHandler;
14
import org.xml.sax.SAXException;
15
16
import com.iver.cit.gvsig.fmap.layers.FLayer;
17
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
18
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
19
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
20
import com.iver.cit.gvsig.gui.dialogs.FInfoDialogXML;
21
import com.iver.cit.gvsig.gui.wizards.FormatListModel;
22
import com.iver.utiles.xmlViewer.XMLContent;
23
24
/**
25
 * This is the generic Feature Info Viewer
26
 *
27
 * If the feature Info comes from a special layer which has registered
28
 * the way to visualize itself, adds a panel that the layer should provide
29
 * otherwise this viewer will add a panel to visualize HTML or a special
30
 * viewer to show XML.
31
 *
32
 * @author laura
33
 *
34
 */
35
public class InfoToolViewer extends JPanel {
36
37
    private javax.swing.JScrollPane jScrollPane = null;
38
    private JList layerList = null;
39
    private javax.swing.JSplitPane jSplitPane1 = null;
40
    private javax.swing.JPanel layerListPanel = null;
41
        private JPanel infoViewerPanel;
42
        private XMLItem[] m_layers;
43 6797 ldiaz
        IInfoToolPanel infoPanel = null;
44 6117 jaume
45
    /**
46
     * This is the default constructor
47
     */
48
    public InfoToolViewer() {
49
        super();
50
        initialize();
51 6797 ldiaz
        this.addComponentListener(new componentListener());
52 6117 jaume
    }
53
54
    public InfoToolViewer(XMLItem[] layers) {
55
            super();
56
            initialize();
57 6332 jmvivo
            setLayers(layers);
58 6117 jaume
    }
59
60 6340 jmvivo
    public void setLayers(XMLItem[] layers){
61 6117 jaume
            m_layers = layers;
62 6340 jmvivo
            initilizeLayerListModel();
63 6332 jmvivo
            updateViewer(0);
64
            layerList.setSelectedIndex(0);
65 6117 jaume
    }
66
67
68
    /**
69
     * This method initializes this
70
     */
71
    private void initialize() {
72
        this.setLayout(new java.awt.BorderLayout());
73
        this.add(getJSplitPane1(), java.awt.BorderLayout.CENTER);
74
        this.setSize(600, 600);
75
        this.setPreferredSize(new Dimension(600, 600));
76
    }
77
78
    /**
79
     * This method initializes jScrollPane
80
     *
81
     * @return javax.swing.JScrollPane
82
     */
83
    private javax.swing.JScrollPane getJScrollPane() {
84
        if (jScrollPane == null) {
85
            jScrollPane = new javax.swing.JScrollPane();
86
            jScrollPane.setSize(new Dimension(600,600));
87
            jScrollPane.setPreferredSize( new Dimension(600,600));
88
            jScrollPane.setViewportView(getLayerListPanel());
89
        }
90
91
        return jScrollPane;
92
    }
93 6340 jmvivo
94
    private void initilizeLayerListModel() {
95
        Vector layerNames = new Vector();
96
        if (m_layers != null)
97
        {
98
                for (int i = 0; i < m_layers.length; i++)
99
                {
100
                        layerNames.add(m_layers[i].getLayer().getName());
101
                }
102
        }
103
104
        FormatListModel model = new FormatListModel((String[])layerNames.toArray(new String[0]));
105
        getJList().setModel(model);
106
    }
107 6117 jaume
108
    public JList getJList(){
109
110
          if (layerList == null) {
111 6340 jmvivo
                  layerList = new JList();
112
                  initilizeLayerListModel();
113
              layerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
114 6117 jaume
          }
115
116
      layerList.addListSelectionListener(new ListSelectionListener() {
117
                                public void valueChanged(javax.swing.event.ListSelectionEvent e) {
118
                                  int changedIdx = layerList.getSelectedIndex();//e.getFirstIndex();
119
120
                                  if (changedIdx == -1) return;
121
                                  updateViewer(changedIdx);
122
                   }
123
          });
124
      return layerList;
125
}
126
    /**
127
     * This method initializes jSplitPane1
128
     *
129
     * @return javax.swing.JSplitPane
130
     */
131
    private javax.swing.JSplitPane getJSplitPane1() {
132
        if (jSplitPane1 == null) {
133
            jSplitPane1 = new javax.swing.JSplitPane();
134
            jSplitPane1.setLeftComponent(getJScrollPane());
135
            jSplitPane1.setRightComponent(getInfoViewerPanel());
136
            jSplitPane1.setDividerSize(4);
137
            jSplitPane1.setDividerLocation(100);
138
            jSplitPane1.setSize( new Dimension(600,600));
139
            jSplitPane1.setPreferredSize( new Dimension(600,600));
140
        }
141
        return jSplitPane1;
142
    }
143
144
    /**
145
     * This method initializes jPanel
146
     *
147
     * @return javax.swing.JPanel
148
     */
149
    private javax.swing.JPanel getLayerListPanel() {
150
        if (layerListPanel == null) {
151
            layerListPanel = new javax.swing.JPanel();
152
            layerListPanel.setLayout(new java.awt.BorderLayout());
153
            layerListPanel.add(getJList(), java.awt.BorderLayout.CENTER);
154
        }
155
156
        return layerListPanel;
157
    }
158
159
    /**
160
     * This method initializes jPanel1
161
     *
162
     * @return javax.swing.JPanel
163
     */
164
    private javax.swing.JPanel getInfoViewerPanel() {
165
        if (infoViewerPanel == null) {
166
            infoViewerPanel = new javax.swing.JPanel();
167
            infoViewerPanel.setLayout(new java.awt.BorderLayout());
168
        }
169
        validate();
170
        return infoViewerPanel;
171
    }
172
173
    /**
174
     * updates the layer to display
175
     *
176
     */
177
    private void updateViewer(int changedIdx)
178
    {
179 6340 jmvivo
            if((m_layers == null) || (m_layers.length == 0)) return;
180 6117 jaume
                  final XMLItem item = m_layers[changedIdx];
181
                  FLayer layer = item.getLayer();
182
183
                  if (layer instanceof InfoByPoint){
184
                          FLyrDefault defaultLayer = (FLyrDefault)layer;
185
                          if (defaultLayer.getProperty("customPanel") != null){
186
187
                                  Class c = (Class)defaultLayer.getProperty("customPanel");
188 6797 ldiaz
189 6117 jaume
                                try {
190
                                        infoPanel = (IInfoToolPanel)c.newInstance();
191
                                } catch (InstantiationException e1) {
192
                                        e1.printStackTrace();
193
                                } catch (IllegalAccessException e1) {
194
                                        e1.printStackTrace();
195
                                }
196 6797 ldiaz
197 6117 jaume
                          infoViewerPanel.removeAll();
198
                          infoViewerPanel.add((JPanel)infoPanel);
199 6797 ldiaz
                          infoPanel.show(item.toString());
200
201 6117 jaume
                          infoViewerPanel.setVisible( true );
202
                          revalidate();
203
204
                          }
205
                          else {
206
207 6504 ldiaz
                                  if (item.toString().toLowerCase().endsWith( "</html>"))//if (item.toString().toLowerCase().startsWith( "<html>"))
208 6117 jaume
                                  {
209 6504 ldiaz
                                          //skip the header info
210 6117 jaume
                                          IInfoToolPanel htmlPanel = new HTMLInfoToolPanel();
211 6797 ldiaz
212 6504 ldiaz
                                          int idx = item.toString().toLowerCase().indexOf("<html");
213
                                          if (idx != -1){
214
                                                  htmlPanel.show(item.toString().substring(idx));
215
                                          }else{
216
                                                  htmlPanel.show(item.toString());
217
                                          }
218 6117 jaume
                                          infoViewerPanel.removeAll();
219
                                          infoViewerPanel.add((JPanel)htmlPanel);
220
                                          infoViewerPanel.setVisible( true );
221
                                          revalidate();
222
                                  }
223
                                  else
224
                                  {
225
                                          FInfoDialogXML dlgXML = new FInfoDialogXML();
226
                                                try {
227
                                                        dlgXML.setModel(new XMLContent() {
228
                                                                private ContentHandler handler;
229
230
                                                                public void setContentHandler(ContentHandler arg0) {
231
                                                                        handler = arg0;
232
                                                                }
233
234
                                                                public void parse() throws SAXException {
235
                                                                        handler.startDocument();
236
                                                                        item.parse( handler);
237
                                                                        handler.endDocument();
238
                                                                }
239
                                                        });
240
                                                } catch (SAXException e1) {
241
                                                        // TODO Auto-generated catch block
242
                                                        e1.printStackTrace();
243
                                                }
244
                                                dlgXML.getXmlTree().setRootVisible(false);
245
                                                DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
246
                                                                .getXmlTree().getModel();
247
                                                DefaultMutableTreeNode n;
248
                                                DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
249
                                                                .getXmlTree().getModel().getRoot();
250
                                                n = root.getFirstLeaf();
251
                                                TreePath path = new TreePath(treeModel.getPathToRoot(n));
252
                                                dlgXML.getXmlTree().expandPath(path);
253
                                                dlgXML.getXmlTree().setSelectionPath(path);
254
                                                //dlgXML.
255
256
                                                infoViewerPanel.removeAll();
257
                                                infoViewerPanel.add(dlgXML);
258
                                                infoViewerPanel.setVisible( true );
259
                                                this.validate();
260
                                            this.doLayout();
261
                                  }
262
                          }
263
                  }
264
    }
265 6797 ldiaz
266
    class componentListener implements ComponentListener{
267
268
                public void componentHidden(ComponentEvent e) {
269
                        // TODO Auto-generated method stub
270
271
                }
272
273
                public void componentMoved(ComponentEvent e) {
274
                        // TODO Auto-generated method stub
275
276
                }
277
278
                public void componentResized(ComponentEvent e) {
279
280
                        //if (e.getComponent() == )
281
                        if (infoPanel != null){
282
                                infoPanel.refreshSize();
283
                        }
284
285
286
                }
287
288
                public void componentShown(ComponentEvent e) {
289
                        // TODO Auto-generated method stub
290
291
                }
292
293
    }
294 6117 jaume
}