KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > menu > MarkersProvider


1 /*
2  * MarkersProvider.java - Markers menu
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001, 2003 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
23 package org.gjt.sp.jedit.menu;
24
25 //{{{ Imports
26
import javax.swing.event.*;
27 import javax.swing.*;
28 import java.awt.event.*;
29 import java.awt.*;
30 import java.util.*;
31 import org.gjt.sp.jedit.*;
32 //}}}
33

34 public class MarkersProvider implements DynamicMenuProvider
35 {
36     //{{{ updateEveryTime() method
37
public boolean updateEveryTime()
38     {
39         return true;
40     } //}}}
41

42     //{{{ update() method
43
public void update(JMenu menu)
44     {
45         final View view = GUIUtilities.getView(menu);
46         Buffer buffer = view.getBuffer();
47
48         Vector markers = buffer.getMarkers();
49
50         if(markers.size() == 0)
51         {
52             JMenuItem mi = new JMenuItem(jEdit.getProperty(
53                 "no-markers.label"));
54             mi.setEnabled(false);
55             menu.add(mi);
56             return;
57         }
58
59         int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
60
61         JMenu current = menu;
62
63         for(int i = 0; i < markers.size(); i++)
64         {
65             final Marker marker = (Marker)markers.elementAt(i);
66             int lineNo = buffer.getLineOfOffset(marker.getPosition());
67
68             if(current.getItemCount() >= maxItems && i != markers.size() - 1)
69             {
70                 //current.addSeparator();
71
JMenu newCurrent = new JMenu(
72                     jEdit.getProperty(
73                     "common.more"));
74                 current.add(newCurrent);
75                 current = newCurrent;
76             }
77
78             JMenuItem mi = new MarkersMenuItem(buffer,
79                 lineNo,marker.getShortcut());
80             mi.addActionListener(new ActionListener()
81             {
82                 public void actionPerformed(ActionEvent evt)
83                 {
84                     view.getTextArea().setCaretPosition(
85                         marker.getPosition());
86                 }
87             });
88             current.add(mi);
89         }
90     } //}}}
91

92     //{{{ MarkersMenuItem class
93
static class MarkersMenuItem extends JMenuItem
94     {
95         //{{{ MarkersMenuItem constructor
96
MarkersMenuItem(Buffer buffer, int lineNo, char shortcut)
97         {
98             String JavaDoc text = buffer.getLineText(lineNo).trim();
99             if(text.length() == 0)
100                 text = jEdit.getProperty("markers.blank-line");
101             setText((lineNo + 1) + ": " + text);
102
103             shortcutProp = "goto-marker.shortcut";
104             MarkersMenuItem.this.shortcut = shortcut;
105         } //}}}
106

107         //{{{ getPreferredSize() method
108
public Dimension getPreferredSize()
109         {
110             Dimension d = super.getPreferredSize();
111
112             String JavaDoc shortcut = getShortcut();
113
114             if(shortcut != null)
115             {
116                 d.width += (getFontMetrics(acceleratorFont)
117                     .stringWidth(shortcut) + 15);
118             }
119             return d;
120         } //}}}
121

122         //{{{ paint() method
123
public void paint(Graphics g)
124         {
125             super.paint(g);
126
127             String JavaDoc shortcut = getShortcut();
128
129             if(shortcut != null)
130             {
131                 g.setFont(acceleratorFont);
132                 g.setColor(getModel().isArmed() ?
133                     acceleratorSelectionForeground :
134                     acceleratorForeground);
135                 FontMetrics fm = g.getFontMetrics();
136                 Insets insets = getInsets();
137                 g.drawString(shortcut,getWidth() - (fm.stringWidth(
138                     shortcut) + insets.right + insets.left + 5),
139                     getFont().getSize() + (insets.top - 1)
140                     /* XXX magic number */);
141             }
142         } //}}}
143

144         //{{{ Private members
145
private String JavaDoc shortcutProp;
146         private char shortcut;
147         private static Font acceleratorFont;
148         private static Color acceleratorForeground;
149         private static Color acceleratorSelectionForeground;
150
151         //{{{ getShortcut() method
152
private String JavaDoc getShortcut()
153         {
154             if(shortcut == '\0')
155                 return null;
156             else
157             {
158                 String JavaDoc shortcutPrefix = jEdit.getProperty(shortcutProp);
159
160                 if(shortcutPrefix == null)
161                     return null;
162                 else
163                 {
164                     return shortcutPrefix + " " + shortcut;
165                 }
166             }
167         } //}}}
168

169         //{{{ Class initializer
170
static
171         {
172             acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
173             acceleratorFont = new Font("Monospaced",
174                 acceleratorFont.getStyle(),
175                 acceleratorFont.getSize());
176             acceleratorForeground = UIManager
177                 .getColor("MenuItem.acceleratorForeground");
178             acceleratorSelectionForeground = UIManager
179                 .getColor("MenuItem.acceleratorSelectionForeground");
180         } //}}}
181

182         //}}}
183
} //}}}
184
}
185
Popular Tags