KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > LogViewer


1 /*
2  * LogViewer.java
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999, 2004 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import java.awt.*;
27 import java.awt.event.*;
28 import javax.swing.*;
29 import javax.swing.border.EmptyBorder JavaDoc;
30 import javax.swing.event.*;
31 import org.gjt.sp.jedit.*;
32 import org.gjt.sp.jedit.msg.PropertiesChanged;
33 import org.gjt.sp.util.Log;
34 //}}}
35

36 public class LogViewer extends JPanel implements DefaultFocusComponent,
37     EBComponent
38 {
39     //{{{ LogViewer constructor
40
public LogViewer()
41     {
42         super(new BorderLayout());
43
44         JPanel caption = new JPanel();
45         caption.setLayout(new BoxLayout(caption,BoxLayout.X_AXIS));
46         caption.setBorder(new EmptyBorder JavaDoc(6,6,6,6));
47
48         String JavaDoc settingsDirectory = jEdit.getSettingsDirectory();
49         if(settingsDirectory != null)
50         {
51             String JavaDoc[] args = { MiscUtilities.constructPath(
52                 settingsDirectory, "activity.log") };
53             JLabel label = new JLabel(jEdit.getProperty(
54                 "log-viewer.caption",args));
55             caption.add(label);
56         }
57
58         caption.add(Box.createHorizontalGlue());
59
60         tailIsOn = jEdit.getBooleanProperty("log-viewer.tail", false);
61         tail = new JCheckBox(
62             jEdit.getProperty("log-viewer.tail.label"),tailIsOn);
63         tail.addActionListener(new ActionHandler());
64         caption.add(tail);
65
66         caption.add(Box.createHorizontalStrut(12));
67
68         copy = new JButton(jEdit.getProperty("log-viewer.copy"));
69         copy.addActionListener(new ActionHandler());
70         caption.add(copy);
71
72         ListModel model = Log.getLogListModel();
73         model.addListDataListener(new ListHandler());
74         list = new LogList(model);
75
76         add(BorderLayout.NORTH,caption);
77         JScrollPane scroller = new JScrollPane(list);
78         Dimension dim = scroller.getPreferredSize();
79         dim.width = Math.min(600,dim.width);
80         scroller.setPreferredSize(dim);
81         add(BorderLayout.CENTER,scroller);
82
83         propertiesChanged();
84     } //}}}
85

86     //{{{ handleMessage() method
87
public void handleMessage(EBMessage msg)
88     {
89         if(msg instanceof PropertiesChanged)
90             propertiesChanged();
91     } //}}}
92

93     //{{{ addNotify() method
94
public void addNotify()
95     {
96         super.addNotify();
97         if(tailIsOn)
98         {
99             int index = list.getModel().getSize() - 1;
100             list.ensureIndexIsVisible(index);
101         }
102
103         EditBus.addToBus(this);
104     } //}}}
105

106     //{{{ removeNotify() method
107
public void removeNotify()
108     {
109         super.removeNotify();
110
111         EditBus.removeFromBus(this);
112     } //}}}
113

114     //{{{ focusOnDefaultComponent() method
115
public void focusOnDefaultComponent()
116     {
117         list.requestFocus();
118     } //}}}
119

120     //{{{ Private members
121
private JList list;
122     private JButton copy;
123     private JCheckBox tail;
124     private boolean tailIsOn;
125
126     //{{{ propertiesChanged() method
127
private void propertiesChanged()
128     {
129         list.setFont(jEdit.getFontProperty("view.font"));
130         list.setFixedCellHeight(list.getFontMetrics(list.getFont())
131             .getHeight());
132     } //}}}
133

134     //}}}
135

136     //{{{ ActionHandler class
137
class ActionHandler implements ActionListener
138     {
139         public void actionPerformed(ActionEvent e)
140         {
141             Object JavaDoc src = e.getSource();
142             if(src == tail)
143             {
144                 tailIsOn = !tailIsOn;
145                 jEdit.setBooleanProperty("log-viewer.tail",tailIsOn);
146                 if(tailIsOn)
147                 {
148                     int index = list.getModel().getSize();
149                     if(index != 0)
150                     {
151                         list.ensureIndexIsVisible(index - 1);
152                     }
153                 }
154             }
155             else if(src == copy)
156             {
157                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
158                 Object JavaDoc[] selected = list.getSelectedValues();
159                 if(selected != null && selected.length != 0)
160                 {
161                     for(int i = 0; i < selected.length; i++)
162                     {
163                         buf.append(selected[i]);
164                         buf.append('\n');
165                     }
166                 }
167                 else
168                 {
169                     ListModel model = list.getModel();
170                     for(int i = 0; i < model.getSize(); i++)
171                     {
172                         buf.append(model.getElementAt(i));
173                         buf.append('\n');
174                     }
175                 }
176                 Registers.setRegister('$',buf.toString());
177             }
178         }
179     } //}}}
180

181     //{{{ ListHandler class
182
class ListHandler implements ListDataListener
183     {
184         public void intervalAdded(ListDataEvent e)
185         {
186             contentsChanged(e);
187         }
188
189         public void intervalRemoved(ListDataEvent e)
190         {
191             contentsChanged(e);
192         }
193
194         public void contentsChanged(ListDataEvent e)
195         {
196             if(tailIsOn)
197             {
198                 SwingUtilities.invokeLater(new Runnable JavaDoc()
199                 {
200                     public void run()
201                     {
202                         int index = list.getModel().getSize() - 1;
203                         list.ensureIndexIsVisible(index);
204                     }
205                 });
206             }
207         }
208     } //}}}
209

210     //{{{ LogList class
211
class LogList extends JList
212     {
213         LogList(ListModel model)
214         {
215             super(model);
216             setVisibleRowCount(24);
217             getSelectionModel().setSelectionMode(
218                 ListSelectionModel.SINGLE_INTERVAL_SELECTION);
219             setAutoscrolls(true);
220         }
221
222         public void processMouseEvent(MouseEvent evt)
223         {
224             if(evt.getID() == MouseEvent.MOUSE_PRESSED)
225             {
226                 startIndex = list.locationToIndex(
227                     evt.getPoint());
228             }
229             super.processMouseEvent(evt);
230         }
231
232         public void processMouseMotionEvent(MouseEvent evt)
233         {
234             if(evt.getID() == MouseEvent.MOUSE_DRAGGED)
235             {
236                 int row = list.locationToIndex(evt.getPoint());
237                 if(row != -1)
238                 {
239                     if(startIndex == -1)
240                     {
241                         list.setSelectionInterval(row,row);
242                         startIndex = row;
243                     }
244                     else
245                         list.setSelectionInterval(startIndex,row);
246                     list.ensureIndexIsVisible(row);
247                     evt.consume();
248                 }
249             }
250             else
251                 super.processMouseMotionEvent(evt);
252         }
253
254         private int startIndex;
255     } //}}}
256
}
257
Popular Tags