sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/ui/NewPIWizard.java
changeset 2 b9ab3b238396
child 5 844b047e260d
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 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 the License "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.carbide.cpp.internal.pi.wizards.ui;
       
    19 
       
    20 import java.io.IOException;
       
    21 import java.util.ArrayList;
       
    22 import java.util.HashMap;
       
    23 import java.util.List;
       
    24 import java.util.Map;
       
    25 import java.util.Set;
       
    26 import java.util.Map.Entry;
       
    27 
       
    28 import org.eclipse.core.resources.IFile;
       
    29 import org.eclipse.core.resources.IProject;
       
    30 import org.eclipse.core.runtime.CoreException;
       
    31 import org.eclipse.core.runtime.IConfigurationElement;
       
    32 import org.eclipse.core.runtime.IPath;
       
    33 import org.eclipse.jface.dialogs.DialogSettings;
       
    34 import org.eclipse.jface.dialogs.IDialogSettings;
       
    35 import org.eclipse.jface.viewers.IStructuredSelection;
       
    36 import org.eclipse.jface.wizard.IWizardPage;
       
    37 import org.eclipse.jface.wizard.Wizard;
       
    38 import org.eclipse.swt.widgets.Composite;
       
    39 import org.eclipse.swt.widgets.Display;
       
    40 import org.eclipse.ui.IImportWizard;
       
    41 import org.eclipse.ui.INewWizard;
       
    42 import org.eclipse.ui.IWorkbench;
       
    43 
       
    44 import com.nokia.carbide.cdt.builder.builder.CarbideCPPBuilder;
       
    45 import com.nokia.carbide.cdt.builder.project.ICarbideBuildConfiguration;
       
    46 import com.nokia.carbide.cdt.builder.project.ISISBuilderInfo;
       
    47 import com.nokia.carbide.cpp.internal.api.sdk.SymbianBuildContext;
       
    48 import com.nokia.carbide.cpp.internal.pi.wizards.ui.util.IPkgEntry;
       
    49 import com.nokia.carbide.cpp.internal.pi.wizards.ui.util.RofsObySymbolPair;
       
    50 import com.nokia.carbide.cpp.pi.button.BupEventMapManager;
       
    51 import com.nokia.carbide.cpp.pi.button.ButtonPlugin;
       
    52 import com.nokia.carbide.cpp.pi.importer.SampleImporter;
       
    53 import com.nokia.carbide.cpp.pi.wizards.WizardsPlugin;
       
    54 import com.nokia.carbide.cpp.sdk.core.ISymbianBuildContext;
       
    55 import com.nokia.carbide.cpp.ui.CarbideUIPlugin;
       
    56 import com.nokia.carbide.cpp.ui.ICarbideSharedImages;
       
    57 import com.nokia.cpp.internal.api.utils.core.Check;
       
    58 import com.nokia.cpp.internal.api.utils.core.FileUtils;
       
    59 
       
    60 public class NewPIWizard extends Wizard implements IImportWizard, INewWizard{
       
    61 	
       
    62 	//	The shared instance.
       
    63 	private static NewPIWizard wizard;
       
    64 	
       
    65 	static final int PROGRESS_WORK_TOTAL = 10000; // handy total for progress
       
    66 													// indicator
       
    67 	static final String DIALOG_SETTING_FILE = WizardsPlugin.getDefault().getStateLocation().append("dialog_settings.xml").toOSString();	//$NON-NLS-1$
       
    68 	
       
    69 	private NewPIWizardPageInputTask pageInput;
       
    70 	private NewPIWizardPageConfigSelectorTask pageConfigSelector;
       
    71 	private NewPIWizardPagePkgListTask pagePkgList;
       
    72 	@SuppressWarnings("unused") //$NON-NLS-1$
       
    73 	private NewPIWizardPageRomSdkSubTask pageRomPkgSdk;
       
    74 	private NewPIWizardPageRomSubTask pageObySym;
       
    75 	private NewPIWizardPageCustomTask pageCustom;
       
    76 	private NewPIWizardPageBupMapTask pageBupMap;
       
    77 	private NewPIWizardPageOutputTask pageOutput;
       
    78 	private ArrayList<IFile> tmpPkgList = new ArrayList<IFile>();
       
    79 	private Set<Integer> traceSet;
       
    80 	
       
    81 	protected IProject piProject;
       
    82 	IWorkbench workbench;
       
    83 	
       
    84 	NewPIWizardSettings wizardSettings = NewPIWizardSettings.getInstance();
       
    85 
       
    86 	/**
       
    87 	 * Constructor for NewPIWizard.
       
    88 	 */
       
    89 	public NewPIWizard() {
       
    90 		super();
       
    91 		wizard = this;
       
    92 		setNeedsProgressMonitor(true);
       
    93 		ICarbideSharedImages carbideImages = CarbideUIPlugin.getDefault().getSharedImages();
       
    94 		setDefaultPageImageDescriptor(carbideImages.getImageDescriptor(carbideImages.IMG_PI_IMPORT_WIZARD_BANNER_75_66));
       
    95 		setWindowTitle(Messages.getString("NewPIWizard.window.title")); //$NON-NLS-1$
       
    96 
       
    97 		//DialogSettings dialogSettings = (DialogSettings) WizardsPlugin.getDefault().getDialogSettings();
       
    98 		IDialogSettings dialogSettings = new DialogSettings("import_wizard");	//$NON-NLS-1$
       
    99 		try {
       
   100 			dialogSettings.load(DIALOG_SETTING_FILE);
       
   101 		} catch (IOException e) {
       
   102 			// saving last session is not that important
       
   103 			//e.printStackTrace();
       
   104 		}
       
   105 
       
   106 		wizardSettings.clear();
       
   107 		wizardSettings.restoreState(dialogSettings);
       
   108 		setDialogSettings(dialogSettings);
       
   109 	}
       
   110 	
       
   111 	static public NewPIWizard getDefault() {
       
   112 		return wizard;
       
   113 	}
       
   114 	
       
   115 	/**
       
   116 	 * Adding the pages to the wizard.
       
   117 	 */
       
   118 
       
   119 	public void addPages() {
       
   120 		addPage(pageInput = new NewPIWizardPageInputTask(this));
       
   121 		addPage(pageConfigSelector = new NewPIWizardPageConfigSelectorTask());
       
   122 		addPage(pagePkgList = new NewPIWizardPagePkgListTask());
       
   123 		// merge SDK to symbol page
       
   124 		//addPage(pageRomPkgSdk = new NewPIWizardPageRomSdkSubTask());
       
   125 		addPage(pageObySym = new NewPIWizardPageRomSubTask());
       
   126 		addPage(pageBupMap = new NewPIWizardPageBupMapTask());
       
   127 		addPage(pageCustom = new NewPIWizardPageCustomTask(this));
       
   128 		addPage(pageOutput = new NewPIWizardPageOutputTask());		
       
   129 	}
       
   130 	
       
   131 	public void createPageControls(Composite pageContainer) {
       
   132 		super.createPageControls(pageContainer);
       
   133 		// flush out all settings for very page, so we can hit finish
       
   134 		IWizardPage[] pages = getPages();
       
   135 		for (IWizardPage page: pages) {
       
   136 			if (page instanceof INewPIWizardSettings) {
       
   137 				((INewPIWizardSettings)page).setupPageFromFromNewPIWizardSettings();
       
   138 			}
       
   139 		}
       
   140 
       
   141 	}
       
   142 
       
   143 	/**
       
   144 	 * This method is called when 'Finish' button is pressed in the wizard. We
       
   145 	 * will create an operation and run it using wizard as execution context.
       
   146 	 */
       
   147 	public boolean performFinish() {
       
   148 		pageOutput.regenerateOutputIfIntputChanged(); // this must stay out of asyncExec() since it require treeviewer selection
       
   149 		Display.getDefault().asyncExec( new Runnable() {
       
   150 			public void run () {
       
   151 				//DialogSettings dialogSettings = (DialogSettings) WizardsPlugin.getDefault().getDialogSettings();
       
   152 				IDialogSettings dialogSettings = getDialogSettings();
       
   153 				if (dialogSettings != null) {
       
   154 					NewPIWizardSettings.getInstance().saveState(dialogSettings);
       
   155 					setDialogSettings(dialogSettings);
       
   156 					try {
       
   157 						dialogSettings.save(DIALOG_SETTING_FILE);
       
   158 					} catch (IOException e) {
       
   159 						// saving last session is not that important
       
   160 						e.printStackTrace();
       
   161 					}
       
   162 				}
       
   163 				createNewProject();
       
   164 			
       
   165 			}
       
   166 		});
       
   167 		
       
   168 		return true;
       
   169 	}
       
   170 	
       
   171 	private IFile createTempPkgFile() {
       
   172 		try {
       
   173 			java.io.File prepocessedPkgFile;
       
   174 			prepocessedPkgFile = java.io.File.createTempFile("importpi", ".pkg", new java.io.File(wizardSettings.outputContainer.getLocation().toString()));	//$NON-NLS-1$ //$NON-NLS-2$
       
   175 			prepocessedPkgFile.deleteOnExit();
       
   176 			// force eclipse be be aware of the added file from the IFile refresh
       
   177 			wizardSettings.outputContainer.refreshLocal(0, null);
       
   178 			IFile tmpPkgFile = FileUtils.convertFileToIFile(wizardSettings.outputContainer, prepocessedPkgFile);
       
   179 			if (tmpPkgFile == null) {
       
   180 				Check.reportFailure(prepocessedPkgFile.getAbsolutePath() + Messages.getString("NewPIWizard.failed.convertFileToIFile"), new Throwable()); //$NON-NLS-1$
       
   181 				return null;
       
   182 			}
       
   183 			tmpPkgList.add(tmpPkgFile);
       
   184 			return tmpPkgFile;
       
   185 		} catch (IOException e1) {
       
   186 			e1.printStackTrace();
       
   187 		} catch (CoreException e) {
       
   188 			e.printStackTrace();
       
   189 		}
       
   190 		return null;
       
   191 	}
       
   192 	
       
   193 	private void cleanTempPkgFile() {
       
   194 		for(IFile file : tmpPkgList) {
       
   195 			// just to delete using java.io as well for consistence
       
   196 			java.io.File javaFile = new java.io.File(file.getLocation().toString());
       
   197 			javaFile.delete();
       
   198 			try {
       
   199 				// force eclipse be be aware of the removed file from the IFile refresh
       
   200 				file.refreshLocal(0, null);
       
   201 			} catch (CoreException e) {
       
   202 				e.printStackTrace();
       
   203 			}
       
   204 		}
       
   205 		tmpPkgList.clear();
       
   206 	}
       
   207 
       
   208 	private void createNewProject() {
       
   209 		SampleImporter sampleImporter = SampleImporter.getInstance();
       
   210 		NewPIWizardSettings wizardSettings = NewPIWizardSettings.getInstance();
       
   211 		Map<Object, IFile> preprocessedMap = new HashMap<Object, IFile>();
       
   212 		sampleImporter.clear();
       
   213 		
       
   214 		sampleImporter.setDatFileName(wizardSettings.sampleFileName);
       
   215 		String container = wizardSettings.outputContainer.getFullPath().toString();
       
   216 		if (container.startsWith("/")) //$NON-NLS-1$
       
   217 			container = container.substring(1, container.length());
       
   218 		sampleImporter.setProjectName(container);
       
   219 		sampleImporter.setPiFileName(wizardSettings.piFileName);
       
   220 		if (wizardSettings.haveRomOnly || wizardSettings.haveAppRom) {
       
   221 			if (wizardSettings.romSdk != null && wizardSettings.romSdk.getEPOCROOT() != null) {
       
   222 				sampleImporter.setRomEpocroot(wizardSettings.romSdk.getEPOCROOT());
       
   223 			}
       
   224 			if (wizardSettings.romSymbolFile != null) {
       
   225 				sampleImporter.setRomSymbolFile(wizardSettings.romSymbolFile);
       
   226 			}
       
   227 			if (wizardSettings.romObyFile != null) {
       
   228 				sampleImporter.setRomObyFile(wizardSettings.romObyFile);
       
   229 			}
       
   230 			sampleImporter.clearRofsObyFileList();
       
   231 			sampleImporter.clearRofsSymbolFileList();
       
   232 			for (RofsObySymbolPair pair : wizardSettings.rofsObySymbolPairList) {
       
   233 				sampleImporter.addRofsObyFile(pair.getObyFile());
       
   234 				sampleImporter.addRofsSymbolFile(pair.getSymbolFile());
       
   235 			}
       
   236 		}
       
   237 		if (wizardSettings.haveAppOnly || wizardSettings.haveAppRom) {
       
   238 			for (IPkgEntry entry :wizardSettings.selectedAppFileList) {
       
   239 				java.io.File javaFile = new java.io.File(entry.getPkgFile());
       
   240 				if (javaFile.exists()) {
       
   241 					IFile tmpPkgFile = createTempPkgFile();
       
   242 					if (tmpPkgFile == null) {
       
   243 						Check.reportFailure(tmpPkgFile.getLocation().toString() + Messages.getString("NewPIWizard.failed.convertFileToIFile"), new Throwable()); //$NON-NLS-1$
       
   244 						return;
       
   245 					}
       
   246 					{
       
   247 						try {
       
   248 							char[] pkgFileBuf = FileUtils.readFileContents(javaFile, null);
       
   249 							String pkgFileStr = new String(pkgFileBuf);
       
   250 							FileUtils.writeFileContents(new java.io.File(tmpPkgFile.getLocation().toString()), pkgFileStr.toCharArray(), null);
       
   251 						} catch (CoreException e) {
       
   252 							e.printStackTrace();
       
   253 						}
       
   254 					}
       
   255 					preprocessedMap.put(entry, tmpPkgFile);
       
   256 				}
       
   257 			}
       
   258 			for (ICarbideBuildConfiguration config : wizardSettings.selectedBuildConfigList) {
       
   259 				ISymbianBuildContext context = SymbianBuildContext.getBuildContextFromDisplayName(config.getDisplayString());
       
   260 				IFile tmpPkgFile;
       
   261 				
       
   262 				
       
   263 				List<ISISBuilderInfo> sisBuilderInfoList = config.getSISBuilderInfoList();
       
   264 				for (ISISBuilderInfo sisBuildInfo :sisBuilderInfoList) {
       
   265 					tmpPkgFile= createTempPkgFile();
       
   266 					if (tmpPkgFile == null) {
       
   267 						Check.reportFailure(tmpPkgFile.getLocation().toString() + Messages.getString("NewPIWizard.failed.convertFileToIFile"), new Throwable()); //$NON-NLS-1$
       
   268 						return;
       
   269 					}
       
   270 					IPath tmpPkgPath = tmpPkgFile.getLocation();
       
   271 					CarbideCPPBuilder.resolvePKGFile(sisBuildInfo.getPKGFullPath(), 
       
   272 							context, 
       
   273 							tmpPkgPath);
       
   274 					preprocessedMap.put(config, tmpPkgFile);
       
   275 				}
       
   276 			}
       
   277 		}
       
   278 		if (preprocessedMap.size() > 0) {
       
   279 			for(Entry<Object, IFile> entry : preprocessedMap.entrySet()) {
       
   280 				String epocroot = ""; //$NON-NLS-1$
       
   281 				if (entry.getKey() instanceof IPkgEntry) {
       
   282 					epocroot = ((IPkgEntry)entry.getKey()).getSdk().getEPOCROOT();
       
   283 				} else if (entry.getKey() instanceof ICarbideBuildConfiguration) {
       
   284 					epocroot = ((ICarbideBuildConfiguration)entry.getKey()).getSDK().getEPOCROOT();
       
   285 				}
       
   286 				sampleImporter.addPkgObyFile(epocroot, entry.getValue().getLocation().toString());
       
   287 			}
       
   288 		}
       
   289 		
       
   290 		if (wizardSettings.keyMapProfile != null) {
       
   291 			//BUP key press profile
       
   292 			sampleImporter.setBupMapProfileId(wizardSettings.keyMapProfile.getProfileId());
       
   293 			if (wizardSettings.keyMapProfile.getSDK() != null) {
       
   294 				sampleImporter.setBupMapSymbianSDKId(wizardSettings.keyMapProfile.getSDK().getUniqueId());
       
   295 				sampleImporter.setBupMapIsBuiltIn(false);
       
   296 				sampleImporter.setBupMapIsWorkspace(false);
       
   297 			}
       
   298 			if (wizardSettings.keyMapProfile.getURI().equals(BupEventMapManager.DEFAULT_PROFILE_URI)) {
       
   299 				sampleImporter.setBupMapSymbianSDKId(""); //$NON-NLS-1$
       
   300 				sampleImporter.setBupMapIsBuiltIn(true);
       
   301 				sampleImporter.setBupMapIsWorkspace(false);
       
   302 			} else if (wizardSettings.keyMapProfile.getURI().equals(BupEventMapManager.WORKSPACE_PREF_KEY_MAP_URI)) {
       
   303 				sampleImporter.setBupMapSymbianSDKId(""); //$NON-NLS-1$
       
   304 				sampleImporter.setBupMapIsBuiltIn(false);
       
   305 				sampleImporter.setBupMapIsWorkspace(true);
       
   306 			}			
       
   307 		}
       
   308 
       
   309 		sampleImporter.importSamples(false);	// not waiting NPI save complete for responsive UI, save NPI on background
       
   310 		
       
   311 		cleanTempPkgFile();
       
   312 	}
       
   313 	
       
   314 	public void init(IWorkbench workbench, IStructuredSelection selection) {
       
   315 		this.workbench = workbench;
       
   316 		setNeedsProgressMonitor(true);
       
   317 	}
       
   318 
       
   319 	public void setInitializationData(IConfigurationElement config,
       
   320 			String propertyName, Object data) throws CoreException {
       
   321 	}
       
   322 			
       
   323 	NewPIWizardSettings getWizardData() {
       
   324 		return wizardSettings;
       
   325 	}
       
   326 	
       
   327 	public boolean canFinish() {
       
   328 		return isAllFollowingPagesComplete(getStartingPage());
       
   329 	}
       
   330 	
       
   331 	private boolean isAllFollowingPagesComplete(IWizardPage page) {
       
   332 		IWizardPage currentPage = page;
       
   333 		while (currentPage != null) {
       
   334 			if (currentPage.isPageComplete() == false) {
       
   335 				return false;
       
   336 			}
       
   337 			currentPage = getNextPage(currentPage);
       
   338 		}
       
   339 		// after ending page
       
   340 		return true;
       
   341 	}
       
   342 	
       
   343 	// all trace ID, we can check this for trace specific handling
       
   344 	public Set<Integer> getTraceSet() {
       
   345 		return traceSet;
       
   346 	}
       
   347 	
       
   348 	// all trace ID, we can do trace specific handling later
       
   349 	public void setTraceSet(Set<Integer> myTraceSet) {
       
   350 		traceSet = myTraceSet;
       
   351 	}
       
   352 	
       
   353 	public IWizardPage getPreviousPage (IWizardPage page) {
       
   354 		NewPIWizardSettings settings = NewPIWizardSettings.getInstance();
       
   355 		if (page == pageOutput && settings.enableCust == false) {
       
   356 			return getPreviousPage(pageCustom);
       
   357 		} else if (page == pageCustom && getTraceSet().contains(ButtonPlugin.getDefault().getTraceId()) == false){
       
   358 			return getPreviousPage(pageBupMap);
       
   359 		} else if (page == pageBupMap && wizardSettings.haveRomOnly == false && wizardSettings.haveAppRom == false) {
       
   360 			return getPreviousPage(pageObySym);
       
   361 		} else if (page == pageObySym && wizardSettings.haveAppOnly == false && wizardSettings.haveAppRom == false) {
       
   362 			return getPreviousPage(pagePkgList);
       
   363 		}
       
   364 
       
   365 		return super.getPreviousPage(page);
       
   366 	}
       
   367 
       
   368 	public IWizardPage getNextPage (IWizardPage page) {
       
   369 		if (page == pageConfigSelector && wizardSettings.haveAppOnly == false && wizardSettings.haveAppRom == false) {
       
   370 			return getNextPage(pagePkgList);
       
   371 		} else if (page == pagePkgList && wizardSettings.haveRomOnly == false && wizardSettings.haveAppRom == false) {
       
   372 			return getNextPage(pageObySym);
       
   373 		} else if (page == pageObySym && getTraceSet().contains(ButtonPlugin.getDefault().getTraceId()) == false) {
       
   374 			return getNextPage(pageBupMap);
       
   375 		} else if (page == pageBupMap && wizardSettings.enableCust == false) {
       
   376 			return getNextPage(pageCustom);
       
   377 		}
       
   378 		return super.getNextPage(page);
       
   379 	}
       
   380 
       
   381 }