PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Files/Buffer_Switcher.bsh

#
Unknown | 261 lines | 223 code | 38 blank | 0 comment | 0 complexity | 9d6208ade84732803b654b7d270f3045 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
  1. /*
  2. * Buffer_Switcher.bsh - a BeanShell macro script for displaying
  3. * and switching between open buffers.
  4. *
  5. * Copyright (C) 2001-2004 Ollie Rutherfurd, oliver@rutherfurd.net
  6. *
  7. * :mode=beanshell:tabSize=4:indentSize=4:maxLineLen=0:noTabs=false:
  8. * :indentOnTab=true:indentOnEnter=true:folding=explicit:collapseFolds=1:
  9. *
  10. * {{{ License
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with the jEdit program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. * }}}
  25. *
  26. * Notes:
  27. *
  28. * This is very similar to the functionality provided by the BufferList
  29. * plugin but, that is overkill for me. I just want a way I can
  30. * see and switch between buffers without having to use the mouse
  31. * to go to the BufferSwitcher widget in the EditPane.
  32. *
  33. * DELETE: closes the selected buffer but not the dialog.
  34. * ENTER: switches to the selected buffer and closes the dialog.
  35. * ESCAPE: closes the dialog.
  36. * SPACE: switches to the selected buffer but does not close the dialog.
  37. *
  38. * $Id: Buffer_Switcher.bsh 4995 2004-03-19 15:58:00Z spestov $
  39. */
  40. import javax.swing.border.EmptyBorder;
  41. /*
  42. * Custom Cell Renderer which displays the buffer icons in the JList.
  43. * As it runs a little slowly on my machine, it is not used by default.
  44. *
  45. * set useCustomCellRenderer to true at the bottom of this macro to use this.
  46. */
  47. // {{{ BufferCellRenderer
  48. BufferCellRenderer()
  49. {
  50. l = new JLabel();
  51. l.setOpaque(true);
  52. Component getListCellRendererComponent(
  53. JList list,
  54. Object value,
  55. int index,
  56. boolean isSelected,
  57. boolean cellHasFocus)
  58. {
  59. l.setText(value.toString());
  60. l.setIcon(value.getIcon());
  61. if (isSelected)
  62. {
  63. l.setBackground(list.getSelectionBackground());
  64. l.setForeground(list.getSelectionForeground());
  65. }
  66. else
  67. {
  68. l.setBackground(list.getBackground());
  69. l.setForeground(list.getForeground());
  70. }
  71. return l;
  72. }
  73. return this;
  74. } // }}}
  75. /*
  76. * BufferSwitcherDialog - Dialog allows one to switch between
  77. * open buffers (and/or close open buffers).
  78. */
  79. BufferSwitcherDialog(doModal, useCustomCellRenderer)
  80. {
  81. // {{{ create dialog
  82. Buffer[] openBuffers = jEdit.getBuffers();
  83. int numOpen = openBuffers.length;
  84. dialog = new JDialog(view, "" + numOpen + " Open Buffers", doModal);
  85. content = new JPanel(new BorderLayout());
  86. content.setBorder(new EmptyBorder(12,12,12,12));
  87. dialog.setContentPane(content);
  88. bufferList = new JList(openBuffers);
  89. bufferList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  90. if(useCustomCellRenderer)
  91. bufferList.setCellRenderer(BufferCellRenderer());
  92. content.add(new JScrollPane(bufferList), BorderLayout.CENTER);
  93. content.add(new JLabel("[ENTER] Switch to; [SPACE] Switch to, keep dialog; [DEL] Close Buffer"), BorderLayout.NORTH);
  94. buttonPanel = new JPanel();
  95. buttonPanel.setLayout(new BoxLayout(buttonPanel,
  96. BoxLayout.X_AXIS));
  97. buttonPanel.setBorder(new EmptyBorder(12,50,0,50));
  98. buttonPanel.add(Box.createGlue());
  99. ok = new JButton("OK");
  100. close = new JButton("Close");
  101. ok.setPreferredSize(close.getPreferredSize());
  102. dialog.getRootPane().setDefaultButton(ok);
  103. buttonPanel.add(ok);
  104. buttonPanel.add(Box.createHorizontalStrut(6));
  105. buttonPanel.add(close);
  106. buttonPanel.add(Box.createGlue());
  107. content.add(buttonPanel, BorderLayout.SOUTH);
  108. // }}}
  109. // {{{ switchBuffer()
  110. void switchBuffer()
  111. {
  112. _buffer = bufferList.getSelectedValue();
  113. if(_buffer == null)
  114. view.getToolkit().beep();
  115. else
  116. view.getEditPane().setBuffer(_buffer);
  117. } // }}}
  118. // {{{ closeBuffer()
  119. void closeBuffer()
  120. {
  121. index = bufferList.getSelectedIndex();
  122. _buffer = bufferList.getSelectedValue();
  123. if(_buffer == null)
  124. view.getToolkit().beep();
  125. else
  126. if(jEdit.closeBuffer(view, _buffer))
  127. {
  128. bufferList.setListData(jEdit.getBuffers());
  129. if(index == jEdit.getBufferCount())
  130. index--;
  131. bufferList.setSelectedIndex(index);
  132. }
  133. numOpen--;
  134. dialog.setTitle("" + numOpen + " Open Buffers");
  135. } // }}}
  136. // {{{ actionPerformed
  137. void actionPerformed(evt)
  138. {
  139. if(evt.getSource() == ok)
  140. switchBuffer();
  141. dialog.dispose();
  142. dialog = null;
  143. } // }}}
  144. // {{{ KeyListener implementation
  145. void keyPressed(evt)
  146. {
  147. if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  148. dialog.dispose();
  149. else if(evt.getKeyCode() == KeyEvent.VK_SPACE)
  150. switchBuffer();
  151. else if(evt.getKeyCode() == KeyEvent.VK_DELETE)
  152. {
  153. evt.consume();
  154. closeBuffer();
  155. }
  156. }
  157. void keyReleased(evt)
  158. {
  159. int selected = bufferList.getSelectedIndex();
  160. if(selected > -1)
  161. bufferList.ensureIndexIsVisible(selected);
  162. }
  163. void keyTyped(evt){}
  164. // }}}
  165. // {{{ MouseListener implementation
  166. void mouseClicked(evt)
  167. {
  168. if(evt.getClickCount() > 1)
  169. {
  170. switchBuffer();
  171. dialog.dispose();
  172. }
  173. }
  174. void mouseEntered(evt){}
  175. void mouseExited(evt){}
  176. void mousePressed(evt){}
  177. void mouseReleased(evt){}
  178. // }}}
  179. // {{{ add listeners
  180. dialog.addKeyListener(this);
  181. bufferList.addKeyListener(this);
  182. bufferList.addMouseListener(this);
  183. ok.addActionListener(this);
  184. close.addActionListener(this);
  185. // }}}
  186. // {{{ display dialog
  187. dialog.pack();
  188. bufferList.setSelectedValue(buffer,true);
  189. dialog.setLocationRelativeTo(view);
  190. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  191. dialog.setVisible(true);
  192. // }}}
  193. }
  194. // SETTINGS {{{
  195. doModal = true;
  196. // whether or not to use custom CellRenderer
  197. useCustomCellRenderer = true;
  198. // show help for keys on first run
  199. showHelp = jEdit.getProperty("macro.buffer_switcher.display-help","1");
  200. if(showHelp.equals("1"))
  201. {
  202. Macros.message(view, "Help for Buffer Switcher macro:\n\n"
  203. + "DELETE closes selected buffer.\n"
  204. + "ENTER switches to selected buffer, closes dialog.\n"
  205. + "ESCAPE closes dialog.\n"
  206. + "SPACE switches to selected buffer, does not close dialog.\n\n"
  207. + "NOTE: This dialog will only be displayed once.");
  208. jEdit.setProperty("macro.buffer_switcher.display-help","0");
  209. }
  210. // }}}
  211. BufferSwitcherDialog(doModal, useCustomCellRenderer);
  212. /*
  213. Macro index data (in DocBook format)
  214. <listitem>
  215. <para><filename>Buffer_Switcher</filename></para>
  216. Displays a modal dialog listing all open buffers,
  217. allowing one to switch to and/or close buffers.
  218. <keycap>ENTER</keycap> switches to a buffer and closes the dialog,
  219. <keycap>DELETE</keycap> closes a buffer, <keycap>SPACE</keycap>
  220. switches to a buffer but does not close the dialog.
  221. </para></abstract>
  222. </listitem>
  223. */