1 11 package org.eclipse.ui.internal.progress; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.jface.resource.JFaceResources; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.graphics.Image; 23 24 28 class JobInfo extends JobTreeElement { 29 30 private IStatus blockedStatus; 31 32 private boolean canceled = false; 33 private List children = Collections.synchronizedList(new ArrayList ()); 34 35 private Job job; 36 37 private GroupInfo parent; 38 39 private TaskInfo taskInfo; 40 41 private int ticks = -1; 43 44 49 JobInfo(Job enclosingJob) { 50 this.job = enclosingJob; 51 } 52 53 58 void addSubTask(String subTaskName) { 59 children.add(new SubTaskInfo(this, subTaskName)); 60 } 61 62 67 void addWork(double workIncrement) { 68 if (taskInfo == null) { 69 return; 70 } 71 if (parent == null || ticks < 1) { 72 taskInfo.addWork(workIncrement); 73 } else { 74 taskInfo.addWork(workIncrement, parent, ticks); 75 } 76 } 77 78 84 void beginTask(String taskName, int work) { 85 taskInfo = new TaskInfo(this, taskName, work); 86 } 87 88 93 public void cancel() { 94 this.canceled = true; 95 this.job.cancel(); 96 ProgressManager.getInstance().refreshJobInfo(this); 98 } 99 100 103 void clearChildren() { 104 children.clear(); 105 } 106 107 112 void clearTaskInfo() { 113 taskInfo = null; 114 } 115 116 123 private int compareJobs(JobInfo jobInfo) { 124 125 Job job2 = jobInfo.getJob(); 126 127 if (job.isUser()) { 129 if (!job2.isUser()) { 130 return -1; 131 } 132 } else { 133 if (job2.isUser()) { 134 return 1; 135 } 136 } 137 138 if (isBlocked()) { 140 if (!jobInfo.isBlocked()) { 141 return 1; 142 } 143 } else { 144 if (jobInfo.isBlocked()) { 145 return -1; 146 } 147 } 148 149 if (job.getPriority() == job2.getPriority()) { 150 return job.getName().compareTo(job2.getName()); 151 } 152 153 if (job.getPriority() > job2.getPriority()) { 154 return -1; 155 } 156 return 1; 157 } 158 159 164 public int compareTo(Object arg0) { 165 166 if (!(arg0 instanceof JobInfo)) { 167 return super.compareTo(arg0); 168 } 169 JobInfo element = (JobInfo) arg0; 170 171 if (isCanceled() && !element.isCanceled()) { 173 return 1; 174 } 175 176 if (element.getJob().getState() == getJob().getState()) { 177 return compareJobs(element); 178 } 179 180 if (getJob().getState() == Job.RUNNING) { 181 return -1; 182 } 183 return 1; 184 185 } 186 187 190 void dispose() { 191 if (parent != null) { 192 parent.removeJobInfo(this); 193 } 194 } 195 196 201 public IStatus getBlockedStatus() { 202 return blockedStatus; 203 } 204 205 210 Object [] getChildren() { 211 return children.toArray(); 212 } 213 214 219 String getCondensedDisplayString() { 220 TaskInfo info = getTaskInfo(); 221 if (info != null) { 222 return info.getDisplayStringWithoutTask(true); 223 } 224 return getJob().getName(); 225 } 226 227 232 public Image getDisplayImage() { 233 int done = getPercentDone(); 234 if (done > 0) { 235 return super.getDisplayImage(); 236 } 237 if (isBlocked()) { 238 return JFaceResources.getImage(ProgressManager.BLOCKED_JOB_KEY); 239 } 240 int state = getJob().getState(); 241 if (state == Job.SLEEPING) { 242 return JFaceResources.getImage(ProgressManager.SLEEPING_JOB_KEY); 243 } 244 if (state == Job.WAITING) { 245 return JFaceResources.getImage(ProgressManager.WAITING_JOB_KEY); 246 } 247 return super.getDisplayImage(); 249 250 } 251 254 String getDisplayString() { 255 return getDisplayString(true); 256 } 257 258 261 String getDisplayString(boolean showProgress) { 262 String name = getDisplayStringWithStatus(showProgress); 263 if (job.isSystem()) { 264 return NLS.bind(ProgressMessages.JobInfo_System, (new Object [] { name })); 265 } 266 return name; 267 } 268 269 277 private String getDisplayStringWithStatus(boolean showProgress) { 278 if (isCanceled()) { 279 return NLS.bind(ProgressMessages.JobInfo_Cancelled, (new Object [] { getJob().getName() })); 280 } 281 if (isBlocked()) { 282 return NLS.bind(ProgressMessages.JobInfo_Blocked, (new Object [] { getJob().getName(), 283 blockedStatus.getMessage() })); 284 } 285 if (getJob().getState() == Job.RUNNING) { 286 if (taskInfo == null) { 287 return getJob().getName(); 288 } 289 return taskInfo.getDisplayString(showProgress); 290 } 291 if (getJob().getState() == Job.SLEEPING) { 292 return NLS.bind(ProgressMessages.JobInfo_Sleeping, (new Object [] { getJob().getName() })); 293 } 294 295 return NLS.bind(ProgressMessages.JobInfo_Waiting, (new Object [] { getJob().getName() })); 296 297 } 298 299 304 GroupInfo getGroupInfo() { 305 if (parent != null && parent.isActive()) { 306 return parent; 307 } 308 return null; 309 } 310 311 316 Job getJob() { 317 return job; 318 } 319 320 325 Object getParent() { 326 return parent; 327 } 328 329 335 int getPercentDone() { 336 TaskInfo info = getTaskInfo(); 337 if (info != null){ 338 if(info.totalWork == IProgressMonitor.UNKNOWN) { 339 return IProgressMonitor.UNKNOWN; 340 } 341 if(info.totalWork == 0) { 342 return 0; 343 } 344 return (int) info.preWork * 100 / info.totalWork; 345 } 346 return IProgressMonitor.UNKNOWN; 347 } 348 349 352 TaskInfo getTaskInfo() { 353 return taskInfo; 354 } 355 356 361 boolean hasChildren() { 362 return children.size() > 0; 363 } 364 365 370 boolean hasTaskInfo() { 371 return taskInfo != null; 372 } 373 374 379 boolean isActive() { 380 return getJob().getState() != Job.NONE; 381 } 382 383 389 public boolean isBlocked() { 390 return getBlockedStatus() != null; 391 } 392 393 398 public boolean isCanceled() { 399 return canceled; 400 } 401 402 407 public boolean isCancellable() { 408 return super.isCancellable(); 409 } 410 411 416 boolean isJobInfo() { 417 return true; 418 } 419 420 426 public void setBlockedStatus(IStatus blockedStatus) { 427 this.blockedStatus = blockedStatus; 428 } 429 430 435 void setGroupInfo(GroupInfo group) { 436 parent = group; 437 } 438 439 444 void setTaskName(String name) { 445 taskInfo.setTaskName(name); 446 } 447 448 455 public void setTicks(int ticks) { 456 this.ticks = ticks; 457 } 458 459 } 460 | Popular Tags |