Added ProgressDialog superclass

This commit is contained in:
Vhati 2013-08-29 14:17:01 -04:00
parent a879071dbd
commit 4a091297ed
5 changed files with 260 additions and 272 deletions

View file

@ -1,68 +1,19 @@
package net.vhati.modmanager.ui;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import net.vhati.modmanager.core.ModPatchObserver;
public class ModPatchDialog extends JDialog implements ActionListener, ModPatchObserver {
private JProgressBar progressBar;
private JTextArea statusArea;
private JButton continueBtn;
private boolean done = false;
private boolean patchingSucceeded = false;
private Runnable successTask = null;
public class ModPatchDialog extends ProgressDialog implements ModPatchObserver {
public ModPatchDialog( Frame owner ) {
super( owner, "Patching...", true );
this.setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE );
progressBar = new JProgressBar();
progressBar.setBorderPainted( true );
JPanel progressHolder = new JPanel( new BorderLayout() );
progressHolder.setBorder( BorderFactory.createEmptyBorder( 10, 15, 0, 15 ) );
progressHolder.add( progressBar );
getContentPane().add( progressHolder, BorderLayout.NORTH );
statusArea = new JTextArea();
statusArea.setBorder( BorderFactory.createEtchedBorder() );
statusArea.setLineWrap( true );
statusArea.setWrapStyleWord( true );
statusArea.setEditable( false );
JPanel statusHolder = new JPanel( new BorderLayout() );
statusHolder.setBorder( BorderFactory.createEmptyBorder( 15, 15, 15, 15 ) );
statusHolder.add( statusArea );
getContentPane().add( statusHolder, BorderLayout.CENTER );
continueBtn = new JButton( "Continue" );
continueBtn.setEnabled( false );
continueBtn.addActionListener( this );
JPanel continueHolder = new JPanel();
continueHolder.setLayout( new BoxLayout( continueHolder, BoxLayout.X_AXIS ) );
continueHolder.setBorder( BorderFactory.createEmptyBorder( 0, 0, 10, 0 ) );
continueHolder.add( Box.createHorizontalGlue() );
continueHolder.add( continueBtn );
continueHolder.add( Box.createHorizontalGlue() );
getContentPane().add( continueHolder, BorderLayout.SOUTH );
public ModPatchDialog( Frame owner, boolean continueOnSuccess ) {
super( owner, true );
this.setTitle( "Patching..." );
this.setSize( 400, 160 );
this.setMinimumSize( this.getPreferredSize() );
@ -70,26 +21,6 @@ public class ModPatchDialog extends JDialog implements ActionListener, ModPatchO
}
@Override
public void actionPerformed( ActionEvent e ) {
Object source = e.getSource();
if ( source == continueBtn ) {
this.setVisible( false );
this.dispose();
if ( done && patchingSucceeded && successTask != null ) {
successTask.run();
}
}
}
private void setStatusText( String message ) {
statusArea.setText( message != null ? message : "..." );
}
/**
* Updates the progress bar.
*
@ -100,26 +31,7 @@ public class ModPatchDialog extends JDialog implements ActionListener, ModPatchO
*/
@Override
public void patchingProgress( final int value, final int max ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if ( value >= 0 && max >= 0 ) {
if ( progressBar.isIndeterminate() )
progressBar.setIndeterminate( false );
if ( progressBar.getMaximum() != max ) {
progressBar.setValue( 0 );
progressBar.setMaximum( max );
}
progressBar.setValue( value );
}
else {
if ( !progressBar.isIndeterminate() )
progressBar.setIndeterminate( true );
progressBar.setValue( 0 );
}
}
});
this.setProgressLater( value, max );
}
/**
@ -129,12 +41,7 @@ public class ModPatchDialog extends JDialog implements ActionListener, ModPatchO
*/
@Override
public void patchingStatus( final String message ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setStatusText( message != null ? message : "..." );
}
});
setStatusTextLater( message != null ? message : "..." );
}
/**
@ -142,12 +49,7 @@ public class ModPatchDialog extends JDialog implements ActionListener, ModPatchO
*/
@Override
public void patchingMod( final File modFile ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setStatusText( String.format( "Installing mod \"%s\"...", modFile.getName() ) );
}
});
setStatusTextLater( String.format( "Installing mod \"%s\"...", modFile.getName() ) );
}
/**
@ -156,46 +58,19 @@ public class ModPatchDialog extends JDialog implements ActionListener, ModPatchO
* If anything went wrong, e may be non-null.
*/
@Override
public void patchingEnded( final boolean success, final Exception e ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if ( success )
setStatusText( "Patching completed." );
else
setStatusText( String.format( "Patching failed: %s", e ) );
done = true;
patchingSucceeded = success;
continueBtn.setEnabled( true );
if ( !ModPatchDialog.this.isShowing() ) {
// The window's not visible, no continueBtn to click.
ModPatchDialog.this.dispose();
if ( patchingSucceeded && successTask != null ) {
successTask.run();
}
}
}
});
public void patchingEnded( boolean outcome, Exception e ) {
setTaskOutcomeLater( outcome, e );
}
/**
* Sets a runnable to trigger after patching successfully.
*/
public void setSuccessTask( Runnable r ) {
successTask = r;
}
@Override
protected void setTaskOutcome( boolean outcome, Exception e ) {
super.setTaskOutcome( outcome, e );
if ( !this.isShowing() ) return;
/**
* Shows or hides this component depending on the value of parameter b.
*
* If patching has already completed, this method will do nothing.
*/
public void setVisible( boolean b ) {
if ( !done ) super.setVisible( b );
if ( succeeded == true )
setStatusText( "Patching completed." );
else
setStatusText( String.format( "Patching failed: %s", e ) );
}
}