/plugins/Tags/trunk/tags/TagStack.java

#
Java | 297 lines | 213 code | 37 blank | 47 comment | 12 complexity | dc639d8b1e37a6102a8e41c10fe2aa33 MD5 | raw file

✨ Summary
  1. /*
  2. * TagStack.java - part of the Tags plugin.
  3. *
  4. * Copyright (C) 2003 Ollie Rutherfurd (oliver@rutherfurd.net)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. * $Id: TagStack.java 7606 2004-11-07 15:52:36Z orutherfurd $
  21. */
  22. package tags;
  23. //{{{ imports
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.util.Vector;
  27. import javax.swing.*;
  28. import javax.swing.border.EtchedBorder;
  29. import javax.swing.event.*;
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.jedit.gui.*;
  32. import org.gjt.sp.util.Log;
  33. //}}}
  34. /**
  35. * Tag stack navigation window.
  36. * @author Ollie Rutherfurd (oliver@rutherfurd.net)
  37. * @version $Id: TagStack.java 7606 2004-11-07 15:52:36Z orutherfurd $
  38. */
  39. public class TagStack
  40. extends JPanel
  41. implements DefaultFocusComponent
  42. {
  43. //{{{ TagStack constructor
  44. public TagStack(View view)
  45. {
  46. super(new BorderLayout());
  47. this.view = view;
  48. listModel = TagsPlugin.getTagStack(view);
  49. list = new JList(listModel);
  50. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  51. list.setCellRenderer(new PositionCellRenderer());
  52. list.addMouseListener(new MouseHandler());
  53. add(new JScrollPane(list),BorderLayout.CENTER);
  54. } //}}}
  55. //{{{ focusOnDefaultComponent() method
  56. public void focusOnDefaultComponent()
  57. {
  58. list.requestFocus();
  59. } //}}}
  60. //{{{ private members
  61. private View view;
  62. private JList list;
  63. private TagStackModel listModel;
  64. private JPopupMenu popupMenu;
  65. private JMenuItem goTo = null;
  66. private JMenuItem remove = null;
  67. private JMenuItem pop = null;
  68. private JMenuItem clear = null;
  69. //}}}
  70. //{{{ MouseHandler class
  71. class MouseHandler extends MouseAdapter
  72. {
  73. public void mouseClicked(MouseEvent evt)
  74. {
  75. int index = list.locationToIndex(new Point(evt.getX(),
  76. evt.getY()));
  77. if(index > -1 && index != list.getSelectedIndex())
  78. list.setSelectedIndex(index);
  79. if(GUIUtilities.isPopupTrigger(evt))
  80. {
  81. showPopupMenu(evt);
  82. }
  83. else
  84. {
  85. if(evt.getClickCount() > 1)
  86. {
  87. StackPosition pos = (StackPosition)
  88. list.getSelectedValue();
  89. if(pos != null)
  90. pos.goTo(view);
  91. view.requestFocus();
  92. }
  93. }
  94. }
  95. } //}}}
  96. //{{{ showPopupMenu() method
  97. private void showPopupMenu(MouseEvent evt)
  98. {
  99. if(popupMenu == null)
  100. {
  101. popupMenu = new JPopupMenu();
  102. goTo = popupMenu.add(new GoToAction());
  103. remove = popupMenu.add(new RemoveAction());
  104. pop = popupMenu.add(new PopAction());
  105. clear = popupMenu.add(new ClearAction());
  106. }
  107. goTo.setEnabled(list.getSelectedValue() != null);
  108. remove.setEnabled(listModel.size() > 0);
  109. pop.setEnabled(listModel.size() > 0);
  110. clear.setEnabled(listModel.size() > 0);
  111. GUIUtilities.showPopupMenu(popupMenu, evt.getComponent(),
  112. evt.getX(), evt.getY());
  113. evt.consume();
  114. } //}}}
  115. //{{{ ClearAction class
  116. class ClearAction extends AbstractAction
  117. {
  118. public ClearAction()
  119. {
  120. super(jEdit.getProperty("tags.tagstack-clear"));
  121. }
  122. public void actionPerformed(ActionEvent evt)
  123. {
  124. listModel.clear();
  125. }
  126. } //}}}
  127. //{{{ GoToAction class
  128. class GoToAction extends AbstractAction
  129. {
  130. public GoToAction()
  131. {
  132. super(jEdit.getProperty("tags.tagstack-goto"));
  133. }
  134. public void actionPerformed(ActionEvent evt)
  135. {
  136. StackPosition pos = null;
  137. pos = (StackPosition)list.getSelectedValue();
  138. pos.goTo(view);
  139. }
  140. } //}}}
  141. //{{{ PopAction class
  142. class PopAction extends AbstractAction
  143. {
  144. public PopAction()
  145. {
  146. super(jEdit.getProperty("tags.tagstack-pop"));
  147. }
  148. public void actionPerformed(ActionEvent evt)
  149. {
  150. TagsPlugin.popPosition(view);
  151. }
  152. } //}}}
  153. //{{{ RemoveAction class
  154. class RemoveAction extends AbstractAction
  155. {
  156. public RemoveAction()
  157. {
  158. super(jEdit.getProperty("tags.tagstack-remove"));
  159. }
  160. public void actionPerformed(ActionEvent evt)
  161. {
  162. StackPosition pos = null;
  163. pos = (StackPosition)list.getSelectedValue();
  164. if(pos != null)
  165. listModel.removeElement(pos);
  166. else
  167. Toolkit.getDefaultToolkit().beep();
  168. }
  169. } //}}}
  170. }
  171. class PositionCellRenderer
  172. extends JPanel
  173. implements ListCellRenderer
  174. {
  175. // XXX should be able to decide whether to have
  176. // before & after lines
  177. //{{{ PositionCellRenderer constructor
  178. PositionCellRenderer()
  179. {
  180. super();
  181. setBorder(new EtchedBorder());
  182. lineNumber = new JLabel();
  183. lineBefore = new JLabel();
  184. line = new JLabel();
  185. lineAfter = new JLabel();
  186. file = new JLabel();
  187. directory = new JLabel();
  188. // XXX use props
  189. Font plain = new Font("Monospaced", Font.PLAIN, 12);
  190. Font bold = new Font("Monospaced", Font.BOLD, 12);
  191. lineNumber.setFont(plain);
  192. file.setFont(plain);
  193. directory.setFont(plain);
  194. lineBefore.setFont(plain);
  195. line.setFont(bold);
  196. lineAfter.setFont(plain);
  197. filePanel = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
  198. filePanel.add(lineNumber);
  199. filePanel.add(file);
  200. filePanel.add(directory);
  201. linesPanel = new JPanel(new GridLayout(3,1));
  202. linesPanel.add(lineBefore);
  203. linesPanel.add(line);
  204. linesPanel.add(lineAfter);
  205. setLayout(new BorderLayout());
  206. add(BorderLayout.NORTH,filePanel);
  207. add(BorderLayout.CENTER,linesPanel);
  208. } //}}}
  209. //{{{ getListCellRendererComponent() method
  210. public Component getListCellRendererComponent(JList list,
  211. Object value, int index, boolean isSelected,
  212. boolean cellHasFocus)
  213. {
  214. StackPosition p = (StackPosition)value;
  215. StringBuffer b = new StringBuffer("" + p.getLineNumber());
  216. // hackish way to add a fixed number of spaces before filename
  217. while(b.length() < 6)
  218. b.append(" ");
  219. lineNumber.setText(b.toString());
  220. file.setText(p.getName());
  221. directory.setText(" (" + p.getDirectory() + ")");
  222. lineBefore.setText(p.getLineBefore() + " ");
  223. line.setText(p.getLine() + " ");
  224. lineAfter.setText(p.getLineAfter() + " ");
  225. Color background = (isSelected ? list.getSelectionBackground()
  226. : list.getBackground());
  227. Color foreground = (isSelected ? list.getSelectionForeground()
  228. : list.getForeground());
  229. setBackground(background);
  230. filePanel.setBackground(background);
  231. lineNumber.setBackground(background);
  232. file.setBackground(background);
  233. directory.setBackground(background);
  234. linesPanel.setBackground(background);
  235. lineBefore.setBackground(background);
  236. line.setBackground(background);
  237. lineAfter.setBackground(background);
  238. setForeground(foreground);
  239. filePanel.setForeground(foreground);
  240. lineNumber.setForeground(Color.BLUE);
  241. file.setForeground(Color.BLUE);
  242. directory.setForeground(Color.BLUE);
  243. linesPanel.setForeground(foreground);
  244. lineBefore.setForeground(foreground);
  245. line.setForeground(foreground);
  246. lineAfter.setForeground(foreground);
  247. return this;
  248. } //}}}
  249. //{{{ private members
  250. private JPanel filePanel;
  251. private JPanel linesPanel;
  252. private JLabel lineNumber;
  253. private JLabel file;
  254. private JLabel directory;
  255. private JLabel lineBefore;
  256. private JLabel line;
  257. private JLabel lineAfter;
  258. //}}}
  259. }
  260. // :collapseFolds=1:noTabs=false:tabSize=4:indentSize=4:deepIndent=false:folding=explicit: