⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newj2meprojectwizard.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.ui.internal.wizards;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.model.MidletSuiteFactory;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.model.impl.MetaData;
import eclipseme.ui.EclipseMEUIStrings;
import eclipseme.ui.internal.EclipseMEUIPlugin;

/**
 * Wizard for creation of a new J2ME Project.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.9 $
 * <br>
 * $Date: 2006/11/12 01:10:47 $
 * <br>
 * @author Craig Setera
 */
public class NewJ2MEProjectWizard 
	extends Wizard 
	implements INewWizard
{
	// Instance variables
	private IConfigurationElement configElement;
	private WizardNewMidletSuiteCreationPage mainPage;
	private J2MEJavaCapabilityConfigurationPage javaWizardPage;
	private MidletSuitePropertiesWizardPage suitePropsPage;
	private boolean projectLocationExisted;
	
	/**
	 * Constructor
	 */
	public NewJ2MEProjectWizard() {
		super();
		
		setWindowTitle(EclipseMEUIStrings.getString("wiz.newproj.title"));
		
		ImageDescriptor descriptor = 
			EclipseMEUIPlugin.getIconImageDescriptor("newjprj_wiz_M.gif");
		setDefaultPageImageDescriptor(descriptor);
	}

	/*
	 * @see Wizard#addPages
	 */	
	public void addPages() {
		super.addPages();
		
		mainPage = new WizardNewMidletSuiteCreationPage();
		mainPage.setTitle(EclipseMEUIStrings.getString("wiz.newproj.main.title"));
		mainPage.setDescription(EclipseMEUIStrings.getString("wiz.newproj.main.description"));

		suitePropsPage = new MidletSuitePropertiesWizardPage();
		javaWizardPage = new J2MEJavaCapabilityConfigurationPage(mainPage);
		
		addPage(mainPage);		
		addPage(suitePropsPage);
		addPage(javaWizardPage);
	}		
	
	/**
	 * @see org.eclipse.jface.wizard.IWizard#canFinish()
	 */
	public boolean canFinish() {
		return suitePropsPage.canFlipToNextPage() && super.canFinish();
	}

	/**
	 * 
	 * @param monitor
	 * @throws InterruptedException
	 * @throws CoreException
	 */
	protected void finishPage(IProgressMonitor monitor) 
		throws InterruptedException, CoreException 
	{
		BasicNewProjectResourceWizard.updatePerspective(configElement);
	}
	
	/**
	 * @see IWizard#performCancel()
	 */
	public boolean performCancel() {
		if (!projectLocationExisted) {
			removeProject();
		}
		
		return super.performCancel();
	}
	
	/**
	 * Remove the already created project, as the user cancelled
	 * the operation.
	 */
	private void removeProject() {
		IJavaProject javaProject = javaWizardPage.getJavaProject();
		if (javaProject != null) { 
			final IProject project = javaProject.getProject();
			
			if (project != null && project.exists()) {
				
				IRunnableWithProgress op = new IRunnableWithProgress() {
					public void run(IProgressMonitor monitor) 
						throws InvocationTargetException, InterruptedException 
					{
						monitor.beginTask("##Remove Project", 3);
		
						try {
							project.delete(true, false, monitor);
						} catch (CoreException e) {
							throw new InvocationTargetException(e);
						} finally {
							monitor.done();
						}
					}
				};
			
				try {
					getContainer().run(false, true, op);
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} catch  (InterruptedException e) {
					// cancel pressed
				}
			}
		}
	}

	/**
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	public boolean performFinish() {
		boolean completed = true;
		
		// The steps necessary for creating the new midlet suite
		IRunnableWithProgress runnable = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) 
				throws InvocationTargetException, InterruptedException 
			{
				// Get theh project created first
				getProjectCreationRunnable().run(monitor);

				// Set the device into the project metadata to make
				// the java project creation happy.
				IProject project = mainPage.getProjectHandle();
				IDevice device = suitePropsPage.getSelectedDevice();
				MetaData metadata = new MetaData(project);
				metadata.setDevice(device);
				try {
					metadata.saveMetaData();
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				}

				// Get the java nature and classpath set up
				getJavaNatureRunnable().run(monitor);

				// Get the J2ME nature and metadata set up
				String jadFileName = suitePropsPage.getJadFileName();
				IJavaProject javaProject = javaWizardPage.getJavaProject();
				
				MidletSuiteFactory.MidletSuiteCreationRunnable runnable =
					MidletSuiteFactory.getMidletSuiteCreationRunnable(project, javaProject, device, jadFileName);
				runnable.setPreprocessingEnable(mainPage.isPreprocessingEnabled());
				runnable.run(monitor);
			}
		};
		
		// TODO Get in progress indicator updated
		try {
			getContainer().run(false, true,
				new WorkspaceModifyDelegatingOperation(runnable));

		} catch (InvocationTargetException e) {
			EclipseMECorePlugin.log(IStatus.ERROR, "performFinish", e.getCause());
			completed = false;
		} catch  (InterruptedException e) {
			completed = false;
		}
		
		return completed;
	}

	/**
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
	}

	/**
	 * Return the handle to the project to be created.
	 * @return
	 */
	IProject getCreatedProjectHandle() {
		return mainPage.getProjectHandle();
	}
	
	/**
	 * Return the runnable used to perform the project
	 * creation work.
	 * 
	 * @return
	 */
	private IRunnableWithProgress getProjectCreationRunnable() {
		return new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) 
				throws InvocationTargetException, InterruptedException
			{
				try {
					// Track whether the project location already exists
					File projectLocationFile = mainPage.getLocationPath().toFile();
					projectLocationExisted = projectLocationFile.exists();

					// Create the project
					IProject project = mainPage.getProjectHandle();
					JavaCapabilityConfigurationPage.createProject(
						project,
						mainPage.getLocationPath(),
						new SubProgressMonitor(monitor, 1));

				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				}
			}
		};
	}

	/**
	 * Return a runnable capable of setting up the Java
	 * nature and settings.
	 * 
	 * @return
	 */
	private IRunnableWithProgress getJavaNatureRunnable() {	
		IRunnableWithProgress runnable = javaWizardPage.getRunnable();
			
		if (runnable == null) {
			javaWizardPage.updateConfiguration();
			runnable = javaWizardPage.getRunnable();
		}
		
		return runnable;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -