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

📄 migrationwizard.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2006 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.migration;

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

import javax.xml.transform.TransformerException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
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.core.JavaCore;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.IPageChangeProvider;
import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.dialogs.PageChangedEvent;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Display;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.MidletSuiteFactory;
import eclipseme.core.model.device.DeviceRegistry;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.persistence.PersistenceException;
import eclipseme.ui.internal.EclipseMEUIPlugin;
import eclipseme.ui.internal.devices.DeviceImportWizardPage;

/**
 * A wizard implementation for helping the user do pre-1.5 migration.
 * <p />
 * Copyright (c) 2003-2006 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.1 $
 * <br>
 * $Date: 2006/03/06 00:55:27 $
 * <br>
 * @author Craig Setera
 */
public class MigrationWizard extends Wizard {
	private static final String PAGE_ONE_TITLE = "EclipseME Migration Overview";
	private static final String PAGE_ONE_DESCRIPTION = "" +
			"An overview of the steps necessary to migrate from a previous version of EclipseME.";
	private static final String PAGE_ONE_TEXT =
		"<form><p>In this new version of EclipseME, platform definitions have been removed in favor " +
		"of device definitions.  This is much closer in similarity to the way that other mobile " +
		"tools work with devices and significantly more flexible. " +
		"There is currently an old platform component definitions file, but no device definitions " +
		"have been defined.  " +
		"This wizard will help you migrate to the new version of EclipseME by taking the " +
		"following steps:</p>" +
		"<li>Select the root file system directories to search for device definitions.</li>" +
		"<li>Search the selected directories for device definitions.</li>" +
		"<li>Allow selection of the found device definitions to imported.</li>" +
		"<li>Select a default device definition.</li>" +
		"<li>Set the default device definition onto EclipseME projects.</li>" +
		"<p>You may choose <b>Cancel</b> at any point during this migration process." +
		"You will not be automatically prompted to do migration again.  If you would like " +
		"to restart the migration, you may choose the \"Launch Migration\" from the " +
		"\"EclipseME\" submenu of the \"Help\" menu.</p>" +
		"<p><b>Press Next to get started selecting the directories to search for devices.</b></p></form>";
	
	private static final String PAGE_THREE_TITLE = "EclipseME Migration Search for Device";
	private static final String PAGE_THREE_DESCRIPTION = "" +
			"Search for device definitions in the selected directories.";
	private static final String PAGE_THREE_TEXT =
		"<form><p>At least one file system root directory was selected from your previous " +
		"configuration.  On the next page, the system will search for device definitions " +
		"within those root directories.  You must select at least one of the found devices " +
		"to proceed with the migration.</p>" +
		"<p><b>Press Next to start searching for devices in the selected directories.</b></p></form>";
		
	
	private static final String PAGE_FIVE_TITLE = "EclipseME Migration Set Project Devices";
	private static final String PAGE_FIVE_DESCRIPTION = "" +
			"Select the devices for EclipseME projects.";
	private static final String PAGE_FIVE_TEXT =
		"<form><p>Migration is almost complete at this point.  You have found devices " +
		"based on the previous steps in the migration.  The next page will allow " +
		"you to associate those devices with the open EclipseME projects in your " +
		"workspace.</p>" +
		"<p>NOTE: This can only be applied to <b>open</b> projects in the current workspace.</p>" +
		"<p><b>Press Next to start searching for devices in the selected directories.</b></p></form>";
		
	// RUnnable for use in syncexec call
	private class GetSelectedDevicesRunnable implements Runnable {
		private IDevice[] devices;
		
		public IDevice[] getDevices() {
			return devices;
		}
		
		public void run() {
			devices = deviceImportPage.getSelectedDevices();
		}
	}
	
	private TextWizardPage overviewPage;
	private SelectRootsWizardPage selectRootsPage;
	private DeviceImportWizardPage deviceImportPage;
	private SelectProjectDevicesWizardPage projectDevicesPage;
	
	/**
	 * Construct a new wizard.
	 */
	public MigrationWizard() {
		super();
		setDialogSettings(EclipseMEUIPlugin.getDialogSettings("150MigrationWizard"));
		setNeedsProgressMonitor(true);
		setWindowTitle("EclipseME Migration");
	}

	/**
	 * @see org.eclipse.jface.wizard.Wizard#addPages()
	 */
	public void addPages() {
		overviewPage = new TextWizardPage(
			"pageOne", 
			PAGE_ONE_TITLE, 
			PAGE_ONE_DESCRIPTION, 
			PAGE_ONE_TEXT);
		selectRootsPage = new SelectRootsWizardPage();
		deviceImportPage = new DeviceImportWizardPage();
		projectDevicesPage = new SelectProjectDevicesWizardPage();
		
		addPage(overviewPage);
		addPage(selectRootsPage);
		TextWizardPage textPage = new TextWizardPage(
			"pageThree", 
			PAGE_THREE_TITLE, 
			PAGE_THREE_DESCRIPTION, 
			PAGE_THREE_TEXT);
		addPage(textPage);
		addPage(deviceImportPage);
		textPage = new TextWizardPage(
				"pageFive", 
				PAGE_FIVE_TITLE, 
				PAGE_FIVE_DESCRIPTION, 
				PAGE_FIVE_TEXT);
		addPage(textPage);
		addPage(projectDevicesPage);
	}

