📄 maingui.java
字号:
/*------------------------------------------------------------------------------Name: MainGUI.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Main class to invoke the xmlBlaster serverVersion: $Id: MainGUI.java 14922 2006-03-12 23:12:20Z ruff $------------------------------------------------------------------------------*/package org.xmlBlaster;import java.util.logging.LogRecord;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.StopWatch;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.MsgUnitRaw;import org.xmlBlaster.util.def.Constants;import org.xmlBlaster.util.log.XbNotifyHandler;import org.xmlBlaster.engine.ServerScope;import org.xmlBlaster.engine.qos.AddressServer;import org.xmlBlaster.protocol.I_Authenticate;import org.xmlBlaster.protocol.I_XmlBlaster;import org.xmlBlaster.client.qos.ConnectQos;import org.xmlBlaster.client.qos.ConnectReturnQos;import org.xmlBlaster.client.key.GetKey;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Checkbox;import java.awt.Color;import java.awt.Font;import java.awt.Frame;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Image;import java.awt.Insets;import java.awt.Label;import java.awt.Panel;import java.awt.TextArea;import java.awt.TextField;import java.awt.Toolkit;import java.awt.event.*;import java.util.Vector;import org.jacorb.poa.gui.beans.FillLevelBar;/** * Start xmlBlaster with a GUI based control panel. * <p /> * A control panel pops up, where you can<br /> * <ul> * <li>Stop xmlBlaster</li> * <li>View the performance monitor</li> * <li>See and adjust logging output</li> * <li>Invoke XPath queries on messages in xmlBlaster</li> * </ul> * The available start parameters are similar to Main * <p> * The login name "__sys__GuiQuery" is reserved!<br /> * </p> * @see org.xmlBlaster.Main */public class MainGUI extends Frame implements Runnable, org.xmlBlaster.util.log.I_LogListener{ private static final long serialVersionUID = 1L; private ServerScope glob; private static Logger log = Logger.getLogger(MainGUI.class.getName()); private Toolkit toolkit = Toolkit.getDefaultToolkit(); /** The xmlBlaster server, is set from Main() constructor */ org.xmlBlaster.Main xmlBlasterMain = null; private Button exitButton; private Button hideButton; private Button clearLogButton; private Button dumpButton; /** TextArea with scroll bars for logging output. */ private TextArea logOutput = null; /** To save memory consumption, limit number of logging lines to this value. */ private final long MAX_LOG_LINES = 3000; /** The actual number of logged lines in the TextArea. */ private long numLogLines = 0; /** Approximate elapsed time since startup of this server. */ private long elapsedTime = 0L; /** Time when xmlBlaster was started */ private long startupTime = 0L; /** Last time the performance was evaluated */ private long lastPollingTime = 0L; /** Performance monitor for number of published messages. */ private FillLevelBar publishedMessagesBar = new FillLevelBar(); private Label publishedLabel = new Label(); // display total count private int peakPublishedMessages = 0; private long publishedMessages = 0L; private long lastPublishedMessages = 0L; /** Performance monitor for number of update messages (callbacks to clients). */ private FillLevelBar sentMessagesBar = new FillLevelBar(); private Label sentLabel = new Label(); private int peakSentMessages = 0; private long sentMessages = 0L; private long lastSentMessages = 0L; /** Performance monitor for number of synchronous accessed messages. */ private FillLevelBar getMessagesBar = new FillLevelBar(); private Label getLabel = new Label(); private int peakGetMessages = 0; private long getMessages = 0L; private long lastGetMessages = 0L; /** XPath query input field. */ private TextField inputTextField = new TextField(); /** Display XPath query results. */ private TextArea queryOutput = null; /** A client accessing xmlBlaster to do some XPath query. */ private GuiQuery clientQuery = null; /** Remember previous query strings. */ private QueryHistory queryHistory; /** * Construct the xmlBlaster GUI. */ public MainGUI(ServerScope glob, org.xmlBlaster.Main main) { this.xmlBlasterMain = main; this.glob = glob; // set the application icon java.net.URL oUrl; oUrl = this.getClass().getResource("AppIcon.gif"); Image img = null; if (oUrl != null) img = java.awt.Toolkit.getDefaultToolkit().getImage(oUrl); if(img != null) { this.setIconImage(img); System.out.println(img.toString()); } else { System.out.println("AppIcon.gif not found"); } setTitle("XmlBlaster Control Panel"); init(); // Poll xmlBlaster internal states PollingThread poller = new PollingThread(this); poller.start(); } /** * Start the GUI thread. */ public void run() { show(); if (this.xmlBlasterMain == null) this.xmlBlasterMain = new org.xmlBlaster.Main(glob, this); } /** * Event fired by Logger.java through interface I_LogListener. * <p /> * log.addLogDevice(this); * <p /> * Log output into TextArea<br /> * If the number of lines displayed is too big, cut half of them */ public void log(LogRecord record) { String str = record.getLevel().toString() + " [" + record.getLoggerName() + "] " + record.getMessage(); if (logOutput == null) { System.err.println(str + "\n"); return; } if (numLogLines > MAX_LOG_LINES) { String text = logOutput.getText(); text = text.substring(text.length()/2, text.length()); logOutput.setText(text); } numLogLines++; logOutput.append(str + "\n"); } /** * Event fired every 1 seconds by the PollingThread. * <p /> * Update the statistic bars. * @param sleepTime Milliseconds how long the PollingThread was sleeping (no zero division check!) */ void pollEvent(long sleepTime) { long current = System.currentTimeMillis(); if (lastPollingTime > 0L) { sleepTime = current - lastPollingTime; // correct sleepTime with the real sleeping time } lastPollingTime = current; elapsedTime += current - startupTime; double sleepSeconds = sleepTime / 1000.0; //double elapsedSeconds = elapsedTime / 1000.0; { publishedMessages = this.glob.getRequestBroker().getNumPublish(); int currentPublishedAvg = (int)((publishedMessages - lastPublishedMessages)/sleepSeconds); if ((publishedMessages - lastPublishedMessages) == 1) currentPublishedAvg = 1; //int totalPublishedAvg = (int)(numPublish/elapsedSeconds); publishedMessagesBar.setCurrentValue(currentPublishedAvg); if (currentPublishedAvg > peakPublishedMessages) { peakPublishedMessages = currentPublishedAvg; publishedMessagesBar.setAvgValue(peakPublishedMessages); } //publishedMessagesBar.setAvgValue(totalPublishedAvg); publishedLabel.setText("Total: " + publishedMessages); lastPublishedMessages = publishedMessages; } { sentMessages = 0;// TODO SessionInfo.sentMessages; int currentSentAvg = (int)((sentMessages - lastSentMessages)/sleepSeconds); if ((sentMessages - lastSentMessages) == 1) currentSentAvg = 1; //int totalSentAvg = (int)(sentMessages/elapsedSeconds); sentMessagesBar.setCurrentValue(currentSentAvg); if (currentSentAvg > peakSentMessages) { peakSentMessages = currentSentAvg; sentMessagesBar.setAvgValue(peakSentMessages); } // sentMessagesBar.setAvgValue(totalSentAvg); sentLabel.setText("Total: " + sentMessages); lastSentMessages = sentMessages; } { getMessages = this.glob.getRequestBroker().getNumGet(); int currentGetAvg = (int)((getMessages - lastGetMessages)/sleepSeconds); if ((getMessages - lastGetMessages) == 1) currentGetAvg = 1; //int totalGetAvg = (int)(numGet/elapsedSeconds); // System.out.println("totally numGet=" + numGet + " current avg=" + currentGetAvg + " total avg=" + totalGetAvg); getMessagesBar.setCurrentValue(currentGetAvg); if (currentGetAvg > peakGetMessages) { peakGetMessages = currentGetAvg; getMessagesBar.setAvgValue(peakGetMessages); } // getMessagesBar.setAvgValue(totalGetAvg); getLabel.setText("Total: " + getMessages); lastGetMessages = getMessages; } } private void registerLogEvents() { XbNotifyHandler.instance().register(Level.ALL.intValue(), this); } private void unregisterLogEvents() { XbNotifyHandler.instance().unregister(Level.ALL.intValue(), this); } /** * Build the GUI layout. */ private void init() { registerLogEvents(); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.insets = new Insets(5,5,5,5); // Exit Button exitButton = new Button("Exit"); class BeepListener implements ActionListener { public void actionPerformed(ActionEvent e) { toolkit.beep(); if (clientQuery != null) clientQuery.logout(); //unregisterLogEvents(); log.info("Good bye!"); System.exit(0); } } exitButton.addActionListener(new BeepListener()); gbc.gridx=0; gbc.gridy=0; gbc.gridwidth=1; gbc.gridheight=1; gbc.weightx = gbc.weighty = 0.0; add(exitButton, gbc); // Hide Button hideButton = new Button("Hide Window"); class HideListener implements ActionListener { public void actionPerformed(ActionEvent e) { hideWindow(); } } hideButton.addActionListener(new HideListener()); gbc.gridx=1; gbc.gridy=0; gbc.gridwidth=1; gbc.gridheight=1; gbc.weightx = gbc.weighty = 0.0; add(hideButton, gbc); // Statistic display with fill level bars int offset = 0; gbc.gridx=offset; gbc.gridy=1; gbc.gridwidth=1; gbc.gridheight=1; gbc.weightx = gbc.weighty = 0.0; createBarPanel(publishedMessagesBar, publishedLabel, "Published", gbc, offset++); createBarPanel(sentMessagesBar, sentLabel, "Update", gbc, offset++); createBarPanel(getMessagesBar, getLabel, "Get", gbc, offset++); { // XPath query GUI Panel panel = new Panel(); panel.setName("QueryPanel"); panel.setLayout(new BorderLayout()); panel.setBackground(java.awt.SystemColor.control); { // Field to enter XPath text Panel inputPanel = new Panel(); inputPanel.setLayout(new BorderLayout()); Label inputLabel = new Label("XPath query: "); inputPanel.add("West", inputLabel); inputTextField.setText("//key"); inputPanel.add("Center", inputTextField); inputTextField.addKeyListener(new XPathKeyListener()); panel.add("North", inputPanel); } { // TextArea to show query results queryOutput = new TextArea(); queryOutput.setEditable(false); panel.add("South", queryOutput); } gbc.gridx=offset; gbc.gridy=1; gbc.gridwidth=3; gbc.gridheight=1; add(panel, gbc); } // Checkboxes for log levels gbc.gridx=0; gbc.gridy=2; gbc.gridwidth=1; gbc.gridheight=1; add(new Label("Choose Logging Level: "), gbc); gbc.gridx=1; gbc.gridwidth=3; gbc.gridheight=1; add(createLogLevelBoxes(), gbc); // Clear logging output - Button clearLogButton = new Button("Clear Log Window"); class ClearListener implements ActionListener { public void actionPerformed(ActionEvent e) { logOutput.setText(""); } } clearLogButton.addActionListener(new ClearListener()); gbc.gridx=4; gbc.gridy=2; gbc.gridwidth=1; gbc.gridheight=1; gbc.weightx = gbc.weighty = 0.0; add(clearLogButton, gbc); // Dump internal state - Button dumpButton = new Button("Dump State"); class DumpListener implements ActionListener { public void actionPerformed(ActionEvent e) { // logOutput.setText(""); // clear log window try { log.info("Dump start"); I_Authenticate auth = xmlBlasterMain.getAuthenticate(); StringBuffer buf = new StringBuffer(auth.toXml()); buf.append(xmlBlasterMain.getXmlBlaster().toXml()); LogRecord record = new LogRecord(Level.INFO, buf.toString()); log(record); log.info("Dump end"); } catch(XmlBlasterException ee) { log.severe("Sorry, dump failed: " + ee.getMessage()); } } } dumpButton.addActionListener(new DumpListener()); gbc.gridx=5; gbc.gridy=2; gbc.gridwidth=1; gbc.gridheight=1; gbc.weightx = gbc.weighty = 0.0; add(dumpButton, gbc); // TextArea for log outputs gbc.gridx=0; gbc.gridy=3; gbc.gridwidth=6; gbc.gridheight=6; gbc.weightx = gbc.weighty = 1.0; logOutput = new TextArea("", 30, 100); // set rows here (width 100 is ignored) logOutput.setEditable(false); add(logOutput, gbc); pack(); startupTime = System.currentTimeMillis(); } /** * Hide the window. * Note that all the resources are still busy, only logging is directed to console */ private void hideWindow() { unregisterLogEvents(); if (isShowing()) { log.info("Press <g> and <Enter> to popup the GUI again (press ? for other options)."); setVisible(false); // dispose(); would clean up all resources } } /** * Hide the window. * Note that all the resources are still busy, only logging is directed to console */ void showWindow() { if (!isShowing()) { registerLogEvents(); if (log.isLoggable(Level.FINE)) log.fine("Show window again ...");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -