/*
 * Copyright (c) 2000-2009 TeamDev Ltd. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */
package com.jniwrapper.win32.samples.demo;

import com.jniwrapper.samples.shell.components.LazyPanel;
import com.jniwrapper.util.StreamUtils;

import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.Window;
import java.io.IOException;
import java.io.InputStream;

/**
 @author Vladimir Kondrashchenko
 */
public class SourcePanel extends LazyPanel
{
    private Class _sample;
    private JEditorPane _sourceArea;

    public SourcePanel(Window parent, Class sample)
    {
        super(parent);
        _sample = sample;
    }

    public void initialize() throws Exception
    {
        String classPath = _sample.getName();
        String className = classPath.substring(classPath.lastIndexOf("."1, classPath.length());
        InputStream inputStream = _sample.getResourceAsStream(className + ".html");
        String sources;
        try
        {
            byte[] bytes = StreamUtils.readBytes(inputStream);
            sources = new String(bytes);
        }
        catch (IOException e)
        {
            sources = "";
        }

        setLayout(new BorderLayout());

        _sourceArea = new JEditorPane("text/html", sources);

        _sourceArea.setCaretPosition(0);
        _sourceArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(_sourceArea);
        scrollPane.setBorder(BorderFactory.createRaisedBevelBorder());
        add(scrollPane, BorderLayout.CENTER);

        super.initialize();
    }
}