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

📄 devicefactory.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.device;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;

import eclipseme.core.importer.IDeviceImporter;
import eclipseme.core.internal.EclipseMECorePlugin;

/**
 * The device factory allows access to creation of devices based on
 * the available registered device importers.
 * <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/03/02 01:40:24 $
 * <br>
 * @author Craig Setera
 */
public class DeviceFactory {
	/**
	 * The extension point for use in registering new device importers.
	 */
	public static final String EXT_DEVICE_IMPORTERS = "deviceImporter";
	
	// Get the registered device importers
	private static IDeviceImporter[] deviceImporters;

	/**
	 * Return all devices that can be found in the specified
	 * directory or any subdirectories.  This method will 
	 * consult all registered IDeviceImporter implementations
	 * that have been registered with the system.
	 * 
	 * @param directory
	 * @param foundDevices 
	 * @param monitor
	 * @return
	 * @throws CoreException 
	 * @throws InterruptedException if the user cancels the operation
	 */
	public static void findDevices(File directory, IFoundDevicesList foundDevices, IProgressMonitor monitor) 
		throws CoreException, InterruptedException 
	{
		monitor.beginTask("Device Search", IProgressMonitor.UNKNOWN);
		IDeviceImporter[] importers = getDeviceImporters();
		addDevices(foundDevices, directory, importers, monitor);
		monitor.done();
	}
	
	/**
	 * Add any devices that can be found in the specified directory
	 * or subdirectories given the specified device importer instances.
	 * 
	 * @param foundDevices
	 * @param directory
	 * @param importers
	 * @param monitor 
	 * @throws InterruptedException if the user cancels the operation
	 */
	private static void addDevices(
		IFoundDevicesList foundDevices, 
		File directory, 
		IDeviceImporter[] importers, 
		IProgressMonitor monitor) 
			throws InterruptedException
	{
		// Give the user the chance to bail out during the search
		if (monitor.isCanceled()) {
			throw new InterruptedException();
		}
		
		// First find any devices in the current directory
		monitor.setTaskName("Searching in " + directory);
		for (int i = 0; i < importers.length; i++) {
			SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
			IDevice[] localFoundDevices = importers[i].getMatchingDevices(directory, subMonitor);
			if (localFoundDevices != null) {
				foundDevices.addDevices(localFoundDevices);
			}
		}
		
		// Now recurse to subdirectories
		File[] subdirectories = directory.listFiles(new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}
		});
		
		for (int i = 0; i < subdirectories.length; i++) {
			addDevices(foundDevices, subdirectories[i], importers, monitor);
		}
		
		monitor.worked(1);
	}

	/**
	 * Return the registered device importer instances.
	 * 
	 * @return
	 * @throws CoreException 
	 */
	private static IDeviceImporter[] getDeviceImporters() 
		throws CoreException 
	{
		if (deviceImporters == null) {
			deviceImporters = readDeviceImporters();
		}
		
		return deviceImporters;
	}
	
	/**
	 * Read the device importers from the registered extensions
	 * to the "deviceImporters" extension point.
	 * 
	 * @return
	 * @throws CoreException 
	 */
	private static IDeviceImporter[] readDeviceImporters() 
		throws CoreException 
	{
		String pluginId = EclipseMECorePlugin.getDefault().getBundle().getSymbolicName();
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		
		IConfigurationElement[] elements =
			registry.getConfigurationElementsFor(pluginId, EXT_DEVICE_IMPORTERS);
		DeviceImporterElement[] deviceElements = new DeviceImporterElement[elements.length];
		
		for (int i = 0; i < elements.length; i++) {
			deviceElements[i] = new DeviceImporterElement(elements[i]);
		}
		
		Arrays.sort(deviceElements, new Comparator() {
			public int compare(Object o1, Object o2) {
				DeviceImporterElement element1 = (DeviceImporterElement) o1;
				DeviceImporterElement element2 = (DeviceImporterElement) o2;
				return element1.getPriority() - element2.getPriority();
			}
		});
		
		IDeviceImporter[] importers = new IDeviceImporter[elements.length];
		for (int i = 0; i < deviceElements.length; i++) {
			importers[i] = deviceElements[i].getDeviceImporter();
		}
		
		return importers;
	}

	/*
	 * Static factory methods only
	 */
	private DeviceFactory() {
		super();
	}
}

⌨️ 快捷键说明

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