Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / info / gui / InfoToolViewer.java @ 10626

History | View | Annotate | Download (9.02 KB)

1
package com.iver.cit.gvsig.project.documents.view.info.gui;
2

    
3
import java.awt.Dimension;
4
import java.awt.event.ComponentEvent;
5
import java.awt.event.ComponentListener;
6
import java.util.Vector;
7

    
8
import javax.swing.JList;
9
import javax.swing.JPanel;
10
import javax.swing.event.ListSelectionListener;
11
import javax.swing.tree.DefaultMutableTreeNode;
12
import javax.swing.tree.DefaultTreeModel;
13
import javax.swing.tree.TreePath;
14

    
15
import org.xml.sax.ContentHandler;
16
import org.xml.sax.SAXException;
17

    
18
import com.iver.cit.gvsig.fmap.layers.FLayer;
19
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
20
import com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint;
21
import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem;
22
import com.iver.cit.gvsig.gui.wizards.FormatListModel;
23
import com.iver.utiles.xmlViewer.XMLContent;
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
        public static Class XULPanelClass = null;
38
        
39
    private javax.swing.JScrollPane jScrollPane = null;
40
    private JList layerList = null;
41
    private javax.swing.JSplitPane jSplitPane1 = null;
42
    private javax.swing.JPanel layerListPanel = null;
43
        private JPanel infoViewerPanel;        
44
        private XMLItem[] m_layers;
45
        IInfoToolPanel infoPanel = null;
46

    
47
    /**
48
     * This is the default constructor
49
     */
50
    public InfoToolViewer() {
51
        super();
52
        initialize();
53
        this.addComponentListener(new componentListener());
54
    }
55
    
56
    public InfoToolViewer(XMLItem[] layers) {
57
            super();
58
            initialize();
59
            setLayers(layers);
60
    }
61
    
62
    public void setLayers(XMLItem[] layers){            
63
            m_layers = layers;
64
            initilizeLayerListModel();
65
            updateViewer(0);
66
            // layerList.setSelectedIndex(0);
67
    }
68

    
69

    
70
    /**
71
     * This method initializes this
72
     */
73
    private void initialize() {
74
        this.setLayout(new java.awt.BorderLayout());
75
        this.add(getJSplitPane1(), java.awt.BorderLayout.CENTER);
76
        this.setSize(600, 600);
77
        this.setPreferredSize(new Dimension(600, 600));
78
    }
79

    
80
    /**
81
     * This method initializes jScrollPane
82
     *
83
     * @return javax.swing.JScrollPane
84
     */
85
    private javax.swing.JScrollPane getJScrollPane() {
86
        if (jScrollPane == null) {
87
            jScrollPane = new javax.swing.JScrollPane();
88
            jScrollPane.setSize(new Dimension(600,600));
89
            jScrollPane.setPreferredSize( new Dimension(600,600));
90
            jScrollPane.setViewportView(getLayerListPanel());
91
        }
92

    
93
        return jScrollPane;
94
    }
95
    
96
    private void initilizeLayerListModel() {
97
        Vector layerNames = new Vector();
98
        if (m_layers != null)
99
        {
100
                for (int i = 0; i < m_layers.length; i++)
101
                {
102
                        layerNames.add(m_layers[i].getLayer().getName());
103
                }                
104
        }         
105
        
106
        FormatListModel model = new FormatListModel((String[])layerNames.toArray(new String[0])); 
107
        getJList().setModel(model);            
108
    }
109

    
110
    public JList getJList(){
111

    
112
          if (layerList == null) {
113
                  layerList = new JList();
114
                  initilizeLayerListModel();
115
              layerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
116
          }
117
  
118
      layerList.addListSelectionListener(new ListSelectionListener() { 
119
                                public void valueChanged(javax.swing.event.ListSelectionEvent e) {
120
                                  int changedIdx = layerList.getSelectedIndex();//e.getFirstIndex();
121
                                 
122
                                  if (changedIdx == -1) return;
123
                                  updateViewer(changedIdx);
124
                   }
125
          });    
126
      return layerList;
127
}
128
    /**
129
     * This method initializes jSplitPane1
130
     *
131
     * @return javax.swing.JSplitPane
132
     */
133
    private javax.swing.JSplitPane getJSplitPane1() {
134
        if (jSplitPane1 == null) {
135
            jSplitPane1 = new javax.swing.JSplitPane();
136
            jSplitPane1.setLeftComponent(getJScrollPane());
137
            jSplitPane1.setRightComponent(getInfoViewerPanel());
138
            jSplitPane1.setDividerSize(4);
139
            jSplitPane1.setDividerLocation(100);
140
            jSplitPane1.setSize( new Dimension(600,600));
141
            jSplitPane1.setPreferredSize( new Dimension(600,600));
142
        }
143
        return jSplitPane1;
144
    }
145

    
146
    /**
147
     * This method initializes jPanel
148
     *
149
     * @return javax.swing.JPanel
150
     */
151
    private javax.swing.JPanel getLayerListPanel() {
152
        if (layerListPanel == null) {
153
            layerListPanel = new javax.swing.JPanel();
154
            layerListPanel.setLayout(new java.awt.BorderLayout());
155
            layerListPanel.add(getJList(), java.awt.BorderLayout.CENTER);
156
        }
157

    
158
        return layerListPanel;
159
    }
160

    
161
    /**
162
     * This method initializes jPanel1
163
     *
164
     * @return javax.swing.JPanel
165
     */
166
    private javax.swing.JPanel getInfoViewerPanel() {
167
        if (infoViewerPanel == null) {
168
            infoViewerPanel = new javax.swing.JPanel();
169
            infoViewerPanel.setLayout(new java.awt.BorderLayout());
170
        }
171
        validate();
172
        return infoViewerPanel;
173
    } 
