📄 logviewerdialog.java
字号:
/* * Copyright (C) 2003 Adam Olsen * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 1, or (at your option) * any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother;import java.awt.*;import java.awt.event.*;import java.io.*;import java.text.*;import java.util.*;import javax.swing.*;import com.valhalla.gui.CopyPasteContextMenu;import com.valhalla.gui.DialogTracker;import com.valhalla.gui.Standard;import com.valhalla.settings.Settings;/** * Allows the user to view the log of any contact Displays a dialog that allows * you to view the log of any Jabber user you have contacted. You can view the * log by date and time * *@author Adam Olsen *@created April 20, 2005 *@version 1.0 */public class LogViewerDialog extends JFrame { private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault()); private String logDirectory; private JList logList = new JList(); private LogListModel model = new LogListModel(); private JPanel container = new JPanel(); private JPanel rightPanel = new JPanel(); private ConversationArea logView; private File logDirectoryFile; private JButton closeButton = new JButton(resources.getString("closeButton")), clearButton = new JButton(resources.getString("clearButton")); private JSplitPane splitPane; private File fileList[]; private LogViewerCaller caller; private LogViewerDialog thisPointer = this; private JPopupMenu popMenu = new JPopupMenu(); private JMenuItem delete = new JMenuItem(resources.getString("deleteButton")); private JScrollPane scrollPane; private JButton searchButton = new JButton(resources.getString("search")); private JTextField searchField = new JTextField(); /** * Default constructor Can be passed a ChatRoomPanel, HeadlineWindow, and a * ChatPanel as the caller * *@param caller the object that called this dialog *@param entryName the user who's log is to be viewed */ public LogViewerDialog(LogViewerCaller caller, String entryName) { super("Log Viewer (" + entryName + ")"); setTitle(resources.getString("logViewer") + " (" + entryName + ")"); setIconImage(Standard.getImage("frameicon.png")); logList.setModel(model); if (caller != null) { this.caller = caller; } logDirectory = JBother.profileDir + File.separatorChar + "logs" + File.separatorChar + entryName.replaceAll("@", "_at_").replaceAll("\\/", "-"); logDirectoryFile = new File(logDirectory); container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS)); setContentPane(container); logView = new ConversationArea(); popMenu.add(delete); logView.setPreferredSize(new Dimension(500, 350)); CopyPasteContextMenu.registerComponent(logView); rightPanel.setLayout(new BorderLayout()); JPanel topPanel = new JPanel(new BorderLayout(2, 2)); topPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0)); topPanel.add(searchField, BorderLayout.CENTER); topPanel.add(searchButton, BorderLayout.EAST); rightPanel.add(topPanel, BorderLayout.NORTH); JScrollPane logScroll = new JScrollPane(logView); rightPanel.add(logScroll, BorderLayout.CENTER); logView.setScroll(logScroll); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0)); buttonPanel.add(Box.createHorizontalGlue()); buttonPanel.add(clearButton); buttonPanel.add(closeButton); rightPanel.add(buttonPanel, BorderLayout.SOUTH); SearchActionListener searchListener = new SearchActionListener(); searchField.addActionListener(searchListener); searchButton.addActionListener(searchListener); loadLogFiles(); scrollPane = new JScrollPane(logList); splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, rightPanel); splitPane.setDividerLocation(150); splitPane.setOneTouchExpandable(true); container.add(splitPane); container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setUpListeners(); pack(); Standard.cascadePlacement(this); DialogTracker.addDialog(this, false, true); if (logList.getModel().getSize() >= 1) { setVisible(true); } else { Standard.warningMessage(null, resources.getString("logViewer"), resources.getString("noLogData")); DialogTracker.removeDialog(this); } } /** * Returns a formatted date string that is used in many places in JBother * *@return the formatted date and time */ public static String getDateName() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); String date = formatter.format(new Date()); return date; } /** * Sets up the listeners for the various events */ private void setUpListeners() { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); closeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { DialogTracker.removeDialog(thisPointer); } }); clearButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { clearHandler(); } }); delete.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int result = JOptionPane.showConfirmDialog(null, resources.getString("sureDeleteItem"), resources.getString("logViewer"), JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { File file = (File) logList.getSelectedValue(); if (file != null) { com.valhalla.Logger.debug("deleting " + file.getPath()); if (file.delete()) { model.removeLog(file); } } } } }); logList.addMouseListener(new LogListMouseListener()); logList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); logList.setCellRenderer(new LogListRenderer()); } /** * Listens for someone to click on an entry date and populates the textArea * with the log data * *@author Adam Olsen *@created April 20, 2005 *@version 1.0 */ private class LogListMouseListener extends MouseAdapter { /** * Description of the Method * *@param e Description of the Parameter */ public void mousePressed(MouseEvent e) { checkPop(e); } /** * Description of the Method * *@param e Description of the Parameter */ public void mouseReleased(MouseEvent e) { checkPop(e); } /** * Description of the Method * *@param e Description of the Parameter */ public void mouseClicked(MouseEvent e) { File file = (File) logList.getSelectedValue(); logView.setText(""); logView.plainAppend(getFileContents(file)); logView.setCaretPosition(0); validate(); checkPop(e); } /** * Description of the Method * *@param e Description of the Parameter */ private void checkPop(MouseEvent e) { if (e.isPopupTrigger()) { int index = logList.locationToIndex(new Point(e.getX(), e.getY())); logList.setSelectedIndex(index); if (logList.getSelectedValue() != null) { popMenu.show(logList, e.getX(), e.getY()); } } } } /** * Clears all log data (deletes all log entries) */ 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(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -