<commit_before>/*
 *
 * Copyright (c) 2008-2010 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */

package VASSAL.tools.swing;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.event.EventListenerList;

import net.miginfocom.swing.MigLayout;
import VASSAL.i18n.Resources;

/**
 * A cancellable progress dialog.
 *
 * @since 3.1.0
 * @author <NAME>
 */
public class ProgressDialog extends JDialog {
  private static final long serialVersionUID = 1L;

  protected final JLabel label;
  protected final JProgressBar progbar;
  protected final JButton cancel;

  protected final EventListenerList listeners = new EventListenerList();

  /**
   * Creates a progress dialog.
   *
   * @param parent the parent frame
   * @param title the dialog title
   * @param text the text beneath the progress bar
   */
  public ProgressDialog(Frame parent, String title, String text) {
    super(parent, title, true);

    // set up the components
    label = new JLabel(text);

    progbar = new JProgressBar(0, 100);
    progbar.setStringPainted(true);
    progbar.setValue(0);

    cancel = new JButton(Resources.getString(Resources.CANCEL));

    // forward clicks on the close decoration to cancellation listeners
    addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        fireCancelledEvent(new ActionEvent(
          ProgressDialog.this, ActionEvent.ACTION_PERFORMED, "cancel" //NON-NLS
        ));
      }
    });

    // forward clicks on the close button to the cancellation listeners
    cancel.addActionListener(this::fireCancelledEvent);

    // create the layout
    final JPanel panel = new JPanel(new MigLayout(
      "insets dialog, fill", "", "unrelated:push[]related[]unrelated:push[]")); //NON-NLS

    // NB: It's necessary to set the minimum width for the label,
    // otherwise if the label text is set to a string which is too long,
    // the label will overflow the container instead of showing ellipses.
    panel.add(progbar, "growx, wrap"); //NON-NLS
    panel.add(label,   "wmin 0, pad 0 0 2pt 0, wrap unrel:push"); //NON-NLS
    panel.add(cancel,  "tag cancel"); //NON-NLS

    add(panel);

    // pack to find the minimum height
    pack();

    // set minimum size
    setMinimumSize(new Dimension(300, getHeight()));

    // pack again to ensure that we respect the minimum size
    pack();
  }

  protected void fireCancelledEvent(ActionEvent e) {
    final Object[] larr = listeners.getListenerList();

    // Process the listeners last to first, notifying
    // those that are interested in this event.
    for (int i = larr.length - 2; i >= 0; i -= 2) {
      if (larr[i] == ActionListener.class) {
        ((ActionListener) larr[i + 1]).actionPerformed(e);
      }
    }
  }

  /**
   * Gets the text label shown beneath the progress bar.
   *
   * @return the text label
   */
  public String getLabel() {
    return label.getText();
  }

  /**
   * Sets the text label shown beneath the progress bar.
   *
   * @param text the text label
   */
  public void setLabel(String text) {
    label.setText(text);<commit_msg>Made it possible to suppress the cancel button in the progress dialog.<commit_after>/*
 *
 * Copyright (c) 2008-2010 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */

package VASSAL.tools.swing;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.event.EventListenerList;

import net.miginfocom.swing.MigLayout;
import VASSAL.i18n.Resources;

/**
 * A cancellable progress dialog.
 *
 * @since 3.1.0
 * @author <NAME>
 */
public class ProgressDialog extends JDialog {
  private static final long serialVersionUID = 1L;

  protected final JLabel label;
  protected final JProgressBar progbar;
  protected final JButton cancel;

  protected final EventListenerList listeners = new EventListenerList();

  /**
   * Creates a progress dialog.
   *
   * @param parent the parent frame
   * @param title the dialog title
   * @param text the text beneath the progress bar
   * @param cancellable show a cancel button if true
   */
  public ProgressDialog(Frame parent, String title, String text, boolean cancellable) {
    super(parent, title, true);

    // set up the components
    label = new JLabel(text);

    progbar = new JProgressBar(0, 100);
    progbar.setStringPainted(true);
    progbar.setValue(0);

    if (cancellable) {
      cancel = new JButton(Resources.getString(Resources.CANCEL));

      // forward clicks on the close decoration to cancellation listeners
      addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
          fireCancelledEvent(new ActionEvent(
            ProgressDialog.this, ActionEvent.ACTION_PERFORMED, "cancel" //NON-NLS
          ));
        }
      });

      // forward clicks on the close button to the cancellation listeners
      cancel.addActionListener(this::fireCancelledEvent);
    }
    else {
      cancel = null;
      setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    }

    // create the layout
    final JPanel panel = new JPanel(new MigLayout(
      "insets dialog, fill", "", "unrelated:push[]related[]unrelated:push[]")); //NON-NLS

    // NB: It's necessary to set the minimum width for the label,
    // otherwise if the label text is set to a string which is too long,
    // the label will overflow the container instead of showing ellipses.
    panel.add(progbar, "growx, wrap"); //NON-NLS
    panel.add(label,   "wmin 0, pad 0 0 2pt 0, wrap unrel:push"); //NON-NLS

    if (cancellable) {
      panel.add(cancel,  "tag cancel"); //NON-NLS
    }

    add(panel);

    // pack to find the minimum height
    pack();

    // set minimum size
    setMinimumSize(new Dimension(300, getHeight()));

    // pack again to ensure that we respect the minimum size
    pack();
  }

  /**
   * Creates a progress dialog.
   *
   * @param parent the parent frame
   * @param title the dialog title
   * @param text the text beneath the progress bar
   */
  public ProgressDialog(Frame parent, String title, String text) {
    this(parent, title, text, true);
  }

  protected void fireCancelledEvent(ActionEvent e) {
    final Object[] larr = listeners.getListenerList();

    // Process the listeners last to first, notifying
    // those that are interested in this event.
    for (int i = larr.length - 2; i >= 0; i -= 2) {
      if (larr[i] == ActionListener.class) {
        ((ActionListener) larr[i + 1]).actionPerformed(e);
      }
    }
  }

  /**
   * Gets the text label shown beneath the progress bar.
   *
   * @return the text label
   */
  public String getLabel() {
    return label.getText();
  }

  /**
   * Sets the text label shown beneath the progress bar.
   *
   * @param text the text label
   */
  public void setLabel(String text) {
    label.setText(text);
{'fraction_non_alphanumeric': 0.09594762388531437, 'fraction_numerical': 0.0069985325657523426, 'mean_word_length': 3.231136580706781, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3955549', 'n_tokens_mistral': 2588, 'n_tokens_neox': 2482, 'n_words': 901}