/* * Buffer_Switcher.bsh - a BeanShell macro script for displaying * and switching between open buffers. * Copyright (C) 2001 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 28 2003-03-25 22:40:28Z oliver $ */ 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 dialog = new JDialog(view, "Open Buffers", doModal); content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(12,12,12,12)); dialog.setContentPane(content); bufferList = new JList(jEdit.getBuffers()); 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); } } // }}} // {{{ 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 = false; // 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) Buffer_Switcher Displays a modal dialog with all the open buffers, allowing one to switch to and/or close buffers. ENTER switches to a buffer and closes the dialog, DELETE closes a buffer, SPACE switches to a buffer but does not close the dialog. */