/*
 * (C) Copyright IBM Corp. 1997-1998  All rights reserved.
 *
 * US Government Users Restricted Rights Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * IBM will not be liable for any damages suffered by you as a result
 * of using the Program. In no event will IBM be liable for any
 * special, indirect or consequential damages or lost profits even if
 * IBM has been advised of the possibility of their occurrence. IBM
 * will not be liable for any third party claims against you.
 */

package com.ibm.xml.sample;

import com.ibm.xml.parser.*;
import java.io.*;
import java.awt.*;
import com.sun.java.swing.*;

/**
 * A sample to display a progress(?) bar while parsing.
 * This programs requires Swing-1.0.
 *
 * @version 1.1
 * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
 */
public class Progress implements TagHandler {
    public static void main(String[] argv) {
        if (1 != argv.length) {
            System.err.println("Require 1 filename.");
            System.exit(1);
        }
        try {
            Progress prog = new Progress(argv[0]);
            InputStream is = new FileInputStream(argv[0]);
            Parser parser = new Parser(argv[0]);
            parser.setTagHandler(prog);
            TXDocument doc = parser.readStream(is);
            is.close();

            if (0 < parser.getNumberOfErrors()) {
                System.exit(1);
            }
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static final int LIMIT = 32;
    int m_depth = 0;
    int m_count = 0;
    JProgressBar m_progress;
    JTextField m_label;
    Graphics m_progressg, m_labelg;

    Progress(String fname) {
        Frame f = new Frame("Parsing: "+fname);
        m_progress = new JProgressBar();
        m_progress.setMinimum(0);
        m_progress.setMaximum(LIMIT-1);
        m_label = new JTextField("0");
        f.add(m_label, "North");
        f.add(m_progress, "Center");
        f.pack();
        f.setSize(384, 80);
        Dimension di = Toolkit.getDefaultToolkit().getScreenSize();
        f.setLocation((di.width-384)/2, (di.height-80)/2);
        f.show();

        m_progressg = m_progress.getGraphics();
        m_labelg = m_label.getGraphics();
    }

                                                // parser.TagHandler
    public void handleStartTag(TXElement el, boolean empty) {
        m_depth ++;
        m_count ++;
        m_progress.setValue(m_depth % LIMIT);
        m_label.setText(Integer.toString(m_count));
        m_progress.update(m_progressg);
        m_label.update(m_labelg);
        //m_progress.repaint(0L);
    }

                                                // parser.TagHandler
    public void handleEndTag(TXElement el, boolean empty) {
        m_depth --;
        m_progress.setValue(m_depth % LIMIT);
        m_progress.update(m_progress.getGraphics());
        //m_progress.repaint(0L);
    }
}
