本站首页    管理页面    写新日志    退出



公告


 求真务实打基础,
 宁缺毋滥读好书。

数据挖掘青年(DMman)


我的分类(专题)

日志更新
问君能有几多愁,恰似一群太监上青楼
我和僵尸有个约会:灵异世界或真实存在?
赤壁(下)观后小感:雷人
英科学家:酒精和烟草的危害大于大麻和摇头
只有社会主义才能拯救世界(由金融危机引发
求职心得(非名牌院校 硕士 计算机)
省外就业协议录入
数据挖掘方面的资源、期刊、会议的网址集合
面试心得(摘)
为学
EI收录中国期刊-核心(2008-5)
混沌理论:随机世界的建模
分子计算机已经问世,纳米计算机指日可待?
绝对好用免费的网络电话
NLP:基于机器学习的人类思想及行为建模
Weka中用于组合多个模型的的装袋、提升
数据挖掘在企业中应用的四种途径
(转)几点做人做事的建议
大学计算机软件专业生应该学什么(转)
一个程序员对学弟学妹建议(转)

最新评论

留言板

链接

Blog信息
blog名称:DMman(数据挖掘青年)
日志总数:102
评论数量:564
留言数量:57
访问次数:1752421
建立时间:2007年4月9日




[Weka]weka源码学习界面编程(2)mySimpleCLI.java
原创空间

数据挖掘青年 发表于 2007/4/11 21:02:58

mySimpleCLI.java 可直接编译通过,然后即可运行java、 kill、 break、 cls命令。如果你在相同目录下有编译过的 .class 文件,也可以在此界面上通过java...命令运行。 500)this.width=500'> 代码如下:   /* *    mySimpleCLI.java *    Copyright (C) 1999 Len Trigg * */     import java.awt.BorderLayout;import java.awt.Font;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.io.PipedInputStream;import java.io.PipedOutputStream;import java.io.PrintStream;import java.io.Reader;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.StringTokenizer;import java.util.Vector; /** * Creates a very simple command line for invoking the main method of * classes. System.out and System.err are redirected to an output area. * Features a simple command history -- use up and down arrows to move * through previous commmands. This gui uses only AWT (i.e. no Swing).  * @version $Revision: 1.6.2.2 $ */public class mySimpleCLI  extends Frame  implements ActionListener {    /** The output area canvas added to the frame */  protected TextArea m_OutputArea = new TextArea();   /** The command input area */  protected TextField m_Input = new TextField();   /** The history of commands entered interactively */  protected Vector m_CommandHistory = new Vector();   /** The current position in the command history */  protected int m_HistoryPos = 0;   /** The new output stream for System.out */  protected PipedOutputStream m_POO = new PipedOutputStream();   /** The new output stream for System.err */  protected PipedOutputStream m_POE = new PipedOutputStream();   /** The thread that sends output from m_POO to the output box */  protected Thread m_OutRedirector;   /** The thread that sends output from m_POE to the output box */  protected Thread m_ErrRedirector;   /** The thread currently running a class main method */  protected Thread m_RunThread;    /**   * A class that sends all lines from a reader to a TextArea component   *   * @author Len Trigg (trigg@cs.waikato.ac.nz)   * @version $Revision: 1.6.2.2 $   */  class ReaderToTextArea extends Thread {     /** The reader being monitored */    protected LineNumberReader m_Input;     /** The output text component */    protected TextArea m_Output;        /**     * Sets up the ReaderToTextArea     *     * @param input the Reader to monitor     * @param output the TextArea to send output to     */    public ReaderToTextArea(Reader input, TextArea output) {      setDaemon(true);      m_Input = new LineNumberReader(input);      m_Output = output;    }     /**     * Sit here listening for lines of input and appending them straight     * to the text component.     */    public void run() {       while (true) { try {   m_Output.append(m_Input.readLine() + '\n'); } catch (Exception ex) {   try {     sleep(100);   } catch (Exception e) {   } }      }    }  }   /**   * A class that handles running the main method of the class   * in a separate thread   *   * @author Len Trigg (trigg@cs.waikato.ac.nz)   * @version $Revision: 1.6.2.2 $   */  class ClassRunner extends Thread {     /** Stores the main method to call */    protected Method m_MainMethod;     /** Stores the command line arguments to pass to the main method */    String [] m_CommandArgs;        /**     * Sets up the class runner thread.     *     * @param theClass the Class to call the main method of     * @param commandArgs an array of Strings to use as command line args     * @exception Exception if an error occurs     */    public ClassRunner(Class theClass, String [] commandArgs)      throws Exception {      setDaemon(true);//标记该线程是后台驻留线程还是用户线程。       Class [] argTemplate = { String[].class };      m_CommandArgs = commandArgs;      m_MainMethod = theClass.getMethod("main", argTemplate);      if (((m_MainMethod.getModifiers() & Modifier.STATIC) == 0)   || (m_MainMethod.getModifiers() & Modifier.PUBLIC) == 0) { throw new NoSuchMethodException("main(String[]) method of " +     theClass.getName() +     " is not public and static.");      }    }     /**     * Starts running the main method.     */    public void run() {            try { Object [] args = { m_CommandArgs }; m_MainMethod.invoke(null, args); if (isInterrupted()) {   System.err.println("[...Interrupted]"); }      } catch (Exception ex) { if (ex.getMessage() == null) {   System.err.println("[...Killed]"); } else {   System.err.println("[Run exception] " + ex.getMessage()); }      } finally { m_RunThread = null;      }    }  }     /**   * Constructor   *   * @exception Exception if an error occurs   */  public mySimpleCLI() throws Exception {     super("mySimpleCLI"); Image icon = Toolkit.getDefaultToolkit().    getImage(ClassLoader.getSystemResource("zhou.gif"));    setIconImage(icon);     setLayout(new BorderLayout());    add(m_OutputArea, "Center");    add(m_Input, "South");     m_Input.addActionListener(this);//enter keyPressed    m_Input.addKeyListener(new KeyAdapter() {      public void keyPressed(KeyEvent e) { doHistory(e);      }    });    m_OutputArea.setEditable(false);    m_OutputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));    // Redirect System.out to the text area    //    System.out.println("Redirecting System.out");    PipedInputStream pio = new PipedInputStream(m_POO);    System.setOut(new PrintStream(m_POO));    Reader r = new InputStreamReader(pio);    m_OutRedirector = new ReaderToTextArea(r, m_OutputArea);    m_OutRedirector.start();     // Redirect System.err to the text area    //    System.err.println("Redirecting System.err");    PipedInputStream pie = new PipedInputStream(m_POE);    System.setErr(new PrintStream(m_POE));    r = new InputStreamReader(pie);    m_ErrRedirector = new ReaderToTextArea(r, m_OutputArea);    m_ErrRedirector.start();        // Finish setting up the frame and show it    pack();    setSize(600,500);     System.out.println("\nWelcome to the WEKA mySimpleCLI\n\n"                       +"Enter commands in the textfield at the bottom of \n"                       +"the window. Use the up and down arrows to move \n"                       +"through previous commands.\n\n");    runCommand("help");  }   /**   * Executes a simple cli command.   *   * @param commands the command string   * @exception Exception if an error occurs   */  public void runCommand(String commands) throws Exception {     System.out.println("> " + commands + '\n');    System.out.flush();    String [] commandArgs = splitOptions(commands);    if (commandArgs.length == 0) {      return;    }    if (commandArgs[0].equals("java")) {      // Execute the main method of a class      commandArgs[0] = "";      try { if (commandArgs.length == 1) {   throw new Exception("No class name given"); } String className = commandArgs[1]; commandArgs[1] = ""; if (m_RunThread != null) {   throw new Exception("An object is already running, use \"break\""         + " to interrupt it."); } Class theClass = Class.forName(className);  // some classes expect a fixed order of the args, i.e., they don't // use Utils.getOption(...) => create new array without first two // empty strings (former "java" and "<classname>") Vector argv = new Vector(); for (int i = 2; i < commandArgs.length; i++)   argv.add(commandArgs[i]);   m_RunThread = new ClassRunner(theClass, (String[]) argv.toArray(new String[argv.size()])); m_RunThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_RunThread.start();       } catch (Exception ex) { System.err.println(ex.getMessage());      }     } else if (commandArgs[0].equals("cls")) {//clear m_OutputArea      // Clear the text area      m_OutputArea.setText("");     } else if (commandArgs[0].equals("break")) {//interrupt thread      if (m_RunThread == null) { System.err.println("Nothing is currently running.");      } else { System.out.println("[Interrupt...]"); m_RunThread.interrupt();      }    } else if (commandArgs[0].equals("kill")) {//kill thread      if (m_RunThread == null) { System.err.println("Nothing is currently running.");      } else { System.out.println("[Kill...]"); m_RunThread.stop(); m_RunThread = null;      }    } else if (commandArgs[0].equals("exit")) {      // Shut down      // fire the frame close event      processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));          } else {      boolean help = ((commandArgs.length > 1)        && commandArgs[0].equals("help"));      if (help && commandArgs[1].equals("java")) { System.err.println("java <classname> <args>\n\n"      + "Starts the main method of <classname> with "      + "the supplied command line arguments (if any).\n"      + "The command is started in a separate thread, "      + "and may be interrupted with the \"break\"\n"      + "command (friendly), or killed with the \"kill\" "      + "command (unfriendly).\n");      } else if (help && commandArgs[1].equals("break")) { System.err.println("break\n\n"      + "Attempts to nicely interrupt the running job, "      + "if any. If this doesn't respond in an\n"      + "acceptable time, use \"kill\".\n");      } else if (help && commandArgs[1].equals("kill")) { System.err.println("kill\n\n"      + "Kills the running job, if any. You should only "      + "use this if the job doesn't respond to\n"      + "\"break\".\n");      } else if (help && commandArgs[1].equals("cls")) { System.err.println("cls\n\n"      + "Clears the output area.\n");      } else if (help && commandArgs[1].equals("exit")) { System.err.println("exit\n\n"      + "Exits the mySimpleCLI program.\n");      } else { // Print a help message System.err.println("Command must be one of:\n"      + "\tjava <classname> <args>\n"      + "\tbreak\n"      + "\tkill\n"      + "\tcls\n"      + "\texit\n"      + "\thelp <command>\n");      }    }  }   /**   * Changes the currently displayed command line when certain keys   * are pressed. The up arrow moves back through history entries   * and the down arrow moves forward through history entries.   *   * @param e a value of type 'KeyEvent'   */  public void doHistory(KeyEvent e) {        if (e.getSource() == m_Input) {      switch (e.getKeyCode()) {      case KeyEvent.VK_UP: if (m_HistoryPos > 0) {   m_HistoryPos--;   String command = (String) m_CommandHistory.elementAt(m_HistoryPos);   m_Input.setText(command); } break;      case KeyEvent.VK_DOWN: if (m_HistoryPos < m_CommandHistory.size()) {   m_HistoryPos++;   String command = "";   if (m_HistoryPos < m_CommandHistory.size()) {     command = (String) m_CommandHistory.elementAt(m_HistoryPos);   }   m_Input.setText(command); } break;      default: break;      }    }  }   /**   * Only gets called when return is pressed in the input area, which   * starts the command running.   *   * @param e a value of type 'ActionEvent'   */  public void actionPerformed(ActionEvent e) {     try {      if (e.getSource() == m_Input) { String command = m_Input.getText(); int last = m_CommandHistory.size() - 1; if ((last < 0)     || !command.equals((String)m_CommandHistory.elementAt(last))) {   m_CommandHistory.addElement(command);   m_HistoryPos = m_CommandHistory.size(); } runCommand(command);  m_Input.setText("");      }    } catch (Exception ex) {      System.err.println(ex.getMessage());    }  } /**   * Split up a string containing options into an array of strings,   * one for each option.   *   * @param optionString the string containing the options   * @return the array of options   */  public static String [] splitOptions(String quotedOptionString) throws Exception{     Vector optionsVec = new Vector();    String str = new String(quotedOptionString);    int i;        while (true){       //trimLeft       i = 0;      while ((i < str.length()) && (Character.isWhitespace(str.charAt(i)))) i++;      str = str.substring(i);            //stop when str is empty      if (str.length() == 0) break;            //if str start with a double quote      if (str.charAt(0) == '"'){  //find the first not anti-slached double quote//// i = 1;//// while(i < str.length()){////   if (str.charAt(i) == str.charAt(0)) break;////   if (str.charAt(i) == '\\'){////     i += 1;////     if (i >= str.length()) ////       throw new Exception("String should not finish with \\");////     if (str.charAt(i) != '\\' &&  str.charAt(i) != '"') ////       throw new Exception("Unknow character \\" + str.charAt(i));////   }////   i += 1;//// }//// if (i >= str.length()) throw new Exception("Quote parse error.");//// //// //add the founded string to the option vector (without quotes)//// String optStr = str.substring(1,i);//// optStr = unbackQuoteChars(optStr);//// optionsVec.addElement(optStr);//// str = str.substring(i+1);      } else { //find first whiteSpace i=0; while((i < str.length()) && (!Character.isWhitespace(str.charAt(i)))) i++;  //add the founded string to the option vector String optStr = str.substring(0,i); optionsVec.addElement(optStr); str = str.substring(i);      }    }        //convert optionsVec to an array of String    String [] options = new String[optionsVec.size()];    for (i = 0; i < optionsVec.size(); i++) {      options[i] = (String)optionsVec.elementAt(i);    }    return options;  }      /**   * Method to start up the simple cli   *   * @param args array of command line arguments. Not used.   */  public static void main(String[] args) {        try {      final mySimpleCLI frame = new mySimpleCLI();      frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent param1) {   System.err.println("window closed");   frame.dispose(); }      });      frame.setVisible(true);    } catch (Exception e) {      System.out.println("Error in main:"+e.getMessage());      System.exit(0);    }  }  }


阅读全文(4442) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.061 second(s), page refreshed 144749732 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号