Project

General

Profile

Download (6.43 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
    private static final long serialVersionUID = 3258788051657003998L;
46

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

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

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

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

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

    
134
    @Override
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
        }
141
        // Send any remaining ticks and clear out the subtask text
142
        double remaining = parentTicks - sentToParent;
143
        if (remaining > 0) {
144
            super.internalWorked(remaining);
145
        }
146
        //clear the sub task if there was one
147
        if (hasSubTask)
148
         {
149
            subTask(""); //$NON-NLS-1$
150
        }
151
        sentToParent = 0;
152
    }
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
    @Override
169
    public void subTask(String name) {
170
        if ((style & SUPPRESS_SUBTASK_LABEL) != 0) {
171
            return;
172
        }
173
        hasSubTask = true;
174
        String label = name;
175
        if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0
176
                && mainTaskLabel != null && mainTaskLabel.length() > 0) {
177
            label = mainTaskLabel + ' ' + label;
178
        }
179
        super.subTask(label);
180
    }
181

    
182
    @Override
183
    public void worked(int work) {
184
        internalWorked(work);
185
    }
186

    
187
	@Override
188
	public void warning(String message) {
189
		// TODO Auto-generated method stub
190
	}
191

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

    
197
}
(10-10/10)