All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.client.util.ProgressModel Maven / Gradle / Ivy

package fr.ird.observe.client.util;

/*-
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import org.jdesktop.beans.AbstractBean;

/**
 * Created on 17/12/16.
 *
 * @author Tony Chemit - [email protected]
 * @since 6.0
 */
public class ProgressModel extends AbstractBean {

    public static final String PROPERTY_CURRENT_STEP = "currentStep";
    public static final String PROPERTY_STEPS_COUNT = "stepsCount";
    private int currentStep;
    private int stepsCount;

    public void installUI(JProgressBar progressBar) {
        progressBar.setVisible(true);
        addPropertyChangeListener(PROPERTY_CURRENT_STEP, evt -> {
            int newValue = (int) evt.getNewValue();
            progressBar.setValue(newValue);
            progressBar.setString((int) (progressBar.getPercentComplete() * 100) + "%");

        });
        addPropertyChangeListener(PROPERTY_STEPS_COUNT, evt -> progressBar.setMaximum((Integer) evt.getNewValue()));
    }

    public int getCurrentStep() {
        return currentStep;
    }

    public void setCurrentStep(int currentStep) {
        int oldValue = this.currentStep;
        this.currentStep = currentStep;
        firePropertyChange(PROPERTY_CURRENT_STEP, oldValue, currentStep);
    }

    public void setStepsCount(int stepsCount) {
        int oldValue = this.stepsCount;
        this.stepsCount = stepsCount;
        firePropertyChange(PROPERTY_STEPS_COUNT, oldValue, stepsCount);
    }

    public int getStepsCount() {
        return stepsCount;
    }

    public void incrementsCurrentStep() {
        SwingUtilities.invokeLater(() -> setCurrentStep(getCurrentStep() + 1));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy