/*
 * Buffer_Switcher.bsh - a BeanShell macro script for displaying
 * and switching between open buffers.
 *
 * Copyright (C) 2001-2004 Ollie Rutherfurd, oliver@rutherfurd.net
 *
 * :mode=beanshell:tabSize=4:indentSize=4:maxLineLen=0:noTabs=false:
 * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
 *
 * {{{ License
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the jEdit program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * }}}
 *
 * Notes:
 *
 * This is very similar to the functionality provided by the BufferList
 * plugin but, that is overkill for me.  I just want a way I can
 * see and switch between buffers without having to use the mouse
 * to go to the BufferSwitcher widget in the EditPane.
 *
 * DELETE: closes the selected buffer but not the dialog.
 * ENTER: switches to the selected buffer and closes the dialog.
 * ESCAPE: closes the dialog.
 * SPACE: switches to the selected buffer but does not close the dialog.
 *
 * $Id: Buffer_Switcher.bsh 21769 2012-06-07 15:09:15Z k_satoda $
 */

import javax.swing.border.EmptyBorder;

//Localization
final static String OpenBuffersLabel   = jEdit.getProperty("macro.rs.BufferSwitcher.OpenBuffers.label",  "open Buffers");
final static String QuickHelpLabel   = jEdit.getProperty("macro.rs.BufferSwitcher.QuickHelp.label",  "[ENTER] Switch to; [SPACE] Switch to, keep dialog; [DEL] Close Buffer");
final static String Help1Label   = jEdit.getProperty("macro.rs.BufferSwitcher.Help1.label",  "Help for Buffer Switcher macro:");
final static String Help2Label   = jEdit.getProperty("macro.rs.BufferSwitcher.Help2.label",  "DELETE closes selected buffer.");
final static String Help3Label   = jEdit.getProperty("macro.rs.BufferSwitcher.Help3.label",  "ENTER switches to selected buffer, closes dialog.");
final static String Help4Label   = jEdit.getProperty("macro.rs.BufferSwitcher.Help4.label",  "ESCAPE closes dialog.");
final static String Help5Label   = jEdit.getProperty("macro.rs.BufferSwitcher.Help5.label",  "SPACE switches to selected buffer, does not close dialog.");
final static String Help6Label   = jEdit.getProperty("macro.rs.BufferSwitcher.Help6.label",  "NOTE: This dialog will only be displayed once");

/*
* Custom Cell Renderer which displays the buffer icons in the JList.
* As it runs a little slowly on my machine, it is not used by default.
*
* set useCustomCellRenderer to true at the bottom of this macro to use this.
*/
// {{{ BufferCellRenderer
BufferCellRenderer()
{

	l = new JLabel();
	l.setOpaque(true);

	Component getListCellRendererComponent(
		JList list,
		Object value,
		int index,
		boolean isSelected,
		boolean cellHasFocus)
	{
		l.setText(value.toString());
		l.setIcon(value.getIcon());

		if (isSelected)
		{
			l.setBackground(list.getSelectionBackground());
			l.setForeground(list.getSelectionForeground());
		}
		else
		{
			l.setBackground(list.getBackground());
			l.setForeground(list.getForeground());
		}

		return l;

	}

	return this;
}	// }}}

