PageRenderTime 99ms CodeModel.GetById 76ms app.highlight 20ms RepoModel.GetById 0ms app.codeStats 1ms

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