📄 maingui.java
字号:
show(); } } /** * Get the events when a Checkbox is selected with the mouse. */ class LogLevelListener implements ItemListener { public void itemStateChanged(ItemEvent e) { /* String name = (String)e.getItem(); try { Level level = Level.parse(value); glob.changeLogLevel(name, level); } catch (XmlBlasterException ex) { log.warning("Cannot set log level attribute '"+ name +"':" + ex.getMessage()); } */ log.warning("is not implemented"); } } /** * Create a Panel with a FillLevelBar and some labels. * @param messageBar The instance of FillLevelBar to use * @param totalCountLabel The instance of total count Label to use * @param token Describing text e.g. "Published" * @param gbc The layout manager * @param offset The position of the panel (grid layout) */ private void createBarPanel(FillLevelBar messageBar, Label totalCountLabel, String token, GridBagConstraints gbc, int offset) { final int LABEL_LOCATION_X = 2; final int LABEL_LOCATION_Y = 10; final int LABEL_WIDTH = 90; final int LABEL_HEIGHT = 12; final int BAR_X = 32; final int BAR_Y = LABEL_LOCATION_Y + 2 * LABEL_HEIGHT; final int BAR_WIDTH = 50; final int BAR_HEIGHT = 130; final int TOTAL_LABEL_Y = BAR_Y + BAR_HEIGHT; final int PANEL_WIDTH = LABEL_LOCATION_X + LABEL_WIDTH + 2; // 94 final int PANEL_HEIGHT = TOTAL_LABEL_Y + LABEL_HEIGHT + 4; // 180 Font barFont = new java.awt.Font("dialog", 2, 10); Panel panel = new Panel(); panel.setName(token + "MessagePanel"); panel.setLayout(null); panel.setBackground(java.awt.SystemColor.control); panel.setSize(PANEL_WIDTH, PANEL_HEIGHT); Label label1 = new Label(); label1.setName("Label1"); label1.setLocation(LABEL_LOCATION_X, LABEL_LOCATION_Y); label1.setText(token); label1.setBackground(java.awt.SystemColor.control); label1.setSize(LABEL_WIDTH, LABEL_HEIGHT); label1.setForeground(java.awt.Color.black); label1.setFont(barFont); label1.setAlignment(1); panel.add(label1, label1.getName()); Label label2 = new Label(); label2.setName("Label2"); label2.setLocation(LABEL_LOCATION_X, LABEL_LOCATION_Y + LABEL_HEIGHT); label2.setText("[messages/sec]"); label2.setBackground(java.awt.SystemColor.control); label2.setSize(LABEL_WIDTH, LABEL_HEIGHT); label2.setForeground(java.awt.Color.black); label2.setFont(barFont); label2.setAlignment(1); panel.add(label2, label2.getName()); messageBar.setName(token + "Messages"); messageBar.setLocation(BAR_X, BAR_Y); messageBar.setBackground(java.awt.SystemColor.control); messageBar.setSize(BAR_WIDTH, BAR_HEIGHT); boolean useAvg = true; boolean isVariable = true; // !!! int MAX_SCALE = 10; messageBar.init(0, 5, MAX_SCALE, Color.green, Color.green, useAvg, isVariable); messageBar.setAvgValue(0); // messageBar.init(0, 5, 100, Color.yellow, Color.green, true, true); panel.add(messageBar, messageBar.getName()); totalCountLabel.setName(token + "Label"); totalCountLabel.setLocation(LABEL_LOCATION_X, TOTAL_LABEL_Y); totalCountLabel.setText("Total: 0"); totalCountLabel.setBackground(java.awt.SystemColor.control); totalCountLabel.setSize(LABEL_WIDTH, LABEL_HEIGHT); totalCountLabel.setForeground(java.awt.Color.black); totalCountLabel.setFont(barFont); totalCountLabel.setAlignment(1); panel.add(totalCountLabel, totalCountLabel.getName()); gbc.gridx=offset; add(panel, gbc); } /** * Create Checkboxes to adjust the logging levels * @return container with checkboxes */ private Panel createLogLevelBoxes() { Panel container = new Panel(); container.setLayout(new GridLayout(1, 7)); Checkbox error = new Checkbox("ERROR", null, true); error.addItemListener(new LogLevelListener()); container.add(error); Checkbox warning = new Checkbox("WARN", null, true); warning.addItemListener(new LogLevelListener()); container.add(warning); Checkbox info = new Checkbox("INFO", null, true); info.addItemListener(new LogLevelListener()); container.add(info); if (true/*log.CALL*/) { // log.CALL=true/false: check for dead code elimination Checkbox calls = new Checkbox("CALL", null, false); calls.addItemListener(new LogLevelListener()); container.add(calls); } if (true/*log.TIME*/) { Checkbox time = new Checkbox("TIME", null, false); time.addItemListener(new LogLevelListener()); container.add(time); } if (true/*log.TRACE*/) { Checkbox trace = new Checkbox("TRACE", null, false); trace.addItemListener(new LogLevelListener()); container.add(trace); } if (true/*log.DUMP*/) { Checkbox dump = new Checkbox("DUMP", null, false); dump.addItemListener(new LogLevelListener()); container.add(dump); } return container; } /** * Access the query history. */ private QueryHistory getQueryHistory() { if (queryHistory == null) queryHistory = new QueryHistory(); return queryHistory; } /** * Invoke: <pre>jaco org.xmlBlaster.MainGUI</pre><br /> * to start xmlBlaster with a control panel */ static public void main(String[] args) { ServerScope glob = new ServerScope(args); Main.controlPanel = new MainGUI(glob, null); Main.controlPanel.run(); } /** * Polls the state of xmlBlaster. */ private class PollingThread extends Thread { private final int POLLING_FREQUENCY = 1000; // sleep 1 sec private final MainGUI mainGUI; /** **/ public PollingThread(MainGUI mainGUI) { this.mainGUI = mainGUI; // !!! setPriority(Thread.MIN_PRIORITY); } /** * Start the timeout thread. **/ public void run() { Thread.currentThread().setName("XmlBlaster GUIPollingThread"); try { if (log.isLoggable(Level.FINE)) log.fine("Starting poller"); while (true) { sleep(POLLING_FREQUENCY); mainGUI.pollEvent(POLLING_FREQUENCY); // Todo: calculate the exact sleeping period } } catch (Exception e) { log.fine("PollingThread problem: "+ e.toString()); //e.printStackTrace(); } } } /** * Handles return key when a XPath query is entered into the TextField. */ private class XPathKeyListener implements KeyListener { /** * Access XPath query string (event from KeyListener). */ public final void keyPressed(KeyEvent ev) { switch (ev.getKeyCode()) { case KeyEvent.VK_ENTER: try { if (clientQuery == null) { clientQuery = new GuiQuery(xmlBlasterMain.getAuthenticate(), xmlBlasterMain.getXmlBlaster()); } queryOutput.setText(""); getQueryHistory().changedHistory(inputTextField.getText()); MsgUnitRaw[] msgArr = clientQuery.get(inputTextField.getText()); for (int ii=0; ii<msgArr.length; ii++) { /* UpdateKey updateKey = new UpdateKey(); updateKey.init(msgArr[ii].getKey()); queryOutput.append("### UpdateKey:\n" + updateKey.printOn().toString()); */ queryOutput.append("### XmlKey:\n" + msgArr[ii].getKey()); queryOutput.append("\n"); queryOutput.append("### Content:\n" + new String(msgArr[ii].getContent()) + "\n"); queryOutput.append("======================================================\n"); } if (msgArr.length == 0) { if (publishedMessages == 0L) { queryOutput.setText("\n"); queryOutput.append (" Sorry, no data in xmlBlaster.\n"); queryOutput.append (" Use a demo client to publish some.\n"); queryOutput.append ("\n"); } else queryOutput.setText("****** Sorry, no match ******"); } } catch(XmlBlasterException e) { log.severe("XmlBlasterException: " + e.getMessage()); } break; case KeyEvent.VK_DOWN: displayHistory(getQueryHistory().getNext()); break; case KeyEvent.VK_UP: displayHistory(getQueryHistory().getPrev()); break; } } public final void keyReleased(KeyEvent ev) { } public final void keyTyped(KeyEvent ev) { } } /** * Scrolling with key arrow up/down your last XPath queries. * @param stmt The XPath stmt to display */ private void displayHistory(String stmt) { if (stmt.length() < 1) return; inputTextField.setText(stmt); } /** * A client accessing xmlBlaster to do some XPath query. */ private class GuiQuery { private String queryType = Constants.XPATH; private StopWatch stop = new StopWatch(); /** Handle to login */ I_Authenticate authenticate; /** The handle to access xmlBlaster */ private I_XmlBlaster xmlBlasterImpl; /** The session id on successful authentication */ private String secretSessionId; private AddressServer addressServer; /** * Login to xmlBlaster and get a sessionId. */ public GuiQuery(I_Authenticate authenticate, I_XmlBlaster xmlBlasterImpl) throws XmlBlasterException { this.xmlBlasterImpl = xmlBlasterImpl; this.authenticate = authenticate; String loginName = glob.getProperty().get("__sys__GuiQuery.loginName", "__sys__GuiQuery"); String passwd = glob.getProperty().get("__sys__GuiQuery.password", "secret"); ConnectQos connectQos = new ConnectQos(authenticate.getGlobal()); connectQos.loadClientPlugin("htpasswd", "1.0", loginName, passwd); connectQos.getSessionQos().setSessionTimeout(0L); // TODO: Port to use "LOCAL" protocol to connect this.addressServer = new AddressServer(authenticate.getGlobal(), "NATIVE", authenticate.getGlobal().getId(), (java.util.Properties)null); String ret = authenticate.connect(addressServer, connectQos.toXml(), null); // synchronous access only, no callback. ConnectReturnQos retQos = new ConnectReturnQos(authenticate.getGlobal(), ret); this.secretSessionId = retQos.getSecretSessionId(); log.info("login for '" + loginName + "' successful."); } /** * Query xmlBlaster. */ MsgUnitRaw[] get(String queryString) { try { GetKey getKey = new GetKey(this.authenticate.getGlobal(), queryString, queryType); stop.restart(); MsgUnitRaw[] msgArr = this.xmlBlasterImpl.get(this.addressServer, this.secretSessionId, getKey.toXml(), "<qos/>"); log.info("Got " + msgArr.length + " messages for query '" + queryString + "'" + stop.nice()); return msgArr; } catch(XmlBlasterException e) { log.severe("XmlBlasterException: " + e.getMessage()); return new MsgUnitRaw[0]; } } /** Logout. */ void logout() { try { this.authenticate.disconnect(this.addressServer, this.secretSessionId, null); } catch (XmlBlasterException e) { } } } /** * Implements a stack to hold the previous XPath queries. */ private class QueryHistory { //private String ME = "QueryHistory"; private Vector stack = new Vector(); private int currentIndex = 0; /** * Constructs a history stack. */ public QueryHistory() { } /** * Add new statement into history. */ public void changedHistory(String stmt) { if (stack.size() > 1) { String last = (String)stack.elementAt(stack.size()-1); if (last.equals(stmt)) return; } currentIndex = stack.size(); stack.addElement(stmt); } /** * Access previous XPath query. */ String getPrev() { if (stack.size() < 1) return ""; if (currentIndex > 0) currentIndex--; return (String)stack.elementAt(currentIndex); } /** * Access next XPath query. */ String getNext() { if (stack.size() < 1) return ""; if (currentIndex < stack.size()-1) currentIndex++; return (String)stack.elementAt(currentIndex); } /** * Access last (the newest), sets current to last. */ String getLast() { if (stack.size() < 1) return ""; currentIndex = stack.size() - 1; return (String)stack.elementAt(currentIndex); } /** * Access first (the oldest), sets current to first. */ String getFirst() { if (stack.size() < 1) return ""; currentIndex = 0; return (String)stack.elementAt(currentIndex); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -