001: /*
002: * LogViewer.java
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2004 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import java.awt.*;
027: import java.awt.event.*;
028: import javax.swing.*;
029: import javax.swing.border.EmptyBorder;
030: import javax.swing.event.*;
031: import org.gjt.sp.jedit.*;
032: import org.gjt.sp.jedit.msg.PropertiesChanged;
033: import org.gjt.sp.util.Log;
034:
035: //}}}
036:
037: /**
038: * @version $Id: LogViewer.java 8702 2007-01-21 23:16:41Z kpouer $
039: */
040: public class LogViewer extends JPanel implements DefaultFocusComponent,
041: EBComponent {
042: //{{{ LogViewer constructor
043: public LogViewer() {
044: super (new BorderLayout());
045:
046: JPanel caption = new JPanel();
047: caption.setLayout(new BoxLayout(caption, BoxLayout.X_AXIS));
048: caption.setBorder(new EmptyBorder(6, 6, 6, 6));
049:
050: String settingsDirectory = jEdit.getSettingsDirectory();
051: if (settingsDirectory != null) {
052: String[] args = { MiscUtilities.constructPath(
053: settingsDirectory, "activity.log") };
054: JLabel label = new JLabel(jEdit.getProperty(
055: "log-viewer.caption", args));
056: caption.add(label);
057: }
058:
059: caption.add(Box.createHorizontalGlue());
060:
061: tailIsOn = jEdit.getBooleanProperty("log-viewer.tail", false);
062: tail = new JCheckBox(
063: jEdit.getProperty("log-viewer.tail.label"), tailIsOn);
064: tail.addActionListener(new ActionHandler());
065: caption.add(tail);
066:
067: caption.add(Box.createHorizontalStrut(12));
068:
069: copy = new JButton(jEdit.getProperty("log-viewer.copy"));
070: copy.addActionListener(new ActionHandler());
071: caption.add(copy);
072:
073: ListModel model = Log.getLogListModel();
074: model.addListDataListener(new ListHandler());
075: list = new LogList(model);
076:
077: add(BorderLayout.NORTH, caption);
078: JScrollPane scroller = new JScrollPane(list);
079: Dimension dim = scroller.getPreferredSize();
080: dim.width = Math.min(600, dim.width);
081: scroller.setPreferredSize(dim);
082: add(BorderLayout.CENTER, scroller);
083:
084: propertiesChanged();
085: } //}}}
086:
087: //{{{ setBounds() method
088: public void setBounds(int x, int y, int width, int height) {
089: super .setBounds(x, y, width, height);
090: scrollLaterIfRequired();
091: } //}}}
092:
093: //{{{ handleMessage() method
094: public void handleMessage(EBMessage msg) {
095: if (msg instanceof PropertiesChanged)
096: propertiesChanged();
097: } //}}}
098:
099: //{{{ addNotify() method
100: public void addNotify() {
101: super .addNotify();
102: if (tailIsOn)
103: scrollToTail();
104:
105: EditBus.addToBus(this );
106: } //}}}
107:
108: //{{{ removeNotify() method
109: public void removeNotify() {
110: super .removeNotify();
111:
112: EditBus.removeFromBus(this );
113: } //}}}
114:
115: //{{{ focusOnDefaultComponent() method
116: public void focusOnDefaultComponent() {
117: list.requestFocus();
118: } //}}}
119:
120: //{{{ Private members
121: private final JList list;
122: private final JButton copy;
123: private final JCheckBox tail;
124: private boolean tailIsOn;
125:
126: //{{{ propertiesChanged() method
127: private void propertiesChanged() {
128: list.setFont(jEdit.getFontProperty("view.font"));
129: list.setFixedCellHeight(list.getFontMetrics(list.getFont())
130: .getHeight());
131: } //}}}
132:
133: //{{{ scrollToTail() method
134: /** Scroll to the tail of the logs. */
135: private void scrollToTail() {
136: int index = list.getModel().getSize();
137: if (index != 0)
138: list.ensureIndexIsVisible(index - 1);
139: } //}}}
140:
141: //{{{ scrollLaterIfRequired() method
142: private void scrollLaterIfRequired() {
143: if (tailIsOn)
144: SwingUtilities.invokeLater(new Runnable() {
145: public void run() {
146: scrollToTail();
147: }
148: });
149: } //}}}
150:
151: //}}}
152:
153: //{{{ ActionHandler class
154: class ActionHandler implements ActionListener {
155: public void actionPerformed(ActionEvent e) {
156: Object src = e.getSource();
157: if (src == tail) {
158: tailIsOn = !tailIsOn;
159: jEdit.setBooleanProperty("log-viewer.tail", tailIsOn);
160: if (tailIsOn) {
161: scrollToTail();
162: }
163: } else if (src == copy) {
164: StringBuilder buf = new StringBuilder();
165: Object[] selected = list.getSelectedValues();
166: if (selected != null && selected.length != 0) {
167: for (int i = 0; i < selected.length; i++) {
168: buf.append(selected[i]);
169: buf.append('\n');
170: }
171: } else {
172: ListModel model = list.getModel();
173: for (int i = 0; i < model.getSize(); i++) {
174: buf.append(model.getElementAt(i));
175: buf.append('\n');
176: }
177: }
178: Registers.setRegister('$', buf.toString());
179: }
180: }
181: } //}}}
182:
183: //{{{ ListHandler class
184: class ListHandler implements ListDataListener {
185: public void intervalAdded(ListDataEvent e) {
186: contentsChanged(e);
187: }
188:
189: public void intervalRemoved(ListDataEvent e) {
190: contentsChanged(e);
191: }
192:
193: public void contentsChanged(ListDataEvent e) {
194: scrollLaterIfRequired();
195: }
196: } //}}}
197:
198: //{{{ LogList class
199: class LogList extends JList {
200: LogList(ListModel model) {
201: super (model);
202: setVisibleRowCount(24);
203: getSelectionModel().setSelectionMode(
204: ListSelectionModel.SINGLE_INTERVAL_SELECTION);
205: setAutoscrolls(true);
206: }
207:
208: public void processMouseEvent(MouseEvent evt) {
209: if (evt.getID() == MouseEvent.MOUSE_PRESSED) {
210: startIndex = list.locationToIndex(evt.getPoint());
211: }
212: super .processMouseEvent(evt);
213: }
214:
215: public void processMouseMotionEvent(MouseEvent evt) {
216: if (evt.getID() == MouseEvent.MOUSE_DRAGGED) {
217: int row = list.locationToIndex(evt.getPoint());
218: if (row != -1) {
219: if (startIndex == -1) {
220: list.setSelectionInterval(row, row);
221: startIndex = row;
222: } else
223: list.setSelectionInterval(startIndex, row);
224: list.ensureIndexIsVisible(row);
225: evt.consume();
226: }
227: } else
228: super .processMouseMotionEvent(evt);
229: }
230:
231: private int startIndex;
232: } //}}}
233: }
|