PageRenderTime 95ms CodeModel.GetById 30ms app.highlight 49ms RepoModel.GetById 12ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/Registers.java

#
Java | 437 lines | 232 code | 45 blank | 160 comment | 38 complexity | 5d123110e812df4e83a6657a7f08ce7e MD5 | raw file
  1/*
  2 * Registers.java - Register manager
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000, 2001 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit;
 24
 25//{{{ Imports
 26import javax.swing.text.*;
 27import java.awt.datatransfer.*;
 28import java.awt.Toolkit;
 29import java.io.*;
 30import java.util.Vector;
 31import org.gjt.sp.jedit.gui.*;
 32import org.gjt.sp.jedit.textarea.*;
 33import org.gjt.sp.util.Log;
 34//}}}
 35
 36/**
 37 * jEdit's registers are an extension of the clipboard metaphor.
 38 *
 39 * @author Slava Pestov
 40 * @version $Id: Registers.java 4243 2002-06-06 05:57:19Z spestov $
 41 */
 42public class Registers
 43{
 44	//{{{ copy() method
 45	/**
 46	 * Convenience method that copies the text selected in the specified
 47	 * text area into the specified register.
 48	 * @param textArea The text area
 49	 * @param register The register
 50	 * @since jEdit 2.7pre2
 51	 */
 52	public static void copy(JEditTextArea textArea, char register)
 53	{
 54		String selection = textArea.getSelectedText();
 55		if(selection == null)
 56			return;
 57
 58		setRegister(register,selection);
 59		HistoryModel.getModel("clipboard").addItem(selection);
 60	} //}}}
 61
 62	//{{{ append() method
 63	/**
 64	 * Convinience method that appends the text selected in the specified
 65	 * text area to the specified register, with a newline between the old
 66	 * and new text.
 67	 * @param textArea The text area
 68	 * @param register The register
 69	 */
 70	public static void append(JEditTextArea textArea, char register)
 71	{
 72		append(textArea,register,"\n",false);
 73	} //}}}
 74
 75	//{{{ append() method
 76	/**
 77	 * Convinience method that appends the text selected in the specified
 78	 * text area to the specified register.
 79	 * @param textArea The text area
 80	 * @param register The register
 81	 * @param separator The text to insert between the old and new text
 82	 */
 83	public static void append(JEditTextArea textArea, char register,
 84		String separator)
 85	{
 86		append(textArea,register,separator,false);
 87	} //}}}
 88
 89	//{{{ append() method
 90	/**
 91	 * Convinience method that appends the text selected in the specified
 92	 * text area to the specified register.
 93	 * @param textArea The text area
 94	 * @param register The register
 95	 * @param separator The text to insert between the old and new text
 96	 * @param cut Should the current selection be removed?
 97	 * @since jEdit 3.2pre1
 98	 */
 99	public static void append(JEditTextArea textArea, char register,
100		String separator, boolean cut)
101	{
102		if(cut && !textArea.isEditable())
103		{
104			textArea.getToolkit().beep();
105			return;
106		}
107
108		String selection = textArea.getSelectedText();
109		if(selection == null)
110			return;
111
112		Register reg = getRegister(register);
113
114		String registerContents = reg.toString();
115		if(reg != null && registerContents != null)
116		{
117			if(registerContents.endsWith(separator))
118				selection = registerContents + selection;
119			else
120				selection = registerContents + separator + selection;
121		}
122
123		setRegister(register,selection);
124		HistoryModel.getModel("clipboard").addItem(selection);
125
126		if(cut)
127			textArea.setSelectedText("");
128	} //}}}
129
130	//{{{ cut() method
131	/**
132	 * Convinience method that copies the text selected in the specified
133	 * text area into the specified register, and then removes it from the
134	 * text area.
135	 * @param textArea The text area
136	 * @param register The register
137	 * @since jEdit 2.7pre2
138	 */
139	public static void cut(JEditTextArea textArea, char register)
140	{
141		if(textArea.isEditable())
142		{
143			String selection = textArea.getSelectedText();
144			if(selection == null)
145				return;
146
147			setRegister(register,selection);
148			HistoryModel.getModel("clipboard").addItem(selection);
149
150			textArea.setSelectedText("");
151		}
152		else
153			textArea.getToolkit().beep();
154	} //}}}
155
156	//{{{ paste() method
157	/**
158	 * Convinience method that pastes the contents of the specified
159	 * register into the text area.
160	 * @param textArea The text area
161	 * @param register The register
162	 * @since jEdit 2.7pre2
163	 */
164	public static void paste(JEditTextArea textArea, char register)
165	{
166		paste(textArea,register,false);
167	} //}}}
168
169	//{{{ paste() method
170	/**
171	 * Convinience method that pastes the contents of the specified
172	 * register into the text area.
173	 * @param textArea The text area
174	 * @param register The register
175	 * @param vertical Vertical (columnar) paste
176	 * @since jEdit 4.1pre1
177	 */
178	public static void paste(JEditTextArea textArea, char register,
179		boolean vertical)
180	{
181		if(!textArea.isEditable())
182		{
183			textArea.getToolkit().beep();
184			return;
185		}
186
187		Register reg = getRegister(register);
188
189		if(reg == null)
190		{
191			textArea.getToolkit().beep();
192			return;
193		}
194		else
195		{
196			String selection = reg.toString();
197			if(selection == null)
198			{
199				textArea.getToolkit().beep();
200				return;
201			}
202
203			if(vertical && textArea.getSelectionCount() == 0)
204			{
205				int caret = textArea.getCaretPosition();
206				int caretLine = textArea.getCaretLine();
207				Selection.Rect rect = new Selection.Rect(
208					caretLine,caret,caretLine,caret);
209				textArea.setSelectedText(rect,selection);
210			}
211			else
212				textArea.setSelectedText(selection);
213
214			HistoryModel.getModel("clipboard").addItem(selection);
215		}
216	} //}}}
217
218	//{{{ getRegister() method
219	/**
220	 * Returns the specified register.
221	 * @param name The name
222	 */
223	public static Register getRegister(char name)
224	{
225		if(registers == null || name >= registers.length)
226			return null;
227		else
228			return registers[name];
229	} //}}}
230
231	//{{{ setRegister() method
232	/**
233	 * Sets the specified register.
234	 * @param name The name
235	 * @param newRegister The new value
236	 */
237	public static void setRegister(char name, Register newRegister)
238	{
239		if(name >= registers.length)
240		{
241			Register[] newRegisters = new Register[
242				Math.min(1<<16,name * 2)];
243			System.arraycopy(registers,0,newRegisters,0,
244				registers.length);
245			registers = newRegisters;
246		}
247
248		registers[name] = newRegister;
249	} //}}}
250
251	//{{{ setRegister() method
252	/**
253	 * Sets the specified register.
254	 * @param name The name
255	 * @param value The new value
256	 */
257	public static void setRegister(char name, String value)
258	{
259		if(name >= registers.length)
260		{
261			Register[] newRegisters = new Register[
262				Math.min(1<<16,name * 2)];
263			System.arraycopy(registers,0,newRegisters,0,
264				registers.length);
265			registers = newRegisters;
266			registers[name] = new StringRegister(value);
267		}
268		else
269		{
270			Register register = registers[name];
271
272			if(register != null)
273				register.setValue(value);
274			else
275				registers[name] = new StringRegister(value);
276		}
277	} //}}}
278
279	//{{{ clearRegister() method
280	/**
281	 * Sets the value of the specified register to <code>null</code>.
282	 * @param name The register name
283	 */
284	public static void clearRegister(char name)
285	{
286		if(name >= registers.length)
287			return;
288
289		Register register = registers[name];
290		if(name == '$' || name == '%')
291			register.setValue("");
292		else
293			registers[name] = null;
294	} //}}}
295
296	//{{{ getRegister() method
297	/**
298	 * Returns an array of all available registers. Some of the elements
299	 * of this array might be null.
300	 */
301	public static Register[] getRegisters()
302	{
303		return registers;
304	} //}}}
305
306	//{{{ Register interface
307	/**
308	 * A register.
309	 */
310	public interface Register
311	{
312		/**
313		 * Converts to a string.
314		 */
315		String toString();
316
317		/**
318		 * Sets the register contents.
319		 */
320		void setValue(String value);
321	} //}}}
322
323	//{{{ ClipboardRegister class
324	/**
325	 * A clipboard register. Register "$" should always be an
326	 * instance of this.
327	 */
328	public static class ClipboardRegister implements Register
329	{
330		Clipboard clipboard;
331
332		public ClipboardRegister(Clipboard clipboard)
333		{
334			this.clipboard = clipboard;
335		}
336
337		/**
338		 * Sets the clipboard contents.
339		 */
340		public void setValue(String value)
341		{
342			StringSelection selection = new StringSelection(value);
343			clipboard.setContents(selection,null);
344		}
345
346		/**
347		 * Returns the clipboard contents.
348		 */
349		public String toString()
350		{
351			try
352			{
353				String selection = (String)(clipboard
354					.getContents(this).getTransferData(
355					DataFlavor.stringFlavor));
356
357				boolean trailingEOL = (selection.endsWith("\n")
358					|| selection.endsWith(System.getProperty(
359					"line.separator")));
360
361				// Some Java versions return the clipboard
362				// contents using the native line separator,
363				// so have to convert it here
364				BufferedReader in = new BufferedReader(
365					new StringReader(selection));
366				StringBuffer buf = new StringBuffer();
367				String line;
368				while((line = in.readLine()) != null)
369				{
370					buf.append(line);
371					buf.append('\n');
372				}
373				// remove trailing \n
374				if(!trailingEOL)
375					buf.setLength(buf.length() - 1);
376				return buf.toString();
377			}
378			catch(Exception e)
379			{
380				Log.log(Log.NOTICE,this,e);
381				return null;
382			}
383		}
384	} //}}}
385
386	//{{{ StringRegister class
387	/**
388	 * Register that stores a string.
389	 */
390	public static class StringRegister implements Register
391	{
392		private String value;
393
394		/**
395		 * Creates a new string register.
396		 * @param value The contents
397		 */
398		public StringRegister(String value)
399		{
400			this.value = value;
401		}
402
403		/**
404		 * Sets the register contents.
405		 */
406		public void setValue(String value)
407		{
408			this.value = value;
409		}
410
411		/**
412		 * Converts to a string.
413		 */
414		public String toString()
415		{
416			return value;
417		}
418
419		/**
420		 * Called when this register is no longer available. This
421		 * implementation does nothing.
422		 */
423		public void dispose() {}
424	} //}}}
425
426	//{{{ Private members
427	private static Register[] registers;
428
429	private Registers() {}
430
431	static
432	{
433		registers = new Register[256];
434		registers['$'] = new ClipboardRegister(Toolkit
435			.getDefaultToolkit().getSystemClipboard());
436	} //}}}
437}