📄 mobimon.java
字号:
calledFromShortcut = true; int is = shortcutList.getSelectedIndex(); String s = shortcutList.getString(is); is = s.indexOf('/'); currentHost = s.substring(0, is); int start = ++is; is = s.indexOf(':'); currentDomain = s.substring(start, is); start = ++is; is = s.indexOf('/', start); currentMBean = s.substring(start, is); currentAction = s.substring(++is); listExpected = false; requestAction(false); } else if (d == mmScreen[state]) { int is = ((List)mmScreen[state]).getSelectedIndex(); String s = ((List)mmScreen[state]).getString(is); StringBuffer b = new StringBuffer(); switch (state) { case 0: listExpected = true; state++; if ((currentHost == null) || (!currentHost.equals(s))) { currentHost = s; currentDomain = null; currentMBean = null; currentAction = null; handleRequest(b.append("host=").append(currentHost) .toString()); } else { display.setCurrent(mmScreen[state]); } break; case 1: listExpected = true; state++; if ((currentDomain == null) || (!currentDomain.equals(s))) { currentDomain = s; currentMBean = null; currentAction = null; handleRequest(b.append("host=").append(currentHost) .append("&domain=").append(s).toString()); } else { display.setCurrent(mmScreen[state]); } break; case 2: listExpected = true; state++; if ((currentMBean == null) || (!currentMBean.equals(s))) { currentMBean = s; currentAction = null; handleRequest(b.append("host=").append(currentHost) .append("&mbean=").append(currentDomain).append(":") .append(replaceEquals(currentMBean)).toString()); } else { display.setCurrent(mmScreen[state]); } break; case 3: calledFromShortcut = false; listExpected = false; currentAction = s; requestAction(true); break; } } } private void handleCancel(Displayable d) { connectScreen(null); } private void handleBack(Displayable d) { if (d instanceof MMCanvas) { pageIndicator = 0; lastPageRequested = 0; if (calledFromShortcut) { currentHost = null; display.setCurrent(shortcutList); } else { mmScreen[state] = buf; display.setCurrent(mmScreen[state]); } } else if (d == actionParamForm) { display.setCurrent (calledFromShortcut ? shortcutList : mmScreen[state]); } else { listExpected = true; switch (state) { case 0: connectScreen(null); break; case 1: case 2: case 3: state--; display.setCurrent(mmScreen[state]); break; } } } private Displayable processResponse(String s) { s.trim(); if (listExpected) { String head = (state == 0) ? title[state] : currentHost + ": " + title[state]; List l = new List(head, Choice.EXCLUSIVE); int start = 0; int end = 0; while ((end = s.indexOf('\n', start)) != -1) { l.append(s.substring(start, end), null); start = ++end; } if (start < (s.length() - 1)) l.append(s.substring(start), null); updateProgressForm("Response processed.", 10); return l; } else { MMCanvas mmc = new MMCanvas(s); updateProgressForm("Response processed.", 10); return mmc; } } private void handleInfo() { buf = mmScreen[state]; listExpected = false; int is = ((List)mmScreen[state]).getSelectedIndex(); String s = ((List)mmScreen[state]).getString(is); currentAction = s; StringBuffer b = new StringBuffer(); handleRequest(b.append("host=").append(currentHost) .append("&mbean=").append(currentDomain).append(":") .append(currentMBean).append("&action=") .append(currentAction).append("&desc=a").toString()); } private void requestAction(boolean storeCurrentScreen) { if (storeCurrentScreen) buf = mmScreen[state]; StringBuffer actbuf = new StringBuffer(); actbuf.append("host=").append(currentHost) .append("&mbean=").append(currentDomain).append(":") .append(replaceEquals(currentMBean)).append("&action=") .append(currentAction); StringBuffer b = new StringBuffer(actbuf.toString()); int store = pageIndicator; String s = sendRequest(b.append("&desc=p").toString()); pageIndicator = store; if ((s != null) && (s.length() > 0)) getActionParams(s); else handleRequest(actbuf.toString()); } private void handleRequest(String request) { currentRequest = request; new Thread(this).start(); } private void getActionParams(String paramDesc) { int start = 0; int end = 0; actionParamForm = new Form("Parameter"); while (start < paramDesc.length()) { end = paramDesc.indexOf(':', start); String pName = paramDesc.substring(start, end); start = ++end; end = paramDesc.indexOf('\n', start); if (end == -1) end = paramDesc.length(); String pType = paramDesc.substring(start, end); start = ++end; if (pType.equals("boolean") || pType.equals("java.lang.Boolean")) { ChoiceGroup cg = new ChoiceGroup(pName, Choice.EXCLUSIVE, boolStr, null); actionParamForm.append(cg); } else { TextField tf = new TextField(pName, null, 20, TextField.ANY); if (pType.equals("int") || pType.equals("java.Lang.Integer") || pType.equals("long") || pType.equals("java.Lang.Long") || pType.equals("short") || pType.equals("java.Lang.Short") || pType.equals("byte") || pType.equals("java.Lang.Byte")) tf.setConstraints(TextField.NUMERIC); actionParamForm.append(tf); } } addCommandsAndDisplay(actionParamForm, null); } public void requestParamsAction() { StringBuffer actbuf = new StringBuffer(); actbuf.append("host=").append(currentHost) .append("&mbean=").append(currentDomain).append(":") .append(currentMBean).append("&action=").append(currentAction); for (int i = 0; i < actionParamForm.size(); i++) { Item it = actionParamForm.get(i); actbuf.append("&").append(it.getLabel()); if (it instanceof TextField) { TextField tf = (TextField)it; actbuf.append("=").append(tf.getString()); } else if (it instanceof ChoiceGroup) { ChoiceGroup cg = (ChoiceGroup)it; actbuf.append("=") .append(cg.getSelectedIndex() == 0 ? "false" : "true"); } } handleRequest(actbuf.toString()); } private String replaceEquals(String s) { StringBuffer b = new StringBuffer(); int start = 0; int end = 0; while (start < s.length()) { end = s.indexOf('=', start); if (end == -1) { b.append(s.substring(start)); break; } b.append(s.substring(start, end)).append("%3d"); start = ++end; } return b.toString(); } private void handlePrev(Displayable d) { if (pageIndicator < 0) { lastPageRequested = pageIndicator; } else if (pageIndicator > 0) { pageIndicator -= 2; lastPageRequested = pageIndicator; } else { return; } listExpected = false; requestAction(false); } private void handleNext(Displayable d) { if (pageIndicator < 0) { pageIndicator += 2; lastPageRequested = pageIndicator; } else if (pageIndicator > 0) { lastPageRequested = pageIndicator; } else { return; } listExpected = false; requestAction(false); } private void createProgressForm() { progressForm = new Form("Action in progress..."); progressGauge = new Gauge("Preparing request...", false, 10, 0); progressForm.append(progressGauge); } private void updateProgressForm(String msg, int gaugeValue) { progressGauge.setLabel(msg); progressGauge.setValue(gaugeValue); } public void run() { String response = sendRequest(currentRequest); if (commError) { Form errorForm = new Form("Comm. Error"); StringItem si = new StringItem(null, response); errorForm.append(si); errorForm.addCommand(back); errorForm.setCommandListener(this); display.setCurrent(errorForm); } else { mmScreen[state] = processResponse(response); addCommandsAndDisplay(mmScreen[state], null); } } private void handleAddShortcut(Displayable d) { int is = ((List)mmScreen[state]).getSelectedIndex(); String s = ((List)mmScreen[state]).getString(is); StringBuffer actbuf = new StringBuffer(); actbuf.append(currentHost).append('/') .append(currentDomain).append(':') .append(currentMBean).append('/') .append(s); s = actbuf.toString(); try { if (database.getNumRecords() == 11) { Alert dbFull = new Alert ("Error", "Shortcut store full!", null, AlertType.ERROR); display.setCurrent(dbFull, d); } else { RecordEnumeration re = database.enumerateRecords (new ShortcutFilter(s), null, false); if (re.hasNextElement()) { Alert alreadyStored = new Alert("Error", "Shortcut already stored!", null, AlertType.ERROR); display.setCurrent(alreadyStored, d); return; } re.destroy(); database.addRecord(s.getBytes(), 0, s.getBytes().length); Alert done = new Alert("Success", "Shortcut stored successfully", null, AlertType.INFO); display.setCurrent(done, d); return; } } catch (Exception e) { errorAlert(e); } } private void shortcutScreen(Displayable d) { try { if (database.getNumRecords() < 2) { Alert nothingThere = new Alert ("Error", "No shortcuts stored!", null, AlertType.ERROR); display.setCurrent(nothingThere, d); } else { shortcutList = new List("Shortcuts", Choice.EXCLUSIVE); RecordEnumeration re = database.enumerateRecords(null, null, false); String s; while (re.hasNextElement()) { s = new String(re.nextRecord()); if (s.startsWith("http")) continue; shortcutList.append(s, null); } re.destroy(); addCommandsAndDisplay(shortcutList, null); } } catch (Exception e) { errorAlert(e); } } private void deleteShortcut(Displayable d) { try { int is = shortcutList.getSelectedIndex(); String s = shortcutList.getString(is); RecordEnumeration re = database.enumerateRecords(new ShortcutFilter(s), null, false); if (re.hasNextElement()) database.deleteRecord(re.nextRecordId()); re.destroy(); shortcutScreen(connectForm); } catch (Exception e) { errorAlert(e); } } private class ShortcutFilter implements RecordFilter { private String compareWith; public ShortcutFilter(String toCompare) { compareWith = toCompare; } public boolean matches(byte[] record) { return compareWith.equals(new String(record)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -