PageRenderTime 99ms CodeModel.GetById 72ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 428 lines | 227 code | 45 blank | 156 comment | 37 complexity | 46c25125ad04b2805e71fa7d66abc42a 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.JEditTextArea;
 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 3959 2002-01-05 03:26:29Z 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		if(!textArea.isEditable())
167		{
168			textArea.getToolkit().beep();
169			return;
170		}
171
172		Register reg = getRegister(register);
173
174		if(reg == null)
175		{
176			textArea.getToolkit().beep();
177			return;
178		}
179		else
180		{
181			String selection = reg.toString();
182			if(selection == null)
183			{
184				textArea.getToolkit().beep();
185				return;
186			}
187
188			// preserve magic pos for easy insertion of the
189			// same string at the start of multiple lines
190			int magic = textArea.getMagicCaretPosition();
191			textArea.setSelectedText(selection);
192
193			if(textArea.getCaretPosition()
194				!= textArea.getLineEndOffset(textArea.getCaretLine()) - 1)
195			{
196				textArea.setMagicCaretPosition(magic);
197			}
198			else
199			{
200				// if user is pasting at end of line, chances are
201				// they want the caret to go the the end of the
202				// line again when they move it up or down
203			}
204
205			HistoryModel.getModel("clipboard").addItem(selection);
206		}
207	} //}}}
208
209	//{{{ getRegister() method
210	/**
211	 * Returns the specified register.
212	 * @param name The name
213	 */
214	public static Register getRegister(char name)
215	{
216		if(registers == null || name >= registers.length)
217			return null;
218		else
219			return registers[name];
220	} //}}}
221
222	//{{{ setRegister() method
223	/**
224	 * Sets the specified register.
225	 * @param name The name
226	 * @param newRegister The new value
227	 */
228	public static void setRegister(char name, Register newRegister)
229	{
230		if(name >= registers.length)
231		{
232			Register[] newRegisters = new Register[
233				Math.min(1<<16,name * 2)];
234			System.arraycopy(registers,0,newRegisters,0,
235				registers.length);
236			registers = newRegisters;
237		}
238
239		registers[name] = newRegister;
240	} //}}}
241
242	//{{{ setRegister() method
243	/**
244	 * Sets the specified register.
245	 * @param name The name
246	 * @param value The new value
247	 */
248	public static void setRegister(char name, String value)
249	{
250		if(name >= registers.length)
251		{
252			Register[] newRegisters = new Register[
253				Math.min(1<<16,name * 2)];
254			System.arraycopy(registers,0,newRegisters,0,
255				registers.length);
256			registers = newRegisters;
257			registers[name] = new StringRegister(value);
258		}
259		else
260		{
261			Register register = registers[name];
262
263			if(register != null)
264				register.setValue(value);
265			else
266				registers[name] = new StringRegister(value);
267		}
268	} //}}}
269
270	//{{{ clearRegister() method
271	/**
272	 * Sets the value of the specified register to <code>null</code>.
273	 * @param name The register name
274	 */
275	public static void clearRegister(char name)
276	{
277		if(name >= registers.length)
278			return;
279
280		Register register = registers[name];
281		if(name == '$' || name == '%')
282			register.setValue("");
283		else
284			registers[name] = null;
285	} //}}}
286
287	//{{{ getRegister() method
288	/**
289	 * Returns an array of all available registers. Some of the elements
290	 * of this array might be null.
291	 */
292	public static Register[] getRegisters()
293	{
294		return registers;
295	} //}}}
296
297	//{{{ Register interface
298	/**
299	 * A register.
300	 */
301	public interface Register
302	{
303		/**
304		 * Converts to a string.
305		 */
306		String toString();
307
308		/**
309		 * Sets the register contents.
310		 */
311		void setValue(String value);
312	} //}}}
313
314	//{{{ ClipboardRegister class
315	/**
316	 * A clipboard register. Register "$" should always be an
317	 * instance of this.
318	 */
319	public static class ClipboardRegister implements Register
320	{
321		Clipboard clipboard;
322
323		public ClipboardRegister(Clipboard clipboard)
324		{
325			this.clipboard = clipboard;
326		}
327
328		/**
329		 * Sets the clipboard contents.
330		 */
331		public void setValue(String value)
332		{
333			StringSelection selection = new StringSelection(value);
334			clipboard.setContents(selection,null);
335		}
336
337		/**
338		 * Returns the clipboard contents.
339		 */
340		public String toString()
341		{
342			try
343			{
344				String selection = (String)(clipboard
345					.getContents(this).getTransferData(
346					DataFlavor.stringFlavor));
347
348				boolean trailingEOL = (selection.endsWith("\n")
349					|| selection.endsWith(System.getProperty(
350					"line.separator")));
351
352				// Some Java versions return the clipboard
353				// contents using the native line separator,
354				// so have to convert it here
355				BufferedReader in = new BufferedReader(
356					new StringReader(selection));
357				StringBuffer buf = new StringBuffer();
358				String line;
359				while((line = in.readLine()) != null)
360				{
361					buf.append(line);
362					buf.append('\n');
363				}
364				// remove trailing \n
365				if(!trailingEOL)
366					buf.setLength(buf.length() - 1);
367				return buf.toString();
368			}
369			catch(Exception e)
370			{
371				Log.log(Log.NOTICE,this,e);
372				return null;
373			}
374		}
375	} //}}}
376
377	//{{{ StringRegister class
378	/**
379	 * Register that stores a string.
380	 */
381	public static class StringRegister implements Register
382	{
383		private String value;
384
385		/**
386		 * Creates a new string register.
387		 * @param value The contents
388		 */
389		public StringRegister(String value)
390		{
391			this.value = value;
392		}
393
394		/**
395		 * Sets the register contents.
396		 */
397		public void setValue(String value)
398		{
399			this.value = value;
400		}
401
402		/**
403		 * Converts to a string.
404		 */
405		public String toString()
406		{
407			return value;
408		}
409
410		/**
411		 * Called when this register is no longer available. This
412		 * implementation does nothing.
413		 */
414		public void dispose() {}
415	} //}}}
416
417	//{{{ Private members
418	private static Register[] registers;
419
420	private Registers() {}
421
422	static
423	{
424		registers = new Register[256];
425		registers['$'] = new ClipboardRegister(Toolkit
426			.getDefaultToolkit().getSystemClipboard());
427	} //}}}
428}