Project

General

Profile

Download (6.73 KB) Statistics
| Branch: | Tag: | Revision:
1

    
2
        /*******************************************************************************
3
         * Copyright (c) 2000, 2007 IBM Corporation and others.
4
         * All rights reserved. This program and the accompanying materials
5
         * are made available under the terms of the Eclipse Public License v1.0
6
         * which accompanies this distribution, and is available at
7
         * http://www.eclipse.org/legal/epl-v10.html
8
         * 
9
         * Contributors:
10
         *     IBM Corporation - initial API and implementation
11
         *******************************************************************************/
12
package eu.etaxonomy.cdm.common.monitor;
13

    
14

    
15
/**
16
 * For new implementations consider using {@link SubMonitor}.
17
 * 
18
 * A progress monitor that uses a given amount of work ticks
19
 * from a parent monitor. It can be used as follows:
20
 * <pre>
21
 *     try {
22
 *         pm.beginTask("Main Task", 100);
23
 *         doSomeWork(pm, 30);
24
 *         SubProgressMonitor subMonitor= new SubProgressMonitor(pm, 40);
25
 *         try {
26
 *             subMonitor.beginTask("", 300);
27
 *             doSomeWork(subMonitor, 300);
28
 *         } finally {
29
 *             subMonitor.done();
30
 *         }
31
 *         doSomeWork(pm, 30);
32
 *     } finally {
33
 *         pm.done();
34
 *     }
35
 * </pre>
36
 * <p>
37
 * This class can be used without OSGi running.
38
 * </p><p>
39
 * This class may be instantiated or subclassed by clients.
40
 * </p>
41
 * 
42
 * @see SubMonitor
43
 */
44
public class SubProgressMonitor extends ProgressMonitorWrapper {
45

    
46
    /**
47
     * Style constant indicating that calls to <code>subTask</code>
48
     * should not have any effect.
49
     *
50
     * @see #SubProgressMonitor(IProgressMonitor,int,int)
51
     */
52
    public static final int SUPPRESS_SUBTASK_LABEL = 1 << 1;
53
    /**
54
     * Style constant indicating that the main task label 
55
     * should be prepended to the subtask label.
56
     *
57
     * @see #SubProgressMonitor(IProgressMonitor,int,int)
58
     */
59
    public static final int PREPEND_MAIN_LABEL_TO_SUBTASK = 1 << 2;
60

    
61
    private int parentTicks = 0;
62
    private double sentToParent = 0.0;
63
    private double scale = 0.0;
64
    private int nestedBeginTasks = 0;
65
    private boolean usedUp = false;
66
    private boolean hasSubTask = false;
67
    private int style;
68
    private String mainTaskLabel;
69

    
70
    /**
71
     * Creates a new sub-progress monitor for the given monitor. The sub 
72
     * progress monitor uses the given number of work ticks from its 
73
     * parent monitor.
74
     *
75
     * @param monitor the parent progress monitor
76
     * @param ticks the number of work ticks allocated from the
77
     *    parent monitor
78
     */
79
    public SubProgressMonitor(IProgressMonitor monitor, int ticks) {
80
        this (monitor, ticks, 0);
81
    }
82

    
83
    /**
84
     * Creates a new sub-progress monitor for the given monitor. The sub 
85
     * progress monitor uses the given number of work ticks from its 
86
     * parent monitor.
87
     *
88
     * @param monitor the parent progress monitor
89
     * @param ticks the number of work ticks allocated from the
90
     *    parent monitor
91
     * @param style one of
92
     *    <ul>
93
     *    <li> <code>SUPPRESS_SUBTASK_LABEL</code> </li>
94
     *    <li> <code>PREPEND_MAIN_LABEL_TO_SUBTASK</code> </li>
95
     *    </ul>
96
     * @see #SUPPRESS_SUBTASK_LABEL
97
     * @see #PREPEND_MAIN_LABEL_TO_SUBTASK
98
     */
99
    public SubProgressMonitor(IProgressMonitor monitor, int ticks,
100
            int style) {
101
        super (monitor);
102
        this .parentTicks = (ticks > 0) ? ticks : 0;
103
        this .style = style;
104
    }
105

    
106
    /* (Intentionally not javadoc'd)
107
     * Implements the method <code>IProgressMonitor.beginTask</code>.
108
     *
109
     * Starts a new main task. Since this progress monitor is a sub
110
     * progress monitor, the given name will NOT be used to update
111
     * the progress bar's main task label. That means the given 
112
     * string will be ignored. If style <code>PREPEND_MAIN_LABEL_TO_SUBTASK
113
     * <code> is specified, then the given string will be prepended to
114
     * every string passed to <code>subTask(String)</code>.
115
     */
116
    public void beginTask(String name, int totalWork) {
117
        nestedBeginTasks++;
118
        // Ignore nested begin task calls.
119
        if (nestedBeginTasks > 1) {
120
            return;
121
        }
122
        // be safe:  if the argument would cause math errors (zero or 
123
        // negative), just use 0 as the scale.  This disables progress for
124
        // this submonitor. 
125
        scale = totalWork <= 0 ? 0 : (double) parentTicks
126
                / (double) totalWork;
127
        if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0) {
128
            mainTaskLabel = name;
129
        }
130
    }
131

    
132
    /* (Intentionally not javadoc'd)
133
     * Implements the method <code>IProgressMonitor.done</code>.
134
     */
135
    public void done() {
136
        // Ignore if more done calls than beginTask calls or if we are still
137
        // in some nested beginTasks
138
        if (nestedBeginTasks == 0 || --nestedBeginTasks > 0)
139
            return;
140
        // Send any remaining ticks and clear out the subtask text
141
        double remaining = parentTicks - sentToParent;
142
        if (remaining > 0)
143
            super.internalWorked(remaining);
144
        //clear the sub task if there was one
145
        if (hasSubTask)
146
            subTask(""); //$NON-NLS-1$
147
        sentToParent = 0;
148
    }
149

    
150

    
151
    /* (non-Javadoc)
152
     * @see eu.etaxonomy.cdm.common.monitor.ProgressMonitorWrapper#internalWorked(double)
153
     */
154
    @Override
155
    public void internalWorked(double work) {
156
        if (usedUp || nestedBeginTasks != 1) {
157
            return;
158
        }
159

    
160
        double realWork = (work > 0.0d) ? scale * work : 0.0d;
161
        super.internalWorked(realWork);
162
        sentToParent += realWork;
163
        if (sentToParent >= parentTicks) {
164
            usedUp = true;
165
        }
166
    }
167

    
168
    /* (Intentionally not javadoc'd)
169
     * Implements the method <code>IProgressMonitor.subTask</code>.
170
     */
171
    public void subTask(String name) {
172
        if ((style & SUPPRESS_SUBTASK_LABEL) != 0) {
173
            return;
174
        }
175
        hasSubTask = true;
176
        String label = name;
177
        if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0
178
                && mainTaskLabel != null && mainTaskLabel.length() > 0) {
179
            label = mainTaskLabel + ' ' + label;
180
        }
181
        super.subTask(label);
182
    }
183

    
184
    /* (Intentionally not javadoc'd)
185
     * Implements the method <code>IProgressMonitor.worked</code>.
186
     */
187
    public void worked(int work) {
188
        internalWorked(work);
189
    }
190

    
191
	@Override
192
	public void warning(String message) {
193
		// TODO Auto-generated method stub
194
		
195
	}
196

    
197
	@Override
198
	public void warning(String message, Throwable throwable) {
199
		// TODO Auto-generated method stub
200
		
201
	}
202
}
(7-7/7)