📄 logviewerdialog.java
字号:
private void clearHandler() { int result = JOptionPane.showConfirmDialog(null, resources.getString("sureClearLog"), resources.getString("clearLogs"), JOptionPane.YES_NO_OPTION); if (result == 0) { if (caller != null) { caller.closeLog(); caller.startLog(); } for (int i = 0; i < fileList.length; i++) { fileList[i].delete(); } DialogTracker.removeDialog(this); } } /** * Returns a list of all available log entries * */ private void loadLogFiles() { if (!logDirectoryFile.isDirectory()) { return; } // opens the directory and lists the files fileList = logDirectoryFile.listFiles(new LogFileFilter()); logView.setText(""); Arrays.sort(fileList, Collections.reverseOrder()); if (!searchField.getText().equals("")) { ArrayList list = new ArrayList(); for (int i = 0; i < fileList.length; i++) { if (fileContainsText(fileList[i], searchField.getText())) { list.add(fileList[i]); } } fileList = (File[]) list.toArray(new File[list.size()]); } if (fileList.length == 0) { logView.setText("No logs found"); model.setLogs(fileList); validate(); return; } logView.append(getFileContents(fileList[0])); new java.util.Timer().schedule(new Scroller(logView.getTextPane()), 100); logList.setSelectedIndex(0); model.setLogs(fileList); validate(); } /** * Description of the Class * *@author synic *@created April 20, 2005 */ class SearchActionListener implements ActionListener { /** * Description of the Method * *@param e Description of the Parameter */ public void actionPerformed(ActionEvent e) { loadLogFiles(); } } private String getFileContents(File file) { String html = ""; try { FileInputStream in = new FileInputStream(file); byte[] contents = new byte[in.available()]; in.read(contents); in.close(); String logEnc = Settings.getInstance().getProperty("keepLogsEncoding"); try { if (logEnc != null) { html = new String(contents, logEnc); } else { html = new String(contents); } } catch (UnsupportedEncodingException ex) { } catch (NullPointerException ex) { } } catch (FileNotFoundException fnfe) { } catch (IOException ioe) { } return html; } /** * Description of the Method * *@param file Description of the Parameter *@param text Description of the Parameter *@return Description of the Return Value */ private boolean fileContainsText(File file, String text) { text = text.toLowerCase(); String html = getFileContents(file); html = html.toLowerCase(); if (html.indexOf(text) > -1) { return true; } else { return false; } } /** * The model that represents the list of buddies in the room * *@author Adam Olsen *@created April 20, 2005 *@version 1.0 */ class LogListModel extends AbstractListModel { private Vector logs = new Vector(); /** * Sets the logs attribute of the LogListModel object * *@param list The new logs value */ public void setLogs(File[] list) { logs.clear(); for (int i = 0; i < list.length; i++) { logs.add(list[i]); } fireChanged(); } /** *@return the number of elements in the list */ public int getSize() { return logs.size(); } /** *@param row the element you want to get *@return the Object at <tt>row</tt> */ public Object getElementAt(int row) { return logs.get(row); } /** *@param log The feature to be added to the Log attribute */ public void addLog(File log) { logs.add(log); fireChanged(); } /** * Removes a buddy from the list * *@param log Description of the Parameter */ public void removeLog(File log) { logs.remove(log); fireChanged(); } /** * Fires a change of the list */ private synchronized void fireChanged() { SwingUtilities.invokeLater( new Runnable() { public void run() { fireContentsChanged(LogListModel.this, 0, logs.size()); logList.validate(); } }); } }}/** * Displays each log entry as a date and time * *@author Adam Olsen *@created April 20, 2005 *@version 1.0 */class LogListRenderer extends JLabel implements ListCellRenderer { /** * Sets the background to opaque */ public LogListRenderer() { setOpaque(true); } /** * Renders each entry * *@param list Description of the Parameter *@param value Description of the Parameter *@param index Description of the Parameter *@param isSelected Description of the Parameter *@param cellHasFocus Description of the Parameter *@return The listCellRendererComponent value */ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { File file = (File) value; String dateString[] = file.toString().replaceAll( ".*\\" + File.separatorChar, "") .replaceAll("\\.log", "").split("\\-"); try { setText(dateString[1] + "-" + dateString[2] + "-" + dateString[0] + " " + dateString[3] + ":" + dateString[4] + ":" + dateString[5]); } catch (Exception e) { setText("Invalid Date"); } setBackground(isSelected ? list.getSelectionBackground() : list.getBackground()); setForeground(isSelected ? list.getSelectionForeground() : list.getForeground()); list.validate(); return this; }}/** * A filter to only accept html files * *@author Adam Olsen *@created April 20, 2005 *@version 1.0 */class LogFileFilter implements FileFilter { // accept html files /** * Description of the Method * *@param f Description of the Parameter *@return Description of the Return Value */ public boolean accept(File f) { if (f.isDirectory()) { return false; } if (f.length() <= 0.0) { return false; } String extension = Utils.getExtension(f); if (extension != null) { if (extension.equals(Utils.log)) { return true; } else { return false; } } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -