/Users/lyon/j4p/src/classUtils/pack/util/TextProgressBar.java

1    package classUtils.pack.util; 
2     
3    /** 
4     * <font color="red">NOT FUNCTIONAL YET</font> 
5     * To change this generated comment edit the template variable "typecomment": 
6     * Window>Preferences>Java>Templates. 
7     * To enable and disable the creation of type comments go to 
8     * Window>Preferences>Java>Code Generation. 
9     *  
10    * @author Cristiano Sadun 
11    */ 
12   public class TextProgressBar { 
13    
14       public static final String ANSI = "ansi"; 
15    
16       private String terminalType; 
17       private int total; 
18       private int incPercentage=10; 
19       private int progress; 
20       private int curPos; 
21       private int maxPos=20; 
22        
23       public TextProgressBar(String terminalType) { 
24           this(terminalType, 0); 
25       } 
26    
27       public TextProgressBar(String terminalType, int total) { 
28           if (ANSI.equals(terminalType)) { 
29               this.terminalType = terminalType; 
30           } else 
31               throw new UnsupportedOperationException( 
32                   "Terminal type " + terminalType + " not supported"); 
33           this.total=total; 
34       } 
35        
36       public synchronized void inc(int n) { 
37           // If there's a total, add a point if progress+total is a proper percentage 
38           // of total; else, just add n points 
39           if (total==0) { 
40               printPoints(n); 
41           } else { 
42               int nPoints = ((progress+n)/total) % incPercentage; 
43               printPoints(nPoints); 
44           } 
45           progress+=n; 
46       } 
47        
48       public void inc() { 
49           inc(1); 
50       } 
51        
52       private void printPoints(int nPoints) { 
53           for(int i=0;i<nPoints;i++) { 
54               if (curPos>=maxPos) { 
55                   cursorBack(curPos); 
56                   curPos=0; 
57               } 
58               curPos++; 
59               System.out.print("."); 
60           } 
61       } 
62        
63       private void cursorBack(int n) { 
64           if (ANSI.equals(terminalType))  
65               escape(String.valueOf(n)+"D"); 
66       } 
67        
68       private void escape(char [] s) { 
69           char [] s2 = new char[s.length+2]; 
70           System.arraycopy(s, 0, s2, 2, s.length); 
71           s2[0]=(char)27; // ESC 
72           s2[1]='['; // bracket 
73           System.out.print(s2); 
74       } 
75        
76       private void escape(String s) { 
77           escape(s.toCharArray()); 
78       } 
79        
80       public static void main(String[] args) throws InterruptedException { 
81           TextProgressBar tpb=new  TextProgressBar(TextProgressBar.ANSI); 
82           for(int i=0;i<50;i++) { 
83               tpb.inc(); 
84               Thread.sleep(100); 
85           } 
86       } 
87       /** 
88        * Returns the total. 
89        * @return int 
90        */ 
91       public int getTotal() { 
92           return total; 
93       } 
94    
95       /** 
96        * Sets the total. 
97        * @param total The total to set 
98        */ 
99       public void setTotal(int total) { 
100          this.total = total; 
101      } 
102   
103      /** 
104       * Returns the progress. 
105       * @return int 
106       */ 
107      public int getProgress() { 
108          return progress; 
109      } 
110   
111      /** 
112       * Sets the progress. 
113       * @param progress The progress to set 
114       */ 
115      public void setProgress(int progress) { 
116          this.progress = progress; 
117      } 
118   
119  } 
120