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

📄 midletsuitefactory.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.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.utils.ColonDelimitedProperties;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.model.impl.MidletSuiteProject;

/**
 * A factory for accessing midlet suite instances based on
 * projects.
 * <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.3 $
 * <br>
 * $Date: 2006/11/26 20:23:42 $
 * <br>
 * @author Craig Setera
 */
public class MidletSuiteFactory {
	
	// Java 1.1 options
	public static final String[] JAVA11_OPTIONS = new String[] {
		JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_3,
		JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_1,
		JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3,
	};

	/**
	 * Return a runnable capable of setting up the J2ME
	 * nature on the project.
	 * 
	 * @return a runnable that can be used to create a new
	 * midlet suite
	 */
	public static MidletSuiteCreationRunnable getMidletSuiteCreationRunnable(
		IProject project,
		IJavaProject javaProject,
		IDevice device,
		String jadFileName) 
	{	
		return new MidletSuiteCreationRunnable(project, javaProject, device, jadFileName);
	}

	/**
	 * Workspace runnable for creating a Midlet Suite within a project.
	 */
	public static class MidletSuiteCreationRunnable {
		private IProject project;
		private IJavaProject javaProject;
		private IDevice device;
		private IPath jadFilePath;
		private boolean preprocessingEnable;
		
		/**
		 * Construct a new runnable for midlet suite creation.
		 * 
		 * @param project
		 */
		private MidletSuiteCreationRunnable(
			IProject project, 
			IJavaProject javaProject,
			IDevice device,
			String jadFileName) 
		{
			this.project = project;
			this.javaProject = javaProject;
			this.device = device;
			this.jadFilePath = new Path(jadFileName);
		}

		/**
		 * @return the preprocessEnable
		 */
		public boolean isPreprocessingEnable() {
			return preprocessingEnable;
		}

		/**
		 * @param preprocessEnable the preprocessEnable to set
		 */
		public void setPreprocessingEnable(boolean preprocessEnable) {
			this.preprocessingEnable = preprocessEnable;
		}

		/**
		 * Run the specified runnable using the specified progress monitor.
		 * 
		 * @param monitor the monitor used to report progress
		 * @throws InvocationTargetException
		 * @throws InterruptedException
		 */
		public void run(IProgressMonitor monitor) 
			throws InvocationTargetException, InterruptedException 
		{
			try {
				// Configure the project
				IMidletSuiteProject suite = getMidletSuiteProject(javaProject);
				suite.setDevice(device, monitor);
				addNatures(monitor);
				setJavaProjectOptions(monitor);
					
				// Set the platform definition for the project
				setProjectMetadata();
				
				// Add the default application descriptor file.
				createApplicationDescriptorInProject(monitor);

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

		/**
		 * Add the J2ME nature to the specified project.
		 * 
		 * @param monitor
		 * @throws CoreException
		 */
		private void addNatures(IProgressMonitor monitor) 
			throws CoreException 
		{
			IProjectDescription desc = project.getDescription();
			ArrayList natures = new ArrayList(Arrays.asList(desc.getNatureIds()));

			boolean updated = addNatureIfNecessary(natures, IEclipseMECoreConstants.J2ME_NATURE_ID);
			updated = updated | addNatureIfNecessary(natures, JavaCore.NATURE_ID);
			if (isPreprocessingEnable()) {
				updated = 
					updated |
					addNatureIfNecessary(natures, IEclipseMECoreConstants.J2ME_PREPROCESSING_NATURE_ID);
			}
			
			if (updated) {
				IProgressMonitor submonitor = new SubProgressMonitor(monitor, 1);
				desc.setNatureIds((String[]) natures.toArray(new String[natures.size()]));
				project.setDescription(desc, submonitor);
			}
		}
		
		/**
		 * Add the specified nature to the list of natures if it is not already
		 * in the list.
		 * 
		 * @param natures
		 * @param nature
		 * @return
		 */
		private boolean addNatureIfNecessary(ArrayList natures, String nature) {
			boolean added = false;
			
			if (!natures.contains(nature)) {
				natures.add(nature);
				added = true;
			}
			
			return added;
		}

		/**
		 * Set the necessary java project options.
		 * @param monitor
		 */
		private void setJavaProjectOptions(IProgressMonitor monitor) {
			Preferences prefs = EclipseMECorePlugin.getDefault().getPluginPreferences();
			boolean forceJava11 = 
				prefs.getBoolean(IEclipseMECoreConstants.PREF_FORCE_JAVA11);
			
			if (forceJava11) {
				for (int i = 0; i < JAVA11_OPTIONS.length;) {
					String option = JAVA11_OPTIONS[i++];
					String value = JAVA11_OPTIONS[i++];
					javaProject.setOption(option, value);
				}
			}
		}

		/**
		 * Set the persistent property on the created project
		 * to track the platform definition.
		 */
		private void setProjectMetadata() 
			throws CoreException 
		{
			IMidletSuiteProject midletprj = getMidletSuiteProject(javaProject);
			midletprj.setJadFileLocation(jadFilePath);
			midletprj.saveMetaData();
		}

		/**
		 * Create the template application descriptor in the project.
		 * 
		 * @param monitor
		 * @throws CoreException
		 */
		private void createApplicationDescriptorInProject(IProgressMonitor monitor)
			throws CoreException, IOException
		{
			// Get the project references
			IMidletSuiteProject midletSuite = getMidletSuiteProject(javaProject);
		
			// Check the JAD file for existence
			IFile jadFile = midletSuite.getJadFile();
			if (!jadFile.exists()) {
				if (jadFilePath.segmentCount() > 1) {
					IContainer container = project;
					for (int i = 0; i < (jadFilePath.segmentCount() - 1); i++) {
						IPath folderPath = new Path(jadFilePath.segment(i));
						IFolder folder = container.getFolder(folderPath);
						if (!folder.exists()) {
							folder.create(true, true, monitor);
						}
						
						container = folder;
					}
				}
				
				InputStream is = getJADFileSource(midletSuite);
				jadFile.create(is, true, monitor);
			}
		}

		/**
		 * @see eclipseme.core.model.IMidletSuiteProject#getDefaultApplicationDescriptorProperties()
		 */
		private ColonDelimitedProperties getDefaultApplicationDescriptorProperties(IMidletSuiteProject suite)
		{
			ColonDelimitedProperties descriptor = new ColonDelimitedProperties();
			
			// Do the OTA URL
			descriptor.setProperty(
				IJADConstants.JAD_MIDLET_JAR_URL,
				suite.getJarFilename());
				
			// Couple of names...
			descriptor.setProperty(
				IJADConstants.JAD_MIDLET_NAME,
				project.getName() + " Midlet Suite");
			descriptor.setProperty(
				IJADConstants.JAD_MIDLET_VENDOR,
				"Midlet Suite Vendor");
			descriptor.setProperty(
				IJADConstants.JAD_MIDLET_VERSION,
				"1.0.0");
				
			// Platform information
			String configVersion = getConfigurationVersion();
			if (configVersion != null) {
				descriptor.setProperty(
						IJADConstants.JAD_MICROEDITION_CONFIG,
						configVersion);
			}

			String profileVersion = getProfileVersion();
			if (profileVersion != null) {
				descriptor.setProperty(
						IJADConstants.JAD_MICROEDITION_PROFILE,
						profileVersion);
			}
			
			return descriptor;
		}

		/**
		 * Return the version of the configuration from the device
		 * or <code>null</code> if the version cannot be determined.
		 * 
		 * @return
		 */
		private String getConfigurationVersion() {
			ILibrary library = device.getConfigurationLibrary();
			
			API api = null;
			if (library != null) {
				api = library.getConfiguration();
			}
			
			return (api == null) ? "1.0" : api.toString();
		}

		/**
		 * Get the bytes that make up the contents of the to-be created JAD
		 * file.
		 * 
		 * @param midletSuite
		 * @return
		 * @throws IOException
		 * @throws CoreException
		 */
		private InputStream getJADFileSource(IMidletSuiteProject midletSuite)
			throws IOException, CoreException
		{
			// The InputStream to be returned...
			InputStream is = null;
			
			// Check for a JAD source file to see if it exists in the
			// project source directory
			String jadName = midletSuite.getJadFile().getName();
			IFolder folder = project.getFolder("bin");
			if (folder.exists()) {
				IFile jadFile = folder.getFile(jadName);
				if (jadFile.exists()) {
					is = jadFile.getContents();
				}
			}
			
			// If we didn't get a previous source file, use defaults
			if (is == null) {
				// Get the application descriptor and write it to 
				// a byte array
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				ColonDelimitedProperties defaultProps = 
					getDefaultApplicationDescriptorProperties(midletSuite);
				defaultProps.store(bos, "");
				
				// Use the byte array as the source to create the
				// new file
				is = new ByteArrayInputStream(bos.toByteArray());
			}
			
			return is;
		}

		/**
		 * Return the version of the profile from the device
		 * or <code>null</code> if the version cannot be determined.
		 * 
		 * @return
		 */
		private String getProfileVersion() {
			ILibrary library = device.getProfileLibrary();
			API api = library.getProfile();
			
			return (api == null) ? null : api.toString();
		}
	}
	
	// Storage of the previously created midlet suite projects
	private static final Map midletSuiteMap = new HashMap();
	
	/**
	 * Return the midlet suite project instance for the
	 * specified java project.
	 * 
	 * @param javaProject the Java project to retrieve the midlet suite
	 * wrapper
	 * @return the midlet suite wrapper
	 */
	public static IMidletSuiteProject getMidletSuiteProject(IJavaProject javaProject)
	{
		IMidletSuiteProject suite = null;
		
		synchronized (midletSuiteMap) {
			suite = (IMidletSuiteProject) midletSuiteMap.get(javaProject);
			if (suite == null) {
				suite = new MidletSuiteProject(javaProject);
				midletSuiteMap.put(javaProject, suite);
			}
		}
		
		return suite; 
	}

	/**
	 * Static-only access 
	 */
	private MidletSuiteFactory() {
		super();
	}
}

⌨️ 快捷键说明

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