PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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 5450 2006-06-20 09:08:13Z vampire0 $
 39 */
 40
 41import javax.swing.border.EmptyBorder;
 42
 43
 44/*
 45* Custom Cell Renderer which displays the buffer icons in the JList.
 46* As it runs a little slowly on my machine, it is not used by default.
 47*
 48* set useCustomCellRenderer to true at the bottom of this macro to use this.
 49*/
 50// {{{ BufferCellRenderer
 51BufferCellRenderer()
 52{
 53
 54	l = new JLabel();
 55	l.setOpaque(true);
 56
 57	Component getListCellRendererComponent(
 58		JList list,
 59		Object value,
 60		int index,
 61		boolean isSelected,
 62		boolean cellHasFocus)
 63	{
 64		l.setText(value.toString());
 65		l.setIcon(value.getIcon());
 66
 67		if (isSelected)
 68		{
 69			l.setBackground(list.getSelectionBackground());
 70			l.setForeground(list.getSelectionForeground());
 71		}
 72		else
 73		{
 74			l.setBackground(list.getBackground());
 75			l.setForeground(list.getForeground());
 76		}
 77
 78		return l;
 79
 80	}
 81
 82	return this;
 83}	// }}}
 84
 85
 86/*
 87* BufferSwitcherDialog - Dialog allows one to switch between
 88* open buffers (and/or close open buffers).
 89*/
 90BufferSwitcherDialog(doModal, useCustomCellRenderer)
 91{
 92
 93	// {{{ create dialog
 94	Buffer[] openBuffers = jEdit.getBuffers();
 95	int numOpen = openBuffers.length;
 96	dialog = new JDialog(view, "" + numOpen + " Open Buffers", doModal);
 97	content = new JPanel(new BorderLayout());
 98	content.setBorder(new EmptyBorder(12,12,12,12));
 99	dialog.setContentPane(content);
100
101	bufferList = new JList(openBuffers);
102	bufferList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
103	if(useCustomCellRenderer)
104		bufferList.setCellRenderer(BufferCellRenderer());
105	content.add(new JScrollPane(bufferList), BorderLayout.CENTER);
106
107	content.add(new JLabel("[ENTER] Switch to; [SPACE] Switch to, keep dialog; [DEL] Close Buffer"), BorderLayout.NORTH);
108
109	buttonPanel = new JPanel();
110	buttonPanel.setLayout(new BoxLayout(buttonPanel, 
111		BoxLayout.X_AXIS));
112
113	buttonPanel.setBorder(new EmptyBorder(12,50,0,50));
114	buttonPanel.add(Box.createGlue());
115	ok = new JButton("OK");
116	close = new JButton("Close");
117	ok.setPreferredSize(close.getPreferredSize());
118	dialog.getRootPane().setDefaultButton(ok);
119	buttonPanel.add(ok);
120	buttonPanel.add(Box.createHorizontalStrut(6));
121	buttonPanel.add(close);
122	buttonPanel.add(Box.createGlue());
123	content.add(buttonPanel, BorderLayout.SOUTH);
124	// }}}
125
126	// {{{ switchBuffer()
127	void switchBuffer()
128	{
129		_buffer = bufferList.getSelectedValue();
130		if(_buffer == null)
131			view.getToolkit().beep();
132		else
133			view.getEditPane().setBuffer(_buffer);
134	}	// }}}
135
136	// {{{ closeBuffer()
137	void closeBuffer()
138	{
139		index = bufferList.getSelectedIndex();
140		_buffer = bufferList.getSelectedValue();
141		if(_buffer == null)
142			view.getToolkit().beep();
143		else
144			if(jEdit.closeBuffer(view, _buffer))
145			{
146				bufferList.setListData(jEdit.getBuffers());
147				if(index == jEdit.getBufferCount())
148					index--;
149				bufferList.setSelectedIndex(index);
150			}
151		numOpen--;
152		dialog.setTitle("" + numOpen + " Open Buffers");
153	}	// }}}
154
155	// {{{ actionPerformed
156	void actionPerformed(evt)
157	{
158		if(evt.getSource() == ok)
159			switchBuffer();
160		dialog.dispose();
161		dialog = null;
162	}	// }}}
163
164	// {{{ KeyListener implementation
165	void keyPressed(evt)
166	{
167		if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
168			dialog.dispose();
169		else if(evt.getKeyCode() == KeyEvent.VK_SPACE)
170			switchBuffer();
171		else if(evt.getKeyCode() == KeyEvent.VK_DELETE)
172		{
173			evt.consume();
174			closeBuffer();
175		}
176	}
177
178	void keyReleased(evt)
179	{
180		int selected = bufferList.getSelectedIndex();
181		if(selected > -1)
182			bufferList.ensureIndexIsVisible(selected);
183	}
184
185	void keyTyped(evt){}
186	// }}}
187
188	// {{{ MouseListener implementation
189	void mouseClicked(evt)
190	{
191		if(evt.getClickCount() > 1)
192		{
193			switchBuffer();
194			dialog.dispose();
195		}
196	}
197
198	void mouseEntered(evt){}
199	void mouseExited(evt){}
200	void mousePressed(evt){}
201	void mouseReleased(evt){}
202	// }}}
203
204	// {{{ add listeners
205	dialog.addKeyListener(this);
206	bufferList.addKeyListener(this);
207	bufferList.addMouseListener(this);
208	ok.addActionListener(this);
209	close.addActionListener(this);
210	// }}}
211
212	// {{{ display dialog
213	dialog.pack();
214	bufferList.setSelectedValue(buffer,true);
215	dialog.setLocationRelativeTo(view);
216	dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
217	dialog.setVisible(true);
218	// }}}
219
220}
221
222
223// SETTINGS {{{
224doModal = true;
225
226// whether or not to use custom CellRenderer
227useCustomCellRenderer = true;
228
229// show help for keys on first run
230showHelp = jEdit.getProperty("macro.buffer_switcher.display-help","1");
231if(showHelp.equals("1"))
232{
233	Macros.message(view, "Help for Buffer Switcher macro:\n\n" 
234		+ "DELETE closes selected buffer.\n" 
235		+ "ENTER switches to selected buffer, closes dialog.\n" 
236		+ "ESCAPE closes dialog.\n"
237		+ "SPACE switches to selected buffer, does not close dialog.\n\n"
238		+ "NOTE: This dialog will only be displayed once.");
239	jEdit.setProperty("macro.buffer_switcher.display-help","0");
240}
241// }}}
242
243
244BufferSwitcherDialog(doModal, useCustomCellRenderer);
245
246
247/*
248
249Macro index data (in DocBook format)
250
251	<listitem>
252		<para><filename>Buffer_Switcher</filename></para>
253		Displays a modal dialog listing all open buffers,
254		allowing one to switch to and/or close buffers.
255		<keycap>ENTER</keycap> switches to a buffer and closes the dialog, 
256		<keycap>DELETE</keycap> closes a buffer, <keycap>SPACE</keycap> 
257		switches to a buffer but does not close the dialog.
258		</para></abstract>
259	</listitem>
260
261*/