/* Copyright (c) 2005 Stanford University and Christopher Bruns
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Created on Jan 17, 2006
 * Original author: Christopher Bruns
 */
package org.simtk.isimsu;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *  
  * @author Christopher Bruns
  * 
  * Window in which the user or developer can view log messages
 */
public class LogDialog extends JDialog implements ActionListener {
    JTextArea logTextArea;
    static JFrame annoyingFrameThatIsNeverShownButIsNeededToSetIconOnDialog
        = new JFrame();
    
    LogDialog(ISIMWrapper parent) {
        
        // super(parent, "ISIM Log", false); // non-modal dialog, naturally
        // Specifying the parent puts the dialog always on top, which I dislike
        super(annoyingFrameThatIsNeverShownButIsNeededToSetIconOnDialog, 
                parent.programName + " Log", 
                false); // non-modal dialog, naturally
        
        annoyingFrameThatIsNeverShownButIsNeededToSetIconOnDialog.setIconImage(
                parent.getIconImage());
        
        setModal(false);

        // Create the panel where the log text will appear
        logTextArea = new JTextArea(35, 40);
        logTextArea.setEditable(false); // Don't allow the user to edit the text though
        logTextArea.setLineWrap(true);
        
        // Log text should be scrollable
        JScrollPane scrollPane = new JScrollPane(logTextArea);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        
        // Put a button at the bottom for closing the log window
        JButton closeButton = new JButton("Close log window");
        closeButton.addActionListener(this);
        // center button in its own panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(parent.getBackground());
        buttonPanel.add(closeButton, BorderLayout.CENTER);
        // Springy emptiness to left and right of button
        buttonPanel.add(Box.createHorizontalGlue(), BorderLayout.WEST);
        buttonPanel.add(Box.createHorizontalGlue(), BorderLayout.EAST);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        
        setLocationRelativeTo(parent); // Start near the ISIM application
        pack();
    }
    
    public void append(String logText) {
        logTextArea.append(logText);
    }
    
    /** 
     * Respond to dismiss button press
     */
    public void actionPerformed(ActionEvent event) {
        setVisible(false);
    }
    
    static public final long serialVersionUID = 01L;
}
