📄 controlpanelscreen.java
字号:
/* * JRemCntl - Copyright (C) 2007 Filippo Di Vattimo <fildiv@gmail.com> * See COPYING */package fildiv.jremcntl.client.ui;import java.util.Vector;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import fildiv.jremcntl.client.core.AppConstants;import fildiv.jremcntl.client.core.AppContext;import fildiv.jremcntl.client.core.JRemClientProtocol;import fildiv.jremcntl.client.core.ServerResponse;import fildiv.jremcntl.client.core.ServerResponseListener;import fildiv.jremcntl.client.core.ServerResponseReader;import fildiv.jremcntl.client.util.UIUtils;import fildiv.jremcntl.common.core.Config;import fildiv.jremcntl.common.core.Context;import fildiv.jremcntl.common.core.JRemClientConnector;import fildiv.jremcntl.common.core.JRemCommandData;import fildiv.jremcntl.common.core.JRemConfigData;import fildiv.jremcntl.common.core.JRemContextData;import fildiv.jremcntl.common.core.JRemMobileKeys;import fildiv.jremcntl.common.core.JRemRuntimeException;import fildiv.jremcntl.common.core.Logger;import fildiv.jremcntl.common.proto.JRemDataTransferException;public class ControlPanelScreen extends AbstractScreen implements CommandListener { private final static Command CMD_BACK = new Command("Back", Command.BACK, 1); private final static Command CMD_CLOSE = new Command("Close", Command.BACK, 3); private final static Command CMD_LOGS = new Command("Logs", Command.SCREEN, 4); private final static Command CMD_DEBUG = new Command("Temp", Command.SCREEN, 4); private JRemClientConnector connector; private ServerResponseReader srr; private Config config; private Context currCtx = null; private Form cpForm; private Form protoForm; private CommandPanelScreen cmdScreen; private ServerResponseListener srl = new ServerResponseListener() { public void connectionClosed() { onConnectionClosed(); } public void connectionLost() { onConnectionLost(); } public void receivedCommandResponse(ServerResponse response) { onServerResponse(response); } }; public ControlPanelScreen(AppContext appCtx, Screen backScreen) { super(appCtx, backScreen); config = null; protoForm = new Form(AppConstants.CONTACTING_SERVER_CAPTION); cmdScreen = new CommandPanelScreen(appCtx); cmdScreen.setBackScreen(this); } public void setLogger(Logger logger) { super.setLogger(logger); cmdScreen.setLogger(logger); } private void addConfigInfo() { UIUtils.appendText(cpForm, "Configuration : " + config.getName()); UIUtils.appendText(cpForm, "\n"); UIUtils.appendText(cpForm, "\n"); UIUtils.appendText(cpForm, config.getContexts().size() + " Contexts"); UIUtils.appendText(cpForm, "\n"); UIUtils.appendText(cpForm, config.getCmdsCount() + " Commands"); } protected void addControlPanelCommands() { cpForm.addCommand(CMD_CLOSE); cpForm.addCommand(CMD_LOGS);// cpForm.addCommand(CMD_TEMP); // Add contexts Vector contexts = config.getContexts(); for (int index = 0; index < contexts.size(); ++index) { JRemContextData ctx = (JRemContextData) contexts.elementAt(index); Command cmd = new Command(ctx.getDesc(), Command.SCREEN, index); cpForm.addCommand(cmd); } } private void retrieveConfig() { if (config != null) return; try { showWait(protoForm, "Synchronizing ..."); if (!getAppCtx().inDebug()) { JRemClientProtocol cc = (JRemClientProtocol) connector.getProtocol(); config = cc.retrieveConfig(); } else { config = retrieveTestConfig(); } } catch (JRemDataTransferException e) { throw new JRemRuntimeException(e); } finally { stopWait(); } } public void commandAction(Command c, Displayable d) { if (d == cpForm) { if (c == CMD_DEBUG) { // nothing } else if (c == CMD_CLOSE) disconnectAndHide(); else if (c == CMD_LOGS) showLogs(); else contextSelected(c); } else { if (c == CMD_BACK) disconnectAndHide(); else cmdScreen.hide(); } } private void disconnectAndHide() { showWait(cpForm, "Disconnecting ..."); try { srr.close(); connector.close(); } catch(Exception e) { log("Error while disconnect : " + e.getMessage()); } finally { stopWait(); config = null; connector = null; hide(); } } private void contextSelected(Command c) { Vector contexts = config.getContexts(); for (int index = 0; index < contexts.size(); ++index) { Context ctx = (Context) contexts.elementAt(index); if (c.getLabel().equals(ctx.getDesc())) { currCtx = ctx; break; } } onContextSelected(); } private void onContextSelected() { cmdScreen.setContext(currCtx); cmdScreen.show(); } protected void onActivate() { currCtx = null; getDisplay().setCurrent(cpForm); } protected void onConnectionClosed() { cmdScreen.reset(); show(); disconnectAndHide(); } private void onConnectionLost() { show(); disconnectAndHide(); } private void onServerResponse(ServerResponse response) { } public void setConnector(JRemClientConnector connector) { this.connector = connector;// assert connector.isConnected(); JRemClientProtocol proto = (JRemClientProtocol) connector.getProtocol(); cmdScreen.setProtocol(proto); retrieveConfig(); initControlPanelForm(); srr = new ServerResponseReader(proto, srl); srr.addServerResponseReaderListener(cmdScreen); } private void initControlPanelForm() { cpForm = new Form(AppConstants.CMDPANEL_SCREEN_CAPTION); cpForm.setCommandListener(this); addConfigInfo(); addControlPanelCommands(); } /* * Only for debug */ private Config retrieveTestConfig() { Config config = new JRemConfigData("Test", ""); int id = 0; Context c = config.appendContext(new JRemContextData(config, id++, "Context 1", Config.TYPE_VIEW_FASTVIEW, "")); c.appendCommand(new JRemCommandData(c, id++, "command 1", "", JRemMobileKeys.KEY_0, "")); c.appendCommand(new JRemCommandData(c, id++, "command 2", "", JRemMobileKeys.KEY_1, "")); c.appendCommand(new JRemCommandData(c, id++, "command 3", "", JRemMobileKeys.KEY_2, "")); c.appendCommand(new JRemCommandData(c, id++, "command 4", "", JRemMobileKeys.KEY_3, "")); c = config.appendContext(new JRemContextData(config, id++, "Context 2", Config.TYPE_VIEW_LISTVIEW, "")); c.appendCommand(new JRemCommandData(c, id++, "command 1", "", JRemMobileKeys.KEY_0, "")); return config; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -