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

📄 v050to055migrator.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.core.internal.migration;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.nature.J2MENature;

/**
 * Helper class for doing migration of the changes between
 * version 0.5.0 to 0.5.5.
 * <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.4 $
 * <br>
 * $Date: 2005/07/07 02:37:17 $
 * <br>
 * @author Craig Setera
 */

public class V050To055Migrator {
	private static final QualifiedName MIGRATED_PROP =
		new QualifiedName(IEclipseMECoreConstants.PLUGIN_ID, "v055migrated");

	// The root of the workspace that allows us to look through projects
	private IWorkspaceRoot workspaceRoot;
	
	/**
	 * Construct a new migration helper.
	 * 
	 * @param workspaceRoot
	 */
	public V050To055Migrator(IWorkspaceRoot workspaceRoot) {
		super();
		this.workspaceRoot = workspaceRoot;
	}
	
	/**
	 * Do the work of this migration.
	 * 
	 * @throws CoreException
	 */
	public void doMigration(IProgressMonitor monitor) 
		throws CoreException
	{
		IProject[] projects = workspaceRoot.getProjects();
		for (int i = 0; i < projects.length; i++) {
			try {
				migrateProjectIfNecessary(projects[i], monitor);
			} catch (CoreException e) {
				EclipseMECorePlugin.log(IStatus.WARNING, "doMigration", e);
			}
		}
	}

	/**
	 * Migrate the specified project if it hasn't already been
	 * migrated.
	 * 
	 * @param project
	 * @param monitor
	 * @throws CoreException
	 */
	private void migrateProjectIfNecessary(IProject project, IProgressMonitor monitor) 
		throws CoreException 
	{
		if (project.isOpen() && J2MENature.hasJ2MENature(project)) {
			String value = project.getPersistentProperty(MIGRATED_PROP);
			
			if (!"true".equals(value)) {
				migrateProject(project, monitor);
				project.setPersistentProperty(MIGRATED_PROP, "true");
			}
		}
	}

	/**
	 * Migrate the specified project.
	 * 
	 * @param project
	 * @param monitor
	 * @throws CoreException
	 */
	private void migrateProject(IProject project, IProgressMonitor monitor) 
		throws CoreException 
	{
		IFolder folder = project.getFolder(EclipseMECorePlugin.getResourcesDirectoryName());
		if (folder.exists()) {
			IJavaProject javaProject = JavaCore.create(project);
			if (javaProject != null) {
				if (!resourcesOnClasspath(folder, javaProject)) {
					IClasspathEntry resEntry = 
						JavaCore.newSourceEntry(folder.getFullPath());
					IClasspathEntry[] entries = javaProject.getRawClasspath();
					IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
					System.arraycopy(entries, 0, newEntries, 0, entries.length);
					newEntries[entries.length] = resEntry;
					javaProject.setRawClasspath(newEntries, null);
				}
			}
		}
	}

	/**
	 * Check to see if the specified folder is on the project's build
	 * path as a source folder.
	 * 
	 * @param folder
	 * @param javaProject
	 * @return
	 * @throws CoreException
	 */
	private boolean resourcesOnClasspath(IFolder folder, IJavaProject javaProject) 
		throws CoreException 
	{
		boolean onClasspath = false;
		
		IPath folderPath = folder.getFullPath();
		IClasspathEntry[] entries = javaProject.getRawClasspath();
		for (int i = 0; i < entries.length; i++) {
			IClasspathEntry entry = entries[i];
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				IPath entryPath = entry.getPath();
				if (entryPath.equals(folderPath)) {
					onClasspath = true;
					break;
				}
			}
		}
		
		return onClasspath;
	}
}

⌨️ 快捷键说明

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