www.pudn.com > busStop_java_srccode.rar > Finder.java


/*********************************************************************** 
 * 
 * 
 *		软件:公交路线选择软件 
 *		作者:李蛟 
 *		学校:浙江工业大学计本 
 *		邮箱:joke_leee@163.com 
 * 
 *		说明:本软件是用于查询市公交路线选择的软件。 
 *			  本软件在对相应文件进行修改后可用于任何类似各个城市的公交路线等的查询。 
 *			  除了用于商业需经得本人同意外,你可以学习使用甚至修改。 
 *			  第一次发布,难免很多的错误和不足,欢迎有兴趣的朋友批评指正,发现问 
 *			  题请与本人联系,咱们可以交流交流! 
 * 
 * 
 ***********************************************************************/ 
 
/*********************************************************************** 
 * 
 *此文件为最外层的操作。 
 *主要是用于手机的显示,接受输入,对内部数据、函数的调用 
 *来完成功能 
 * 
 *并有一些附加类,帮助完成操作 
 * 
 ***********************************************************************/ 
 
 
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import java.io.*; 
import java.lang.Thread; 
//import query.*; 
 
/********************************************** 
 *主类 
 **********************************************/ 
public class Finder extends MIDlet implements CommandListener 
{ 
	public Finder() 
	{ 
		display=Display.getDisplay(this); 
		inputStop=new List("公交站点", List.IMPLICIT); 
		inputBusBox=new TextBox("输入车次号", null, 3,TextField.NUMERIC); 
		inputBus=new List("车次选择", List.IMPLICIT); 
 
		//用于inputbox 
		okBusCmd=new Command("查询", Command.OK, 0); 
		cancelBusCmd=new Command("取消", Command.OK,1); 
		inputBusCmd=new Command("输入车次", Command.BACK,0); 
 
		//用于外围的 
		inputCmd=new Command("输入站点", Command.BACK, 0); 
		startCmd=new Command("设为起点站",Command.OK,1); 
		endCmd=new Command("设为目的站",Command.OK,2); 
		depthCmd=new Command("最多转车数",Command.OK,3); 
		queryCmd=new Command("开始搜索",Command.OK,4); 
		stopCmd=new Command("站点查询", Command.OK, 5); 
		busCmd=new Command("车次查询", Command.OK, 6); 
		helpCmd=new Command("使用帮助",Command.OK,7); 
		inputHelpCmd=new Command("输入法",Command.OK,8); 
		aboutCmd=new Command("关于本软件",Command.OK,9); 
		exitCmd=new Command("退出",Command.HELP,3); 
		 
		okCmdInput=new Command("确定", Command.OK, 0); 
		cancelCmdInput=new Command("取消", Command.CANCEL, 0); 
 
		inputStop.addCommand(inputCmd);			 
		inputStop.addCommand(startCmd); 
		inputStop.addCommand(endCmd); 
		inputStop.addCommand(queryCmd); 
		inputStop.addCommand(depthCmd); 
		inputStop.addCommand(stopCmd); 
		inputStop.addCommand(busCmd); 
		inputStop.addCommand(helpCmd); 
		inputStop.addCommand(inputHelpCmd); 
		inputStop.addCommand(aboutCmd); 
		inputStop.addCommand(exitCmd); 
		 
		inputBusBox.addCommand(okBusCmd); 
		inputBusBox.addCommand(cancelBusCmd); 
		 
		inputBus.addCommand(okBusCmd); 
		inputBus.addCommand(cancelBusCmd); 
		inputBus.addCommand(inputBusCmd); 
		 
		setCurrent(inputStop); 
		query=new Query(); 
	} 
	 
