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

📄 platform.java

📁 j2me实现的移动机器人代码(Java实现)
💻 JAVA
字号:
package name.lxm.robot.arch;

import java.util.*;
import java.io.*;
import org.jdom.*;
import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import java.awt.*;


/**
 * <p>This class is just as its name, it makes all things running. </p>
 *
 * <p>This platform is in charge of controlling the modules' initialization and destry, providing a container for modules to run and providing a kind of communication methanism among these modules.</p>
 *
 * <p>It's simple but powerfull.</p>
 */
public class Platform implements Runnable
{
	/** the robot configuration xml document */
	private RobotConfDoc confdoc = null;
	/** mark itself */
	private static Platform instance = null;
	/** used to store and index all the modules in the platform */
	private HashMap module_hash;
	private JFrame topWindow = null;
	private JDesktopPane desktop = null;
	
	/**
	 * Inner constructor
	 */
	protected Platform()
	{
		module_hash = new HashMap();
	}
	
	/**
	 * Initialize the platform, and get the platform object reference
	 * @return the platform reference
	 */
	public static Platform loadPlatform()
	{
		if(instance == null)
			instance = new Platform();
		return instance;
	}
	
	/**
	 * Another way to get the platform reference
	 * @return the platform reference
	 * @see #loadPlatform()
	 */
	public static Platform getPlatform()
	{
		return loadPlatform();
	}
	
	/**
	 * Get the desired module object
	 * @param module_name 
	 * The name of the module which is wanted
	 */
	public Module getModule(String module_name)
	{
		return (Module) module_hash.get(module_name);
	}

	/**
	 * Get the requested wire configuration
	 * @param id String - the id of the wire
	 * @return WireConfig - the wire configuration
	 */
	public WireConfig getWireConfig(String id)
	{
		HashMap wcs = confdoc.getWireConfigs();
		return (WireConfig) wcs.get(id);
	}

	/**
	 * <p>Config the platform. </p>
	 * <p>To use this platform, 3 steps is needed: first, 
	 * get an instance of the platform; then, config it; and last,
	 * booting it. </p>
	 * 
	 * @param file - The configuration file, 
	 * containing the modules and wires information
	 * @throws IOException,
	 * 	IllegalXMLFormatException,
	 * 	JDOMException
	 * @see #booting()
	 */
	public void config(String file) 
		throws IOException, 
		       IllegalXMLFormatException, 
		       JDOMException
	{
		Reader r = new FileReader(file);
		confdoc = new RobotConfDoc(r);
	}

	/**
	 * booting the platform, 
	 * install all the modules and make the system working.
	 * 
	 * @throws Exception 
	 * @see #config(String)
	 */
	public void booting() throws Exception
	{
		if(confdoc.requireUI())
		{
		//create ui
			desktop = new JDesktopPane();
			desktop.setVisible(true);
			topWindow = new JFrame(confdoc.getRobotName());
			topWindow.setSize(new Dimension(800, 600));
			topWindow.setContentPane(desktop);		
			topWindow.setResizable(true);
			topWindow.setVisible(true);
			topWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		}
		Iterator iterator = confdoc.getModuleDocs();
		while(iterator.hasNext())
		{
			//Loading modules...
			ModuleDoc md = (ModuleDoc) iterator.next();
			Module m = loadModule(md);
			//initialize the modules
			m.init(md);
			//put it in the module hash
			module_hash.put(m.getName(), m);
		}

		//start the modules
		iterator = module_hash.values().iterator();
		while(iterator.hasNext())
		{
			Module m = (Module) iterator.next();
			m.start();
			logit("Module " + m.getName() + " has been started.");
		}
		//booting the simple shell
		logit("Now, booting the shell...");
		new Thread(this).start();
	}

	/**
	 * loading module from xml configuration
	 * @param md - ModuleDoc, the robot configuration file
	 * @throws Exception - Something wrong, check the message
	 * @return the Module Object that has just been generated
	 */
	protected Module loadModule(ModuleDoc md) throws Exception
	{
		String cn = md.getClassName();
		if(cn == null || cn.length() == 0)
		{
			throw new Exception("Wrong Configuration for Module");
		}
		Object m = Class.forName(cn)
			.newInstance();
		if(m instanceof Module)
		{
			logit("Module " + cn + " has been loaded.");
			return (Module) m;
		}else{
			throw new Exception("Failed loading module: " + cn);
		}
	}

	/**
	 * Unload the platform, ready for shutdown the software.
	 * @throws Exception something wrong, check the message
	 */
	public void unLoad() throws Exception
	{
		Iterator i = module_hash.values().iterator();
		while(i.hasNext())
		{
			Module m = (Module) i.next();
			m.stop();
		}
		
		i = module_hash.values().iterator();
		while(i.hasNext())
		{
			Module m = (Module) i.next();
			m.destroy();
		}
	}


	public static void main(String[] args)
	{
		String rc;
		if(args.length > 0 )
			rc = args[0];
		else
			rc = "mobile.xml";
		System.out.println("Booting the platform accroding to " + rc);
		System.out.println("Creating a Platform instance. ");
		try{
			Platform platform = Platform.getPlatform();
			platform.config(rc);
			platform.booting();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	private void logit(String msg)
	{
		System.out.println(msg);
	}

	public void run()
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;
		System.out.print(">>");
		try{
			while((line = br.readLine()) != null )
			{
				if(line.equals("quit"))
				{
					try{
						unLoad();
					}catch(Exception e2)
					{
						e2.printStackTrace();
					}
					System.exit(0);
					break;
				}else
					showHelp();
				System.out.print(">>");
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	private void showHelp()
	{
		logit("Command List:");
		logit("    quit - shutdown this program ");
		logit("    help - show this information ");
	}

	public JFrame getFrameWindow()
	{
		return topWindow;
	}

	public JDesktopPane getDesktop()
	{
		return desktop;
	}

	public void updateWindow()
	{
		if(desktop != null)
		{
			JInternalFrame[] i_frames = desktop.getAllFrames();
		       for(int i=0; i<i_frames.length; i++)
		       {
			       i_frames[i].show();
		       }	       
		}
	}
}

⌨️ 快捷键说明

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