KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > TaskInfo


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

11 package org.eclipse.ui.internal.progress;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.osgi.util.NLS;
15
16 /**
17  * The TaskInfo is the info on a task with a job. It is assumed that there is
18  * only one task running at a time - any previous tasks in a Job will be
19  * deleted.
20  */

21 public class TaskInfo extends SubTaskInfo {
22     double preWork = 0;
23
24     int totalWork = 0;
25
26     /**
27      * Create a new instance of the receiver with the supplied total work and
28      * task name.
29      *
30      * @param parentJobInfo
31      * @param infoName
32      * @param total
33      */

34     TaskInfo(JobInfo parentJobInfo, String JavaDoc infoName, int total) {
35         super(parentJobInfo, infoName);
36         totalWork = total;
37     }
38
39     /**
40      * Add the work increment to the total.
41      *
42      * @param workIncrement
43      */

44     void addWork(double workIncrement) {
45
46         // Don't bother if we are indeterminate
47
if (totalWork == IProgressMonitor.UNKNOWN) {
48             return;
49         }
50         preWork += workIncrement;
51
52     }
53
54     /**
55      * Add the amount of work to the recevier. Update a parent monitor by the
56      * increment scaled to the amount of ticks this represents.
57      *
58      * @param workIncrement
59      * int the amount of work in the receiver
60      * @param parentMonitor
61      * The IProgressMonitor that is also listening
62      * @param parentTicks
63      * the number of ticks this monitor represents
64      */

65     void addWork(double workIncrement, IProgressMonitor parentMonitor,
66             int parentTicks) {
67         // Don't bother if we are indeterminate
68
if (totalWork == IProgressMonitor.UNKNOWN) {
69             return;
70         }
71
72         addWork(workIncrement);
73         parentMonitor.internalWorked(workIncrement * parentTicks / totalWork);
74     }
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.eclipse.ui.internal.progress.JobTreeElement#getDisplayString(boolean)
80      */

81     String JavaDoc getDisplayString(boolean showProgress) {
82
83         if (totalWork == IProgressMonitor.UNKNOWN) {
84             return unknownProgress();
85         }
86
87         if (taskName == null) {
88             return getDisplayStringWithoutTask(showProgress);
89         }
90
91         if (showProgress) {
92             String JavaDoc[] messageValues = new String JavaDoc[3];
93             messageValues[0] = String.valueOf(getPercentDone());
94             messageValues[1] = jobInfo.getJob().getName();
95             messageValues[2] = taskName;
96
97             return NLS
98                     .bind(ProgressMessages.JobInfo_DoneMessage, messageValues);
99         }
100         String JavaDoc[] messageValues = new String JavaDoc[2];
101         messageValues[0] = jobInfo.getJob().getName();
102         messageValues[1] = taskName;
103
104         return NLS.bind(ProgressMessages.JobInfo_DoneNoProgressMessage,
105                 messageValues);
106
107     }
108
109     /**
110      * Get the display String without the task name.
111      *
112      * @param showProgress
113      * Whether or not we are showing progress
114      *
115      * @return String
116      */

117     String JavaDoc getDisplayStringWithoutTask(boolean showProgress) {
118
119         if (!showProgress || totalWork == IProgressMonitor.UNKNOWN) {
120             return jobInfo.getJob().getName();
121         }
122
123         return NLS.bind(ProgressMessages.JobInfo_NoTaskNameDoneMessage, jobInfo
124                 .getJob().getName(), String.valueOf(getPercentDone()));
125     }
126
127     /**
128      * Return an integer representing the amount of work completed. If progress
129      * is indeterminate return IProgressMonitor.UNKNOWN.
130      *
131      * @return int IProgressMonitor.UNKNOWN or a value between 0 and 100.
132      */

133     int getPercentDone() {
134         if (totalWork == IProgressMonitor.UNKNOWN) {
135             return IProgressMonitor.UNKNOWN;
136         }
137
138         return Math.min((int) (preWork * 100 / totalWork), 100);
139     }
140
141     /**
142      * Return the progress for a monitor whose totalWork is
143      * <code>IProgressMonitor.UNKNOWN</code>.
144      *
145      * @return String
146      */

147     private String JavaDoc unknownProgress() {
148         if (taskName == null) {
149             return jobInfo.getJob().getName();
150         }
151         String JavaDoc[] messageValues = new String JavaDoc[2];
152         messageValues[0] = jobInfo.getJob().getName();
153         messageValues[1] = taskName;
154         return NLS
155                 .bind(ProgressMessages.JobInfo_UnknownProgress, messageValues);
156
157     }
158 }
159
Popular Tags