OSDN Git Service

MOD: Cancel TimerTask for aboidance all ready clocse dialog Exception.
authorTakuya Ono <takuya-o@users.sourceforge.jp>
Wed, 22 Aug 2007 20:33:33 +0000 (20:33 +0000)
committerTakuya Ono <takuya-o@users.sourceforge.jp>
Wed, 22 Aug 2007 20:33:33 +0000 (20:33 +0000)
src/org/jent/checksmtp/ToListUI.java
test/org/jent/checksmtp/ToListUITest.java

index 8bb9ac5..fe4d580 100644 (file)
@@ -74,15 +74,20 @@ public class ToListUI extends javax.swing.JFrame {
     //Auto confirm timer
     int timeOut = ApplicationProperties.getConfirmTimeout();
     if ( timeOut > 0 ) {
-      TimerTask task = new TimerTask() {
-        public void run() {
-          jButtonOKActionPerformed(null);
-        }
-      };
-      confirmTimer.schedule(task, timeOut * 1000L);
+      confirmTimer.schedule(confirmTimeoutTimerTask, timeOut * 1000L);
+    }
+  }
+  private void cancelConfirmTimeoutTimerTask() {
+    if ( !confirmTimeoutTimerTask.cancel() ) {
+      System.err.println("Already do task?");
     }
   }
   private static Timer confirmTimer = new Timer();
+  private TimerTask confirmTimeoutTimerTask = new TimerTask() {
+    public void run() {
+      jButtonOKActionPerformed(null);
+    }
+  };
   
   
   /** This method is called from within the constructor to
@@ -173,11 +178,13 @@ public class ToListUI extends javax.swing.JFrame {
   }// </editor-fold>//GEN-END:initComponents
   
   private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
+    cancelConfirmTimeoutTimerTask();
     restorePosition();
     resultNotify.sayNG();
   }//GEN-LAST:event_formWindowClosing
-  
   private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
+    cancelConfirmTimeoutTimerTask();
     restorePosition();
     resultNotify.sayNG();
     this.dispose();
@@ -185,13 +192,21 @@ public class ToListUI extends javax.swing.JFrame {
   
   private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
     //push OK
-    restorePosition();
-    resultNotify.sayOK();
-    this.dispose();
+    //if ( isVisible() ) {  //Care after dialog closing, confirm time out occurred.  -> use cancelTimerTask.
+      restorePosition();
+      resultNotify.sayOK();
+      this.dispose();
+    //}
   }//GEN-LAST:event_jButtonOKActionPerformed
   
   private boolean restorePosition() {
-    Point locationPoint = getLocationOnScreen();  //TODO: IllegalComponentStateException
+    Point locationPoint;
+    if ( isVisible() ) {
+      locationPoint = getLocationOnScreen();  //TODO: IllegalComponentStateException
+    } else {
+      locationPoint = new Point(ApplicationProperties.getConfirmDialogPosX(),
+              ApplicationProperties.getConfirmDialogPosY());
+    }
     int x = locationPoint.x;
     int y = locationPoint.y;
     int h = this.getHeight();
index 11251cf..b1f233c 100644 (file)
@@ -27,21 +27,22 @@ public class ToListUITest extends TestCase implements ResultNotify {
   
   protected void setUp() throws Exception {
   }
-   
+  
   /**
    * Test of main method, of class org.jent.checksmtp.ToListUI.
+   *
+   * public void testMain() {
+   * System.out.println("main");
+   *
+   * String[] args = null;
+   *
+   * //ToListUI.main(args);
+   *
+   * //  review the generated test code and remove the default call to fail.
+   * //fail("The test case is a prototype.");
+   *
+   * }
    */
-  public void testMain() {
-    System.out.println("main");
-   
-    String[] args = null;
-     
-    //ToListUI.main(args);
-     
-    //  review the generated test code and remove the default call to fail.
-    //fail("The test case is a prototype.");
-     
-  }
   
   private void pushTestSetup(String msg) {
     arrayList.add(msg);
@@ -66,28 +67,52 @@ public class ToListUITest extends TestCase implements ResultNotify {
   }
   
   public void sayOK() {
+    System.out.println("sayOK");
     result = true;
     notifyResult();
   }
   
   public void sayNG() {
+    System.out.println("sayNG");
     result = false;
     notifyResult();
   }
-    
+  
   public void testPushOK() {
+    result = false;
     pushTestSetup("Please push OK button.");
     assertTrue("Time out or push CANCEL", result);
   }
   
   public void testPushCANCEL() {
+    result = true;
     pushTestSetup("Please push CANCEL button.");
     assertTrue("Time out or push OK", !result);
   }
   
   public void testPushClose() {
+    result = true;
     pushTestSetup("Please close Window.");
     assertTrue("Time out or push OK", !result);
   }
   
+  public void testPushCancelAfterTimeOut() {
+    ApplicationProperties.setConfirmTimeout(5);
+    
+    result = true;
+    pushTestSetup("Test Confirm Timeout 5sec. Please push CANCEL button.");
+    assertTrue("Time out or push OK", !result);
+    try {
+      System.out.println("Wait 10sec.");
+      synchronized(this) {
+        wait(10 * 1000);
+      }
+    } catch (InterruptedException ex) {
+      ex.printStackTrace();
+      fail("Wait Interrupted.");
+    }
+    assertTrue("Confirm Timeout Occurred wrong.", !result);
+    
+  }
+  
 }