/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/statusbar/ErrorsWidgetFactory.java

# · Java · 318 lines · 234 code · 39 blank · 45 comment · 23 complexity · 2a0973689a74debc376a3891d9c944e2 MD5 · raw file

  1. /*
  2. * ErrorsWidgetFactory.java - The error widget service
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2008-2011 Matthieu Casanova
  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.statusbar;
  23. //{{{ Imports
  24. import org.gjt.sp.jedit.View;
  25. import org.gjt.sp.jedit.jEdit;
  26. import org.gjt.sp.jedit.GUIUtilities;
  27. import org.gjt.sp.jedit.gui.EnhancedDialog;
  28. import org.gjt.sp.jedit.syntax.SyntaxStyle;
  29. import org.gjt.sp.util.Log;
  30. import javax.swing.*;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.io.PrintStream;
  34. import java.io.ByteArrayOutputStream;
  35. //}}}
  36. /**
  37. * This widget will show you in the status bar the last errors reported in jEdit.
  38. * @author Matthieu Casanova
  39. * @since jEdit 4.3pre15
  40. */
  41. public class ErrorsWidgetFactory implements StatusWidgetFactory
  42. {
  43. //{{{ getWidget() method
  44. @Override
  45. public Widget getWidget(View view)
  46. {
  47. Widget errorWidget = new ErrorWidget(view);
  48. return errorWidget;
  49. } //}}}
  50. //{{{ ErrorWidget class
  51. private static class ErrorWidget implements Widget
  52. {
  53. private final ErrorHighlight errorHighlight;
  54. ErrorWidget(View view)
  55. {
  56. errorHighlight = new ErrorHighlight(view);
  57. }
  58. @Override
  59. public JComponent getComponent()
  60. {
  61. return errorHighlight;
  62. }
  63. @Override
  64. public void update()
  65. {
  66. errorHighlight.update();
  67. }
  68. @Override
  69. public void propertiesChanged()
  70. {
  71. }
  72. } //}}}
  73. //{{{ ErrorHighlight class
  74. private static class ErrorHighlight extends JLabel implements ActionListener
  75. {
  76. private int currentSize;
  77. private final Color foregroundColor;
  78. //{{{ ErrorHighlight constructor
  79. ErrorHighlight(View view)
  80. {
  81. String defaultFont = jEdit.getProperty("view.font");
  82. int defaultFontSize = jEdit.getIntegerProperty("view.fontsize", 12);
  83. SyntaxStyle invalid = GUIUtilities.parseStyle(
  84. jEdit.getProperty("view.style.invalid"),
  85. defaultFont,defaultFontSize);
  86. foregroundColor = invalid.getForegroundColor();
  87. setForeground(foregroundColor);
  88. setBackground(jEdit.getColorProperty("view.status.background"));
  89. addMouseListener(new MyMouseAdapter(view));
  90. } //}}}
  91. //{{{ addNotify() method
  92. @Override
  93. public void addNotify()
  94. {
  95. super.addNotify();
  96. update();
  97. int millisecondsPerMinute = 1000;
  98. timer = new Timer(millisecondsPerMinute, this);
  99. timer.start();
  100. ToolTipManager.sharedInstance().registerComponent(this);
  101. } //}}}
  102. //{{{ removeNotify() method
  103. @Override
  104. public void removeNotify()
  105. {
  106. timer.stop();
  107. ToolTipManager.sharedInstance().unregisterComponent(this);
  108. super.removeNotify();
  109. } //}}}
  110. //{{{ getToolTipLocation() method
  111. @Override
  112. public Point getToolTipLocation(MouseEvent event)
  113. {
  114. return new Point(event.getX(), -20);
  115. } //}}}
  116. //{{{ actionPerformed() method
  117. @Override
  118. public void actionPerformed(ActionEvent e)
  119. {
  120. update();
  121. } //}}}
  122. private Timer timer;
  123. //{{{ update() method
  124. private void update()
  125. {
  126. int size = Log.throwables.size();
  127. if (size != currentSize)
  128. {
  129. currentSize = size;
  130. if (size == 0)
  131. {
  132. setText(null);
  133. setToolTipText(size + " error");
  134. }
  135. else
  136. {
  137. setForeground(foregroundColor);
  138. setText(Integer.toString(size) + " error(s)");
  139. setToolTipText(size + " error(s)");
  140. }
  141. }
  142. } //}}}
  143. //{{{ MyMouseAdapter class
  144. private class MyMouseAdapter extends MouseAdapter
  145. {
  146. private final View view;
  147. MyMouseAdapter(View view)
  148. {
  149. this.view = view;
  150. }
  151. @Override
  152. public void mouseClicked(MouseEvent e)
  153. {
  154. if (Log.throwables.isEmpty())
  155. return;
  156. if (GUIUtilities.isRightButton(e.getModifiers()))
  157. {
  158. JPopupMenu menu = GUIUtilities.loadPopupMenu("errorwidget.popupmenu");
  159. GUIUtilities.showPopupMenu(menu, ErrorHighlight.this, e.getX(), e.getY());
  160. }
  161. else if (e.getClickCount() == 2)
  162. new ErrorDialog(view);
  163. }
  164. } //}}}
  165. } //}}}
  166. //{{{ ErrorDialog class
  167. private static class ErrorDialog extends EnhancedDialog
  168. {
  169. private final JTextArea textArea;
  170. private final ByteArrayOutputStream byteArrayOutputStream;
  171. private final PrintStream printStream;
  172. private final JButton removeThisError;
  173. private final JButton removeAllErrors;
  174. private final Object[] throwables;
  175. private final JComboBox combo;
  176. //{{{ ErrorDialog constructor
  177. private ErrorDialog(Frame view)
  178. {
  179. super(view, "Errors", false);
  180. byteArrayOutputStream = new ByteArrayOutputStream();
  181. printStream = new PrintStream(byteArrayOutputStream);
  182. throwables = Log.throwables.toArray();
  183. textArea = new JTextArea();
  184. textArea.setEditable(false);
  185. if (throwables.length != 0)
  186. {
  187. Throwable throwable = (Throwable) throwables[0];
  188. setThrowable(throwable);
  189. }
  190. combo = new JComboBox(throwables);
  191. combo.addItemListener(new ItemListener()
  192. {
  193. public void itemStateChanged(ItemEvent e)
  194. {
  195. setThrowable((Throwable) combo.getSelectedItem());
  196. }
  197. });
  198. getContentPane().add(combo, BorderLayout.NORTH);
  199. getContentPane().add(new JScrollPane(textArea));
  200. Box buttons = new Box(BoxLayout.X_AXIS);
  201. buttons.add(Box.createGlue());
  202. buttons.add(removeThisError = new JButton(jEdit.getProperty("grab-key.remove")));
  203. buttons.add(Box.createHorizontalStrut(6));
  204. buttons.add(removeAllErrors = new JButton(jEdit.getProperty("common.clearAll")));
  205. ErrorDialog.MyActionListener actionListener = new MyActionListener();
  206. removeThisError.addActionListener(actionListener);
  207. removeAllErrors.addActionListener(actionListener);
  208. buttons.add(Box.createGlue());
  209. getContentPane().add(buttons, BorderLayout.SOUTH);
  210. pack();
  211. GUIUtilities.loadGeometry(this,"status.errorWidget");
  212. setVisible(true);
  213. } //}}}
  214. //{{{ setThrowable() method
  215. private void setThrowable(Throwable throwable)
  216. {
  217. if (throwable == null)
  218. {
  219. textArea.setText(null);
  220. }
  221. else
  222. {
  223. throwable.printStackTrace(printStream);
  224. textArea.setText(byteArrayOutputStream.toString());
  225. textArea.setCaretPosition(0);
  226. byteArrayOutputStream.reset();
  227. }
  228. } //}}}
  229. //{{{ dispose() method
  230. @Override
  231. public void dispose()
  232. {
  233. GUIUtilities.saveGeometry(this, "status.errorWidget");
  234. super.dispose();
  235. } //}}}
  236. //{{{ ok() method
  237. @Override
  238. public void ok()
  239. {
  240. dispose();
  241. } //}}}
  242. //{{{ cancel() method
  243. @Override
  244. public void cancel()
  245. {
  246. dispose();
  247. } //}}}
  248. //{{{ MyActionListener class
  249. private class MyActionListener implements ActionListener
  250. {
  251. @Override
  252. public void actionPerformed(ActionEvent e)
  253. {
  254. Object source = e.getSource();
  255. if (source == removeThisError)
  256. {
  257. Throwable throwable = (Throwable) combo.getSelectedItem();
  258. if (throwable != null)
  259. {
  260. Log.throwables.remove(throwable);
  261. combo.removeItem(throwable);
  262. if (combo.getItemCount() == 0)
  263. {
  264. dispose();
  265. }
  266. }
  267. }
  268. else if (source == removeAllErrors)
  269. {
  270. for (Object throwable : throwables)
  271. {
  272. Log.throwables.remove(throwable);
  273. }
  274. dispose();
  275. }
  276. }
  277. } //}}}
  278. } //}}}
  279. }