/* * XMLPanel.java * * Created on October 9, 2001, 9:06 AM */ package com.imsl.demo.xml; import com.imsl.chart.Chart; import com.imsl.chart.JPanelChart; import com.imsl.chart.xml.ChartXML; import java.io.*; import javax.swing.*; /** * View an XML chart description file. * The view is a tabbed pane. One tab shows the chart and the other shows * the XML file. The XML file can be editted and the results reflected in * the chart. * * @author brophy */ class XMLPanel extends javax.swing.JPanel { private JPanelChart panelChart; private JTextArea textArea; private ChartXML chartXML; /** * Creates new form XMLPanel. * @param name is the resource name of the XML description. * @see java.lang.ClassLoader.html#getResourceAsStream(java.lang.String) */ public XMLPanel() { initComponents(); panelChart = new JPanelChart(); textArea = new JTextArea(); textArea.setTabSize(4); textArea.setFont(java.awt.Font.getFont("SansSerif")); JScrollPane jScrollPane = new JScrollPane(textArea); jTabbedPane1.addTab("Chart", panelChart); jTabbedPane1.addTab("XML", jScrollPane); } public void setInputStream(InputStream is) throws IOException { // read the resource into a StringBuffer and initialize text area. Reader reader = new InputStreamReader(is); StringBuffer sb = new StringBuffer(4096); char buffer[] = new char[2048]; while (true) { int n = reader.read(buffer); if (n < 0) break; sb.append(buffer, 0, n); } is.close(); textArea.setText(sb.toString()); textArea.setCaretPosition(0); createChart(); repaint(); } public Chart getChart() { if (chartXML == null) return null; return chartXML.getChart(); } /** * Creates a chart from its XML description. */ private void createChart() { try { String text = textArea.getText(); if (text.length() == 0) return; chartXML = new ChartXML(new org.xml.sax.InputSource(new StringReader(text)), true); Chart chart = chartXML.getChart(); panelChart.setChart(chart); chart.setComponent(panelChart); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.toString(), "Error", JOptionPane.ERROR_MESSAGE); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents jTabbedPane1 = new javax.swing.JTabbedPane(); setLayout(new java.awt.BorderLayout()); jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM); jTabbedPane1.setPreferredSize(new java.awt.Dimension(500, 500)); jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { jTabbedPane1StateChanged(evt); } }); add(jTabbedPane1, java.awt.BorderLayout.CENTER); }//GEN-END:initComponents private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged // create the chart when the the chart tab (index==0) is selected. if (jTabbedPane1.getSelectedIndex() == 0) createChart(); }//GEN-LAST:event_jTabbedPane1StateChanged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTabbedPane jTabbedPane1; // End of variables declaration//GEN-END:variables }