PageRenderTime 2321ms CodeModel.GetById 40ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 2239ms

/jEdit/tags/jedit-4-2-pre14/macros/Editing/Mode_Switcher.bsh

#
Unknown | 187 lines | 178 code | 9 blank | 0 comment | 0 complexity | 931a1f05811a08fe981e7ac31c0bc89b MD5 | raw file
  1/*
  2 * ModeSwitcher.bsh - a BeanShell macro script for changing the current
  3 * buffer's edit mode.
  4 *
  5 * Copyright (C) 2004 Nicholas O'Leary nol@deferential.net
  6 * 
  7 * :mode=beanshell:tabSize=3:indentSize=3: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 *  There are two other ways to change the buffers mode:
 28 *    - enter 'buffer.mode=[mode]' in the action bar
 29 *    - change it in the Buffer Options dialog
 30 *  Whilst both of these do the job, I wanted a way to achieve it with minimum
 31 *  effort, and keypresses.
 32 *  It also has the benefit of auto-completion of mode names.
 33 * 
 34 * $Id: Mode_Switcher.bsh 5028 2004-04-28 21:15:29Z spestov $
 35 */
 36
 37import javax.swing.border.EmptyBorder;
 38
 39Mode[] modes = jEdit.getModes();
 40JDialog dialog = new JDialog(view, "Buffer Mode Switcher", true);
 41dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 42JPanel content = new JPanel(new BorderLayout());
 43content.setBorder(new EmptyBorder(12,12,12,12));
 44Mode[] modes = jEdit.getModes();
 45String[] names = new String[modes.length];
 46for(int i=0;i<modes.length;i++) {
 47	names[i] = modes[i].getName();
 48}
 49Arrays.sort(names);
 50JTextField textfield = new JTextField() {
 51	protected String[] names;
 52	protected boolean shifted = false;
 53	//{{{ setNames
 54	public void setNames(String[] a){
 55		names = a;
 56	} //}}}
 57	public boolean getFocusTraversalKeysEnabled(){return false;}
 58	//{{{ processKeyEvent
 59	protected void processKeyEvent(KeyEvent evt)
 60	{
 61		if (evt.getID() == KeyEvent.KEY_RELEASED) {
 62			if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
 63				shifted = false;
 64			}
 65		} else if(evt.getID() == KeyEvent.KEY_PRESSED) {
 66			if (evt.getKeyCode() == KeyEvent.VK_SHIFT) {
 67				shifted = true;
 68			} else if(evt.getKeyCode() == KeyEvent.VK_TAB) {
 69				// Get the current text
 70				String txt = getText();
 71				String original = txt;
 72				// See if some text is selected
 73				if (getSelectedText() != null)
 74					txt = txt.substring(0,txt.length()-getSelectedText().length());
 75				// txt represents the unhighlighted text in the box. This is used
 76				// to find further matches.
 77				
 78				// See if the current text is a known mode
 79				int index = Arrays.binarySearch(names,original);
 80				if (index < 0) index = 0;
 81				int indexStep = 1;
 82				if (shifted) indexStep = -1;
 83				index+=indexStep;
 84				if (index == names.length) index = 0;
 85				if (index < 0) index = names.length-1;
 86				int match = -1;
 87				boolean foundExact = false;
 88				boolean keepLooping = true;
 89				// Loop through modes, starting at current+1
 90				int i = index;
 91				while(keepLooping) {
 92					// Skip if the mode name is shorter than the current text
 93					if (names[i].length()>=txt.length())
 94					{
 95						// If the mode matches, escape
 96						if (names[i].substring(0,txt.length()).equals(txt)) {
 97							match = i;
 98							break;
 99						}
100					}
101					// Loop the loop
102					i+=indexStep;
103					if (i == names.length) i = 0;
104					if (i < 0) i = names.length-1;
105					if (i==index) break;
106				}
107				// If a match has been found...
108				if (match >= 0) {
109					setText(names[match]);
110					setSelectionStart(txt.length());
111					setSelectionEnd(names[match].length());
112				}
113				return;
114			}
115		}
116		super.processKeyEvent(evt);
117	} //}}}
118};
119textfield.setColumns(20);
120textfield.setNames(names);
121Mode m = buffer.getMode();
122// Set the inital text to the current mode, and highlight it, so a key
123// press will clear the entry.
124if (m != null) {
125	textfield.setText(m.getName());
126	textfield.setSelectionStart(0);
127	textfield.setSelectionEnd(m.getName().length());
128}
129content.add(new JLabel("Enter buffer mode:"), BorderLayout.NORTH);
130content.add(textfield, BorderLayout.CENTER);
131Vector v = new Vector();
132
133// KeyListener Interface
134//{{{ keyPressed
135void keyPressed(evt)
136{
137	if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
138		dialog.dispose();
139	else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
140	{
141		Mode m = jEdit.getMode(textfield.getText());
142		if (m!=null)
143		{
144			buffer.setMode(m);
145			Log.log(Log.NOTICE,
146					  BeanShell.class,
147					  "Changing mode of buffer ["+
148						  buffer.getName()+"] to ["+
149						  m.getName()+"]");
150		} else {
151			Log.log(Log.WARNING,
152					  BeanShell.class,
153					  "Mode ["+textfield.getText()+"] not found");
154		}
155		evt.consume();
156		dialog.dispose();
157	}
158}//}}}
159void keyReleased(evt) {}
160void keyTyped(evt) {}
161
162dialog.addKeyListener(this);
163textfield.addKeyListener(this);
164dialog.setContentPane(content);
165dialog.pack();
166dialog.setLocationRelativeTo(view);
167dialog.setVisible(true);
168
169/*
170
171Macro index data (in DocBook format)
172
173   <listitem>
174      <para><filename>Mode_Switcher.bsh</filename></para>
175      Displays a modal dialog with the current buffer's mode in a text field,
176      allowing one to change the mode by typing in its name.
177      <keycap>ENTER</keycap> selects the current mode; if the text is not a 
178      valid mode, the dialog still dismisses, but a warning is logged to the
179      activity log.
180      <keycap>ESACPE</keycap> closes the dialog with no further action.
181      <keycap>TAB</keycap> attempts to auto-complete the mode name. Pressing
182      <keycap>TAB</keycap> repeatedly cycles through the possible completions. 
183		<keycap>SHIFT-TAB</keycap> cycles through the completions in reverse.
184      </para></abstract>
185   </listitem>
186
187*/