/jEdit/tags/jedit-4-3-pre15/org/gjt/sp/jedit/gui/BufferSwitcher.java

# · Java · 113 lines · 75 code · 13 blank · 25 comment · 8 complexity · 4a2cc52676e647e16e666f220857e244 MD5 · raw file

  1. /*
  2. * BufferSwitcher.java - Status bar
  3. * Copyright (C) 2000, 2004 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.gui;
  20. import javax.swing.event.*;
  21. import javax.swing.*;
  22. import java.awt.event.*;
  23. import java.awt.*;
  24. import org.gjt.sp.jedit.*;
  25. import org.gjt.sp.jedit.bufferset.BufferSet;
  26. /** BufferSwitcher class
  27. @version $Id: BufferSwitcher.java 12725 2008-05-29 20:20:30Z kpouer $
  28. */
  29. public class BufferSwitcher extends JComboBox
  30. {
  31. // private members
  32. private EditPane editPane;
  33. private boolean updating;
  34. public BufferSwitcher(final EditPane editPane)
  35. {
  36. this.editPane = editPane;
  37. //setFont(new Font("Dialog",Font.BOLD,10));
  38. setRenderer(new BufferCellRenderer());
  39. setMaximumRowCount(jEdit.getIntegerProperty("bufferSwitcher.maxRowCount",10));
  40. addActionListener(new ActionHandler());
  41. addPopupMenuListener(new PopupMenuListener()
  42. {
  43. public void popupMenuWillBecomeVisible(
  44. PopupMenuEvent e) {}
  45. public void popupMenuWillBecomeInvisible(
  46. PopupMenuEvent e)
  47. {
  48. editPane.getTextArea().requestFocus();
  49. }
  50. public void popupMenuCanceled(PopupMenuEvent e)
  51. {
  52. editPane.getTextArea().requestFocus();
  53. }
  54. });
  55. }
  56. public void updateBufferList()
  57. {
  58. // if the buffer count becomes 0, then it is guaranteed to
  59. // become 1 very soon, so don't do anything in that case.
  60. BufferSet bufferSet = editPane.getBufferSet();
  61. if(bufferSet.size() == 0)
  62. return;
  63. updating = true;
  64. setMaximumRowCount(jEdit.getIntegerProperty("bufferSwitcher.maxRowCount",10));
  65. setModel(new DefaultComboBoxModel(bufferSet.getAllBuffers()));
  66. setSelectedItem(editPane.getBuffer());
  67. setToolTipText(editPane.getBuffer().getPath());
  68. updating = false;
  69. }
  70. class ActionHandler implements ActionListener
  71. {
  72. public void actionPerformed(ActionEvent evt)
  73. {
  74. if(!updating)
  75. {
  76. Buffer buffer = (Buffer)getSelectedItem();
  77. if(buffer != null)
  78. editPane.setBuffer(buffer);
  79. }
  80. }
  81. }
  82. class BufferCellRenderer extends DefaultListCellRenderer
  83. {
  84. public Component getListCellRendererComponent(
  85. JList list, Object value, int index,
  86. boolean isSelected, boolean cellHasFocus)
  87. {
  88. super.getListCellRendererComponent(list,value,index,
  89. isSelected,cellHasFocus);
  90. Buffer buffer = (Buffer)value;
  91. if(buffer == null)
  92. setIcon(null);
  93. else {
  94. setIcon(buffer.getIcon());
  95. setToolTipText(buffer.getPath());
  96. }
  97. return this;
  98. }
  99. }
  100. }