srcanaapps/apiquerytool/com.nokia.s60tools.apiquery/src/com/nokia/s60tools/apiquery/shared/job/FindFileFromFoldersJob.java
changeset 0 a02c979e8dfd
equal deleted inserted replaced
-1:000000000000 0:a02c979e8dfd
       
     1 /*
       
     2 * Copyright (c) 2008 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 package com.nokia.s60tools.apiquery.shared.job;
       
    19 
       
    20 import java.io.File;
       
    21 import java.io.FileNotFoundException;
       
    22 import java.io.IOException;
       
    23 import java.util.Collection;
       
    24 import java.util.Iterator;
       
    25 import java.util.List;
       
    26 
       
    27 import org.eclipse.core.runtime.IProgressMonitor;
       
    28 import org.eclipse.core.runtime.IStatus;
       
    29 import org.eclipse.core.runtime.Platform;
       
    30 import org.eclipse.core.runtime.Status;
       
    31 import org.eclipse.core.runtime.jobs.Job;
       
    32 
       
    33 import com.nokia.s60tools.apiquery.shared.resources.Messages;
       
    34 import com.nokia.s60tools.apiquery.shared.util.console.APIQueryConsole;
       
    35 import com.nokia.s60tools.util.resource.FileFinder;
       
    36 import com.nokia.s60tools.util.resource.IFileFinderObserver;
       
    37 
       
    38 /**
       
    39  * Job for seeking header file from under SDK:s include paths given given. 
       
    40  */
       
    41 public class FindFileFromFoldersJob extends AbstractJob implements IFileFinderObserver {
       
    42 
       
    43 
       
    44 	
       
    45 	/**
       
    46 	 * Total steps as double, to be able to count percentages.
       
    47 	 */
       
    48 	double stepsAsDouble = 1;
       
    49 
       
    50 	/**
       
    51 	 * Name of the file to be seeked
       
    52 	 */
       
    53 	private final String fileName;
       
    54 
       
    55 	/**
       
    56 	 * Paths where to seek the file
       
    57 	 */
       
    58 	private final List<File> paths;
       
    59 
       
    60 	/**
       
    61 	 * Unique ID of SDK where file is seeked.
       
    62 	 */
       
    63 	private final String sdkId;
       
    64 
       
    65 	/**
       
    66 	 * Collection holds files found to be returned for client.
       
    67 	 */
       
    68 	private Collection<File> foundFiles = null;
       
    69 	
       
    70 	
       
    71 	/**
       
    72 	 * Create a Job to seek .metaxml files under selected SDK.
       
    73 	 * @param name
       
    74 	 * @param sdkInformation
       
    75 	 */
       
    76 	public FindFileFromFoldersJob(String name, List<File> paths, String fileName, String sdkId) {
       
    77 		super(name);
       
    78 		this.paths = paths;
       
    79 		this.fileName = fileName;
       
    80 		this.sdkId = sdkId;
       
    81 		setUser(true);
       
    82 	}
       
    83 
       
    84 
       
    85 	/* (non-Javadoc)
       
    86 	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
       
    87 	 */
       
    88 	protected IStatus run(IProgressMonitor monitor) {
       
    89 		setMonitor(monitor);
       
    90 		try {			
       
    91 			reportStartTime();
       
    92 			FileFinder finder = new FileFinder(APIQueryConsole.getInstance());
       
    93 
       
    94 			String fileNames [] = new String[1];
       
    95 			fileNames[0] = fileName;
       
    96 			String [] folders = new String[paths.size()];
       
    97 			int i = 0;
       
    98 			for (Iterator<File> iterator = paths.iterator(); iterator.hasNext();) {
       
    99 				File file = (File) iterator.next();
       
   100 				folders[i] = file.getAbsolutePath();
       
   101 				i++;
       
   102 			}
       
   103 			finder.seekFilesFromFolders(this, fileNames, folders);
       
   104 			return Job.ASYNC_FINISH;
       
   105 		} catch (Exception e) {
       
   106 			IStatus status = new Status(
       
   107 					Status.ERROR,Platform.PI_RUNTIME,
       
   108 					Status.ERROR,Messages.getString("FindFileFromFoldersJob.ErrorOnSeekin_StatusMsg_Part1") +fileName +Messages.getString("FindFileFromFoldersJob.ErrorOnSeekin_StatusMsg_Part2") +sdkId, e);  //$NON-NLS-1$ //$NON-NLS-2$
       
   109 			return status;
       
   110 		}
       
   111 		
       
   112 	}
       
   113 
       
   114 	/* (non-Javadoc)
       
   115 	 * @see com.nokia.s60tools.util.resource.IFileFinderObserver#completed(int, java.util.Collection)
       
   116 	 */
       
   117 	public void completed(int exitValue, Collection<File> files) {
       
   118 
       
   119 		try {
       
   120 			if(isCanceled() || exitValue == IStatus.CANCEL){
       
   121 				interrupted(Messages.getString("FindFileFromFoldersJob.CancelledByUser_Msg"));  //$NON-NLS-1$
       
   122 			}
       
   123 			else if(exitValue == IStatus.OK){
       
   124 				handleSeekCompleatedOK(files);
       
   125 			}
       
   126 			else{
       
   127 				handleSeekCompleatedError(exitValue);
       
   128 			}
       
   129 			reportEndTime();
       
   130 		} catch (Exception e) {
       
   131 			//Handle file not found e.g. errors.
       
   132 			e.printStackTrace();
       
   133 			handleSeekCompleatedError(e);
       
   134 		}
       
   135 	}
       
   136 
       
   137 	/**
       
   138 	 * Handle Error situations after getting data
       
   139 	 * @param e
       
   140 	 */
       
   141 	private void handleSeekCompleatedError(Exception e) {
       
   142 		String message = Messages.getString("FindFileFromFoldersJob.ErrorSeekingFile_ErrMsg_Part1") +fileName +Messages.getString("FindFileFromFoldersJob.ErrorSeekingFile_ErrMsg_Part2") +sdkId;  //$NON-NLS-1$ //$NON-NLS-2$
       
   143 		APIQueryConsole.getInstance().println(message, APIQueryConsole.MSG_ERROR);
       
   144 		IStatus status = new Status(
       
   145 				Status.ERROR,Platform.PI_RUNTIME,
       
   146 				Status.ERROR,message, e);
       
   147 		
       
   148 		done(status);
       
   149 	}
       
   150 	
       
   151 	/**
       
   152 	 * Handle error return values
       
   153 	 * @param exitValue
       
   154 	 */
       
   155 	private void handleSeekCompleatedError(int exitValue) {
       
   156 		String message = Messages.getString("FindFileFromFoldersJob.ErrorSeekingFileWithValue_ErrMsg_Part1") +fileName +Messages.getString("FindFileFromFoldersJob.ErrorSeekingFileWithValue_ErrMsg_Part2") +sdkId + Messages.getString("FindFileFromFoldersJob.ErrorSeekingFileWithValue_ErrMsg_Part3") +exitValue;  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   157 		APIQueryConsole.getInstance().println(message, APIQueryConsole.MSG_ERROR);		
       
   158 		IStatus status = new Status(
       
   159 				Status.ERROR,Platform.PI_RUNTIME,
       
   160 				Status.ERROR,message, null);
       
   161 		
       
   162 		done(status);
       
   163 	}
       
   164 
       
   165 	/**
       
   166 	 * If status was OK, handling completed situation
       
   167 	 * @param files
       
   168 	 * @throws FileNotFoundException
       
   169 	 * @throws IOException
       
   170 	 */
       
   171 	private void handleSeekCompleatedOK(Collection<File> files) throws FileNotFoundException, IOException {
       
   172 
       
   173 		//We are pretty much done, so marking that we are in 100% of progress, opening file found won't happen in this job anyway
       
   174 		progress(steps, Messages.getString("FindFileFromFoldersJob.FileFound_Progress_Msg_Part1") +fileName +Messages.getString("FindFileFromFoldersJob.FileFound_Progress_Msg_Part2"));  //$NON-NLS-1$ //$NON-NLS-2$
       
   175 		foundFiles = files;
       
   176 		reportEndTime();
       
   177 		done(Status.OK_STATUS);			
       
   178 	}
       
   179 
       
   180 
       
   181 	/**
       
   182 	 * Get file found in job
       
   183 	 * @return files found
       
   184 	 */
       
   185 	public Collection<File> getFoundSourceFiles(){
       
   186 		return foundFiles;
       
   187 	}
       
   188 
       
   189 	/* (non-Javadoc)
       
   190 	 * @see com.nokia.s60tools.util.resource.IFileFinderObserver#interrupted(java.lang.String)
       
   191 	 */
       
   192 	public void interrupted(String reasonMsg) {
       
   193 		done(Status.CANCEL_STATUS);
       
   194 		super.cancel();
       
   195 	}
       
   196 	
       
   197 	/* (non-Javadoc)
       
   198 	 * @see com.nokia.s60tools.util.resource.IFileFinderObserver#isCancelled()
       
   199 	 */
       
   200 	public boolean isCanceled(){
       
   201 		return getMonitor().isCanceled();
       
   202 	}
       
   203 	
       
   204 	/* (non-Javadoc)
       
   205 	 * @see com.nokia.s60tools.util.resource.IFileFinderObserver#beginTask(java.lang.String, int)
       
   206 	 */
       
   207 	public void beginTask(String name, int steps) {
       
   208 
       
   209 		//Its not allowed to dived with "0"
       
   210 		if(steps < 1){
       
   211 			steps = 1;
       
   212 		}
       
   213 		this.steps = steps;
       
   214 		stepsAsDouble = new Double(steps).doubleValue();
       
   215 		
       
   216 		getMonitor().beginTask(name, PROGRESS_COMPLETED_PERCENTAGE);
       
   217 	}
       
   218 
       
   219 	/* (non-Javadoc)
       
   220 	 * @see com.nokia.s60tools.apiquery.shared.job.AbstractJob#progress(int, java.lang.String)
       
   221 	 */
       
   222 	public void progress(int stepsCompleated, String prosessing) {
       
   223 
       
   224         try {
       
   225         	int persentage = (new Double( stepsCompleated / stepsAsDouble * 100)).intValue();
       
   226         	
       
   227 			super.progress(persentage, prosessing);
       
   228 		} catch (JobCancelledByUserException e) {
       
   229 			e.printStackTrace();
       
   230 			done(Status.CANCEL_STATUS);
       
   231 		}
       
   232 	}
       
   233 
       
   234 
       
   235 }