View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
23   */
24  
25  package mk.fsgrep.util;
26  
27  
28  import java.util.*;
29  
30  import mk.fsgrep.util.output.OutputDestination;
31  
32  
33  
34  /***
35   * Draws a text-based progress bar (percentage completion).
36   * 
37   * @author  Murali Krishnan
38   *
39   */
40  public class TextProgressBar implements ProgressBar {
41  
42      //------------------------------------------------------------
43      //- Class Variables
44  
45  
46  
47      //------------------------------------------------------------
48      //- Class Functions
49  
50  
51  
52      //------------------------------------------------------------
53      //- Instance Variables
54  
55      private LinkedList<Integer> _segments = null;
56      private int _totalCount = 0;
57      private int _segmentCount = 0;
58      private OutputDestination _out = null;
59      private boolean _defined = false;
60  
61  
62  
63      //------------------------------------------------------------
64      //- Constructors
65  
66      public TextProgressBar(OutputDestination pOut) {
67          _out = pOut;
68      }
69  
70  
71      //------------------------------------------------------------
72      //- Accessors
73  
74      protected LinkedList<Integer> getSegments() {return _segments;}
75      protected int getTotalCount() {return _totalCount;}
76      protected int getSegmentCount() {return _segmentCount;}
77      protected OutputDestination getOut() {return _out;}
78      protected boolean isDefined() {return _defined;}
79  
80  
81  
82      //------------------------------------------------------------
83      //- Settors
84  
85      protected void setOut(OutputDestination val) {_out=val;}
86  
87  
88      //------------------------------------------------------------
89      //- Private/Protected Utility Functions
90  
91      protected void decrTotalCount() {_totalCount--;}
92  
93      protected void decrSegmentCount() {
94          if (--_segmentCount <= 0) {
95              getOut().print("=");
96              initializeSegment();
97          }
98      }
99  
100 
101     protected void initializeSegment() {
102         if (!getSegments().isEmpty()) {
103             _segmentCount = getSegments().removeFirst();
104         }
105     }
106 
107 
108     protected void printHeader() {
109         String header = "+--  Progress Bar  -----------------------------------------------------------+";
110 
111         if (getTotalCount() < 79) {
112             StringBuffer buffer = new StringBuffer("+");
113             for (int i = 1; i < getTotalCount()-1; i++) {
114                 buffer.append("-");
115             }
116             buffer.append("+");
117 
118             header = buffer.toString();
119         }
120 
121         getOut().println(header);
122     }
123 
124 
125     //------------------------------------------------------------
126     //- Public Interface Functions
127 
128 
129 
130     //------------------------------------------------------------
131     //- Class Interface Functions
132 
133     public void update() {
134         if (isDefined()) {
135             decrSegmentCount();
136             decrTotalCount();
137         }
138     }
139 
140 
141     public void finish() {
142         if (isDefined()) {
143             getOut().println("");
144 
145             if (getTotalCount() > 0) {
146                 System.err.println("(" + getTotalCount() + " missing.)");
147             }
148         }
149     }
150 
151 
152     public void initialize(int pTotalCount) {
153         _segments = new LinkedList<Integer>();
154         _totalCount = 0;
155         _segmentCount = 0;
156         _defined = false;
157 
158         // Show the text progress bar only if there are a significant
159         // number of files.  Obviously this is subjective.
160         if (pTotalCount > 100) {
161             _defined = true;
162             _totalCount = pTotalCount;
163 
164             if (getTotalCount() > 79) {
165                 double exactQuotient;
166                 double remaider;
167                 double bucket = 0.0;
168                 int intPart;
169                 int total = getTotalCount();
170                 for (int i=79; i>0; i--) {
171                     exactQuotient = ((double) total) / i;
172                     intPart = (int) exactQuotient;
173                     remaider = exactQuotient - intPart;
174                     bucket += remaider;
175                     if (bucket > 1.0) {
176                         intPart++;
177                         bucket -= 1.0;
178                     }
179 
180                     getSegments().addLast(intPart);
181                     total -= intPart;
182                 }
183             } else {
184                 for (int i = 0; i < getTotalCount(); i++) {
185                     getSegments().add(1);
186                 }
187             }
188 
189             initializeSegment();
190 
191             printHeader();
192         }
193     }
194 
195 
196 
197     //------------------------------------------------------------
198     //- Inner Classes
199 
200 
201 
202     //------------------------------------------------------------
203     //- Main
204 
205 }