1 30 package com.genimen.djeneric.tools.logbrowser; 31 32 import java.awt.AWTEvent ; 33 import java.awt.BorderLayout ; 34 import java.awt.Color ; 35 import java.awt.Component ; 36 import java.awt.Dimension ; 37 import java.awt.Image ; 38 import java.awt.Toolkit ; 39 import java.awt.datatransfer.Clipboard ; 40 import java.awt.datatransfer.StringSelection ; 41 import java.awt.event.ActionEvent ; 42 import java.awt.event.WindowEvent ; 43 import java.io.File ; 44 import java.io.FilenameFilter ; 45 import java.net.URL ; 46 47 import javax.swing.ImageIcon ; 48 import javax.swing.JButton ; 49 import javax.swing.JLabel ; 50 import javax.swing.JOptionPane ; 51 import javax.swing.JPanel ; 52 import javax.swing.JScrollPane ; 53 import javax.swing.JTabbedPane ; 54 import javax.swing.JTextArea ; 55 import javax.swing.JViewport ; 56 57 import com.genimen.djeneric.language.Messages; 58 import com.genimen.djeneric.tools.common.DjenericTool; 59 import com.genimen.djeneric.ui.Util; 60 import com.genimen.djeneric.util.DjEnvironment; 61 import com.genimen.djeneric.util.DjFileUtil; 62 import com.genimen.djeneric.util.DjLogger; 63 import com.genimen.djeneric.util.DjVersion; 64 65 public class LogBrowser extends DjenericTool 66 { 67 private static final long serialVersionUID = 1L; 68 private final String PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("logbrowser.properties"); 69 70 public final static String OK_MSG = Messages.getString("global.Ok"); 71 72 Color _normalColor = new Color (102, 102, 153); 73 JTabbedPane _tabs = new JTabbedPane (); 74 JPanel _buttonPanel = new JPanel (); 75 BorderLayout _buttonPanelLayout = new BorderLayout (); 76 JPanel _clearPanel = new JPanel (); 77 JButton _butClear = new JButton (); 78 JPanel _clearAllPanel = new JPanel (); 79 JButton _butClearAll = new JButton (); 80 JLabel _lblStatus = new JLabel (); 81 JButton _butClipboard = new JButton (); 82 83 86 public LogBrowser() 87 { 88 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 89 setIconImage(getImage("logbrowser.gif")); 90 91 try 92 { 93 setStatusLabel(_lblStatus); 94 jbInit(); 95 loadProps(PROPERTIES_FILE_NAME); 96 97 int width = Integer.parseInt(getProperty("window.width", "400")); 98 int height = Integer.parseInt(getProperty("window.height", "400")); 99 setSize(new Dimension (width, height)); 100 setTitle(Messages.getString("LogBrowser.title", DjVersion.getVersion())); 101 102 String djDir = DjEnvironment.getDjenericDir(); 103 File dir = new File (djDir); 104 String [] logs = dir.list(new LogFilenameFilter()); 105 for (int i = 0; i < logs.length; i++) 106 { 107 _tabs.add(createLogPane(logs[i]), logs[i]); 108 } 109 if (logs.length == 0) setStatusMessage(Messages.getString("LogBrowser.NoLogs", djDir)); 110 } 111 catch (Exception e) 112 { 113 handleException(e); 114 System.exit(-1); 115 } 116 } 117 118 123 private void jbInit() throws Exception 124 { 125 _buttonPanel.setLayout(_buttonPanelLayout); 126 _butClear.setText(Messages.getString("LogBrowser.Clear")); 127 _butClear.addActionListener(new java.awt.event.ActionListener () 128 { 129 public void actionPerformed(ActionEvent e) 130 { 131 _butClear_actionPerformed(e); 132 } 133 }); 134 _butClearAll.setText(Messages.getString("LogBrowser.ClearAll")); 135 _butClearAll.addActionListener(new java.awt.event.ActionListener () 136 { 137 public void actionPerformed(ActionEvent e) 138 { 139 _butClearAll_actionPerformed(e); 140 } 141 }); 142 143 _butClipboard.setText(Messages.getString("LogBrowser.CopyToClipboard")); 144 _butClipboard.addActionListener(new java.awt.event.ActionListener () 145 { 146 public void actionPerformed(ActionEvent e) 147 { 148 _butClipboard_actionPerformed(e); 149 } 150 }); 151 _buttonPanel.add(_clearPanel, BorderLayout.EAST); 152 _clearPanel.add(_butClipboard, null); 153 _buttonPanel.add(_clearAllPanel, BorderLayout.WEST); 154 _clearAllPanel.add(_butClearAll, null); 155 _clearAllPanel.add(_butClear, null); 156 _buttonPanel.add(_lblStatus, BorderLayout.NORTH); 157 158 this.getContentPane().add(_tabs, BorderLayout.CENTER); 159 this.getContentPane().add(_buttonPanel, BorderLayout.SOUTH); 160 Util.sizeButtons(getContentPane()); 161 } 162 163 protected void processWindowEvent(WindowEvent e) 164 { 165 if (e.getID() == WindowEvent.WINDOW_CLOSING) 166 { 167 exitProgram(); 168 } 169 super.processWindowEvent(e); 170 171 } 172 173 protected void updateProperties() 174 { 175 setProperty("window.width", "" + getWidth()); 176 setProperty("window.height", "" + getHeight()); 177 178 } 179 180 public static ImageIcon getImageIcon(String fileName) 181 { 182 URL url = LogBrowser.class.getResource("images/" + fileName); 183 if (url != null) return new ImageIcon (url); 184 else return new ImageIcon (""); 185 } 187 188 public static Image getImage(String fileName) 189 { 190 URL url = LogBrowser.class.getResource("images/" + fileName); 191 if (url != null) return Toolkit.getDefaultToolkit().getImage(url); 192 193 else return Toolkit.getDefaultToolkit().getImage(""); 194 } 195 196 JScrollPane createLogPane(String fileName) throws Exception 197 { 198 JTextArea edt = new JTextArea (); 199 edt.setFont(new java.awt.Font ("Monospaced", 0, 12)); 200 JScrollPane scr = new JScrollPane (); 201 202 scr.getViewport().add(edt, null); 203 edt.setEditable(false); 204 205 String log = DjFileUtil.readFile(DjEnvironment.getDjenericDir() + File.separator + fileName); 206 edt.setText(log); 207 208 return scr; 209 } 210 211 class LogFilenameFilter implements FilenameFilter 212 { 213 public boolean accept(File dir, String name) 214 { 215 return name.toLowerCase().endsWith(".log"); 216 } 217 } 218 219 void _butClipboard_actionPerformed(ActionEvent e) 223 { 224 if (_tabs.getComponentCount() == 0) return; 225 Component comp = _tabs.getSelectedComponent(); 226 if (comp != null) 227 { 228 JScrollPane scr = (JScrollPane ) comp; 229 JViewport vp = (JViewport ) scr.getComponent(0); 230 JTextArea ta = (JTextArea ) vp.getComponent(0); 231 Clipboard clipboard = getToolkit().getSystemClipboard(); 232 StringSelection data = new StringSelection (ta.getText()); 233 clipboard.setContents(data, data); 234 } 235 } 236 237 void _butClearAll_actionPerformed(ActionEvent e) 238 { 239 if (_tabs.getComponentCount() == 0) return; 240 boolean clearMsg = true; 241 int result = JOptionPane.showOptionDialog(this, Messages.getString("LogBrowser.ClearLogMsg"), Messages 242 .getString("LogBrowser.ClearAllLogFiles"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 243 new String []{Messages.getString("LogBrowser.ClearAllLogFiles"), 244 Messages.getString("global.Cancel")}, null); 245 if (result == 0) 246 { 247 String [] files = new String [_tabs.getComponentCount()]; 248 Component [] comps = _tabs.getComponents(); 249 250 for (int i = 0; i < files.length; i++) 251 { 252 files[i] = _tabs.getTitleAt(i); 253 } 254 for (int i = 0; i < files.length; i++) 255 { 256 File fl = new File (DjEnvironment.getDjenericDir() + File.separator + files[i]); 257 if (fl.delete()) _tabs.remove(comps[i]); 258 else 259 { 260 setStatusMessage(Messages.getString("LogBrowser.CouldNotClear", files[i]), false); 261 clearMsg = false; 262 } 263 } 264 } 265 if (clearMsg) setStatusMessage(""); 266 } 267 268 void _butClear_actionPerformed(ActionEvent e) 269 { 270 if (_tabs.getComponentCount() == 0) return; 271 int idx = _tabs.getSelectedIndex(); 272 String fileName = _tabs.getTitleAt(idx); 273 boolean clearMsg = true; 274 275 int result = JOptionPane.showOptionDialog(this, Messages.getString("LogBrowser.About2Clear", fileName), Messages 276 .getString("LogBrowser.Clear") 277 + fileName, 278 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 279 new String []{Messages.getString("LogBrowser.Clear"), 280 Messages.getString("global.Cancel")}, null); 281 if (result == 0) 282 { 283 File fl = new File (DjEnvironment.getDjenericDir() + File.separator + fileName); 284 if (fl.delete()) _tabs.remove(_tabs.getSelectedComponent()); 285 else 286 { 287 setStatusMessage(Messages.getString("LogBrowser.CouldNotClear", fileName), false); 288 clearMsg = false; 289 } 290 } 291 if (clearMsg) setStatusMessage(""); 292 } 293 294 public static void main(String [] args) 295 { 296 try 297 { 298 DjenericTool.setLookAndFeel(); 299 300 new LogBrowser().startApp(); 301 } 302 catch (Exception e) 303 { 304 DjLogger.log(e); 305 } 306 } 307 } | Popular Tags |