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

/jEdit/tags/jedit-4-3-pre5/macros/Editing/Mode_Switcher.bsh

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