📄 deviceregistry.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.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.eclipse.core.runtime.IPath;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.persistence.XMLPersistenceProvider;
import eclipseme.core.internal.utils.XMLUtils;
import eclipseme.core.persistence.IPersistable;
import eclipseme.core.persistence.IPersistenceProvider;
import eclipseme.core.persistence.PersistenceException;
/**
* The device registry provides a place to store and
* retrieve devices by name.
* <br/>
* This registry is implemented as a multi-level registry
* in which the top-level device groups reference lower-level
* devices.
* <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.1 $
* <br>
* $Date: 2006/02/11 21:26:57 $
* <br>
* @author Craig Setera
*/
public class DeviceRegistry implements IPersistable {
/** Singleton instance of the device registry */
public static final DeviceRegistry singleton = new DeviceRegistry();
private static final String FILENAME = "devices.xml";
// Map of device group names that maps to a lower-level
// map of device instances by name
private Map deviceGroupsMap;
// A list of registry listeners
private ArrayList listenerList;
// The default device
private IDevice defaultDevice;
private DeviceRegistry() {
super();
listenerList = new ArrayList();
}
/**
* Add a new device instance to the device registry.
*
* @param device
* @throws IllegalArgumentException if the device is not well formed.
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
public void addDevice(IDevice device)
throws IllegalArgumentException, PersistenceException
{
if ((device.getGroupName() == null) || (device.getName() == null)) {
throw new IllegalArgumentException();
}
Map groupMap = getDeviceGroupMap(device.getGroupName(), true);
groupMap.put(device.getName(), device);
fireDeviceAddedEvent(device);
}
/**
* Add the specified listener to the list of listeners for changes
* in this registry.
*
* @param listener
*/
public void addRegistryListener(IDeviceRegistryListener listener) {
listenerList.add(listener);
}
/**
* Clear the registry of all entries.
*
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
public void clear()
throws PersistenceException
{
getDeviceGroupsMap().clear();
}
/**
* Return a list of all of the devices in the registry.
*
* @return
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
public List getAllDevices()
throws PersistenceException
{
ArrayList deviceList = new ArrayList();
Iterator groups = getDeviceGroupsMap().values().iterator();
while (groups.hasNext()) {
Map deviceMap = (Map) groups.next();
deviceList.addAll(deviceMap.values());
}
return deviceList;
}
/**
* Return the default device or <code>null</code> if one
* is not specified.
*
* @return
*/
public IDevice getDefaultDevice() {
return defaultDevice;
}
/**
* Return the device with the specified key or <code>null</code>
* if no such device is found in the registry.
*
* @param groupName
* @param deviceName
* @return
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
public IDevice getDevice(String groupName, String deviceName)
throws PersistenceException
{
IDevice device = null;
Map groupMap = getDeviceGroupMap(groupName, false);
if (groupMap != null) {
device = (IDevice) groupMap.get(deviceName);
}
return device;
}
/**
* Return the number of devices registered.
*
* @return
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
public int getDeviceCount()
throws PersistenceException
{
int deviceCount = 0;
Iterator groupMaps = getDeviceGroupsMap().values().iterator();
while (groupMaps.hasNext()) {
Map groupMap = (Map) groupMaps.next();
deviceCount += groupMap.size();
}
return deviceCount;
}
/**
* Return the registered device groups.
*
* @return
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
public List getDeviceGroups()
throws PersistenceException
{
return new ArrayList(getDeviceGroupsMap().keySet());
}
/**
* Return the devices found in the specified group or
* <code>null</code> if the specified group cannot be
* found.
*
* @param groupName
* @return
* @throws PersistenceException
*/
public List getDevices(String groupName)
throws PersistenceException
{
List devices = null;
Map devicesGroupMap = getDeviceGroupMap(groupName, false);
if (devicesGroupMap != null) {
devices = new ArrayList(devicesGroupMap.values());
}
return devices;
}
/**
* Load the contents of the device registry from the storage file
* in the plug-in state location.
*
* @throws PersistenceException
*/
public void load()
throws PersistenceException
{
File storeFile = getComponentStoreFile();
if (storeFile.exists()) {
try {
Document document = XMLUtils.readDocument(storeFile);
XMLPersistenceProvider pprovider = new XMLPersistenceProvider(document);
loadUsing(pprovider);
} catch (ParserConfigurationException e) {
throw new PersistenceException(e.getMessage(), e);
} catch (SAXException e) {
throw new PersistenceException(e.getMessage(), e);
} catch (IOException e) {
throw new PersistenceException(e.getMessage(), e);
}
}
}
/**
* @see eclipseme.core.persistence.IPersistable#loadUsing(eclipseme.core.persistence.IPersistenceProvider)
*/
public void loadUsing(IPersistenceProvider persistenceProvider)
throws PersistenceException
{
getDeviceGroupsMap().clear();
int deviceCount = persistenceProvider.loadInteger("deviceCount");
for (int i = 0; i < deviceCount; i++) {
IDevice device = (IDevice) persistenceProvider.loadPersistable("device" + i);
addDevice(device);
}
defaultDevice = (IDevice) persistenceProvider.loadReference("defaultDevice");
}
/**
* Remove the specified device from the registry if it
* exists.
*
* @param device
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
public void removeDevice(IDevice device)
throws PersistenceException
{
Map groupMap = getDeviceGroupMap(device.getGroupName(), false);
if (groupMap != null) {
groupMap.remove(device.getName());
// Remove empty groups
if (groupMap.isEmpty()) {
deviceGroupsMap.remove(device.getGroupName());
}
// Clear the default device if it is being removed
if (device.equals(defaultDevice)) {
defaultDevice = null;
}
}
fireDeviceRemovedEvent(device);
}
/**
* Remove the specified listener to the list of listeners for changes
* in this registry.
*
* @param listener
*/
public void removeRegistryListener(IDeviceRegistryListener listener) {
listenerList.remove(listener);
}
/**
* Set the default device.
*
* @param device
*/
public void setDefaultDevice(IDevice device) {
defaultDevice = device;
}
/**
* Store out the contents of the registry into the standard
* device storage file in the plug-in state location.
*
* @throws PersistenceException
* @throws TransformerException
* @throws IOException
*/
public void store()
throws PersistenceException, TransformerException, IOException
{
XMLPersistenceProvider pprovider = new XMLPersistenceProvider("deviceRegistry");
storeUsing(pprovider);
XMLUtils.writeDocument(getComponentStoreFile(), pprovider.getDocument());
}
/**
* @see eclipseme.core.persistence.IPersistable#storeUsing(eclipseme.core.persistence.IPersistenceProvider)
*/
public void storeUsing(IPersistenceProvider persistenceProvider)
throws PersistenceException
{
List allDevices = getAllDevices();
persistenceProvider.storeInteger("deviceCount", allDevices.size());
int index = 0;
Iterator iterator = allDevices.iterator();
while (iterator.hasNext()) {
IDevice device = (IDevice) iterator.next();
persistenceProvider.storePersistable("device" + index++, device);
}
if (defaultDevice != null) {
persistenceProvider.storeReference("defaultDevice", defaultDevice);
}
}
/**
* Do the initial device group maps creation and loading.
*
* @throws PersistenceException
*/
private void createAndLoadDeviceGroups()
throws PersistenceException
{
deviceGroupsMap = new HashMap();
load();
}
/**
* Fire an event that indicates a device has been added.
*
* @param device
*/
private void fireDeviceAddedEvent(IDevice device) {
Iterator listeners = listenerList.iterator();
while (listeners.hasNext()) {
IDeviceRegistryListener listener = (IDeviceRegistryListener) listeners.next();
listener.deviceAdded(device);
}
}
/**
* Fire an event that indicates a device has been removed.
*
* @param device
*/
private void fireDeviceRemovedEvent(IDevice device) {
Iterator listeners = listenerList.iterator();
while (listeners.hasNext()) {
IDeviceRegistryListener listener = (IDeviceRegistryListener) listeners.next();
listener.deviceRemoved(device);
}
}
/**
* Get the file for the device store file.
*
* @return
*/
private File getComponentStoreFile()
{
IPath pluginStatePath = EclipseMECorePlugin.getDefault().getStateLocation();
IPath storePath = pluginStatePath.append(FILENAME);
return storePath.toFile();
}
/**
* Return the map of devices for the specified group. If the
* map is null and createIfNull is specified, a new map will
* be created.
*
* @param groupName
* @param createIfNull
* @return
* @throws PersistenceException if there is a problem doing the initial
* registry load
*/
private Map getDeviceGroupMap(String groupName, boolean createIfNull)
throws PersistenceException
{
Map groupsMap = getDeviceGroupsMap();
Map deviceGroupMap = (Map) groupsMap.get(groupName);
if ((deviceGroupMap == null) && createIfNull) {
deviceGroupMap = new HashMap();
groupsMap.put(groupName, deviceGroupMap);
}
return deviceGroupMap;
}
/**
* Return the device groups map.
*
* @return
* @throws PersistenceException
*/
private synchronized Map getDeviceGroupsMap()
throws PersistenceException
{
if (deviceGroupsMap == null) {
createAndLoadDeviceGroups();
}
return deviceGroupsMap;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -