/* * Copyright 2011 BetaSteward_at_googlemail.com. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. */ /* * ReconnectDialog.java * * Created on 20-May-2011, 2:39:21 PM */ package mage.client.dialog; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.SwingWorker; import mage.remote.Session; /** * * @author BetaSteward_at_googlemail.com */ public class ReconnectDialog extends MageDialog { private static final int MAX_ATTEMPTS = 3; private boolean result = false; private boolean cancel = false; private ReconnectTask reconnectTask; /** Creates new form ReconnectDialog */ public ReconnectDialog() { initComponents(); } public void showDialog(Session session) { result = false; cancel = false; reconnectTask = new ReconnectTask(session, this); reconnectTask.execute(); this.setModal(true); this.setLocation(100, 100); this.setVisible(true); } public void closeDialog() { if (reconnectTask != null) reconnectTask.cancel(true); this.setVisible(false); } public void update(String message) { this.jLabel1.setText(message); } public boolean getResult() { return result; } /** 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. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); btnCancel = new javax.swing.JButton(); btnCancel.setText("Cancel"); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnCancel, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnCancel) .addContainerGap()) ); pack(); }// //GEN-END:initComponents private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed this.cancel = true; closeDialog(); }//GEN-LAST:event_btnCancelActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnCancel; private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables class ReconnectTask extends SwingWorker { private Session session; private ReconnectDialog dialog; ReconnectTask(Session session, ReconnectDialog dialog) { this.session = session; this.dialog = dialog; } @Override protected Void doInBackground() throws Exception { int numAttempts = 0; while (numAttempts < MAX_ATTEMPTS && !cancel) { numAttempts++; this.publish("Attempting to reconnect: attempt " + numAttempts + "of " + MAX_ATTEMPTS); Thread.sleep(2000); if (session.connect()) { result = true; closeDialog(); break; } } return null; } @Override protected void process(List view) { dialog.update(view.get(0)); } } }