	public void startApp() 
	{ 
		new About(this, "帮助", Strings.help); 
		//display.setCurrent(new Alert("提示","按(输入站点)模糊查找站点。", null, AlertType.ERROR)); 
		 
		literalInput=new LiteralInput(); 
		literalInput.addCommand(okCmdInput); 
		literalInput.addCommand(cancelCmdInput); 
		addStopName(); 
	} 
	public void pauseApp(){} 
	public void destroyApp(boolean conditional){} 
	public void setCurrent() 
	{ 
		m_currDisplay.setCommandListener(this); 
		display.setCurrent(m_currDisplay); 
	} 
	public void setCurrent(Displayable d) 
	{ 
		m_currDisplay=d; 
		m_currDisplay.setCommandListener(this); 
		display.setCurrent(m_currDisplay); 
	} 
	public void commandAction(Command c,Displayable d) 
	{ 
		if((c==List.SELECT_COMMAND || c==stopCmd) && d==inputStop) 
		{ 
			//query.createStopAndBus(); 
			setCurrent(inputBus); 
			display.setCurrent(new Alert("显示", "经过此站的车次", null, AlertType.INFO)); 
			addBuss(inputStop.getString(inputStop.getSelectedIndex())); 
		}else  if(c==inputCmd) 
		{ 
			setCurrent(literalInput); 
		}else if(c==startCmd) 
		{ 
			setStartStop(); 
			//System.out.println("start"); 
			//String info=inputStop.getString(inputStop.getSelectedIndex()); 
			//display.setCurrent(new Alert("出发站点", info, null, AlertType.INFO)); 
			//info=null; 
		}else if(c==endCmd) 
		{ 
			setEndStop(); 
			//System.out.println("end"); 
			//String info=inputStop.getString(inputStop.getSelectedIndex()); 
			//display.setCurrent(new Alert("目的站点", info, null, AlertType.INFO)); 
			//info=null; 
 
		}else if(c==depthCmd) 
		{ 
			new SetDepth(this);	 
		}else if(c==queryCmd) 
		{ 
			display.setCurrent(new Alert("提示", "开始查询!\n请等待...", null, AlertType.INFO)); 
			query.createStopAndBus(); 
			System.gc(); 
			gotoRouteSee(query.isFinded()); 
		}else if(c==busCmd) 
		{ 
			if(inputBus.size()<=0) 
				setCurrent(inputBusBox); 
			else  
				setCurrent(inputBus); 
		}else if(c==exitCmd) 
		{ 
			destroyApp(false); 
			notifyDestroyed(); 
		}else if(c==aboutCmd) 
		{  
			new About(this, "关于本软件", Strings.about); 
		}else if(c==helpCmd) 
		{ 
			new About(this, "帮助", Strings.help); 
		}else if(c==inputHelpCmd) 
		{ 
			new About(this, "关于输入法", Strings.input); 
		}else if(c==okCmdInput) 
		{ 
			String input=literalInput.getOutputString(); 
			if(input==null) 
			{ 
				display.setCurrent(new Alert("错误", "未输入任何字符", null, AlertType.ERROR)); 
			}else 
			{ 
				query.searchStopName(input, inputStop); 
				//addStopName(); 
				setCurrent(inputStop); 
				input=null; 
			} 
		}else if(c==cancelCmdInput) 
		{ 
			setCurrent(inputStop); 
		}//下面是用于bus查询的 
		else if(c==inputBusCmd && d==inputBus) 
		{ 
			setCurrent(inputBusBox); 
		}else if(c==okBusCmd && d==inputBusBox) 
		{ 
			String busInput=inputBusBox.getString(); 
			setCurrent(inputBus); 
			addBus(busInput); 
			busInput=null; 
		}else if((c==okBusCmd || c==List.SELECT_COMMAND) && d==inputBus) 
		{ 
			if(inputBus.size()<=0) return; 
			String bus=inputBus.getString(inputBus.getSelectedIndex()); 
			setCurrent(inputStop); 
			addStops(bus); 
			display.setCurrent(new Alert("显示", "所经过的站点", null, AlertType.INFO)); 
			bus=null; 
		}else if(c==cancelBusCmd && d==inputBusBox) 
		{ 
			setCurrent(inputBus); 
		}else if(c==cancelBusCmd && d==inputBus) 
		{ 
			setCurrent(inputStop); 
		} 
		 
	} 
 
	//在stops中加入站点名称 
	private void addStopName() 
	{ 
		new AddStops(this).start(); 
	} 
	 
	private void gotoRouteSee(boolean found) 
	{ 
		new RouteSee(this); 
	} 
 
	//向inputBus中加入站点上的bus 
	private void addBuss(final String stop) 
	{ 
		int number=query.getStopIDFromName(stop); 
		if(number<0 || number>=query.getStopNum()) return; 
		int num=inputBus.size(); 
		for(int i=0; i=owner.query.getLineNum()) 
			{  
				currentRoute=-1; 
				owner.display.setCurrent(new Alert("提示", "已是最后一条路线\n将显示第1条路线", null, AlertType.WARNING)); 
				getNextRoute(true); 
				return; 
			} 
		}else 
		{ 
			currentRoute--; 
			if(currentRoute<0) 
			{  
				currentRoute=owner.query.getLineNum(); 
				owner.display.setCurrent(new Alert("提示", "已是第一条路线\n将显示最后一条路线", null, AlertType.WARNING)); 
				getNextRoute(false); 
				return; 
			} 
		} 
		//某条路线的长度 
		int num=owner.query.getGoodLine(currentRoute).getBusNum(); 
		String no="路线:"+(new Integer(currentRoute+1).toString())+" 乘车数:";  
		text.append(no); 
		text.append(new Integer(num).toString()); 
		text.append("\n\n"); 
		text.append(owner.query.getStopName(owner.query.getStartStop())+"\n"); 
		for(int i=0; i=owner.query.getStopNum()) return; 
		String stop=owner.query.getStopName(num); 
		text=new Form(stop); 
		//text=new Form("站点查询"); 
		//text.append(stop); 
		 
