1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.sdkuilib.internal.tasks; 18 19import com.android.sdklib.internal.repository.ITask; 20import com.android.sdklib.internal.repository.ITaskFactory; 21import com.android.sdklib.internal.repository.ITaskMonitor; 22 23import org.eclipse.swt.widgets.Shell; 24 25/** 26 * An {@link ITaskFactory} that creates a new {@link ProgressTask} dialog 27 * for each new task. 28 */ 29public final class ProgressTaskFactory implements ITaskFactory { 30 31 private final Shell mShell; 32 33 public ProgressTaskFactory(Shell shell) { 34 mShell = shell; 35 } 36 37 @Override 38 public void start(String title, ITask task) { 39 start(title, null /*parentMonitor*/, task); 40 } 41 42 @Override 43 public void start(String title, ITaskMonitor parentMonitor, ITask task) { 44 45 if (parentMonitor == null) { 46 ProgressTask p = new ProgressTask(mShell, title); 47 p.start(task); 48 } else { 49 // Use all the reminder of the parent monitor. 50 if (parentMonitor.getProgressMax() == 0) { 51 parentMonitor.setProgressMax(1); 52 } 53 54 ITaskMonitor sub = parentMonitor.createSubMonitor( 55 parentMonitor.getProgressMax() - parentMonitor.getProgress()); 56 try { 57 task.run(sub); 58 } finally { 59 int delta = 60 sub.getProgressMax() - sub.getProgress(); 61 if (delta > 0) { 62 sub.incProgress(delta); 63 } 64 } 65 } 66 } 67} 68