/*
* BufferSwitcherDialog - Dialog allows one to switch between
* open buffers (and/or close open buffers).
*/
BufferSwitcherDialog(doModal, useCustomCellRenderer)
{

	// {{{ create dialog
	Buffer[] openBuffers = jEdit.getBuffers();
	int numOpen = openBuffers.length;
	dialog = new JDialog(view, numOpen + " " + OpenBuffersLabel, doModal);
	content = new JPanel(new BorderLayout());
	content.setBorder(new EmptyBorder(12,12,12,12));
	dialog.setContentPane(content);

	bufferList = new JList(openBuffers);
	bufferList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	if(useCustomCellRenderer)
		bufferList.setCellRenderer(BufferCellRenderer());
	content.add(new JScrollPane(bufferList), BorderLayout.CENTER);

	content.add(new JLabel(QuickHelpLabel), BorderLayout.NORTH);

	buttonPanel = new JPanel();
	buttonPanel.setLayout(new BoxLayout(buttonPanel, 
		BoxLayout.X_AXIS));

	buttonPanel.setBorder(new EmptyBorder(12,50,0,50));
	buttonPanel.add(Box.createGlue());
	ok = new JButton("OK");
	close = new JButton("Close");
	ok.setPreferredSize(close.getPreferredSize());
	dialog.getRootPane().setDefaultButton(ok);
	buttonPanel.add(ok);
	buttonPanel.add(Box.createHorizontalStrut(6));
	buttonPanel.add(close);
	buttonPanel.add(Box.createGlue());
	content.add(buttonPanel, BorderLayout.SOUTH);
	// }}}

	// {{{ switchBuffer()
	void switchBuffer()
	{
		_buffer = bufferList.getSelectedValue();
		if(_buffer == null)
			view.getToolkit().beep();
		else
			view.getEditPane().setBuffer(_buffer);
	}	// }}}

	// {{{ closeBuffer()
	void closeBuffer()
	{
		index = bufferList.getSelectedIndex();
		_buffer = bufferList.getSelectedValue();
		if(_buffer == null)
			view.getToolkit().beep();
		else
			if(jEdit.closeBuffer(view, _buffer))
			{
				bufferList.setListData(jEdit.getBuffers());
				if(index == jEdit.getBufferCount())
					index--;
				bufferList.setSelectedIndex(index);
			}
		numOpen--;
		dialog.setTitle("" + numOpen + " Open Buffers");
	}	// }}}

	// {{{ actionPerformed
	void actionPerformed(evt)
	{
		if(evt.getSource() == ok)
			switchBuffer();
		dialog.dispose();
		dialog = null;
	}	// }}}

	// {{{ KeyListener implementation
	void keyPressed(evt)
	{
		if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
			dialog.dispose();
		else if(evt.getKeyCode() == KeyEvent.VK_SPACE)
			switchBuffer();
		else if(evt.getKeyCode() == KeyEvent.VK_DELETE)
		{
			evt.consume();
			closeBuffer();
		}
	}

	void keyReleased(evt)
	{
		int selected = bufferList.getSelectedIndex();
		if(selected > -1)
			bufferList.ensureIndexIsVisible(selected);
	}

	void keyTyped(evt){}
	// }}}

	// {{{ MouseListener implementation
	void mouseClicked(evt)
	{
		if(evt.getClickCount() > 1)
		{
			switchBuffer();
			dialog.dispose();
		}
	}

	void mouseEntered(evt){}
	void mouseExited(evt){}
	void mousePressed(evt){}
	void mouseReleased(evt){}
	// }}}

	// {{{ add listeners
	dialog.addKeyListener(this);
	bufferList.addKeyListener(this);
	bufferList.addMouseListener(this);
	ok.addActionListener(this);
	close.addActionListener(this);
	// }}}

	// {{{ display dialog
	dialog.pack();
	bufferList.setSelectedValue(buffer,true);
	dialog.setLocationRelativeTo(view);
	dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	dialog.setVisible(true);
	// }}}

}


// SETTINGS {{{
doModal = true;

// whether or not to use custom CellRenderer
useCustomCellRenderer = true;

// show help for keys on first run
showHelp = jEdit.getProperty("macro.buffer_switcher.display-help","1");
if(showHelp.equals("1"))
{
	Macros.message(view, Help1Label + "\n\n" 
		+ Help2Label + "\n"
		+ Help3Label + "\n"
		+ Help4Label + "\n"
		+ Help5Label + "\n\n"
		+ Help6Label);
	jEdit.setProperty("macro.buffer_switcher.display-help","0");
}
// }}}


BufferSwitcherDialog(doModal, useCustomCellRenderer);


/*

Macro index data (in DocBook format)

	<listitem>
		<para><filename>Buffer_Switcher</filename></para>
		Displays a modal dialog listing all open buffers,
		allowing one to switch to and/or close buffers.
		<keycap>ENTER</keycap> switches to a buffer and closes the dialog, 
		<keycap>DELETE</keycap> closes a buffer, <keycap>SPACE</keycap> 
		switches to a buffer but does not close the dialog.
		</para></abstract>
	</listitem>

*/