174
    
175
    /**
176
     * updates the layer to display
177
     *
178
     */
179
    private void updateViewer(int changedIdx)
180
    {
181
            if((m_layers == null) || (m_layers.length == 0)) return;              
182
                  final XMLItem item = m_layers[changedIdx];
183
                  FLayer layer = item.getLayer(); 
184
                
185
                  if (layer instanceof InfoByPoint){                                                                          
186
                          FLyrDefault defaultLayer = (FLyrDefault)layer;
187
              if ( XULPanelClass!=null && defaultLayer.getProperty("infoTool.XULFile") != null){
188
                    try {
189
                                        IXULInfoToolSupport infoPanel = (IXULInfoToolSupport) XULPanelClass.newInstance();
190
                                        infoPanel.setXULFile((String)defaultLayer.getProperty("infoTool.XULFile"));
191
                                        infoViewerPanel.removeAll();
192
                                        infoViewerPanel.add((JPanel)infoPanel);
193
                                        infoPanel.show(item);
194
                                        infoViewerPanel.setVisible( true ); 
195
                                        revalidate();
196
                                        return;
197
                                } catch (Exception e) {
198
                                        // TODO Auto-generated catch block
199
                                        e.printStackTrace();
200
                                }
201
                      
202
              }
203
              
204
              if (defaultLayer.getProperty("customPanel") != null){
205
                                  
206
                                Object o = (Object)defaultLayer.getProperty("customPanel");
207
                                if (o instanceof IInfoToolPanel) {
208
                                          infoPanel = (IInfoToolPanel)o;
209
                                } else {
210
                                         try {
211
                                                Class c = (Class)o;
212
                                                 infoPanel = (IInfoToolPanel)c.newInstance();
213
                                        } catch (InstantiationException e1) {
214
                                                e1.printStackTrace();
215
                                        } catch (IllegalAccessException e1) {
216
                                                e1.printStackTrace();
217
                                        }
218
                                }
219
                                infoViewerPanel.removeAll();
220
                                infoViewerPanel.add((JPanel)infoPanel);
221
                                infoPanel.show(item);
222
                                infoViewerPanel.setVisible( true ); 
223
                                revalidate();
224

    
225
                          } else {
226
                                  
227
                                  if (item.toString().toLowerCase().endsWith( "</html>"))//if (item.toString().toLowerCase().startsWith( "<html>"))
228
                                  {
229
                                          //skip the header info
230
                                          IInfoToolPanel htmlPanel = new HTMLInfoToolPanel();
231
                                         
232
                                          int idx = item.toString().toLowerCase().indexOf("<html");
233
                                          if (idx != -1){
234
                                                  htmlPanel.show(item.toString().substring(idx));                                          
235
                                          }else{
236
                                                  htmlPanel.show(item.toString());
237
                                          }
238
                                          infoViewerPanel.removeAll();
239
                                          infoViewerPanel.add((JPanel)htmlPanel);
240
                                          infoViewerPanel.setVisible( true );                                                           
241
                                          revalidate();
242
                                  } else {
243
                                          FInfoDialogXML dlgXML = new FInfoDialogXML();
244
                                                try {
245
                                                        dlgXML.setModel(new XMLContent() {
246
                                                                private ContentHandler handler;
247

    
248
                                                                public void setContentHandler(ContentHandler arg0) {
249
                                                                        handler = arg0;
250
                                                                }
251

    
252
                                                                public void parse() throws SAXException {
253
                                                                        handler.startDocument();
254
                                                                        item.parse( handler);
255
                                                                        handler.endDocument();
256
                                                                }
257
                                                        });
258
                                                        dlgXML.getXmlTree().setRootVisible(false);
259
                                                        DefaultTreeModel treeModel = (DefaultTreeModel) dlgXML
260
                                                                        .getXmlTree().getModel();
261
                                                        DefaultMutableTreeNode n;
262
                                                        DefaultMutableTreeNode root = (DefaultMutableTreeNode) dlgXML
263
                                                                        .getXmlTree().getModel().getRoot();
264
                                                        n = root.getFirstLeaf();
265
                                                        TreePath path = new TreePath(treeModel.getPathToRoot(n));
266
                                                        dlgXML.getXmlTree().expandPath(path);
267
                                                        dlgXML.getXmlTree().setSelectionPath(path);
268
                                                        //dlgXML.
269
                                                        
270
                                                        infoViewerPanel.removeAll();
271
                                                        infoViewerPanel.add(dlgXML);
272
                                                        infoViewerPanel.setVisible( true ); 
273
                                                        this.validate();
274
                                                    this.doLayout();        
275
                                                        
276
                                                } catch (SAXException e1) {
277
                                                        e1.printStackTrace();
278
                                                }
279
                                  }
280
                          }
281
                  }            
282
    }
283
    
284
    class componentListener implements ComponentListener{
285

    
286
                public void componentHidden(ComponentEvent e) {
287
                        // TODO Auto-generated method stub
288
                        
289
                }
290

    
291
                public void componentMoved(ComponentEvent e) {
292
                        // TODO Auto-generated method stub
293
                        
294
                }
295

    
296
                public void componentResized(ComponentEvent e) {
297
                        
298
                        //if (e.getComponent() == )
299
                        if (infoPanel != null){
300
                                infoPanel.refreshSize();
301
                        }
302
                        
303
                        
304
                }
305

    
306
                public void componentShown(ComponentEvent e) {
307
                        // TODO Auto-generated method stub
308
                        
309
                }
310
            
311
    }
312
}