srcanaapps/apiquerytool/com.nokia.s60tools.apiquery/src/com/nokia/s60tools/apiquery/shared/job/AbstractJob.java
changeset 0 a02c979e8dfd
equal deleted inserted replaced
-1:000000000000 0:a02c979e8dfd
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.s60tools.apiquery.shared.job;
       
    20 
       
    21 import java.util.Date;
       
    22 
       
    23 import org.eclipse.core.runtime.IProgressMonitor;
       
    24 import org.eclipse.core.runtime.jobs.Job;
       
    25 
       
    26 import com.nokia.s60tools.apiquery.shared.resources.Messages;
       
    27 import com.nokia.s60tools.util.debug.DbgUtility;
       
    28 
       
    29 /**
       
    30  * Abstract implementation of {@link Job}. Implements few functionalities so sub classes
       
    31  * can be written with less code.
       
    32  */
       
    33 public abstract class AbstractJob extends Job implements IJobProgressStatus, IManageableJob{
       
    34 
       
    35 	protected static final int PROGRESS_COMPLETED_PERCENTAGE = 100;
       
    36 	protected int previousPercentage = 0;
       
    37 	private IProgressMonitor monitor = null;
       
    38 	private Process process = null;
       
    39 	private long runMethodStartTime; 
       
    40 	
       
    41 	protected int steps = 100; // percentage figure
       
    42 	
       
    43 	/**
       
    44 	 * Use only this constructor
       
    45 	 * 
       
    46 	 * @param name Job name (root component name)
       
    47 	 */
       
    48 	public AbstractJob(String name) {
       
    49 		super(name);		
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * Get start time and print it
       
    54 	 * @return start time in ms (System.currentTimeMillis())
       
    55 	 */
       
    56 	public long reportStartTime(){
       
    57 		this.runMethodStartTime = System.currentTimeMillis();
       
    58 		DbgUtility.println(DbgUtility.PRIORITY_OPERATION, 
       
    59 							"Job: '" +super.getName() + "' start time: "   //$NON-NLS-1$ //$NON-NLS-2$
       
    60 							+ new Date(runMethodStartTime).toString());	
       
    61 		return runMethodStartTime;	
       
    62 	}
       
    63 	
       
    64 	/**
       
    65 	 * count time taken since startTime, printing it
       
    66 	 * @param runMethodStartTime
       
    67 	 */
       
    68 	public void reportEndTime(){
       
    69 		long endTime = System.currentTimeMillis();		
       
    70 		DbgUtility.println(DbgUtility.PRIORITY_OPERATION, 
       
    71 				"Job " +super.getName() + " ended: " + new Date(endTime).toString());  //$NON-NLS-1$ //$NON-NLS-2$
       
    72 		DbgUtility.println(DbgUtility.PRIORITY_OPERATION,
       
    73 						  "TOTAL: "  //$NON-NLS-1$
       
    74 				           + (endTime-runMethodStartTime)/1000.0 + " seconds!"); //$NON-NLS-1$
       
    75 	}
       
    76 	
       
    77 
       
    78 
       
    79 	/* (non-Javadoc)
       
    80 	 * @see com.nokia.s60tools.apiquery.shared.job.IJobProgressStatus#progress(int, java.lang.String)
       
    81 	 */
       
    82 	public void progress(int percentage, String prosessing) 
       
    83 		throws JobCancelledByUserException {
       
    84 				
       
    85         if (isCanceled()){
       
    86         	String msg = Messages.getString("AbstractJob.JobCancelled_Part1_ErrMsg") +super.getName() + Messages.getString("AbstractJob.JobCancelled_Part2_ErrMsg"); //$NON-NLS-1$ //$NON-NLS-2$
       
    87 		    throw new JobCancelledByUserException(msg);
       
    88         }
       
    89         //Making sure that there is no percentages higher than 100
       
    90         if(percentage > PROGRESS_COMPLETED_PERCENTAGE){
       
    91         	percentage = PROGRESS_COMPLETED_PERCENTAGE;
       
    92         }
       
    93         
       
    94         monitor.subTask(percentage + Messages.getString("AbstractJob.Percentage_Complete_Msg")        		         //$NON-NLS-1$
       
    95         		        + Messages.getString("AbstractJob.Prosessing_Msg") //$NON-NLS-1$
       
    96         		        + prosessing);			
       
    97         monitor.worked(percentage - previousPercentage);
       
    98         previousPercentage = percentage;
       
    99 
       
   100 	}
       
   101 
       
   102 
       
   103 	/* (non-Javadoc)
       
   104 	 * @see com.nokia.s60tools.apiquery.shared.job.IJobProgressStatus#isCanceled()
       
   105 	 */
       
   106 	public boolean isCanceled() {
       
   107 		return monitor.isCanceled();
       
   108 	}	
       
   109 	
       
   110 
       
   111 	/* (non-Javadoc)
       
   112 	 * @see com.nokia.s60tools.apiquery.shared.job.IManageableJob#forcedShutdown()
       
   113 	 */
       
   114 	public void forcedShutdown() {
       
   115 		if(process != null){
       
   116 			process.destroy();
       
   117 			try {
       
   118 				process.waitFor();
       
   119 			} catch (InterruptedException e) {
       
   120 				e.printStackTrace();
       
   121 			}					
       
   122 		}
       
   123 	}
       
   124 
       
   125 
       
   126 	/**
       
   127 	 * Get Process
       
   128 	 * @return Process
       
   129 	 */
       
   130 	protected Process getProcess() {
       
   131 		return process;
       
   132 	}
       
   133 
       
   134 
       
   135 	/**
       
   136 	 * Set process
       
   137 	 * @param process
       
   138 	 */
       
   139 	protected void setProcess(Process process) {
       
   140 		this.process = process;
       
   141 	}
       
   142 
       
   143 	/**
       
   144 	 * Get progress monitor
       
   145 	 * @return
       
   146 	 */
       
   147 	protected IProgressMonitor getMonitor() {
       
   148 		return monitor;
       
   149 	}
       
   150 
       
   151 	/**
       
   152 	 * Set progress monitor to this Job
       
   153 	 * @param monitor
       
   154 	 */
       
   155 	protected void setMonitor(IProgressMonitor monitor) {
       
   156 		this.monitor = monitor;
       
   157 	}
       
   158 	
       
   159 }