	/**
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	public boolean performFinish() {
		boolean successful = false;
		
		IRunnableWithProgress runnable = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) 
				throws InvocationTargetException, InterruptedException 
			{
				monitor.beginTask("Migration", 2);
				try {
					importDevices(monitor);
					monitor.worked(1);
					setProjectDevices(monitor);
					monitor.worked(1);
				} catch (Exception e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		
		try {
			getContainer().run(true, false, runnable);
			successful = true;
		} catch (InvocationTargetException ite) {
			Throwable e = ite.getCause();
			EclipseMECorePlugin.log(IStatus.WARNING, "Error adding new device", e);
			EclipseMEUIPlugin.displayError(
				getShell(), 
				IStatus.WARNING, 
				-999, 
				"Device Registry Error", 
				"Error adding new device to device registry.", 
				e);
		} catch (InterruptedException e) {
		}

		return successful;
	}

	/**
	 * @see org.eclipse.jface.wizard.Wizard#setContainer(org.eclipse.jface.wizard.IWizardContainer)
	 */
	public void setContainer(IWizardContainer wizardContainer) {
		super.setContainer(wizardContainer);
		
		if (wizardContainer instanceof IPageChangeProvider) {
			IPageChangeProvider provider = (IPageChangeProvider) wizardContainer;
			provider.addPageChangedListener(new IPageChangedListener() {
				public void pageChanged(PageChangedEvent event) {
					handlePageChanged(event);
				}
			});
		}
	}

	/**
	 * Handle the change of page.
	 * 
	 * @param event
	 */
	private void handlePageChanged(PageChangedEvent event) {
		IDialogPage selected = (IDialogPage) event.getSelectedPage();
		if (selected == deviceImportPage) {
			String[] roots = selectRootsPage.getSelectedRoots();
			deviceImportPage.searchRoots(roots);
		} else if (selected == projectDevicesPage) {
			IDevice[] devices = deviceImportPage.getSelectedDevices();
			projectDevicesPage.setSelectedDevices(devices);
		}
	}
	
	/**
	 * Do the actual work of importing the devices selected by the
	 * user.
	 * @param monitor 
	 * 
	 * @return
	 * @throws PersistenceException 
	 * @throws IllegalArgumentException 
	 * @throws IOException 
	 * @throws TransformerException 
	 */
	private void importDevices(IProgressMonitor monitor) 
		throws IllegalArgumentException, PersistenceException, TransformerException, IOException 
	{
		GetSelectedDevicesRunnable runnable = new GetSelectedDevicesRunnable();
		Display.getDefault().syncExec(runnable);
		IDevice[] selectedDevices = runnable.getDevices();
		
		DeviceRegistry registry = DeviceRegistry.singleton;
		for (int i = 0; i < selectedDevices.length; i++) {
			IDevice device = selectedDevices[i];
			registry.addDevice(device);
		}
		registry.store();
	}

	/**
	 * Set the device on the project based on the specified mapping.
	 * 
	 * @param mapping
	 * @param monitor 
	 * @throws CoreException 
	 */
	private void setProjectDevice(ProjectDeviceMapping mapping, IProgressMonitor monitor) 
		throws CoreException 
	{
		IProject project = mapping.project;
		IJavaProject javaProject = JavaCore.create(project);
		if (javaProject != null) {
			IMidletSuiteProject midletSuite = MidletSuiteFactory.getMidletSuiteProject(javaProject);
			if (midletSuite != null) {
				midletSuite.setDevice(mapping.device, monitor);
				midletSuite.saveMetaData();
			}
		}
	}

	/**
	 * Set project devices.
	 * @param monitor 
	 * @throws CoreException 
	 */
	private void setProjectDevices(IProgressMonitor monitor) 
		throws CoreException 
	{
		ProjectDeviceMapping[] mappings = projectDevicesPage.getProjectDeviceMappings();
		SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, mappings.length);
		try {
			for (int i = 0; i < mappings.length; i++) {
				ProjectDeviceMapping mapping = mappings[i];
				subMonitor.setTaskName("Setting project " + mapping.project.getName() + " device");
				setProjectDevice(mapping, subMonitor);
				subMonitor.worked(1);
			}
		} finally {
			subMonitor.done();
		}
	}
}

⌨️ 快捷键说明

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