		text.addCommand(exit); 
		text.setCommandListener(this); 
		addBus(num); 
		 
		owner.display.setCurrent(text); 
	} 
	 
	public void commandAction(Command c,Displayable d) 
	{ 
		if(c==exit) 
		{ 
			System.gc(); 
			owner.setCurrent(); 
		} 
	} 
	 
	public void addBus(int number) 
	{ 
		if(number<0 || number>=owner.query.getStopNum()) return; 
		if(text.size()>0) text.delete(0); 
		//某条路线的长度 
		int num=owner.query.m_stopList.getBusNumOnStop(number); 
		//System.out.println(num); 
		int busID; 
		for(int i=0; i0) form.delete(0); 
			form.append("未找到你输入的车次!"); 
			form.setCommandListener(this); 
			owner.display.setCurrent(form); 
		} 
	} 
	 
	//根据选择的bus,来加入相应的站点名 
	public void addStop() 
	{ 
		if(form.size()>0) form.delete(0); 
		int busID=owner.query.getBusIdFromName(bus); 
		//某条路线的长度 
		int num=owner.query.m_busList.getStopNumOnBus(busID); 
		int stopID; 
		for(int i=0; i'0') owner.query.setDepth((int)i-'0'); 
		} 
		owner.setCurrent(); 
	} 
//@@ATTRIBUTE 
	Form form; 
	TextField inputDepth; 
	Command okCmd,calcelCmd; 
	Finder owner; 
} 
 
//about 
class About implements CommandListener 
{ 
	public About(Finder own, String title, String content) 
	{ 
		owner=own; 
		form=new Form(title); 
		form.append(content); 
		okCmd=new Command("确定",Command.OK,0); 
		 
		form.addCommand(okCmd); 
		form.setCommandListener(this); 
 
		owner.display.setCurrent(form); 
	} 
 
	public void commandAction(Command cmd, Displayable dis) 
	{ 
		if(cmd==okCmd){ 
			owner.setCurrent(); 
		} 
	} 
//@@ATTRIBUTE 
	Form form; 
	Command okCmd; 
	Finder owner; 
} 
 
class Strings 
{ 
	static String about="软件:公交路线选择软件\n作者:李蛟\n浙江工业大学计本026班\n邮箱:joke_leee@163.com\n" 
			+"本软件是用于查询市公交路线选择的软件。" 
			+"本软件在对相应文件进行修改后可用于任何类似各个城市的公交路线等的查询。此软件对以前未发布版本作" 
			+"了重大改进(才敢拿出来show^_^)。除了用于商业需经得本人同意外,你可以学习使用" 
			+"甚至修改。第一次发布,难免很多的错误和不足,欢迎有兴趣的朋友批评指正,发现问" 
			+"题请与本人联系,咱们可以交流交流!"; 
	static String help="输入站点:输入某个站点的部分名称,搜索站点。进行操作。\n\n" 
			+"站点查询:查询经过本站点的公交车。可以用中键'i'代替。\n\n" 
			+"车次查询:通过输入车次号码,查找车次,并查找此车次所经过的站点。可以用中键'i'代替。\n\n" 
			+"设为起点站:设置当前选择的站点为搜索路线时的出发站点。\n\n" 
			+"设为目的站:设置当前选择的站点为搜索路线时的出目的站。\n\n" 
			+"最多转车数:设置选择路线最多的转车数量,即转车次数需大于所设置数时,次路线不在考虑之内,这样可以加快搜索速度。\n\n" 
			+"开始搜索:在设置了起止站点后,开始搜索。时间可能比较长,请耐心等待。\n\n"; 
			 
	static String input="本输入法是为了此软件而作的,可以移植到其他java手机程序中。由于容量大,刚开始输入时" 
			+"会比较慢,请耐心等待。等待过程中不要随便乱按按键!对输入法有什么意见也可以通知我,不甚感激!"; 
}