001: /*
002: * MarkersProvider.java - Markers menu
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2001, 2003 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.menu;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import java.awt.event.*;
028: import java.awt.*;
029: import java.util.List;
030:
031: import org.gjt.sp.jedit.*;
032:
033: //}}}
034:
035: public class MarkersProvider implements DynamicMenuProvider {
036: //{{{ updateEveryTime() method
037: public boolean updateEveryTime() {
038: return true;
039: } //}}}
040:
041: //{{{ update() method
042: public void update(JMenu menu) {
043: final View view = GUIUtilities.getView(menu);
044: Buffer buffer = view.getBuffer();
045:
046: List<Marker> markers = buffer.getMarkers();
047:
048: if (markers.isEmpty()) {
049: JMenuItem mi = new JMenuItem(jEdit
050: .getProperty("no-markers.label"));
051: mi.setEnabled(false);
052: menu.add(mi);
053: return;
054: }
055:
056: int maxItems = jEdit.getIntegerProperty("menu.spillover", 20);
057:
058: JMenu current = menu;
059:
060: for (int i = 0; i < markers.size(); i++) {
061: final Marker marker = markers.get(i);
062: int lineNo = buffer.getLineOfOffset(marker.getPosition());
063:
064: if (current.getItemCount() >= maxItems
065: && i != markers.size() - 1) {
066: //current.addSeparator();
067: JMenu newCurrent = new JMenu(jEdit
068: .getProperty("common.more"));
069: current.add(newCurrent);
070: current = newCurrent;
071: }
072:
073: JMenuItem mi = new MarkersMenuItem(buffer, lineNo, marker
074: .getShortcut());
075: mi.addActionListener(new ActionListener() {
076: public void actionPerformed(ActionEvent evt) {
077: view.getTextArea().setCaretPosition(
078: marker.getPosition());
079: }
080: });
081: current.add(mi);
082: }
083: } //}}}
084:
085: //{{{ MarkersMenuItem class
086: static class MarkersMenuItem extends JMenuItem {
087: //{{{ MarkersMenuItem constructor
088: MarkersMenuItem(Buffer buffer, int lineNo, char shortcut) {
089: String text = buffer.getLineText(lineNo).trim();
090: if (text.length() == 0)
091: text = jEdit.getProperty("markers.blank-line");
092: setText((lineNo + 1) + ": " + text);
093:
094: shortcutProp = "goto-marker.shortcut";
095: MarkersMenuItem.this .shortcut = shortcut;
096: } //}}}
097:
098: //{{{ getPreferredSize() method
099: public Dimension getPreferredSize() {
100: Dimension d = super .getPreferredSize();
101:
102: String shortcut = getShortcut();
103:
104: if (shortcut != null) {
105: d.width += (getFontMetrics(acceleratorFont)
106: .stringWidth(shortcut) + 15);
107: }
108: return d;
109: } //}}}
110:
111: //{{{ paint() method
112: public void paint(Graphics g) {
113: super .paint(g);
114:
115: String shortcut = getShortcut();
116:
117: if (shortcut != null) {
118: g.setFont(acceleratorFont);
119: g
120: .setColor(getModel().isArmed() ? acceleratorSelectionForeground
121: : acceleratorForeground);
122: FontMetrics fm = g.getFontMetrics();
123: Insets insets = getInsets();
124: g.drawString(shortcut, getWidth()
125: - (fm.stringWidth(shortcut) + insets.right
126: + insets.left + 5), getFont().getSize()
127: + (insets.top - 1)
128: /* XXX magic number */);
129: }
130: } //}}}
131:
132: //{{{ Private members
133: private String shortcutProp;
134: private char shortcut;
135: private static Font acceleratorFont;
136: private static Color acceleratorForeground;
137: private static Color acceleratorSelectionForeground;
138:
139: //{{{ getShortcut() method
140: private String getShortcut() {
141: if (shortcut == '\0')
142: return null;
143: else {
144: String shortcutPrefix = jEdit.getProperty(shortcutProp);
145:
146: if (shortcutPrefix == null)
147: return null;
148: else {
149: return shortcutPrefix + ' ' + shortcut;
150: }
151: }
152: } //}}}
153:
154: //{{{ Class initializer
155: static {
156: acceleratorFont = UIManager
157: .getFont("MenuItem.acceleratorFont");
158: acceleratorFont = new Font("Monospaced", acceleratorFont
159: .getStyle(), acceleratorFont.getSize());
160: acceleratorForeground = UIManager
161: .getColor("MenuItem.acceleratorForeground");
162: acceleratorSelectionForeground = UIManager
163: .getColor("MenuItem.acceleratorSelectionForeground");
164: } //}}}
165:
166: //}}}
167: } //}}}
168: }
|