001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.progress;
011:
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.osgi.util.NLS;
014:
015: /**
016: * The TaskInfo is the info on a task with a job. It is assumed that there is
017: * only one task running at a time - any previous tasks in a Job will be
018: * deleted.
019: */
020: public class TaskInfo extends SubTaskInfo {
021: double preWork = 0;
022:
023: int totalWork = 0;
024:
025: /**
026: * Create a new instance of the receiver with the supplied total work and
027: * task name.
028: *
029: * @param parentJobInfo
030: * @param infoName
031: * @param total
032: */
033: TaskInfo(JobInfo parentJobInfo, String infoName, int total) {
034: super (parentJobInfo, infoName);
035: totalWork = total;
036: }
037:
038: /**
039: * Add the work increment to the total.
040: *
041: * @param workIncrement
042: */
043: void addWork(double workIncrement) {
044:
045: // Don't bother if we are indeterminate
046: if (totalWork == IProgressMonitor.UNKNOWN) {
047: return;
048: }
049: preWork += workIncrement;
050:
051: }
052:
053: /**
054: * Add the amount of work to the recevier. Update a parent monitor by the
055: * increment scaled to the amount of ticks this represents.
056: *
057: * @param workIncrement
058: * int the amount of work in the receiver
059: * @param parentMonitor
060: * The IProgressMonitor that is also listening
061: * @param parentTicks
062: * the number of ticks this monitor represents
063: */
064: void addWork(double workIncrement, IProgressMonitor parentMonitor,
065: int parentTicks) {
066: // Don't bother if we are indeterminate
067: if (totalWork == IProgressMonitor.UNKNOWN) {
068: return;
069: }
070:
071: addWork(workIncrement);
072: parentMonitor.internalWorked(workIncrement * parentTicks
073: / totalWork);
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.eclipse.ui.internal.progress.JobTreeElement#getDisplayString(boolean)
080: */
081: String getDisplayString(boolean showProgress) {
082:
083: if (totalWork == IProgressMonitor.UNKNOWN) {
084: return unknownProgress();
085: }
086:
087: if (taskName == null) {
088: return getDisplayStringWithoutTask(showProgress);
089: }
090:
091: if (showProgress) {
092: String[] messageValues = new String[3];
093: messageValues[0] = String.valueOf(getPercentDone());
094: messageValues[1] = jobInfo.getJob().getName();
095: messageValues[2] = taskName;
096:
097: return NLS.bind(ProgressMessages.JobInfo_DoneMessage,
098: messageValues);
099: }
100: String[] messageValues = new String[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 getDisplayStringWithoutTask(boolean showProgress) {
118:
119: if (!showProgress || totalWork == IProgressMonitor.UNKNOWN) {
120: return jobInfo.getJob().getName();
121: }
122:
123: return NLS.bind(ProgressMessages.JobInfo_NoTaskNameDoneMessage,
124: jobInfo.getJob().getName(), String
125: .valueOf(getPercentDone()));
126: }
127:
128: /**
129: * Return an integer representing the amount of work completed. If progress
130: * is indeterminate return IProgressMonitor.UNKNOWN.
131: *
132: * @return int IProgressMonitor.UNKNOWN or a value between 0 and 100.
133: */
134: int getPercentDone() {
135: if (totalWork == IProgressMonitor.UNKNOWN) {
136: return IProgressMonitor.UNKNOWN;
137: }
138:
139: return Math.min((int) (preWork * 100 / totalWork), 100);
140: }
141:
142: /**
143: * Return the progress for a monitor whose totalWork is
144: * <code>IProgressMonitor.UNKNOWN</code>.
145: *
146: * @return String
147: */
148: private String unknownProgress() {
149: if (taskName == null) {
150: return jobInfo.getJob().getName();
151: }
152: String[] messageValues = new String[2];
153: messageValues[0] = jobInfo.getJob().getName();
154: messageValues[1] = taskName;
155: return NLS.bind(ProgressMessages.JobInfo_UnknownProgress,
156: messageValues);
157:
158: }
159: }
|