1 22 23 package org.gjt.sp.jedit.menu; 24 25 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 34 public class MarkersProvider implements DynamicMenuProvider 35 { 36 public boolean updateEveryTime() 38 { 39 return true; 40 } 42 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 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 } 92 static class MarkersMenuItem extends JMenuItem 94 { 95 MarkersMenuItem(Buffer buffer, int lineNo, char shortcut) 97 { 98 String 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 } 107 public Dimension getPreferredSize() 109 { 110 Dimension d = super.getPreferredSize(); 111 112 String shortcut = getShortcut(); 113 114 if(shortcut != null) 115 { 116 d.width += (getFontMetrics(acceleratorFont) 117 .stringWidth(shortcut) + 15); 118 } 119 return d; 120 } 122 public void paint(Graphics g) 124 { 125 super.paint(g); 126 127 String 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 ); 141 } 142 } 144 private String shortcutProp; 146 private char shortcut; 147 private static Font acceleratorFont; 148 private static Color acceleratorForeground; 149 private static Color acceleratorSelectionForeground; 150 151 private String getShortcut() 153 { 154 if(shortcut == '\0') 155 return null; 156 else 157 { 158 String shortcutPrefix = jEdit.getProperty(shortcutProp); 159 160 if(shortcutPrefix == null) 161 return null; 162 else 163 { 164 return shortcutPrefix + " " + shortcut; 165 } 166 } 167 } 169 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 } 182 } } 185 | Popular Tags |