File: Buffer_Switcher.bsh

package info (click to toggle)
jedit 5.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,252 kB
  • ctags: 11,190
  • sloc: java: 98,480; xml: 94,070; makefile: 52; sh: 42; cpp: 6; python: 6
file content (269 lines) | stat: -rw-r--r-- 7,786 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * 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>

*/