PageRenderTime 358ms CodeModel.GetById 345ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/MarkersMenu.java

#
Java | 201 lines | 143 code | 25 blank | 33 comment | 19 complexity | 9c796df5bbf184a8f350a166f9291676 MD5 | raw file
  1/*
  2 * MarkersMenu.java - Markers menu
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001 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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import java.awt.event.*;
 28import java.awt.*;
 29import java.util.*;
 30import org.gjt.sp.jedit.*;
 31import org.gjt.sp.util.Log;
 32//}}}
 33
 34public class MarkersMenu extends EnhancedMenu
 35{
 36	//{{{ MarkersMenu constructor
 37	public MarkersMenu()
 38	{
 39		super("markers");
 40	} //}}}
 41
 42	//{{{ setPopupMenuVisible() method
 43	public void setPopupMenuVisible(boolean b)
 44	{
 45		if(b)
 46		{
 47			final View view = GUIUtilities.getView(this);
 48
 49			if(getMenuComponentCount() != 0)
 50			{
 51				for(int i = getMenuComponentCount() - 1;
 52					i >= 0;
 53					i--)
 54				{
 55					Component comp = getMenuComponent(i);
 56					if(comp instanceof JSeparator)
 57						break;
 58					else
 59						remove(comp);
 60				}
 61			}
 62
 63			Buffer buffer = view.getBuffer();
 64
 65			Vector markers = buffer.getMarkers();
 66
 67			if(markers.size() == 0)
 68			{
 69				JMenuItem mi = new JMenuItem(jEdit.getProperty(
 70					"no-markers.label"));
 71				mi.setEnabled(false);
 72				add(mi);
 73				super.setPopupMenuVisible(b);
 74				return;
 75			}
 76
 77			JMenu current = this;
 78
 79			for(int i = 0; i < markers.size(); i++)
 80			{
 81				final Marker marker = (Marker)markers.elementAt(i);
 82				int lineNo = buffer.getLineOfOffset(marker.getPosition());
 83
 84				if(current.getItemCount() >= 20)
 85				{
 86					//current.addSeparator();
 87					JMenu newCurrent = new JMenu(
 88						jEdit.getProperty(
 89						"common.more"));
 90					current.add(newCurrent);
 91					current = newCurrent;
 92				}
 93
 94				JMenuItem mi = new MarkersMenuItem(buffer,
 95					lineNo,marker.getShortcut());
 96				mi.addActionListener(new ActionListener()
 97				{
 98					public void actionPerformed(ActionEvent evt)
 99					{
100						view.getTextArea().setCaretPosition(
101							marker.getPosition());
102					}
103				});
104				current.add(mi);
105			}
106		}
107
108		super.setPopupMenuVisible(b);
109	} //}}}
110
111	//{{{ MarkersMenuItem class
112	static class MarkersMenuItem extends JMenuItem
113	{
114		//{{{MarkersMenuItem constructor
115		MarkersMenuItem(Buffer buffer, int lineNo, char shortcut)
116		{
117			String text = buffer.getLineText(lineNo).trim();
118			if(text.length() == 0)
119				text = jEdit.getProperty("markers.blank-line");
120			setText(lineNo + ": " + text);
121
122			shortcutProp = "goto-marker.shortcut";
123			MarkersMenuItem.this.shortcut = shortcut;
124		} //}}}
125
126		//{{{ getPreferredSize() method
127		public Dimension getPreferredSize()
128		{
129			Dimension d = super.getPreferredSize();
130
131			String shortcut = getShortcut();
132
133			if(shortcut != null)
134			{
135				d.width += (getFontMetrics(acceleratorFont)
136					.stringWidth(shortcut) + 10);
137			}
138			return d;
139		} //}}}
140
141		//{{{ paint() method
142		public void paint(Graphics g)
143		{
144			super.paint(g);
145
146			String shortcut = getShortcut();
147
148			if(shortcut != null)
149			{
150				g.setFont(acceleratorFont);
151				g.setColor(getModel().isArmed() ?
152					acceleratorSelectionForeground :
153					acceleratorForeground);
154				FontMetrics fm = g.getFontMetrics();
155				Insets insets = getInsets();
156				g.drawString(shortcut,getWidth() - (fm.stringWidth(
157					shortcut) + insets.right + insets.left),
158					getFont().getSize() + (insets.top - 1)
159					/* XXX magic number */);
160			}
161		} //}}}
162
163		//{{{ Private members
164		private String shortcutProp;
165		private char shortcut;
166		private static Font acceleratorFont;
167		private static Color acceleratorForeground;
168		private static Color acceleratorSelectionForeground;
169
170		//{{{ getShortcut() method
171		private String getShortcut()
172		{
173			if(shortcut == '\0')
174				return null;
175			else
176			{
177				String shortcutPrefix = jEdit.getProperty(shortcutProp);
178
179				if(shortcutPrefix == null)
180					return null;
181				else
182				{
183					return shortcutPrefix + " " + shortcut;
184				}
185			}
186		} //}}}
187
188		//{{{ Class initializer
189		static
190		{
191			acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
192			acceleratorFont = new Font("Monospaced",
193				acceleratorFont.getStyle(),
194				acceleratorFont.getSize());
195			acceleratorForeground = UIManager
196				.getColor("MenuItem.acceleratorForeground");
197			acceleratorSelectionForeground = UIManager
198				.getColor("MenuItem.acceleratorSelectionForeground");
199		} //}}}
200	} //}}}
201}