// JProgressBarWithCancel.java
//
// Created: September 2005
//
// Copyright 2005 Martin Senger (martin.senger@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You
// may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//

package org.biomoby.service.dashboard;

import org.tulsoft.tools.gui.SwingUtils;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.JProgressBar;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;

/**
 * !!!WORK IN PROGRESS!!!
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: JProgressBarWithCancel.java,v 1.3 2005/11/06 16:47:11 senger Exp $
 */

public class JProgressBarWithCancel
    extends JPanel {

    // components
    JProgressBar progressBar;
    JButton cancelButton;

    /*********************************************************************
     * Constructor with a horizontal progress bar. The initial and
     * minimum values are 0, and the maximum is 100.
     ********************************************************************/
    public JProgressBarWithCancel() {
	this (0, 100);
    }

    /*********************************************************************
     * Creates a horizontal progress bar with the specified minimum
     * and maximum. Sets the initial value of the progress bar to the
     * specified minimum.
     ********************************************************************/
    public JProgressBarWithCancel (int min, int max) {
	super (new GridBagLayout());
	init (min, max);
    }

    /*********************************************************************
     * Create yourself (called from constructors).
     ********************************************************************/
    protected void init (int min, int max) {
	progressBar = new JProgressBar (min, max);
	progressBar.setValue (0);
	progressBar.setStringPainted (true);

	Icon cancelIcon = SwingUtils.createIcon ("images/smallCancel.gif", this);
	cancelButton = new JButton (cancelIcon);
        cancelButton.setFocusPainted (false);
        cancelButton.setMargin (new Insets (0,0,0,0));
        cancelButton.setContentAreaFilled (false);
        cancelButton.setToolTipText ("Cancel the process");

	// put it together ( TBD: should depend on progressBar.getOrientation() )
 	SwingUtils.addComponent (this, progressBar,  0, 0, 1, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 1.0, 0.0);
 	SwingUtils.addComponent (this, cancelButton, 1, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 0.0, 0.0);
    }

    /*********************************************************************
     * Get the underlying progress bar component.
     ********************************************************************/
    public JProgressBar get() {
	return progressBar;
    }

    /*********************************************************************
     * Get the underlying cancel button component.
     ********************************************************************/
    public JButton getButton() {
	return cancelButton;
    }

    /*********************************************************************
     *
     ********************************************************************/
    public void addActionListener (ActionListener l) {
	getButton().addActionListener (l);
    }

}
