sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/wizard/NewSMTProjectWizard.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // ${file_name}
       
    15 // 
       
    16 //
       
    17 
       
    18 package com.symbian.smt.gui.wizard;
       
    19 
       
    20 import java.lang.reflect.InvocationTargetException;
       
    21 import java.net.URI;
       
    22 
       
    23 import org.eclipse.core.resources.IProject;
       
    24 import org.eclipse.core.resources.IProjectDescription;
       
    25 import org.eclipse.core.resources.IResource;
       
    26 import org.eclipse.core.resources.IWorkspace;
       
    27 import org.eclipse.core.resources.IWorkspaceDescription;
       
    28 import org.eclipse.core.resources.IWorkspaceRunnable;
       
    29 import org.eclipse.core.resources.IncrementalProjectBuilder;
       
    30 import org.eclipse.core.resources.ProjectScope;
       
    31 import org.eclipse.core.resources.ResourcesPlugin;
       
    32 import org.eclipse.core.runtime.CoreException;
       
    33 import org.eclipse.core.runtime.IConfigurationElement;
       
    34 import org.eclipse.core.runtime.IExecutableExtension;
       
    35 import org.eclipse.core.runtime.IProgressMonitor;
       
    36 import org.eclipse.core.runtime.IStatus;
       
    37 import org.eclipse.core.runtime.OperationCanceledException;
       
    38 import org.eclipse.core.runtime.Status;
       
    39 import org.eclipse.core.runtime.SubProgressMonitor;
       
    40 import org.eclipse.core.runtime.jobs.Job;
       
    41 import org.eclipse.core.runtime.preferences.DefaultScope;
       
    42 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
       
    43 import org.eclipse.core.runtime.preferences.IScopeContext;
       
    44 import org.eclipse.core.runtime.preferences.InstanceScope;
       
    45 import org.eclipse.jface.dialogs.MessageDialog;
       
    46 import org.eclipse.jface.viewers.ISelection;
       
    47 import org.eclipse.jface.viewers.IStructuredSelection;
       
    48 import org.eclipse.jface.wizard.Wizard;
       
    49 import org.eclipse.swt.widgets.Composite;
       
    50 import org.eclipse.ui.INewWizard;
       
    51 import org.eclipse.ui.IWorkbench;
       
    52 import org.eclipse.ui.IWorkbenchWizard;
       
    53 import org.eclipse.ui.actions.WorkspaceModifyOperation;
       
    54 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
       
    55 
       
    56 import com.symbian.smt.gui.AbstractPersistentDataStore;
       
    57 import com.symbian.smt.gui.Activator;
       
    58 import com.symbian.smt.gui.Logger;
       
    59 import com.symbian.smt.gui.ManageResources;
       
    60 import com.symbian.smt.gui.PersistentDataStore;
       
    61 
       
    62 public class NewSMTProjectWizard extends Wizard implements INewWizard,
       
    63 		IExecutableExtension {
       
    64 	private NewProjectCreationPageCaseInsensitive projectCreationWizardPage;
       
    65 
       
    66 	private NewProjectWizardSystemDefsPage systemDefsWizardPage;
       
    67 	private NewProjectWizardTabbedPropertiesPage tabbedPropertiesWizardPage;
       
    68 
       
    69 	private ISelection selection;
       
    70 	private IWorkbench workbench;
       
    71 	private IConfigurationElement config;
       
    72 	private IProject projectHandle;
       
    73 
       
    74 	/**
       
    75 	 * This is the entry point for creating and managing the wizard
       
    76 	 * 
       
    77 	 * @return void
       
    78 	 */
       
    79 	public NewSMTProjectWizard() {
       
    80 		super();
       
    81 		setNeedsProgressMonitor(true);
       
    82 	}
       
    83 
       
    84 	/**
       
    85 	 * Adds pages to the wizard
       
    86 	 * 
       
    87 	 * @return void
       
    88 	 */
       
    89 	public void addPages() {
       
    90 		projectCreationWizardPage = new NewProjectCreationPageCaseInsensitive(
       
    91 				"page1");
       
    92 
       
    93 		projectCreationWizardPage.setTitle("System Model Manager Wizard");
       
    94 		projectCreationWizardPage
       
    95 				.setDescription("Enter a name for the new project...");
       
    96 		addPage(projectCreationWizardPage);
       
    97 
       
    98 		systemDefsWizardPage = new NewProjectWizardSystemDefsPage(selection);
       
    99 		addPage(systemDefsWizardPage);
       
   100 
       
   101 		tabbedPropertiesWizardPage = new NewProjectWizardTabbedPropertiesPage(
       
   102 				selection);
       
   103 		addPage(tabbedPropertiesWizardPage);
       
   104 	}
       
   105 
       
   106 	private void copyFilesIntoProject() {
       
   107 		// Add the folders and files to the project
       
   108 		ManageResources.updateShapesFiles(projectHandle,
       
   109 				tabbedPropertiesWizardPage.getDefaultShapesFiles());
       
   110 		ManageResources.updateLevelsFiles(projectHandle,
       
   111 				tabbedPropertiesWizardPage.getDefaultLevelsFiles());
       
   112 		ManageResources.updateLocalisationFiles(projectHandle,
       
   113 				tabbedPropertiesWizardPage.getDefaultLocalisationFiles());
       
   114 		ManageResources.updateDependenciesFiles(projectHandle,
       
   115 				tabbedPropertiesWizardPage.getDefaultDependenciesFiles());
       
   116 		ManageResources.updateSystemInfoFiles(projectHandle,
       
   117 				tabbedPropertiesWizardPage.getDefaultSystemInfoFiles());
       
   118 		ManageResources.updateColoursFiles(projectHandle,
       
   119 				tabbedPropertiesWizardPage.getDefaultColoursFiles());
       
   120 		ManageResources.updateBorderStylesFiles(projectHandle,
       
   121 				tabbedPropertiesWizardPage.getDefaultBorderStylesFiles());
       
   122 		ManageResources.updateBorderShapesFiles(projectHandle,
       
   123 				tabbedPropertiesWizardPage.getDefaultBorderShapesFiles());
       
   124 		ManageResources.updatePatternsFiles(projectHandle,
       
   125 				tabbedPropertiesWizardPage.getDefaultPatternsFiles());
       
   126 		ManageResources.updateS12XmlFiles(projectHandle,
       
   127 				tabbedPropertiesWizardPage.getDefaultS12XmlFiles());
       
   128 
       
   129 		// Add the system definition files to the project
       
   130 		ManageResources.updateSystemDefinitionFiles(projectHandle,
       
   131 				systemDefsWizardPage.getSystemDefinitions(), false);
       
   132 	}
       
   133 
       
   134 	public void createPageControls(Composite pageContainer) {
       
   135 		super.createPageControls(pageContainer);
       
   136 
       
   137 		IScopeContext defaultScope = new DefaultScope();
       
   138 		IEclipsePreferences defaultNode = defaultScope
       
   139 				.getNode(Activator.PLUGIN_ID);
       
   140 
       
   141 		IScopeContext instanceScope = new InstanceScope();
       
   142 		IEclipsePreferences instanceNode = instanceScope
       
   143 				.getNode(Activator.PLUGIN_ID);
       
   144 		PersistentDataStore instanceStore = new PersistentDataStore(
       
   145 				instanceNode, defaultNode);
       
   146 
       
   147 		tabbedPropertiesWizardPage.initialize(instanceStore);
       
   148 	}
       
   149 
       
   150 	/**
       
   151 	 * Sets up the project - project folder, nature, files, folders etc
       
   152 	 * 
       
   153 	 * @return void
       
   154 	 */
       
   155 	void createProject(IProjectDescription description, IProject proj,
       
   156 			IProgressMonitor monitor) throws CoreException,
       
   157 			OperationCanceledException {
       
   158 		try {
       
   159 
       
   160 			monitor.beginTask("", 2000);
       
   161 
       
   162 			proj.create(description, new SubProgressMonitor(monitor, 1000));
       
   163 
       
   164 			if (monitor.isCanceled()) {
       
   165 				throw new OperationCanceledException();
       
   166 			}
       
   167 
       
   168 			proj.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(
       
   169 					monitor, 1000));
       
   170 
       
   171 			try {
       
   172 				String[] natures = description.getNatureIds();
       
   173 				String[] newNatures = new String[natures.length + 1];
       
   174 				System.arraycopy(natures, 0, newNatures, 0, natures.length);
       
   175 				newNatures[natures.length] = "com.symbian.smt.gui.nature";
       
   176 				description.setNatureIds(newNatures);
       
   177 				proj.setDescription(description, null);
       
   178 			} catch (CoreException e) {
       
   179 				Logger.log(e.getMessage(), e);
       
   180 			}
       
   181 
       
   182 		} finally {
       
   183 			monitor.done();
       
   184 		}
       
   185 	}
       
   186 
       
   187 	/**
       
   188 	 * We will accept the selection in the workbench to see if we can initialise
       
   189 	 * from it.
       
   190 	 * 
       
   191 	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
       
   192 	 */
       
   193 	public void init(IWorkbench workbench, IStructuredSelection selection) {
       
   194 		this.selection = selection;
       
   195 		this.workbench = workbench;
       
   196 	}
       
   197 
       
   198 	/**
       
   199 	 * This method is called when the finish button is pressed in the wizard
       
   200 	 * 
       
   201 	 * @return boolean
       
   202 	 */
       
   203 	public boolean performFinish() {
       
   204 		projectHandle = projectCreationWizardPage.getProjectHandle();
       
   205 
       
   206 		if (projectHandle == null) {
       
   207 			return false;
       
   208 		}
       
   209 
       
   210 		URI projectURI = (!projectCreationWizardPage.useDefaults()) ? projectCreationWizardPage
       
   211 				.getLocationURI()
       
   212 				: null;
       
   213 
       
   214 		IWorkspace workspace = ResourcesPlugin.getWorkspace();
       
   215 
       
   216 		final IProjectDescription desc = workspace
       
   217 				.newProjectDescription(projectHandle.getName());
       
   218 
       
   219 		desc.setLocationURI(projectURI);
       
   220 
       
   221 		WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
       
   222 			protected void execute(IProgressMonitor monitor)
       
   223 					throws CoreException {
       
   224 				createProject(desc, projectHandle, monitor);
       
   225 			}
       
   226 		};
       
   227 
       
   228 		try {
       
   229 			getContainer().run(false, false, op);
       
   230 		} catch (InterruptedException e) {
       
   231 			return false;
       
   232 		} catch (InvocationTargetException e) {
       
   233 			Throwable realException = e.getTargetException();
       
   234 			MessageDialog.openError(getShell(), "Error", realException
       
   235 					.getMessage());
       
   236 			return false;
       
   237 		}
       
   238 		
       
   239 		completePerformFinish();
       
   240 
       
   241 		// If auto building has been disabled then we force the build
       
   242 
       
   243 	    IWorkspaceDescription description = workspace.getDescription();
       
   244 	    // If auto building has been disabled then we force the build
       
   245 	    if (!description.isAutoBuilding()) {
       
   246 			Job j= new Job("Building workspace") {
       
   247 				@Override
       
   248 				protected IStatus run(IProgressMonitor monitor) {
       
   249 					try {
       
   250 						projectHandle.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
       
   251 					} catch (CoreException e) {
       
   252 						Logger.log(e.getMessage(), e);
       
   253 					}
       
   254 					
       
   255 					return new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, "updating properties succeeded", null);
       
   256 				}
       
   257 			};
       
   258 			j.schedule();
       
   259 	    }
       
   260 		
       
   261 		return true;
       
   262 	}
       
   263 
       
   264 	private void completePerformFinish() {
       
   265 		IWorkspaceRunnable  op = new IWorkspaceRunnable () {
       
   266 			public void run(IProgressMonitor monitor) throws CoreException  {
       
   267 				monitor.beginTask("Creating project", 0);
       
   268 				
       
   269 				// Persist the project information
       
   270 				// This needs to be done first as the builder will try to read from the
       
   271 				// persistent store
       
   272 				persistInformation();
       
   273 
       
   274 				// Copy the files into the project / Create file shortcut icons
       
   275 				copyFilesIntoProject();
       
   276 
       
   277 				BasicNewProjectResourceWizard.updatePerspective(config);
       
   278 				BasicNewProjectResourceWizard.selectAndReveal(projectHandle, workbench
       
   279 						.getActiveWorkbenchWindow());
       
   280 
       
   281 				monitor.worked(1);
       
   282 				monitor.done();
       
   283 			}
       
   284 		};
       
   285 		
       
   286 		try {
       
   287 			ResourcesPlugin.getWorkspace().run(op, projectHandle, IWorkspace.AVOID_UPDATE, null);
       
   288 		} catch (CoreException e) {
       
   289 			MessageDialog.openError(getShell(), "Error", e.getMessage());
       
   290 			return;
       
   291 		}
       
   292 	}
       
   293 	
       
   294 	private void persistInformation() {
       
   295 		IScopeContext projectScope = new ProjectScope(projectHandle);
       
   296 		IEclipsePreferences node = projectScope.getNode(Activator.PLUGIN_ID);
       
   297 
       
   298 		AbstractPersistentDataStore dataStore = new PersistentDataStore(node);
       
   299 
       
   300 		// Persist the Output Filename
       
   301 		dataStore.setOutputFilename(tabbedPropertiesWizardPage
       
   302 				.getOutputFilename());
       
   303 		
       
   304 		// Persist the Model Labels
       
   305 		dataStore.setCopyrightText(tabbedPropertiesWizardPage
       
   306 				.getCopyrightText());
       
   307 		dataStore.setDistributionTexts(tabbedPropertiesWizardPage
       
   308 				.getDistributionTexts());
       
   309 		dataStore.setSelectedDistributionText(tabbedPropertiesWizardPage
       
   310 				.getSelectedDistributionText());
       
   311 		dataStore.setModelName(tabbedPropertiesWizardPage.getModelName());
       
   312 		dataStore.setModelVersion(tabbedPropertiesWizardPage.getModelVersion());
       
   313 		dataStore.setModelVersionTexts(tabbedPropertiesWizardPage
       
   314 				.getModelVersionTexts());
       
   315 		dataStore.setSelectedModelVersionText(tabbedPropertiesWizardPage
       
   316 				.getSelectedModelVersionText());
       
   317 		dataStore.setSystemName(tabbedPropertiesWizardPage.getSystemName());
       
   318 		dataStore.setSystemVersion(tabbedPropertiesWizardPage
       
   319 				.getSystemVersion());
       
   320 
       
   321 		// Persist the Model Control
       
   322 		dataStore.setHighlightCoreOS(tabbedPropertiesWizardPage
       
   323 				.getHighlightCoreOS());
       
   324 		dataStore.setLevelOfDetail(tabbedPropertiesWizardPage
       
   325 				.getLevelOfDetail());
       
   326 		dataStore.setPrintedDpis(tabbedPropertiesWizardPage.getPrintedDpis());
       
   327 		dataStore.setSelectedPrintedDpi(tabbedPropertiesWizardPage
       
   328 				.getSelectedPrintedDpi());
       
   329 		dataStore.setSuppressMouseOverEffect(tabbedPropertiesWizardPage
       
   330 				.getSuppressMouseOverEffect());
       
   331 		dataStore.setFixItemSize(tabbedPropertiesWizardPage.getFixItemSize());
       
   332 
       
   333 		// Persist the Resources, the selected file is persisted by the
       
   334 		// ManageResources widget
       
   335 		// All files (Selection is persisted as part of method
       
   336 		// copyFilesIntoProject())
       
   337 		dataStore.setBorderShapesFiles(tabbedPropertiesWizardPage
       
   338 				.getBorderShapesFiles());
       
   339 		dataStore.setBorderStylesFiles(tabbedPropertiesWizardPage
       
   340 				.getBorderStylesFiles());
       
   341 		dataStore.setColoursFiles(tabbedPropertiesWizardPage.getColoursFiles());
       
   342 		dataStore.setDependenciesFiles(tabbedPropertiesWizardPage
       
   343 				.getDependenciesFiles());
       
   344 		dataStore.setLevelsFiles(tabbedPropertiesWizardPage.getLevelsFiles());
       
   345 		dataStore.setLocalisationFiles(tabbedPropertiesWizardPage
       
   346 				.getLocalisationFiles());
       
   347 		dataStore.setPatternsFiles(tabbedPropertiesWizardPage
       
   348 				.getPatternsFiles());
       
   349 		dataStore.setShapesFiles(tabbedPropertiesWizardPage.getShapesFiles());
       
   350 		dataStore.setSystemInfoFiles(tabbedPropertiesWizardPage
       
   351 				.getSystemInfoFiles());
       
   352 		dataStore.setS12XmlFiles(tabbedPropertiesWizardPage.getS12XmlFiles());
       
   353 
       
   354 		// Persist the Ignore Items
       
   355 		dataStore.setIgnoreItems(tabbedPropertiesWizardPage.getIgnoreItems());
       
   356 
       
   357 		// Persist the Filter Items
       
   358 		dataStore
       
   359 				.setFilterHasItems(tabbedPropertiesWizardPage.getFilterItems());
       
   360 
       
   361 		// Persist the Advanced Options
       
   362 		dataStore.setAdvancedOptions(tabbedPropertiesWizardPage
       
   363 				.getAdvancedOptions());
       
   364 
       
   365 
       
   366 	}
       
   367 
       
   368 	public void setInitializationData(IConfigurationElement config,
       
   369 			String propertyName, Object data) throws CoreException {
       
   370 		this.config = config;
       
   371 	}
       
   372 
       
   373 }