1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
   |  
public class SqlToCsv_SwingWorker 
{
	private static Logger _logError = Logger.getLogger( SqlToCsv_SwingWorker.class );
	private final Launcher _launcher;
	private final JLabel _info = new JLabel( "Progression .... " );
	private JProgressBar _progressbar;
	private int nbProd;
 
	public SqlToCsv_SwingWorker( final Launcher launcher ) 
	{
		_logError.info( "Lancement export BDD to CSV" );
		_launcher = launcher;
		nbProd = getNbProduct();
		_progressbar = new JProgressBar( 0, nbProd );
 
		EventQueue.invokeLater(new Runnable() {
			@Override public void run() {				
                runCalc();
            }
        });
	}
 
	public void runCalc() 
	{
		final JDialog dlg = new JDialog( _launcher, "Progress Dialog", true );
//	    JLabel dpb = new JLabel( "toto" );
		dlg.add(BorderLayout.CENTER, _progressbar);
	    dlg.add(BorderLayout.NORTH, _info);
	    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
	    dlg.setSize(300, 75);
	    dlg.setLocationRelativeTo(_launcher);
 
		_progressbar.setIndeterminate( true );
        TwoWorker task = new TwoWorker( dlg );
        task.addPropertyChangeListener( new PropertyChangeListener() {
            @Override public void propertyChange(PropertyChangeEvent e) {
                if( "progress".equals(e.getPropertyName()) ) {
                	_progressbar.setIndeterminate( false );
                	_progressbar.setValue( (Integer)e.getNewValue() );
                }
            }
        });
 
        task.execute();
        dlg.setVisible( true );
    }
 
	private class TwoWorker extends SwingWorker<Void, Integer>
	{
		private final JDialog _dial;
		public TwoWorker( final JDialog dial ) {
			super();
			_dial = dial;
		}
        @Override protected Void doInBackground() throws Exception {
        	InputSql inputSql = new InputSql( "" );
    		Product prod;    		
    		final SqlQuery sql = new SqlQuery();
    		try 
    		{
    			sql.connect();
    			ResultSet rs = sql.requete( "SELECT id FROM produit" );
    			try { 
	    		    int currentRow=0;
	    			while( rs.next() )
	    			{
	    				setProgress( (currentRow*100)/nbProd );
	    				publish( currentRow );
	    				String id_prod = rs.getString( 1 );				
	    				inputSql.setIdProduit( id_prod );
	    				prod = new Product();
	    				prod = inputSql.getProductInformation();
	    				String product_sirom = format( prod );
	//    				save( product_sirom );
	    		        currentRow++;
	    			}	
    			}
    			finally {
    				rs.close();
    			}
    			sql.close();
    		} 
    		catch( SQLException e ) {
    			_logError.error( "Erreur sql lors de la récupération des informations" +
    					"necessaires pour l'export csv" + e );
    			JOptionPane.showConfirmDialog( null,
    					"ERREUR sql pendant l'export de la base de données\n" +
    					"Le fichier csv de sortie est incomplet.", "Confirm",
    			        JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE );
    			e.printStackTrace();
    		}	
    		finally {
    			_dial.setVisible( false );
    		}
    		return null; 
        }
        @Override protected void process(List<Integer> chunks) {
        	_info.setText( "Sauvegarde du produit " + chunks.get(chunks.size()-1) 
        			+ " sur " + nbProd );
        	_progressbar.setValue( chunks.get(chunks.size()-1) );
        }
	}
} | 
Partager