<commit_before>package de.invation.code.toval.graphic.dialog;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import de.invation.code.toval.graphic.util.SpringUtilities;
import de.invation.code.toval.misc.SimpleSwingTask;

public class PropertyBasedLoadingProgressDialog extends JDialog implements PropertyChangeListener {

	private static final long serialVersionUID = -6065059569175534353L;
	
	private JProgressBar progressBarStep;
	private JTextField txtTask;
	private JTextField txtStep;
	private JPanel panelBars;

	public PropertyBasedLoadingProgressDialog(Component component, String title) {
		this(SwingUtilities.getWindowAncestor(component), title);
	}
	
	public PropertyBasedLoadingProgressDialog(Window owner, String title) {
		super(owner);
		this.setResizable(true);
        this.setModal(true);
        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(getPanelBars(), BorderLayout.PAGE_START);
		getContentPane().add(new JPanel(), BorderLayout.CENTER);
		setTitle(title);
		
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); // prevent the user from closing the dialog
		pack();
        this.setLocationRelativeTo(owner);
	}
	
	private JPanel getPanelBars(){
		if(panelBars == null){
			panelBars = new JPanel(new SpringLayout());
			panelBars.add(new JLabel("Task:", SwingConstants.LEADING));
			panelBars.add(getTextFieldTask());
			panelBars.add(new JLabel("Step:", SwingConstants.LEADING));
			panelBars.add(getTextFieldStep());
			panelBars.add(new JLabel("Progress:"));
			panelBars.add(getProgressBarStep());
			SpringUtilities.makeCompactGrid(panelBars, 3, 2, 10, 10, 10, 10);
		}
		return panelBars;
	}
	
	private JTextField getTextFieldTask(){
		if(txtTask == null){
			txtTask = new JTextField();
			txtTask.setEditable(false);
			txtTask.setBorder(BorderFactory.createEmptyBorder());
		}
		return txtTask;
	}
	
	private JTextField getTextFieldStep(){
		if(txtStep == null){
			txtStep = new JTextField();
			txtStep.setEditable(false);
			txtStep.setBorder(BorderFactory.createEmptyBorder());
		}
		return txtStep;
	}
	
	private JProgressBar getProgressBarStep(){
		if(progressBarStep == null){
			progressBarStep = new JProgressBar(0, 100);
	        progressBarStep.setValue(0);
	        progressBarStep.setStringPainted(true);
		}
		return progressBarStep;
	}
	
	public void setTask(String task){
		getTextFieldTask().setText(task);
	}
	
	public void setStep(String step){
		getTextFieldStep().setText(step);
	}
	
	public void setStepProgress(int stepProgress){
		getProgressBarStep().setValue(stepProgress);
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equals(SimpleSwingTask.PROPERTY_NAME_TASK_ACTION)){
			setTask((String) evt.getNewValue());
			setStep("");
		} else if(evt.getPropertyName().equals(SimpleSwingTask.PROPERTY_NAME_STEP_ACTION)){
			setStep((String) evt.getNewValue());
		} else if (evt.getPropertyName().equals(SimpleSwingTask.PROPERTY_NAME_TASK_STEP_PROGRESS)) {
			setStepProgress((Integer) evt.getNewValue());
		} 
	}
	
	public static void main(String[] args) throws InterruptedException {
		PropertyBasedLoadingProgressDialog dialog = new PropertyBasedLoadingProgressDialog(null, "Test");
		TestTask loadingTaskAdonis = new TestTask() {

			@Override
			protected void done() {
				super.done();
				dialog.dispose();
			}

		};
		loadingTaskAdonis.addPropertyChangeListener(dialog);
		loadingTaskAdonis.execute();
		dialog.setVisible(true);
	}
	
	private static class TestTask extends SimpleSwingTask<Integer, Integer> {

		@Override
		protected Integer internalProcedure() throws Exception {
			Thread.sleep(1000);
			notifyStepAction("Teststep 1");
			notifyStepProgress(20);
			Thread.sleep(1000);
			notifyStepAction("Teststep 2 -sfsfd-sdf-sdfsfd-sd-f-sdf-s-df-s-df-s-df--s-dgf-d-fg-d-fg-d-fg--dfg");
			notifyStepProgress(40);
			Thread.sleep(5000);
			notifyStepAction("Teststep 3");
			notifyStepProgress(60);
			Thread.sleep(1000);
			notifyStepAction("Teststep 4");
			notifyStepProgress(80);
			return 1;
		}
		
	}

}
<commit_msg>Fixed a bug in property based loading progress dialog where width of dialog was not adjusted to step messages.<commit_after>package de.invation.code.toval.graphic.dialog;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import de.invation.code.toval.graphic.util.SpringUtilities;
import de.invation.code.toval.misc.SimpleSwingTask;

public class PropertyBasedLoadingProgressDialog extends JDialog implements PropertyChangeListener {

