PageRenderTime 74ms CodeModel.GetById 64ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 103 lines | 70 code | 11 blank | 22 comment | 7 complexity | dfc428cce4d4669944f19299aa9ecb38 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
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.event.*;
 23import javax.swing.*;
 24import java.awt.event.*;
 25import java.awt.*;
 26import org.gjt.sp.jedit.*;
 27
 28public class BufferSwitcher extends JComboBox
 29{
 30	public BufferSwitcher(final EditPane editPane)
 31	{
 32		this.editPane = editPane;
 33
 34		//setFont(new Font("Dialog",Font.BOLD,10));
 35		setRenderer(new BufferCellRenderer());
 36		setMaximumRowCount(jEdit.getIntegerProperty("bufferSwitcher.maxRowCount",10));
 37		addActionListener(new ActionHandler());
 38		addPopupMenuListener(new PopupMenuListener()
 39		{
 40			public void popupMenuWillBecomeVisible(
 41				PopupMenuEvent e) {}
 42
 43			public void popupMenuWillBecomeInvisible(
 44				PopupMenuEvent e)
 45			{
 46				editPane.getTextArea().requestFocus();
 47			}
 48
 49			public void popupMenuCanceled(PopupMenuEvent e)
 50			{
 51				editPane.getTextArea().requestFocus();
 52			}
 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		if(jEdit.getBufferCount() == 0)
 61			return;
 62
 63		updating = true;
 64		setMaximumRowCount(jEdit.getIntegerProperty("bufferSwitcher.maxRowCount",10));
 65		setModel(new DefaultComboBoxModel(jEdit.getBuffers()));
 66		setSelectedItem(editPane.getBuffer());
 67		updating = false;
 68	}
 69
 70	// private members
 71	private EditPane editPane;
 72	private boolean updating;
 73
 74	class ActionHandler implements ActionListener
 75	{
 76		public void actionPerformed(ActionEvent evt)
 77		{
 78			if(!updating)
 79			{
 80				Buffer buffer = (Buffer)getSelectedItem();
 81				if(buffer != null)
 82					editPane.setBuffer(buffer);
 83			}
 84		}
 85	}
 86
 87	class BufferCellRenderer extends DefaultListCellRenderer
 88	{
 89		public Component getListCellRendererComponent(
 90			JList list, Object value, int index,
 91			boolean isSelected, boolean cellHasFocus)
 92		{
 93			super.getListCellRendererComponent(list,value,index,
 94				isSelected,cellHasFocus);
 95			Buffer buffer = (Buffer)value;
 96			if(buffer == null)
 97				setIcon(null);
 98			else
 99				setIcon(buffer.getIcon());
100			return this;
101		}
102	}
103}