KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > logbrowser > LogBrowser


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.logbrowser;
31
32 import java.awt.AWTEvent JavaDoc;
33 import java.awt.BorderLayout JavaDoc;
34 import java.awt.Color JavaDoc;
35 import java.awt.Component JavaDoc;
36 import java.awt.Dimension JavaDoc;
37 import java.awt.Image JavaDoc;
38 import java.awt.Toolkit JavaDoc;
39 import java.awt.datatransfer.Clipboard JavaDoc;
40 import java.awt.datatransfer.StringSelection JavaDoc;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.WindowEvent JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FilenameFilter JavaDoc;
45 import java.net.URL JavaDoc;
46
47 import javax.swing.ImageIcon JavaDoc;
48 import javax.swing.JButton JavaDoc;
49 import javax.swing.JLabel JavaDoc;
50 import javax.swing.JOptionPane JavaDoc;
51 import javax.swing.JPanel JavaDoc;
52 import javax.swing.JScrollPane JavaDoc;
53 import javax.swing.JTabbedPane JavaDoc;
54 import javax.swing.JTextArea JavaDoc;
55 import javax.swing.JViewport JavaDoc;
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 JavaDoc PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("logbrowser.properties");
69
70   public final static String JavaDoc OK_MSG = Messages.getString("global.Ok");
71
72   Color JavaDoc _normalColor = new Color JavaDoc(102, 102, 153);
73   JTabbedPane JavaDoc _tabs = new JTabbedPane JavaDoc();
74   JPanel JavaDoc _buttonPanel = new JPanel JavaDoc();
75   BorderLayout JavaDoc _buttonPanelLayout = new BorderLayout JavaDoc();
76   JPanel JavaDoc _clearPanel = new JPanel JavaDoc();
77   JButton JavaDoc _butClear = new JButton JavaDoc();
78   JPanel JavaDoc _clearAllPanel = new JPanel JavaDoc();
79   JButton JavaDoc _butClearAll = new JButton JavaDoc();
80   JLabel JavaDoc _lblStatus = new JLabel JavaDoc();
81   JButton JavaDoc _butClipboard = new JButton JavaDoc();
82
83   /**
84    * Construct the frame
85    */

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 JavaDoc(width, height));
100       setTitle(Messages.getString("LogBrowser.title", DjVersion.getVersion()));
101
102       String JavaDoc djDir = DjEnvironment.getDjenericDir();
103       File JavaDoc dir = new File JavaDoc(djDir);
104       String JavaDoc[] 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 JavaDoc e)
112     {
113       handleException(e);
114       System.exit(-1);
115     }
116   }
117
118   /**
119    * Component initialization
120    *
121    *@exception Exception Description of the Exception
122    */

123   private void jbInit() throws Exception JavaDoc
124   {
125     _buttonPanel.setLayout(_buttonPanelLayout);
126     _butClear.setText(Messages.getString("LogBrowser.Clear"));
127     _butClear.addActionListener(new java.awt.event.ActionListener JavaDoc()
128     {
129       public void actionPerformed(ActionEvent JavaDoc e)
130       {
131         _butClear_actionPerformed(e);
132       }
133     });
134     _butClearAll.setText(Messages.getString("LogBrowser.ClearAll"));
135     _butClearAll.addActionListener(new java.awt.event.ActionListener JavaDoc()
136     {
137       public void actionPerformed(ActionEvent JavaDoc e)
138       {
139         _butClearAll_actionPerformed(e);
140       }
141     });
142
143     _butClipboard.setText(Messages.getString("LogBrowser.CopyToClipboard"));
144     _butClipboard.addActionListener(new java.awt.event.ActionListener JavaDoc()
145     {
146       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc 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 JavaDoc getImageIcon(String JavaDoc fileName)
181   {
182     URL JavaDoc url = LogBrowser.class.getResource("images/" + fileName);
183     if (url != null) return new ImageIcon JavaDoc(url);
184     else return new ImageIcon JavaDoc("");
185     // Default to empty image
186
}
187
188   public static Image JavaDoc getImage(String JavaDoc fileName)
189   {
190     URL JavaDoc 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 JavaDoc createLogPane(String JavaDoc fileName) throws Exception JavaDoc
197   {
198     JTextArea JavaDoc edt = new JTextArea JavaDoc();
199     edt.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
200     JScrollPane JavaDoc scr = new JScrollPane JavaDoc();
201
202     scr.getViewport().add(edt, null);
203     edt.setEditable(false);
204
205     String JavaDoc log = DjFileUtil.readFile(DjEnvironment.getDjenericDir() + File.separator + fileName);
206     edt.setText(log);
207
208     return scr;
209   }
210
211   class LogFilenameFilter implements FilenameFilter JavaDoc
212   {
213     public boolean accept(File JavaDoc dir, String JavaDoc name)
214     {
215       return name.toLowerCase().endsWith(".log");
216     }
217   }
218
219   // This is a dirty piece of code that relies on the actual structure of the
220
// tabbed pane and its scrollpane/textarea. But what the heck, this is a simple
221
// logbrowser anyway..
222
void _butClipboard_actionPerformed(ActionEvent JavaDoc e)
223   {
224     if (_tabs.getComponentCount() == 0) return;
225     Component JavaDoc comp = _tabs.getSelectedComponent();
226     if (comp != null)
227     {
228       JScrollPane JavaDoc scr = (JScrollPane JavaDoc) comp;
229       JViewport JavaDoc vp = (JViewport JavaDoc) scr.getComponent(0);
230       JTextArea JavaDoc ta = (JTextArea JavaDoc) vp.getComponent(0);
231       Clipboard JavaDoc clipboard = getToolkit().getSystemClipboard();
232       StringSelection JavaDoc data = new StringSelection JavaDoc(ta.getText());
233       clipboard.setContents(data, data);
234     }
235   }
236
237   void _butClearAll_actionPerformed(ActionEvent JavaDoc 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 JavaDoc[]{Messages.getString("LogBrowser.ClearAllLogFiles"),
244                                                   Messages.getString("global.Cancel")}, null);
245     if (result == 0)
246     {
247       String JavaDoc[] files = new String JavaDoc[_tabs.getComponentCount()];
248       Component JavaDoc[] 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 JavaDoc fl = new File JavaDoc(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 JavaDoc e)
269   {
270     if (_tabs.getComponentCount() == 0) return;
271     int idx = _tabs.getSelectedIndex();
272     String JavaDoc 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 JavaDoc[]{Messages.getString("LogBrowser.Clear"),
280                                                   Messages.getString("global.Cancel")}, null);
281     if (result == 0)
282     {
283       File JavaDoc fl = new File JavaDoc(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 JavaDoc[] args)
295   {
296     try
297     {
298       DjenericTool.setLookAndFeel();
299
300       new LogBrowser().startApp();
301     }
302     catch (Exception JavaDoc e)
303     {
304       DjLogger.log(e);
305     }
306   }
307 }
Popular Tags