	private static final long serialVersionUID = -6065059569175534353L;
	
	private JProgressBar progressBarStep;
	private JTextField txtTask;
	private JTextField txtStep;
	private JPanel panelBars;

	public PropertyBasedLoadingProgressDialog(Component component, String title) {
		this(SwingUtilities.getWindowAncestor(component), title);
	}
	
	public PropertyBasedLoadingProgressDialog(Window owner, String title) {
		super(owner);
		this.setResizable(true);
        this.setModal(true);
        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(getPanelBars(), BorderLayout.PAGE_START);
		getContentPane().add(new JPanel(), BorderLayout.CENTER);
		setTitle(title);
		
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); // prevent the user from closing the dialog
		pack();
        this.setLocationRelativeTo(owner);
	}
	
	private JPanel getPanelBars(){
		if(panelBars == null){
			panelBars = new JPanel(new SpringLayout());
			fillAndLayoutPanelBars();
		}
		return panelBars;
	}
	
	private void fillAndLayoutPanelBars(){
		panelBars.add(new JLabel("Task:", SwingConstants.LEADING));
		panelBars.add(getTextFieldTask());
		panelBars.add(new JLabel("Step:", SwingConstants.LEADING));
		panelBars.add(getTextFieldStep());
		panelBars.add(new JLabel("Progress:"));
		panelBars.add(getProgressBarStep());
		SpringUtilities.makeCompactGrid(panelBars, 3, 2, 10, 10, 10, 10);
	}
	
	private JTextField getTextFieldTask(){
		if(txtTask == null){
			txtTask = new JTextField();
			txtTask.setEditable(false);
			txtTask.setBorder(BorderFactory.createEmptyBorder());
		}
		return txtTask;
	}
	
	private JTextField getTextFieldStep(){
		if(txtStep == null){
			txtStep = new JTextField();
			txtStep.setEditable(false);
			txtStep.setBorder(BorderFactory.createEmptyBorder());
		}
		return txtStep;
	}
	
	private JProgressBar getProgressBarStep(){
		if(progressBarStep == null){
			progressBarStep = new JProgressBar(0, 100);
	        progressBarStep.setValue(0);
	        progressBarStep.setStringPainted(true);
		}
		return progressBarStep;
	}
	
	public void setTask(String task){
		getTextFieldTask().setText(task);
		panelBars.removeAll();
		fillAndLayoutPanelBars();
		invalidate();
		pack();
		repaint();
	}
	
	public void setStep(String step){
		getTextFieldStep().setText(step);
		panelBars.removeAll();
		fillAndLayoutPanelBars();
		invalidate();
		pack();
		repaint();
	}
	
	public void setStepProgress(int stepProgress){
		getProgressBarStep().setValue(stepProgress);
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equals(SimpleSwingTask.PROPERTY_NAME_TASK_ACTION)){
			setTask((String) evt.getNewValue());
			setStep("");
		} else if(evt.getPropertyName().equals(SimpleSwingTask.PROPERTY_NAME_STEP_ACTION)){
			setStep((String) evt.getNewValue());
		} else if (evt.getPropertyName().equals(SimpleSwingTask.PROPERTY_NAME_TASK_STEP_PROGRESS)) {
			setStepProgress((Integer) evt.getNewValue());
		} 
	}
	
	public static void main(String[] args) throws InterruptedException {
		PropertyBasedLoadingProgressDialog dialog = new PropertyBasedLoadingProgressDialog(null, "Test");
		TestTask loadingTaskAdonis = new TestTask() {

			@Override
			protected void done() {
				super.done();
				dialog.dispose();
			}

		};
		loadingTaskAdonis.addPropertyChangeListener(dialog);
		loadingTaskAdonis.execute();
		dialog.setVisible(true);
	}
	
	private static class TestTask extends SimpleSwingTask<Integer, Integer> {

		@Override
		protected Integer internalProcedure() throws Exception {
			Thread.sleep(1000);
			notifyStepAction("Teststep 1");
			notifyStepProgress(20);
			Thread.sleep(1000);
			notifyStepAction("Teststep 2 -sfsfd-sdf-sdfsfd-sd-f-sdf-s-df-s-df-s-df--s-dgf-d-fg-d-fg-d-fg--dfg");
			notifyStepProgress(40);
			Thread.sleep(2000);
			notifyStepAction("Teststep 3");
			notifyStepProgress(60);
			Thread.sleep(1000);
			notifyStepAction("Teststep 4");
			notifyStepProgress(80);
			return 1;
		}
		
	}

}

{'fraction_non_alphanumeric': 0.11181780878128847, 'fraction_numerical': 0.012925728354534264, 'mean_word_length': 4.365437534397358, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5939637', 'n_tokens_mistral': 3381, 'n_tokens_neox': 2993, 'n_words': 530}