PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/menu/MarkersProvider.java

#
Java | 184 lines | 126 code | 24 blank | 34 comment | 16 complexity | 39186614311d985687ad965621164383 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.menu;
  23. //{{{ Imports
  24. import javax.swing.event.*;
  25. import javax.swing.*;
  26. import java.awt.event.*;
  27. import java.awt.*;
  28. import java.util.*;
  29. import org.gjt.sp.jedit.*;
  30. //}}}
  31. public class MarkersProvider implements DynamicMenuProvider
  32. {
  33. //{{{ updateEveryTime() method
  34. public boolean updateEveryTime()
  35. {
  36. return true;
  37. } //}}}
  38. //{{{ update() method
  39. public void update(JMenu menu)
  40. {
  41. final View view = GUIUtilities.getView(menu);
  42. Buffer buffer = view.getBuffer();
  43. Vector markers = buffer.getMarkers();
  44. if(markers.size() == 0)
  45. {
  46. JMenuItem mi = new JMenuItem(jEdit.getProperty(
  47. "no-markers.label"));
  48. mi.setEnabled(false);
  49. menu.add(mi);
  50. return;
  51. }
  52. int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
  53. JMenu current = menu;
  54. for(int i = 0; i < markers.size(); i++)
  55. {
  56. final Marker marker = (Marker)markers.elementAt(i);
  57. int lineNo = buffer.getLineOfOffset(marker.getPosition());
  58. if(current.getItemCount() >= maxItems && i != markers.size() - 1)
  59. {
  60. //current.addSeparator();
  61. JMenu newCurrent = new JMenu(
  62. jEdit.getProperty(
  63. "common.more"));
  64. current.add(newCurrent);
  65. current = newCurrent;
  66. }
  67. JMenuItem mi = new MarkersMenuItem(buffer,
  68. lineNo,marker.getShortcut());
  69. mi.addActionListener(new ActionListener()
  70. {
  71. public void actionPerformed(ActionEvent evt)
  72. {
  73. view.getTextArea().setCaretPosition(
  74. marker.getPosition());
  75. }
  76. });
  77. current.add(mi);
  78. }
  79. } //}}}
  80. //{{{ MarkersMenuItem class
  81. static class MarkersMenuItem extends JMenuItem
  82. {
  83. //{{{ MarkersMenuItem constructor
  84. MarkersMenuItem(Buffer buffer, int lineNo, char shortcut)
  85. {
  86. String text = buffer.getLineText(lineNo).trim();
  87. if(text.length() == 0)
  88. text = jEdit.getProperty("markers.blank-line");
  89. setText((lineNo + 1) + ": " + text);
  90. shortcutProp = "goto-marker.shortcut";
  91. MarkersMenuItem.this.shortcut = shortcut;
  92. } //}}}
  93. //{{{ getPreferredSize() method
  94. public Dimension getPreferredSize()
  95. {
  96. Dimension d = super.getPreferredSize();
  97. String shortcut = getShortcut();
  98. if(shortcut != null)
  99. {
  100. d.width += (getFontMetrics(acceleratorFont)
  101. .stringWidth(shortcut) + 15);
  102. }
  103. return d;
  104. } //}}}
  105. //{{{ paint() method
  106. public void paint(Graphics g)
  107. {
  108. super.paint(g);
  109. String shortcut = getShortcut();
  110. if(shortcut != null)
  111. {
  112. g.setFont(acceleratorFont);
  113. g.setColor(getModel().isArmed() ?
  114. acceleratorSelectionForeground :
  115. acceleratorForeground);
  116. FontMetrics fm = g.getFontMetrics();
  117. Insets insets = getInsets();
  118. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  119. shortcut) + insets.right + insets.left + 5),
  120. getFont().getSize() + (insets.top - 1)
  121. /* XXX magic number */);
  122. }
  123. } //}}}
  124. //{{{ Private members
  125. private String shortcutProp;
  126. private char shortcut;
  127. private static Font acceleratorFont;
  128. private static Color acceleratorForeground;
  129. private static Color acceleratorSelectionForeground;
  130. //{{{ getShortcut() method
  131. private String getShortcut()
  132. {
  133. if(shortcut == '\0')
  134. return null;
  135. else
  136. {
  137. String shortcutPrefix = jEdit.getProperty(shortcutProp);
  138. if(shortcutPrefix == null)
  139. return null;
  140. else
  141. {
  142. return shortcutPrefix + " " + shortcut;
  143. }
  144. }
  145. } //}}}
  146. //{{{ Class initializer
  147. static
  148. {
  149. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  150. acceleratorFont = new Font("Monospaced",
  151. acceleratorFont.getStyle(),
  152. acceleratorFont.getSize());
  153. acceleratorForeground = UIManager
  154. .getColor("MenuItem.acceleratorForeground");
  155. acceleratorSelectionForeground = UIManager
  156. .getColor("MenuItem.acceleratorSelectionForeground");
  157. } //}}}
  158. //}}}
  159. } //}}}
  160. }