File: ProgressBar.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (165 lines) | stat: -rw-r--r-- 5,545 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package ij.gui;

import ij.macro.Interpreter;
import java.awt.*;
import java.awt.image.*;

/**
 * This is the progress bar that is displayed in the lower right hand corner of
 * the ImageJ window. Use one of the static IJ.showProgress() methods to display
 * and update the progress bar.
 */
public class ProgressBar extends Canvas {

    public static final int WIDTH = 120;
    public static final int HEIGHT = 20;

    private int canvasWidth, canvasHeight;
    private int x, y, width, height;
    private long lastTime = 0;
    private boolean showBar;
    private boolean batchMode;

    private Color barColor = Color.gray;
    private Color fillColor = new Color(204, 204, 255);
    private Color backgroundColor = ij.ImageJ.backgroundColor;
    private Color frameBrighter = backgroundColor.brighter();
    private Color frameDarker = backgroundColor.darker();
    private boolean dualDisplay = false;
    private double slowX = 0.0;//box
    private double fastX = 0.0;//dot

    /**
     * This constructor is called once by ImageJ at startup.
     */
    public ProgressBar(int canvasWidth, int canvasHeight) {
    	init(canvasWidth, canvasHeight);
	}
    
    public void init(int canvasWidth, int canvasHeight) {
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
        x = 3;
        y = 5;
        width = canvasWidth - 8;
        height = canvasHeight - 7;
    }

    void fill3DRect(Graphics g, int x, int y, int width, int height) {
        g.setColor(fillColor);
        g.fillRect(x + 1, y + 1, width - 2, height - 2);
        g.setColor(frameDarker);
        g.drawLine(x, y, x, y + height);
        g.drawLine(x + 1, y, x + width - 1, y);
        g.setColor(frameBrighter);
        g.drawLine(x + 1, y + height, x + width, y + height);
        g.drawLine(x + width, y, x + width, y + height - 1);
    }

    /**
     * Updates the progress bar, where abs(progress) should run from 0 to 1.
     * If abs(<code>progress</code>) == 1 the bar is erased. The bar is updated only
     * if more than 90 ms have passed since the last call. Does nothing if the
     * ImageJ window is not present.
     * @param progress Length of the progress bar to display (0...1). 
     * Using <code>progress</code> with negative sign (0 .. -1) will regard subsequent calls with
     * positive argument as sub-ordinate processes that are displayed as moving dot.
     */
    public void show(double progress) {
        show(progress, false);
    }

    /**
     * Updates the progress bar, where abs(progress) should run from 0 to 1.
     * @param progress Length of the progress bar to display (0...1). 
     * @param showInBatchMode show progress bar in batch mode macros?
     */
    public void show(double progress, boolean showInBatchMode) {
        boolean finished = false;
        if (progress<=-1)
            finished = true;
        if (!dualDisplay && progress >= 1)
            finished = true;
        if (!finished) {
            if (progress < 0) {
                slowX = -progress;
                fastX = 0.0;
                dualDisplay = true;
            } else if (dualDisplay)
                fastX = progress;
            if (!dualDisplay)
                slowX = progress;
        }
        if (!showInBatchMode && (batchMode || Interpreter.isBatchMode()))
            return;
        if (finished) {//clear the progress bar
            slowX = 0.0;
            fastX = 0.0;
            showBar = false;
            dualDisplay = false;
            repaint();
            return;
        }
        long time = System.currentTimeMillis();
        if (time-lastTime<90 && progress!=1.0)
            return;
        lastTime = time;
        showBar = true;
        repaint();
    }

    /**
     * Updates the progress bar, where the length of the bar is set to
     * (<code>(abs(currentIndex)+1)/abs(finalIndex)</code> of the maximum bar
     * length. Use a negative <code>currentIndex</code> to show subsequent
     * plugin calls as moving dot. The bar is erased if
     * <code>currentIndex&gt;=finalIndex-1</code> or <code>finalIndex == 0</code>.
     */
    public void show(int currentIndex, int finalIndex) {
        boolean wasNegative = currentIndex < 0;
        double progress = ((double) Math.abs(currentIndex) + 1.0) / Math.abs(finalIndex);
        if (wasNegative)
            progress = -progress;
        if (finalIndex == 0)
            progress = -1;
        show(progress);
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        if (showBar) {
            fill3DRect(g, x - 1, y - 1, width + 1, height + 1);
            drawBar(g);
        } else {
            g.setColor(backgroundColor);
            g.fillRect(0, 0, canvasWidth, canvasHeight);
        }
    }

    void drawBar(Graphics g) {
        int barEnd = (int) (width * slowX);
        if (Toolbar.getToolId()==Toolbar.ANGLE)
			g.setColor(Color.getHSBColor(((float)(System.currentTimeMillis()%1000))/1000, 0.5f, 1.0f));
		else
       		g.setColor(barColor);
        g.fillRect(x, y, barEnd, height);
        if (dualDisplay && fastX > 0) {
            int dotPos = (int) (width * fastX);
            g.setColor(Color.BLACK);
            if (dotPos > 1 && dotPos < width - 7)
                g.fillOval(dotPos, y + 3, 7, 7);
        }
    }

    public Dimension getPreferredSize() {
        return new Dimension(canvasWidth, canvasHeight);
    }

    public void setBatchMode(boolean batchMode) {
        this.batchMode = batchMode;
    }

}