/jEdit/tags/jedit-4-3-pre5/macros/Files/Buffer_Switcher.bsh
Unknown | 261 lines | 223 code | 38 blank | 0 comment | 0 complexity | bc27c818ff0584e33f923765232c0528 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
- /*
- * 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 5450 2006-06-20 09:08:13Z vampire0 $
- */
- import javax.swing.border.EmptyBorder;
- /*
- * 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 + " Open Buffers", 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("[ENTER] Switch to; [SPACE] Switch to, keep dialog; [DEL] Close Buffer"), 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, "Help for Buffer Switcher macro:\n\n"
- + "DELETE closes selected buffer.\n"
- + "ENTER switches to selected buffer, closes dialog.\n"
- + "ESCAPE closes dialog.\n"
- + "SPACE switches to selected buffer, does not close dialog.\n\n"
- + "NOTE: This dialog will only be displayed once.");
- 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>
- */