/jEdit/tags/jedit-4-3-pre1/org/gjt/sp/jedit/gui/LogViewer.java

# · Java · 256 lines · 193 code · 29 blank · 34 comment · 27 complexity · 03a12a65c853fcc07c5f24ee1c83d775 MD5 · raw file

  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. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import javax.swing.*;
  27. import javax.swing.border.EmptyBorder;
  28. import javax.swing.event.*;
  29. import org.gjt.sp.jedit.*;
  30. import org.gjt.sp.jedit.msg.PropertiesChanged;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. public class LogViewer extends JPanel implements DefaultFocusComponent,
  34. EBComponent
  35. {
  36. //{{{ LogViewer constructor
  37. public LogViewer()
  38. {
  39. super(new BorderLayout());
  40. JPanel caption = new JPanel();
  41. caption.setLayout(new BoxLayout(caption,BoxLayout.X_AXIS));
  42. caption.setBorder(new EmptyBorder(6,6,6,6));
  43. String settingsDirectory = jEdit.getSettingsDirectory();
  44. if(settingsDirectory != null)
  45. {
  46. String[] args = { MiscUtilities.constructPath(
  47. settingsDirectory, "activity.log") };
  48. JLabel label = new JLabel(jEdit.getProperty(
  49. "log-viewer.caption",args));
  50. caption.add(label);
  51. }
  52. caption.add(Box.createHorizontalGlue());
  53. tailIsOn = jEdit.getBooleanProperty("log-viewer.tail", false);
  54. tail = new JCheckBox(
  55. jEdit.getProperty("log-viewer.tail.label"),tailIsOn);
  56. tail.addActionListener(new ActionHandler());
  57. caption.add(tail);
  58. caption.add(Box.createHorizontalStrut(12));
  59. copy = new JButton(jEdit.getProperty("log-viewer.copy"));
  60. copy.addActionListener(new ActionHandler());
  61. caption.add(copy);
  62. ListModel model = Log.getLogListModel();
  63. model.addListDataListener(new ListHandler());
  64. list = new LogList(model);
  65. add(BorderLayout.NORTH,caption);
  66. JScrollPane scroller = new JScrollPane(list);
  67. Dimension dim = scroller.getPreferredSize();
  68. dim.width = Math.min(600,dim.width);
  69. scroller.setPreferredSize(dim);
  70. add(BorderLayout.CENTER,scroller);
  71. propertiesChanged();
  72. } //}}}
  73. //{{{ handleMessage() method
  74. public void handleMessage(EBMessage msg)
  75. {
  76. if(msg instanceof PropertiesChanged)
  77. propertiesChanged();
  78. } //}}}
  79. //{{{ addNotify() method
  80. public void addNotify()
  81. {
  82. super.addNotify();
  83. if(tailIsOn)
  84. {
  85. int index = list.getModel().getSize() - 1;
  86. list.ensureIndexIsVisible(index);
  87. }
  88. EditBus.addToBus(this);
  89. } //}}}
  90. //{{{ removeNotify() method
  91. public void removeNotify()
  92. {
  93. super.removeNotify();
  94. EditBus.removeFromBus(this);
  95. } //}}}
  96. //{{{ focusOnDefaultComponent() method
  97. public void focusOnDefaultComponent()
  98. {
  99. list.requestFocus();
  100. } //}}}
  101. //{{{ Private members
  102. private JList list;
  103. private JButton copy;
  104. private JCheckBox tail;
  105. private boolean tailIsOn;
  106. //{{{ propertiesChanged() method
  107. private void propertiesChanged()
  108. {
  109. list.setFont(jEdit.getFontProperty("view.font"));
  110. list.setFixedCellHeight(list.getFontMetrics(list.getFont())
  111. .getHeight());
  112. } //}}}
  113. //}}}
  114. //{{{ ActionHandler class
  115. class ActionHandler implements ActionListener
  116. {
  117. public void actionPerformed(ActionEvent e)
  118. {
  119. Object src = e.getSource();
  120. if(src == tail)
  121. {
  122. tailIsOn = !tailIsOn;
  123. jEdit.setBooleanProperty("log-viewer.tail",tailIsOn);
  124. if(tailIsOn)
  125. {
  126. int index = list.getModel().getSize();
  127. if(index != 0)
  128. {
  129. list.ensureIndexIsVisible(index - 1);
  130. }
  131. }
  132. }
  133. else if(src == copy)
  134. {
  135. StringBuffer buf = new StringBuffer();
  136. Object[] selected = list.getSelectedValues();
  137. if(selected != null && selected.length != 0)
  138. {
  139. for(int i = 0; i < selected.length; i++)
  140. {
  141. buf.append(selected[i]);
  142. buf.append('\n');
  143. }
  144. }
  145. else
  146. {
  147. ListModel model = list.getModel();
  148. for(int i = 0; i < model.getSize(); i++)
  149. {
  150. buf.append(model.getElementAt(i));
  151. buf.append('\n');
  152. }
  153. }
  154. Registers.setRegister('$',buf.toString());
  155. }
  156. }
  157. } //}}}
  158. //{{{ ListHandler class
  159. class ListHandler implements ListDataListener
  160. {
  161. public void intervalAdded(ListDataEvent e)
  162. {
  163. contentsChanged(e);
  164. }
  165. public void intervalRemoved(ListDataEvent e)
  166. {
  167. contentsChanged(e);
  168. }
  169. public void contentsChanged(ListDataEvent e)
  170. {
  171. if(tailIsOn)
  172. {
  173. SwingUtilities.invokeLater(new Runnable()
  174. {
  175. public void run()
  176. {
  177. int index = list.getModel().getSize() - 1;
  178. list.ensureIndexIsVisible(index);
  179. }
  180. });
  181. }
  182. }
  183. } //}}}
  184. //{{{ LogList class
  185. class LogList extends JList
  186. {
  187. LogList(ListModel model)
  188. {
  189. super(model);
  190. setVisibleRowCount(24);
  191. getSelectionModel().setSelectionMode(
  192. ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  193. setAutoscrolls(true);
  194. }
  195. public void processMouseEvent(MouseEvent evt)
  196. {
  197. if(evt.getID() == MouseEvent.MOUSE_PRESSED)
  198. {
  199. startIndex = list.locationToIndex(
  200. evt.getPoint());
  201. }
  202. super.processMouseEvent(evt);
  203. }
  204. public void processMouseMotionEvent(MouseEvent evt)
  205. {
  206. if(evt.getID() == MouseEvent.MOUSE_DRAGGED)
  207. {
  208. int row = list.locationToIndex(evt.getPoint());
  209. if(row != -1)
  210. {
  211. if(startIndex == -1)
  212. {
  213. list.setSelectionInterval(row,row);
  214. startIndex = row;
  215. }
  216. else
  217. list.setSelectionInterval(startIndex,row);
  218. list.ensureIndexIsVisible(row);
  219. evt.consume();
  220. }
  221. }
  222. else
  223. super.processMouseMotionEvent(evt);
  224. }
  225. private int startIndex;
  226. } //}}}
  227. }