View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.r66gui;
22  
23  import javax.swing.JEditorPane;
24  import javax.swing.JProgressBar;
25  
26  import openr66.client.ProgressBarTransfer;
27  import openr66.protocol.networkhandler.NetworkTransaction;
28  import openr66.protocol.utils.R66Future;
29  
30  /**
31   * @author Frederic Bregier
32   *
33   */
34  public class ProgressDirectTransfer extends ProgressBarTransfer {
35      private JProgressBar progressBar;
36      private JEditorPane textFieldStatus;
37      private boolean firstCall = true;
38      private int nbBlock = 1;
39      private long lastTime = System.currentTimeMillis();
40      private int lastRank = 0;
41      
42      /**
43       * @param future
44       * @param remoteHost
45       * @param filename
46       * @param rulename
47       * @param fileinfo
48       * @param isMD5
49       * @param blocksize
50       * @param id
51       * @param networkTransaction
52       * @param callbackdelay
53       */
54      public ProgressDirectTransfer(R66Future future, String remoteHost,
55              String filename, String rulename, String fileinfo, boolean isMD5,
56              int blocksize, long id, NetworkTransaction networkTransaction,
57              long callbackdelay, JProgressBar progressBar, JEditorPane textFieldStatus) {
58          super(future, remoteHost, filename, rulename, fileinfo, isMD5,
59                  blocksize, id, networkTransaction, callbackdelay);
60          this.textFieldStatus = textFieldStatus;
61          this.progressBar = progressBar;
62          this.progressBar.setIndeterminate(true);
63          this.progressBar.setValue(0);
64          this.progressBar.setVisible(true);
65          this.textFieldStatus.setText("Initializing transfer...");
66      }
67  
68      /* (non-Javadoc)
69       * @see openr66.client.ProgressBarTransfer#callBack(int, int)
70       */
71      @Override
72      public void callBack(int currentBlock, int blocksize) {
73          if (firstCall) {
74              if (filesize != 0) {
75                  this.progressBar.setIndeterminate(false);
76                  nbBlock = (int) (Math.ceil(((double)filesize/(double)blocksize)));
77              }
78              firstCall = false;
79          }
80          long newtime = System.currentTimeMillis()+1;
81          int sendsize = ((currentBlock-lastRank)*blocksize);
82          long time = ((newtime-lastTime)*1024)/1000;
83          long speedKB = sendsize/time;
84          if (filesize == 0) {
85              this.textFieldStatus.setText("Bytes transmitted: "+(currentBlock*blocksize)+" at "+speedKB+" KB/s");
86          } else {
87              this.progressBar.setValue(currentBlock*100/nbBlock);
88              this.textFieldStatus.setText("Bytes transmitted: "+(currentBlock*blocksize)+
89                      " on "+filesize+" at "+speedKB+" KB/s");
90          }
91          lastTime = newtime-1;
92          lastRank = currentBlock;
93      }
94  
95      /* (non-Javadoc)
96       * @see openr66.client.ProgressBarTransfer#lastCallBack(boolean, int, int)
97       */
98      @Override
99      public void lastCallBack(boolean success, int currentBlock, int blocksize) {
100         this.progressBar.setIndeterminate(false);
101         if (filesize == 0) {
102             this.textFieldStatus.setText("Finally Bytes transmitted: "+(currentBlock*blocksize)+" with Status: "+success);
103         } else {
104             this.progressBar.setValue(100);
105             this.textFieldStatus.setText("Finally Bytes transmitted: "+(currentBlock*blocksize)+" with Status: "+success);
106         }
107     }
108 }