📄 connectorform.java
字号:
/* The Bluetooth Library for client-server communication Copyright (C) 2006 Martin Vysny This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */package net.sf.btw.ui;import java.util.Vector;import javax.bluetooth.UUID;import javax.microedition.lcdui.Alert;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.Item;import javax.microedition.lcdui.ItemCommandListener;import javax.microedition.lcdui.StringItem;import javax.microedition.lcdui.Ticker;import net.sf.btw.btlib.ILookupListener;import net.sf.btw.btlib.Peer;import net.sf.btw.btlib.ServerInfo;import net.sf.btw.btlib.ServerLookup;import net.sf.btw.tools.Logger;/** * The connector form that allows user to select whether it wants to be a server * or a client. It immediately starts scanning for servers in range. * * @author Martin Vysny */public final class ConnectorForm extends Form implements ILookupListener, CommandListener, ItemCommandListener { private final Command playCommand = new Command("Start", Command.ITEM, 1); private final Command refreshCommand = new Command("Refresh", Command.SCREEN, 1); private final Command logCommand = new Command("Show log", Command.SCREEN, 2); private final Command closeCommand = new Command("Close", Command.BACK, 1); /** * Used for server scanning. If <code>null</code> then there is no running * lookup. */ private volatile ServerLookup lookup; /** * The display instance. */ private final Display display; private final IConnectorListener listener; private final String serverLabel; private final String clientLabel; /** * Creates new connector form. * * @param uuid * UUID of the application. * @param display * the display instance. * @param listener * listens for connector events. * @param serverLabel * Label of the item which starts new server. * @param clientLabel * label of the item which connects to a client. */ public ConnectorForm(final UUID uuid, final Display display, final IConnectorListener listener, String serverLabel, String clientLabel) { super("Connection setup"); this.listener = listener; this.display = display; this.serverId = uuid; this.serverLabel = serverLabel; this.clientLabel = clientLabel; addCommand(refreshCommand); if (Logger.isLoggable(Logger.LEVEL_INFO)) addCommand(logCommand); addCommand(closeCommand); final String deviceName = Peer.getDeviceName(); final Item item = new StringItem(this.serverLabel, deviceName); item.setDefaultCommand(playCommand); item.addCommand(playCommand); item.setItemCommandListener(this); append(item); startServerScan(); setCommandListener(this); } /** * The server UUID. */ public final UUID serverId; /* * (non-Javadoc) * * @see net.sf.btw.btlib.IClientListener#serverFound(net.sf.btw.btlib.ServerInfo) */ public void serverFound(ServerInfo server) { servers.addElement(server); final Item item = new StringItem(clientLabel, server.displayableName); item.setDefaultCommand(playCommand); item.addCommand(playCommand); item.setItemCommandListener(this); append(item); } /* * (non-Javadoc) * * @see net.sf.btw.btlib.IClientListener#serverScanError(java.lang.Exception) */ public void serverScanError(Exception ex) { final Alert alert = new Alert( "Error", "Error occured. Please see log for details. " + ex.getMessage(), null, AlertType.ERROR); alert.setTimeout(3000); display.setCurrent(alert, this); } /* * (non-Javadoc) * * @see net.sf.btw.btlib.IClientListener#serverScanFinished(java.util.Vector, * boolean) */ public void serverScanFinished(Vector servers, boolean completely) { setTicker(null); } /* * (non-Javadoc) * * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, * javax.microedition.lcdui.Displayable) */ public void commandAction(Command cmd, Displayable arg1) { try { if (cmd == refreshCommand) { startServerScan(); } else if (cmd == logCommand) { new Logger(this, display); } else if (cmd == closeCommand) { stopServerScan(); listener.closeConnector(); } } catch (Exception e) { display.setCurrent(new Alert("Error", "Error occured. Please see log for details. " + e.getMessage(), null, AlertType.ERROR), this); } } /** * A list of nearby servers. List of {@link ServerInfo} instances. */ private final Vector servers = new Vector(); private void stopServerScan() { setTicker(null); final ServerLookup l = lookup; if (l != null) { l.interrupt(); try { l.join(); } catch (InterruptedException e) { Logger.warn(null, e); } } lookup = null; } /** * Starts a scan for nearby bluetooth servers. */ private void startServerScan() { stopServerScan(); while (size() > 1) { delete(size() - 1); } servers.removeAllElements(); setTicker(new Ticker("Scanning for bluetooth devices")); lookup = new ServerLookup(serverId, this); lookup.searchForServers(); } private int getItemIndex(final Item item) { for (int i = 0; i < size(); i++) { if (get(i) == item) return i; } return -1; } /* * (non-Javadoc) * * @see javax.microedition.lcdui.ItemCommandListener#commandAction(javax.microedition.lcdui.Command, * javax.microedition.lcdui.Item) */ public void commandAction(Command c, Item item) { if (c == playCommand) { final int index = getItemIndex(item); if (index < 0) return; stopServerScan(); if (index == 0) { listener.startServer(); return; } final ServerInfo info = (ServerInfo) servers.elementAt(index - 1); listener.connectToServer(info); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -