[11073] | 1 | /**
|
---|
| 2 | *#########################################################################
|
---|
| 3 | *
|
---|
| 4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
| 5 | * library suite from the New Zealand Digital Library Project at the
|
---|
| 6 | * University of Waikato, New Zealand.
|
---|
| 7 | *
|
---|
| 8 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 9 | *
|
---|
| 10 | * Copyright (C) New Zealand Digital Library Project
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or modify
|
---|
| 13 | * it under the terms of the GNU General Public License as published by
|
---|
| 14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | * (at your option) any later version.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful,
|
---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | * GNU General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, write to the Free Software
|
---|
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | *########################################################################
|
---|
| 26 | */
|
---|
| 27 | package org.greenstone.gatherer.gui;
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | import javax.swing.*;
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | /** A progress bar that doesn't pack a sad when you feed it longs (such as the sizes of files) which get mangled into negative numbers. I know how it feels, as I often have negative progress. Also tries to be efficent when updating the JProgressBar, so only updates when the value actually changes, which become necessary when you move a large number of files with millions of bytes (where upon shifting a file of a thousand bytes would be pretty insignificant, maybe not even a percent, but would fire several progress updates).
|
---|
| 34 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 35 | */
|
---|
| 36 | public class GProgressBar
|
---|
| 37 | extends JProgressBar
|
---|
| 38 | {
|
---|
| 39 | private long cur_value = 0L;
|
---|
| 40 | private long max_value = 0L;
|
---|
| 41 |
|
---|
| 42 | /** The previous percentage value. */
|
---|
| 43 | private int previous = -1;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | public GProgressBar()
|
---|
| 47 | {
|
---|
| 48 | super(0, 100);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | public void addMaximum(long amount)
|
---|
| 53 | {
|
---|
| 54 | if (amount > 0) {
|
---|
| 55 | max_value = max_value + amount;
|
---|
| 56 | update();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | public void addValue(long amount)
|
---|
| 62 | {
|
---|
| 63 | if (amount > 0) {
|
---|
| 64 | cur_value = cur_value + amount;
|
---|
| 65 | update();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | public void clear()
|
---|
| 71 | {
|
---|
| 72 | previous = -1;
|
---|
| 73 | update();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | public void reset()
|
---|
| 78 | {
|
---|
| 79 | cur_value = 0L;
|
---|
| 80 | max_value = 0L;
|
---|
| 81 | previous = -1;
|
---|
| 82 | update();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | public void setIndeterminate(boolean indeterminate)
|
---|
| 87 | {
|
---|
| 88 | SwingUtilities.invokeLater(new SafeSetIndeterminateTask(indeterminate));
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | public void setString(String label)
|
---|
| 93 | {
|
---|
| 94 | SwingUtilities.invokeLater(new SafeSetStringTask(label));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[11075] | 97 |
|
---|
| 98 | public void setMaximum(int value)
|
---|
| 99 | {
|
---|
| 100 | max_value = value;
|
---|
| 101 | update();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[11073] | 104 |
|
---|
| 105 | public void setValue(int value)
|
---|
| 106 | {
|
---|
| 107 | cur_value = value;
|
---|
| 108 | update();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | /** Only bother updating the progress bar if the percentage has changed. */
|
---|
| 113 | private void update()
|
---|
| 114 | {
|
---|
| 115 | int percentage = -1;
|
---|
| 116 | if (cur_value == 0L || max_value == 0L) {
|
---|
| 117 | percentage = 0;
|
---|
| 118 | }
|
---|
| 119 | else {
|
---|
| 120 | long per_value = (cur_value * 100) / max_value;
|
---|
| 121 | percentage = (int) per_value;
|
---|
| 122 | if (percentage > 100) {
|
---|
| 123 | percentage = 100;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | if (percentage != previous) {
|
---|
| 128 | previous = percentage;
|
---|
| 129 | setString(percentage + "%");
|
---|
| 130 | SwingUtilities.invokeLater(new SafeSetValueTask(percentage));
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | /** Internal class to call JProgressBar.setIndeterminate safely. */
|
---|
| 136 | private class SafeSetIndeterminateTask
|
---|
| 137 | implements Runnable
|
---|
| 138 | {
|
---|
| 139 | private boolean indeterminate;
|
---|
| 140 |
|
---|
| 141 | public SafeSetIndeterminateTask(boolean indeterminate)
|
---|
| 142 | {
|
---|
| 143 | this.indeterminate = indeterminate;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public void run()
|
---|
| 147 | {
|
---|
| 148 | unsafeSetIndeterminate(indeterminate);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /** This method is unsafe, -unless- it is called from the Event Queue (eg. from SafeSetIndeterminateTask). */
|
---|
| 153 | private void unsafeSetIndeterminate(boolean indeterminate)
|
---|
| 154 | {
|
---|
| 155 | super.setIndeterminate(indeterminate);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | /** Internal class to call JProgressBar.setString safely. */
|
---|
| 160 | private class SafeSetStringTask
|
---|
| 161 | implements Runnable
|
---|
| 162 | {
|
---|
| 163 | private String label;
|
---|
| 164 |
|
---|
| 165 | public SafeSetStringTask(String label)
|
---|
| 166 | {
|
---|
| 167 | this.label = label;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | public void run()
|
---|
| 171 | {
|
---|
| 172 | unsafeSetString(label);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /** This method is unsafe, -unless- it is called from the Event Queue (eg. from SafeSetStringTask). */
|
---|
| 177 | private void unsafeSetString(String label)
|
---|
| 178 | {
|
---|
| 179 | super.setString(label);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 | /** Internal class to call JProgressBar.setValue safely. */
|
---|
| 184 | private class SafeSetValueTask
|
---|
| 185 | implements Runnable
|
---|
| 186 | {
|
---|
| 187 | private int value;
|
---|
| 188 |
|
---|
| 189 | public SafeSetValueTask(int value)
|
---|
| 190 | {
|
---|
| 191 | this.value = value;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | public void run()
|
---|
| 195 | {
|
---|
| 196 | unsafeSetValue(value);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /** This method is unsafe, -unless- it is called from the Event Queue (eg. from SafeSetValueTask). */
|
---|
| 201 | private void unsafeSetValue(int value)
|
---|
| 202 | {
|
---|
| 203 | super.setValue(value);
|
---|
| 204 | }
|
---|
| 205 | }
|
---|