📄 connectorcanvas.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.Timer;import java.util.TimerTask;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.Canvas;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;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 canvas that allows user to select whether it wants to be a * server or a client. It immediately starts scanning for servers in range. * * @author Jan Tomka */public final class ConnectorCanvas extends Canvas implements ILookupListener, CommandListener { /** * The ConnectorCanvas line item representation. Can be a label common * to the following selectable items, a selectable allowing user to * choose, or a special scan item, displayed as the last item flashing * dots while Bluetooth scan is in progress. * * @author Jan Tomka */ private final class ConnectorCanvasItem { /** * Type value for selectable items. */ public static final int SELECTABLE = 0; /** * Type value for label items. */ public static final int LABEL = 1; /** * Type value for single special scan item. */ public static final int SCAN = 2; /** * String displayed, representing an item. */ private String string; /** * Type of item, one of SELECTABLE, LABEL or SCAN. This value * cannot be changed during the item lifetime. */ private final int type; /** * Creates new connector canvas item. * * @param string * @param type */ public ConnectorCanvasItem(String string, int type) { this.string = string; this.type = type; } /** * Sets connector canvas item string. * * @param string * New value of item string. */ public void setString(String string) { this.string = string; } /** * Returns current item string. * * @return Current value of item string. */ public String getString() { return string; } /** * Returns current item type. * * @return */ public int getType() { return type; } /** * Retuns <code>true</code> if item is selectable. Effectively * identical to condition <code>type == * ConnectorCanvasItem.SELECTABLE</code>. */ public boolean isSelectable() { return type == ConnectorCanvasItem.SELECTABLE; } } /** * Vector of ConnectorCanvasItem's currently present in * ConnectorCanvas. */ private final Vector items = new Vector(); /** * Font used to draw item strings. Always the device's default font. */ private final static Font font = Font.getDefaultFont(); /** * Canvas background color. Default value is 0xffffff (white). */ private int bgColor = 0xffffff; /** * Label item text color. Default value is 0x000000 (black). */ private int labelColor = 0x000000; /** * Label item background color. Default value is 0xffffff (white). */ private int labelBgColor = 0xffffff; /** * Selectable item text color. Default value is 0x0000ff (blue). */ private int selectableColor = 0x0000ff; /** * Selectable item background color. Default value is 0xffffff * (white). */ private int selectableBgColor = 0xffffff; /** * Selected item text color. Default value is 0xffffff (white). */ private int selectedColor = 0xffffff; /** * Selected item background color. Default value is 0x0000ff (blue). */ private int selectedBgColor = 0x0000ff; /** * Label item left side offset. Label item is drawn this number of * pixels from the canvas' left border. */ private int labelOffset = 2; /** * Selectable item left side offset. Selectable item is drawn this * number of pixels from the canvas' left border. */ private int selectableOffset = 12; /** * Index of currently selected item. Only items with type SELECTABLE * can be selected. When ConnectorCanvas is created, no item is * selected before one is appended. From then on, the first item * appended is selected by default. */ private int itemSelected = -1; /** * Index of special scan item. There can be only one item present with * type <code>SCAN</code>. It's position is always at the very end of * the items vector. Thus, the two possible values of this variable at * any time are <code>-1</code> (no scan item present) or * <code>(items.size() - 1)</code>. */ private int scanItem = -1; /** * Number of dots that flash at the end of a special scan line. Value * of this constant is 3. */ private final int lastDotsMax = 3; /** * Current number of dots drawn at the end of a special scan line. */ private int lastDots = 0; /** * Timer used to periodically update number of dots drawn at the end * of a special scan line. */ private Timer dotsTimer = new Timer(); /** * Task that performs updates of number of dots drawn at the end of a * special scan line. */ private TimerTask dotsTimerTask = new DotsTimerTask(); private class DotsTimerTask extends TimerTask { public void run() { lastDots = (lastDots + 1) % (lastDotsMax + 1); repaint(); } }; private final Command startCommand = new Command("Start", Command.OK, 1); /** * Command that causes ConnectorCanvas perform Bluetooth rescan. */ private final Command refreshCommand = new Command("Refresh", Command.SCREEN, 1); private final Command logCommand = new Command("Show log", Command.SCREEN, 2); /** * Command that closes the ConnectorCanvas itself. */ private final Command closeCommand = new Command("Close", Command.BACK, 1); /** * The server UUID. */ public final UUID serverId; /** * A list of nearby servers. List of {@link ServerInfo} instances. */ private final Vector servers = new Vector(); /** * 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; /** * Connector listener interface implemented on the application side. * ConnectorCanvas events are sent to it, to make application respond * accordingly to the user actions done in ConnectorCanvas. */ private final IConnectorListener listener; /** * String to be assigned to the label item displayed above the server * name. */ private final String serverLabel; /** * String to be assigned to the label item displayed above the list of * client names. */ 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. */ public ConnectorCanvas(final UUID uuid, final Display display, final IConnectorListener listener, String serverLabel, String clientLabel) { super(); this.listener = listener; this.display = display; this.serverId = uuid; this.serverLabel = serverLabel; this.clientLabel = clientLabel; addCommand(startCommand); addCommand(refreshCommand); addCommand(closeCommand); if (Logger.isLoggable(Logger.LEVEL_INFO)) addCommand(logCommand); initCanvas(); startServerScan(); setCommandListener(this); } /** * Initializes ConnectorCanvas. Removes all items and appends label * with serverLabel, selectable server name, label with clientLabel * and special scan item. */ private void initCanvas() { items.removeAllElements(); itemSelected = -1; scanItem = -1; final String deviceName = Peer.getDeviceName(); appendItem(this.serverLabel, ConnectorCanvasItem.LABEL); appendItem(deviceName, ConnectorCanvasItem.SELECTABLE); appendItem(clientLabel, ConnectorCanvasItem.LABEL); appendItem("Scanning", ConnectorCanvasItem.SCAN); } /** * Returns <code>true</code> if canvas items contain special scan * item. */ private boolean hasScanLine() { return scanItem >= 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -