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

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

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