srcanaapps/apiquerytool/com.nokia.s60tools.apiquery/src/com/nokia/s60tools/apiquery/popup/actions/CheckProjectAction.java
changeset 0 a02c979e8dfd
equal deleted inserted replaced
-1:000000000000 0:a02c979e8dfd
       
     1 /*
       
     2 * Copyright (c) 2007 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.popup.actions;
       
    19 
       
    20 import org.eclipse.core.resources.IFile;
       
    21 import org.eclipse.core.resources.IFolder;
       
    22 import org.eclipse.core.resources.IProject;
       
    23 import org.eclipse.core.resources.IWorkspace;
       
    24 import org.eclipse.core.resources.IWorkspaceRunnable;
       
    25 import org.eclipse.core.resources.ResourcesPlugin;
       
    26 import org.eclipse.core.runtime.CoreException;
       
    27 import org.eclipse.core.runtime.IPath;
       
    28 import org.eclipse.core.runtime.IProgressMonitor;
       
    29 import org.eclipse.core.runtime.IStatus;
       
    30 import org.eclipse.core.runtime.Status;
       
    31 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
       
    32 import org.eclipse.core.runtime.jobs.IJobChangeListener;
       
    33 import org.eclipse.core.runtime.jobs.Job;
       
    34 import org.eclipse.jface.action.IAction;
       
    35 import org.eclipse.jface.viewers.ISelection;
       
    36 import org.eclipse.jface.viewers.StructuredSelection;
       
    37 import org.eclipse.swt.SWT;
       
    38 import org.eclipse.swt.widgets.Display;
       
    39 import org.eclipse.swt.widgets.Shell;
       
    40 import org.eclipse.ui.IObjectActionDelegate;
       
    41 import org.eclipse.ui.IWorkbenchPage;
       
    42 import org.eclipse.ui.IWorkbenchPart;
       
    43 import org.eclipse.ui.PlatformUI;
       
    44 import org.eclipse.ui.dialogs.SaveAsDialog;
       
    45 import org.eclipse.ui.ide.IDE;
       
    46 
       
    47 import com.nokia.s60tools.apiquery.job.ActiveProjectQueryJob;
       
    48 import com.nokia.s60tools.apiquery.servlets.APIQueryWebServerConfigurator;
       
    49 import com.nokia.s60tools.apiquery.shared.plugin.APIQueryPlugin;
       
    50 import com.nokia.s60tools.apiquery.shared.resources.Messages;
       
    51 import com.nokia.s60tools.apiquery.shared.ui.dialogs.APIQueryMessageBox;
       
    52 import com.nokia.s60tools.apiquery.shared.util.console.APIQueryConsole;
       
    53 import com.nokia.s60tools.apiquery.ui.dialogs.OpenReportStatusDialog;
       
    54 import com.nokia.s60tools.util.console.IConsolePrintUtility;
       
    55 
       
    56 /**
       
    57  * Check project action is invoked from <b>API Query > for Active Project</b> popup menu.
       
    58  * The action finds all the identifier candidates from the project and used them 
       
    59  * to form a query used to figure out which APIs the project is using. This
       
    60  * action supports only projects with Carbide.c++ project nature.
       
    61  */
       
    62 public class CheckProjectAction implements IObjectActionDelegate, IJobChangeListener {
       
    63 	
       
    64 
       
    65 	private static final String DEFAULT_EXPORT_FILENAME_SUFFIX = "-project_is_using_APIs.html"; //$NON-NLS-1$
       
    66 
       
    67 	/**
       
    68 	 * The project that user has selected from a project.
       
    69 	 */
       
    70 	IProject selectedProject;
       
    71 		
       
    72 	
       
    73 	/**
       
    74 	 * Constructor.
       
    75 	 */
       
    76 	public CheckProjectAction() {
       
    77 		super();
       
    78 
       
    79 	}
       
    80 
       
    81 	/* (non-Javadoc)
       
    82 	 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
       
    83 	 */
       
    84 	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
       
    85 	}
       
    86 
       
    87 	/* (non-Javadoc)
       
    88 	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
       
    89 	 */
       
    90 	public void run(IAction action) {
       
    91 		try {
       
    92 
       
    93 			final IPath path = openSaveAsDialog();
       
    94 			if(path == null){
       
    95 				return ;
       
    96 			}
       
    97 			
       
    98 			//When API Query for project is started, making sure that web server is running
       
    99 			APIQueryWebServerConfigurator.startServer(APIQueryWebServerConfigurator.Active_Project_Start);
       
   100 			
       
   101 			//Creating job, must be running inside of Runnable, because of UI Thread connections
       
   102 			final ActiveProjectQueryJob job = new ActiveProjectQueryJob(
       
   103 					Messages.getString("CheckProjectAction.APIQuery_JobName_Msg")+action.getText() +" (" + getProjectName()  //$NON-NLS-1$ //$NON-NLS-2$
       
   104 					+").", selectedProject, path); //$NON-NLS-1$
       
   105 		
       
   106 			job.setPriority(Job.DECORATE);
       
   107 			//Cant .join() to job because of deathlock @see .join() documentation.
       
   108 			job.addJobChangeListener(this);			
       
   109 			job.reportStartTime();
       
   110 			//Start to run
       
   111 			job.schedule();
       
   112 			
       
   113 		} catch (Exception e) {
       
   114 			e.printStackTrace();
       
   115 			APIQueryConsole.getInstance().println(e.getMessage(), 
       
   116 					 IConsolePrintUtility.MSG_ERROR);
       
   117 			showErrorDialog(Messages.getString("CheckProjectAction.CannotGenerateReport_ErrMsg")  //$NON-NLS-1$
       
   118 					+getProjectName() +". " +e.getMessage());			 //$NON-NLS-1$
       
   119 		}
       
   120 	}
       
   121 
       
   122 
       
   123 	/* (non-Javadoc)
       
   124 	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
       
   125 	 */
       
   126 	public void selectionChanged(IAction action, ISelection selection) {
       
   127 		// Storing the selected file instance used for resolving the active project
       
   128 		StructuredSelection structSel = (StructuredSelection) selection;
       
   129 		Object elem = structSel.getFirstElement();
       
   130 		if(elem instanceof IProject){
       
   131 			 this.selectedProject = (IProject) elem;			
       
   132 		}
       
   133 		else if(elem instanceof IFile){
       
   134 			 this.selectedProject = ((IFile) elem).getProject();						
       
   135 		}
       
   136 		else if(elem instanceof IFolder){
       
   137 			 this.selectedProject = ((IFolder) elem).getProject();						
       
   138 		}
       
   139 		else{
       
   140 			action.setEnabled(false);
       
   141 			return;
       
   142 		}
       
   143 		action.setEnabled(true);
       
   144 	}
       
   145 	
       
   146 	
       
   147 	/**
       
   148 	 * Shows save as dialog and set this.exportFileName
       
   149 	 * @return IPath path if fileName was set, null if dialog was not epened or cancel was pushed
       
   150 	 *
       
   151 	 */
       
   152 	private IPath openSaveAsDialog() {
       
   153 		Shell shell = APIQueryPlugin.getCurrentlyActiveWbWindowShell();
       
   154 		SaveAsDialog saveAs = new SaveAsDialog(shell);
       
   155 		IFile file = selectedProject.getFile(getDefaultFileName());
       
   156 		saveAs.setOriginalFile(file);
       
   157 		
       
   158 		int status = saveAs.open();
       
   159 		if(status == SaveAsDialog.OK ){
       
   160 			IPath path = saveAs.getResult();
       
   161 			if(saveAs.getReturnCode() == SaveAsDialog.OK ){
       
   162 				return path;
       
   163 			}else{
       
   164 				return null;
       
   165 			}
       
   166 		}
       
   167 		else{
       
   168 			return null;
       
   169 		}
       
   170 	}
       
   171 	
       
   172 	
       
   173 	/**
       
   174 	 * Asking user if he/she want's to open created file or not
       
   175 	 * by opening a Dialog
       
   176 	 * @param newFile
       
   177 	 * @return
       
   178 	 */
       
   179 	private boolean openRequired(IFile newFile){
       
   180 		Shell sh = APIQueryPlugin.getCurrentlyActiveWbWindowShell();
       
   181 		String fileName = newFile.getName();
       
   182 		OpenReportStatusDialog in = new OpenReportStatusDialog(sh, fileName);
       
   183 		
       
   184 		in.open();
       
   185 		if( in.getReturnCode() == OpenReportStatusDialog.OK){
       
   186 			return true;
       
   187 		}
       
   188 		else{
       
   189 			return false;
       
   190 		}
       
   191 	}
       
   192 	
       
   193 	/**
       
   194 	 * Opens generated report to workspace
       
   195 	 * @param newFile
       
   196 	 * @throws CoreException
       
   197 	 */
       
   198 	private void openReport(final IFile newFile) throws CoreException{
       
   199 		
       
   200 		if(!openRequired(newFile)){
       
   201 			return;
       
   202 		}
       
   203 		
       
   204 		final IWorkspace workspace = ResourcesPlugin.getWorkspace();
       
   205 		//Runnable to open new file
       
   206 		final IWorkspaceRunnable runOpen = new IWorkspaceRunnable() {
       
   207 			public void run(IProgressMonitor monitor) throws CoreException {
       
   208 				// do the actual work in here
       
   209 	
       
   210 				IWorkbenchPage page = PlatformUI.getWorkbench()
       
   211 						.getActiveWorkbenchWindow().getActivePage();
       
   212 				try {
       
   213 					IDE.openEditor(page, newFile, true);
       
   214 	
       
   215 				} catch (Exception e) {
       
   216 					//PartInitException may occur
       
   217 					e.printStackTrace();
       
   218 					Status status = new Status(IStatus.ERROR,
       
   219 							"com.nokia.s60tools.apiquery", 0, e.getMessage(), e); //$NON-NLS-1$
       
   220 	
       
   221 					throw new CoreException(status);
       
   222 				} 
       
   223 	
       
   224 			}
       
   225 		};	
       
   226 		workspace.run(runOpen, null, IWorkspace.AVOID_UPDATE, null);
       
   227 	}
       
   228 
       
   229 
       
   230 	/**
       
   231 	 * get default name export file to
       
   232 	 * @return filename
       
   233 	 */
       
   234 	private String getDefaultFileName() {
       
   235 		String name = getProjectName() +DEFAULT_EXPORT_FILENAME_SUFFIX;
       
   236 		return name;
       
   237 	}
       
   238 
       
   239 
       
   240 	/**
       
   241 	 * get project name
       
   242 	 * @return project name
       
   243 	 */
       
   244 	private String getProjectName() {
       
   245 		return selectedProject.getName();
       
   246 	}
       
   247 	
       
   248 	/**
       
   249 	 * Show an error Dialog
       
   250 	 * @param errMsg
       
   251 	 */
       
   252 	
       
   253 	private void showErrorDialog(String errMsg) {
       
   254 		APIQueryMessageBox mbox = new APIQueryMessageBox(errMsg, SWT.ICON_ERROR | SWT.OK);
       
   255 		mbox.open();
       
   256 	}
       
   257 
       
   258 
       
   259 	/* (non-Javadoc)
       
   260 	 * @see org.eclipse.core.runtime.jobs.IJobChangeListener#aboutToRun(org.eclipse.core.runtime.jobs.IJobChangeEvent)
       
   261 	 */
       
   262 	public void aboutToRun(IJobChangeEvent event) {
       
   263 	}
       
   264 	/* (non-Javadoc)
       
   265 	 * @see org.eclipse.core.runtime.jobs.IJobChangeListener#awake(org.eclipse.core.runtime.jobs.IJobChangeEvent)
       
   266 	 */
       
   267 	public void awake(IJobChangeEvent event) {
       
   268 	}
       
   269 	/* (non-Javadoc)
       
   270 	 * @see org.eclipse.core.runtime.jobs.IJobChangeListener#running(org.eclipse.core.runtime.jobs.IJobChangeEvent)
       
   271 	 */
       
   272 	public void running(IJobChangeEvent event) {
       
   273 	}
       
   274 	/* (non-Javadoc)
       
   275 	 * @see org.eclipse.core.runtime.jobs.IJobChangeListener#scheduled(org.eclipse.core.runtime.jobs.IJobChangeEvent)
       
   276 	 */
       
   277 	public void scheduled(IJobChangeEvent event) {
       
   278 	}
       
   279 	/* (non-Javadoc)
       
   280 	 * @see org.eclipse.core.runtime.jobs.IJobChangeListener#sleeping(org.eclipse.core.runtime.jobs.IJobChangeEvent)
       
   281 	 */
       
   282 	public void sleeping(IJobChangeEvent event) {
       
   283 	}	
       
   284 
       
   285 	/* (non-Javadoc)
       
   286 	 * @see org.eclipse.core.runtime.jobs.IJobChangeListener#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
       
   287 	 * 
       
   288 	 * When done, asking if user wants to open created file. If Cancelled, doing nothing,
       
   289 	 * if an error occurs, system will show an error dialog.
       
   290 	 * 
       
   291 	 */
       
   292 	public void done(IJobChangeEvent event) {
       
   293 
       
   294 		ActiveProjectQueryJob job = (ActiveProjectQueryJob) event.getJob();
       
   295 		
       
   296 		job.reportEndTime();
       
   297 		
       
   298 		IStatus status = job.getResult();
       
   299 		
       
   300 		if(status.getSeverity() == IStatus.OK){
       
   301 			final IFile file = job.getGeneratedReportFile();
       
   302 
       
   303 				
       
   304 			// Must do query in Runnable, because of UI actions from job
       
   305 			Runnable activeProjectQueryJobRunnable = new Runnable() {
       
   306 				public void run() {
       
   307 					try {
       
   308 						openReport(file);
       
   309 					} catch (CoreException e) {
       
   310 						APIQueryConsole.getInstance().println(Messages.getString("CheckProjectAction.UnableToOpenReport_ErrMsg") + e.getMessage(),  //$NON-NLS-1$
       
   311 								 IConsolePrintUtility.MSG_ERROR);
       
   312 						e.printStackTrace();
       
   313 					}
       
   314 				}
       
   315 			};
       
   316 
       
   317 			// Showing a visible message has to be done in its own thread
       
   318 			// in order not to cause invalid thread access
       
   319 			Display.getDefault().asyncExec(activeProjectQueryJobRunnable);				
       
   320 				
       
   321 		
       
   322 		}
       
   323 
       
   324 	}
       
   325 
       
   326 }