sqlviewer.java

来自「Weka」· Java 代码 · 共 649 行 · 第 1/2 页

JAVA
649
字号
          tmpStr += "" + s.charAt(i);        }        else {          if (tmpStr.startsWith("\""))            tmpStr = tmpStr.substring(1, tmpStr.length() - 1);          result.addElement(tmpStr);          tmpStr = "";        }      }      else {        tmpStr += "" + s.charAt(i);      }    }        // add last element    if (!tmpStr.equals("")) {      if (tmpStr.startsWith("\""))        tmpStr = tmpStr.substring(1, tmpStr.length() - 1);      result.addElement(tmpStr);    }    return result;  }  /**   * converts the given model into a comma-separated string   * @param m       the model to convert   * @return        the string representation of the model   */  protected String modelToString(DefaultListModel m) {    String      result;    String      tmpStr;    int         i;    int         n;    boolean     quote;    result = "";    for (i = 0; i < m.size(); i++) {      if (i > 0)        result += ",";            tmpStr = m.get(i).toString();      quote  = (tmpStr.indexOf(",") > -1) || (tmpStr.indexOf(" ") > -1);            if (quote)        result += "\"";            for (n = 0; n < tmpStr.length(); n++) {        // double quotes        if (tmpStr.charAt(n) == '"')          result += "" + "\"\"";        // normal character        else          result += "" + tmpStr.charAt(n);      }            if (quote)        result += "\"";    }    return result;  }  /**   * loads the history properties of the SqlViewer in the user's home directory   * @param set       whether to set the read properties in the panels or not   * @see #HISTORY_FILE   */  protected void loadHistory(boolean set) {    BufferedInputStream		str;    File			file;    int				width;    int				height;    try {      file = new File(getHistoryFilename());      if (file.exists()) {        str = new BufferedInputStream(            new FileInputStream(getHistoryFilename()));        m_History.load(str);      }    }    catch (Exception e) {      e.printStackTrace();    }    // set the histories    if (set) {      m_ConnectionPanel.setHistory(          stringToModel(            m_History.getProperty(ConnectionPanel.HISTORY_NAME, "")));      m_QueryPanel.setHistory(          stringToModel(            m_History.getProperty(QueryPanel.HISTORY_NAME, "")));      m_QueryPanel.setMaxRows(          Integer.parseInt(m_History.getProperty(QueryPanel.MAX_ROWS, "100")));      width  = Integer.parseInt(m_History.getProperty(WIDTH, "0"));      height = Integer.parseInt(m_History.getProperty(HEIGHT, "0"));      if ((width != 0) && (height != 0))	setPreferredSize(new Dimension(width, height));    }  }  /**   * saves the history properties of the SqlViewer in the user's home directory   * @see #HISTORY_FILE   */  protected void saveHistory() {    BufferedOutputStream    str;        try {      str = new BufferedOutputStream(          new FileOutputStream(getHistoryFilename()));      m_History.store(str, "SQL-Viewer-History");    }    catch (Exception e) {      e.printStackTrace();    }  }  /**   * obtains the size of the panel and saves it in the history   *    * @see #saveHistory()   */  public void saveSize() {    m_History.setProperty(WIDTH, "" + getSize().width);    m_History.setProperty(HEIGHT, "" + getSize().height);    saveHistory();  }    /**   * calls the clear method of all sub-panels to set back to default values   * and free up memory   */  public void clear() {    m_ConnectionPanel.clear();    m_QueryPanel.clear();    m_ResultPanel.clear();    m_InfoPanel.clear();  }  /**   * returns the database URL from the currently active tab in the ResultPanel,   * otherwise an empty string.   * @see ResultPanel   * @return        the currently selected tab's URL   */  public String getURL() {    return m_URL;  }  /**   * returns the user from the currently active tab in the ResultPanel,   * otherwise an empty string.   * @see ResultPanel   * @return        the currently selected tab's user   */  public String getUser() {    return m_User;  }  /**   * returns the password from the currently active tab in the ResultPanel,   * otherwise an empty string.   * @see ResultPanel   * @return        the currently selected tab's password   */  public String getPassword() {    return m_Password;  }  /**   * returns the query from the currently active tab in the ResultPanel,   * otherwise an empty string.   * @see ResultPanel   * @return        the currently selected tab's query   */  public String getQuery() {    return m_Query;  }  /**   * adds the given listener to the list of listeners   * @param l       the listener to add to the list   */  public void addConnectionListener(ConnectionListener l) {    m_ConnectionPanel.addConnectionListener(l);  }  /**   * removes the given listener from the list of listeners   * @param l       the listener to remove   */  public void removeConnectionListener(ConnectionListener l) {    m_ConnectionPanel.removeConnectionListener(l);  }  /**   * adds the given listener to the list of listeners   * @param l       the listener to add to the list   */  public void addQueryExecuteListener(QueryExecuteListener l) {    m_QueryPanel.addQueryExecuteListener(l);  }  /**   * removes the given listener from the list of listeners   * @param l       the listener to remove   */  public void removeQueryExecuteListener(QueryExecuteListener l) {    m_QueryPanel.removeQueryExecuteListener(l);  }  /**   * adds the given listener to the list of listeners   * @param l       the listener to add to the list   */  public void addResultChangedListener(ResultChangedListener l) {    m_ResultPanel.addResultChangedListener(l);  }  /**   * removes the given listener from the list of listeners   * @param l       the listener to remove   */  public void removeResultChangedListener(ResultChangedListener l) {    m_ResultPanel.removeResultChangedListener(l);  }  /**   * adds the given listener to the list of listeners   * @param l       the listener to add to the list   */  public void addHistoryChangedListener(HistoryChangedListener l) {    m_ConnectionPanel.addHistoryChangedListener(l);    m_QueryPanel.addHistoryChangedListener(l);  }  /**   * removes the given listener from the list of listeners   * @param l       the listener to remove   */  public void removeHistoryChangedListener(HistoryChangedListener l) {    m_ConnectionPanel.removeHistoryChangedListener(l);    m_QueryPanel.removeHistoryChangedListener(l);  }  /** for monitoring the Memory consumption */  private static Memory m_Memory = new Memory(true);    /** the sql viewer */  private static SqlViewer m_Viewer;    /**   * starts the SQL-Viewer interface.   */  public static void main(String[] args) {    LookAndFeel.setLookAndFeel();        try {      // uncomment to disable the memory management:      //m_Memory.setEnabled(false);      final JFrame jf = new JFrame("Weka SQL-Viewer");      m_Viewer = new SqlViewer(jf);      jf.getContentPane().setLayout(new BorderLayout());      jf.getContentPane().add(m_Viewer, BorderLayout.CENTER);      jf.addWindowListener(new WindowAdapter() {        public void windowClosing(WindowEvent e) {          m_Viewer.saveSize();          jf.dispose();          System.exit(0);        }      });      jf.pack();      jf.setSize(800, 600);      jf.setVisible(true);      Thread memMonitor = new Thread() {        public void run() {          while (true) {            try {              this.sleep(4000);              System.gc();              if (m_Memory.isOutOfMemory()) {                // clean up                jf.dispose();                m_Viewer = null;                System.gc();                // stop threads                m_Memory.stopThreads();                // display error                System.err.println("\ndisplayed message:");                m_Memory.showOutOfMemory();                System.err.println("\nexiting");                System.exit(-1);              }            }             catch (InterruptedException ex) {               ex.printStackTrace();             }          }        }      };      memMonitor.setPriority(Thread.MAX_PRIORITY);      memMonitor.start();    }     catch (Exception ex) {      ex.printStackTrace();      System.err.println(ex.getMessage());    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?