📄 connectorcanvas.java
字号:
} /** * Sets canvas background color. */ public void setBgColor(int v) { bgColor = v; } /** * Sets label item text color. */ public void setLabelColor(int v) { labelColor = v; } /** * Sets label item background color. */ public void setLabelBgColor(int v) { labelBgColor = v; } /** * Sets selectable item text color. */ public void setSelectableColor(int v) { selectableColor = v; } /** * Sets selectable item background color. */ public void setSelectableBgColor(int v) { selectableBgColor = v; } /** * Sets selected item text color. */ public void setSelectedColor(int v) { selectedColor = v; } /** * Sets selected item background color. */ public void setSelectedBgColor(int v) { selectedBgColor = v; } /** * Sets label item left side offset. */ public void setLabelOffset(int v) { labelOffset = v; } /** * Sets selectable item left side offset. */ public void setSelectableOffset(int v) { selectableOffset = v; } /** * Appends an item to the ConnectorCanvas. Updates internal canvas * state accordingly and repaints canvas. */ public void appendItem(String s, int type) { ConnectorCanvasItem item = new ConnectorCanvasItem(s, type); int newIndex; /* Always prepend the Scanning line */ if (hasScanLine()) { newIndex = scanItem; scanItem++; } else newIndex = items.size(); if (item.isSelectable() && itemSelected < 0) itemSelected = newIndex; else if (type == ConnectorCanvasItem.SCAN) { if (hasScanLine()) return; else scanItem = newIndex; } if (newIndex >= items.size()) items.addElement(item); else items.insertElementAt(item, newIndex); repaint(); } /** * Paints connector canvas. Iterates through the items vector, sets * visual properties of current item and draws its string. */ public void paint(Graphics g) { g.setColor(bgColor); g.fillRect(0, 0, getWidth(), getHeight()); for (int i = 0; i < items.size(); i++) { ConnectorCanvasItem item = (ConnectorCanvasItem) items.elementAt(i); int offset; int color; int bgcolor; /* Initialize properties of a line to draw. */ if (item.getType() == ConnectorCanvasItem.LABEL) { offset = labelOffset; color = labelColor; bgcolor = labelBgColor; } else { offset = selectableOffset; if (i == itemSelected) { color = selectedColor; bgcolor = selectedBgColor; } else { color = selectableColor; bgcolor = selectableBgColor; } } // String to draw. String s = item.getString(); // String to calculate width from. String sw = s; if (item.getType() == ConnectorCanvasItem.SCAN) { for (int j = 0; j < lastDots; j++) s += "."; sw = s; for (int j = lastDots; i < lastDotsMax; j++) { sw += "."; } } g.setColor(bgcolor); g.fillRect(offset, font.getHeight() * i, font.stringWidth(sw), font .getHeight()); g.setColor(color); g.drawString(s, offset, font.getHeight() * i, Graphics.TOP | Graphics.LEFT); } } /** * Starts a scan for nearby bluetooth servers. Reinitializes connector * canvas, starts flashing dots at the end of special scan item and * starts scanning. */ private void startServerScan() { stopServerScan(); initCanvas(); dotsTimer.schedule(dotsTimerTask, 300, 300); servers.removeAllElements(); lookup = new ServerLookup(serverId, this); lookup.searchForServers(); } /** * Stops the scan process. Stops flashing dots at the end of special * scan item, removes it from the items and interrupts the server * lookup thread. */ private void stopServerScan() { if (hasScanLine()) { dotsTimerTask.cancel(); dotsTimerTask=new DotsTimerTask(); items.removeElementAt(scanItem); scanItem = -1; repaint(); } final ServerLookup l = lookup; if (l != null) { l.interrupt(); try { l.join(); } catch (InterruptedException e) { Logger.warn(null, e); } } lookup = null; } /** * Handles event of new server found. Appends new server information into * servers vector and appends its displayable name to the list of * canvas items. * * @param server * Information about new server found. * * @see net.sf.btw.btlib.IClientListener#serverFound(net.sf.btw.btlib.ServerInfo) */ public void serverFound(ServerInfo server) { servers.addElement(server); appendItem(server.displayableName, ConnectorCanvasItem.SELECTABLE); } /** * Handles an error occurance while scan is in progress. Displays an * error alert for 3 seconds. * * @param ex * Exception containing information about the error that * occured. * * @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); } /** * Handles the end of scanning process. Stops flashing dots after the * special scan line and removes it from the canvas items. * * @param servers * Unused. * @param completely * Unused. * * @see net.sf.btw.btlib.IClientListener#serverScanFinished(java.util.Vector, * boolean) */ public void serverScanFinished(Vector servers, boolean completely) { if (scanItem >= 0) { dotsTimerTask.cancel(); dotsTimerTask = new DotsTimerTask(); items.removeElementAt(scanItem); scanItem = -1; repaint(); } } /** * Returns the sequence number of currently selected item, ignoring * all items that are not selectable. The result is index of currently * selected item it would have if only selectable items were present * in connector canvas item vector. */ private int getSelectedItemIndex() { int result = 0; for (int i = 0; i < items.size(); i++) { ConnectorCanvasItem item = (ConnectorCanvasItem) items.elementAt(i); if (item.isSelectable()) if (i == itemSelected) return result; else result++; } return -1; } /** * Handle events when user presses a keypad button. Performs item * selecting by means of UP/DOWN user actions and starting a server, * or connecting to one when FIRE user action is received. * * @param keyCode * Code of key the user pressed. */ public void keyPressed(int keyCode) { keyCode = getGameAction(keyCode); if (itemSelected < 0) return; int savItemSelected = itemSelected; if (keyCode == Canvas.DOWN) { /* Make the following selected. */ for (int i = itemSelected + 1; i < items.size(); i++) { ConnectorCanvasItem item = (ConnectorCanvasItem) items.elementAt(i); if (item.isSelectable()) itemSelected = i; } } else if (keyCode == Canvas.UP) { /* Make the previous selected. */ for (int i = itemSelected - 1; i >= 0; i--) { ConnectorCanvasItem item = (ConnectorCanvasItem) items.elementAt(i); if (item.isSelectable()) itemSelected = i; } } else if (keyCode == Canvas.FIRE) { final int index = getSelectedItemIndex(); if (index < 0) return; stopServerScan(); if (index == 0) { listener.startServer(); return; } final ServerInfo info = (ServerInfo) servers.elementAt(index - 1); listener.connectToServer(info); } if (savItemSelected != itemSelected) repaint(); } /** * Handle events when user fires a command. When refresh command is * received, restart scan process. When close command is received, * close the connector canvas. * * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, * javax.microedition.lcdui.Displayable) */ public void commandAction(Command cmd, Displayable arg1) { try { if (cmd == startCommand) { keyPressed(getKeyCode(Canvas.FIRE)); } else if (cmd == refreshCommand) { startServerScan(); } else if (cmd == closeCommand) { stopServerScan(); listener.closeConnector(); } else if (cmd == logCommand) { new Logger(this, display); } } catch (Exception e) { Logger.error("ConnectorCanvas.commandAction()", e); display.setCurrent(new Alert("Error", "Error occured. Please see log for details. " + e.getMessage(), null, AlertType.ERROR), this); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -