/* Copyright 2004-2014 Jim Voris
*
* 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 com.qumasoft.guitools.qwin.dialog;
import com.qumasoft.guitools.qwin.ParentProgressDialogInterface;
import com.qumasoft.guitools.qwin.ProgressDialogInterface;
import com.qumasoft.guitools.qwin.QWinUtility;
import java.util.logging.Level;
import javax.swing.SwingUtilities;
/**
* A parent/child progress dialog. This dialog has two separate progress bars. The 'parent' progress bar is for showing overall progress; the 'child' progress bar is for
* showing progress of some sub-task.
* @author Jim Voris
*/
public final class ParentChildProgressDialog extends AbstractQWinCommandDialog implements ProgressDialogInterface, ParentProgressDialogInterface {
private static final long serialVersionUID = 5017934693543686742L;
private boolean isCancelledFlag = false;
private boolean autoCloseFlag = true;
private int progressMax;
private boolean progressDialogVisibleFlag = false;
/**
* Create a parent/child progress bar dialog.
* @param parent the parent frame.
* @param modal is this modal.
* @param min the minimum value for indicating progress for the child progress bar.
* @param max the maximum value for indicating progress for the child progress bar.
*/
public ParentChildProgressDialog(java.awt.Frame parent, boolean modal, int min, int max) {
super(parent, modal);
initComponents();
initProgressBar(min, max);
getRootPane().setDefaultButton(cancelButton);
cancelButton.requestFocusInWindow();
setFont();
center();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
parentActionLabel = new javax.swing.JLabel();
parentActivityLabel = new javax.swing.JLabel();
parentProgressBar = new javax.swing.JProgressBar();
childActionLabel = new javax.swing.JLabel();
childActivityLabel = new javax.swing.JLabel();
childProgressBar = new javax.swing.JProgressBar();
jPanel1 = new javax.swing.JPanel();
cancelButton = new javax.swing.JButton();
setTitle("Progress");
setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
parentActionLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
parentActionLabel.setText("Action");
parentActivityLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
parentActivityLabel.setText("Activity");
parentProgressBar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
childActionLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
childActionLabel.setText("Action");
childActivityLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
childActivityLabel.setText("Activity");
childProgressBar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
cancelButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
cancelButton.setText("Cancel");
cancelButton.setMaximumSize(new java.awt.Dimension(100, 29));
cancelButton.setMinimumSize(new java.awt.Dimension(100, 29));
cancelButton.setPreferredSize(new java.awt.Dimension(100, 29));
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(250, Short.MAX_VALUE)
.add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(234, 234, 234))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(childProgressBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(10, 10, 10)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, childActionLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, childActivityLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, parentActionLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 569, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, parentActivityLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, parentProgressBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(parentActionLabel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(parentActivityLabel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(parentProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(10, 10, 10)
.add(childActionLabel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(childActivityLabel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(childProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(10, 10, 10)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
{//GEN-HEADEREND:event_cancelButtonActionPerformed
closeDialog(null);
}//GEN-LAST:event_cancelButtonActionPerformed
@Override
public void dismissDialog() {
cancelButtonActionPerformed(null);
}
@Override
public void initProgressBar(int min, int max) {
childProgressBar.setMinimum(min);
childProgressBar.setMaximum(max);
progressMax = max;
}
@Override
public void initParentProgressBar(int min, int max) {
parentProgressBar.setMinimum(min);
parentProgressBar.setMaximum(max);
}
@Override
public void setProgress(final int progress) {
Runnable set = new Runnable() {
@Override
public void run() {
privateSetProgress(progress);
}
};
try {
SwingUtilities.invokeLater(set);
} catch (Exception e) {
QWinUtility.logProblem(Level.WARNING, "Caught exception: " + e.getClass().toString() + " : " + e.getLocalizedMessage());
}
}
private void privateSetProgress(final int progress) {
childProgressBar.setValue(progress);
if (getAutoClose()) {
if (progress >= progressMax) {
closeDialog(null);
}
}
}
@Override
public void setAction(final String action) {
Runnable set = new Runnable() {
@Override
public void run() {
privateSetAction(action);
}
};
try {
SwingUtilities.invokeLater(set);
} catch (Exception e) {
QWinUtility.logProblem(Level.WARNING, "Caught exception: " + e.getClass().toString() + " : " + e.getLocalizedMessage());
}
}
private void privateSetAction(final String action) {
childActionLabel.setText(action);
}
@Override
public void setActivity(final String activity) {
Runnable set = new Runnable() {
@Override
public void run() {
privateSetActivity(activity);
}
};
try {
SwingUtilities.invokeLater(set);
} catch (Exception e) {
QWinUtility.logProblem(Level.WARNING, "Caught exception: " + e.getClass().toString() + " : " + e.getLocalizedMessage());
}
}
private void privateSetActivity(final String activity) {
childActivityLabel.setText(activity);
}
public void close() {
closeDialog(null);
}
@Override
public boolean getIsCancelled() {
return isCancelledFlag;
}
public boolean getAutoClose() {
return autoCloseFlag;
}
public void setAutoClose(boolean flag) {
autoCloseFlag = flag;
}
@Override
public void setParentProgress(final int progress) {
Runnable set = new Runnable() {
@Override
public void run() {
privateSetParentProgress(progress);
}
};
try {
SwingUtilities.invokeLater(set);
} catch (Exception e) {
QWinUtility.logProblem(Level.WARNING, "Caught exception: " + e.getClass().toString() + " : " + e.getLocalizedMessage());
}
}
private void privateSetParentProgress(final int progress) {
parentProgressBar.setValue(progress);
}
@Override
public void setParentAction(final String action) {
Runnable set = new Runnable() {
@Override
public void run() {
privateSetParentAction(action);
}
};
try {
SwingUtilities.invokeLater(set);
} catch (Exception e) {
QWinUtility.logProblem(Level.WARNING, "Caught exception: " + e.getClass().toString() + " : " + e.getLocalizedMessage());
}
}
private void privateSetParentAction(final String action) {
parentActionLabel.setText(action);
}
@Override
public void setParentActivity(final String activity) {
Runnable set = new Runnable() {
@Override
public void run() {
privateSetParentActivity(activity);
}
};
try {
SwingUtilities.invokeLater(set);
} catch (Exception e) {
QWinUtility.logProblem(Level.WARNING, "Caught exception: " + e.getClass().toString() + " : " + e.getLocalizedMessage());
}
}
private void privateSetParentActivity(final String activity) {
parentActivityLabel.setText(activity);
}
@Override
public void setProgressDialogVisibleFlag(boolean flag) {
progressDialogVisibleFlag = flag;
}
@Override
public boolean getProgressDialogVisibleFlag() {
return progressDialogVisibleFlag;
}
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt)
{//GEN-FIRST:event_closeDialog
setVisible(false);
isCancelledFlag = true;
dispose();
}//GEN-LAST:event_closeDialog
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JLabel childActionLabel;
private javax.swing.JLabel childActivityLabel;
private javax.swing.JProgressBar childProgressBar;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel parentActionLabel;
private javax.swing.JLabel parentActivityLabel;
private javax.swing.JProgressBar parentProgressBar;
// End of variables declaration//GEN-END:variables
}