PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit;
  23. //{{{ Imports
  24. import javax.swing.text.*;
  25. import java.awt.datatransfer.*;
  26. import java.awt.Toolkit;
  27. import java.io.*;
  28. import java.util.Vector;
  29. import org.gjt.sp.jedit.gui.*;
  30. import org.gjt.sp.jedit.textarea.JEditTextArea;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. /**
  34. * jEdit's registers are an extension of the clipboard metaphor.
  35. *
  36. * @author Slava Pestov
  37. * @version $Id: Registers.java 3959 2002-01-05 03:26:29Z spestov $
  38. */
  39. public class Registers
  40. {
  41. //{{{ copy() method
  42. /**
  43. * Convenience method that copies the text selected in the specified
  44. * text area into the specified register.
  45. * @param textArea The text area
  46. * @param register The register
  47. * @since jEdit 2.7pre2
  48. */
  49. public static void copy(JEditTextArea textArea, char register)
  50. {
  51. String selection = textArea.getSelectedText();
  52. if(selection == null)
  53. return;
  54. setRegister(register,selection);
  55. HistoryModel.getModel("clipboard").addItem(selection);
  56. } //}}}
  57. //{{{ append() method
  58. /**
  59. * Convinience method that appends the text selected in the specified
  60. * text area to the specified register, with a newline between the old
  61. * and new text.
  62. * @param textArea The text area
  63. * @param register The register
  64. */
  65. public static void append(JEditTextArea textArea, char register)
  66. {
  67. append(textArea,register,"\n",false);
  68. } //}}}
  69. //{{{ append() method
  70. /**
  71. * Convinience method that appends the text selected in the specified
  72. * text area to the specified register.
  73. * @param textArea The text area
  74. * @param register The register
  75. * @param separator The text to insert between the old and new text
  76. */
  77. public static void append(JEditTextArea textArea, char register,
  78. String separator)
  79. {
  80. append(textArea,register,separator,false);
  81. } //}}}
  82. //{{{ append() method
  83. /**
  84. * Convinience method that appends the text selected in the specified
  85. * text area to the specified register.
  86. * @param textArea The text area
  87. * @param register The register
  88. * @param separator The text to insert between the old and new text
  89. * @param cut Should the current selection be removed?
  90. * @since jEdit 3.2pre1
  91. */
  92. public static void append(JEditTextArea textArea, char register,
  93. String separator, boolean cut)
  94. {
  95. if(cut && !textArea.isEditable())
  96. {
  97. textArea.getToolkit().beep();
  98. return;
  99. }
  100. String selection = textArea.getSelectedText();
  101. if(selection == null)
  102. return;
  103. Register reg = getRegister(register);
  104. String registerContents = reg.toString();
  105. if(reg != null && registerContents != null)
  106. {
  107. if(registerContents.endsWith(separator))
  108. selection = registerContents + selection;
  109. else
  110. selection = registerContents + separator + selection;
  111. }
  112. setRegister(register,selection);
  113. HistoryModel.getModel("clipboard").addItem(selection);
  114. if(cut)
  115. textArea.setSelectedText("");
  116. } //}}}
  117. //{{{ cut() method
  118. /**
  119. * Convinience method that copies the text selected in the specified
  120. * text area into the specified register, and then removes it from the
  121. * text area.
  122. * @param textArea The text area
  123. * @param register The register
  124. * @since jEdit 2.7pre2
  125. */
  126. public static void cut(JEditTextArea textArea, char register)
  127. {
  128. if(textArea.isEditable())
  129. {
  130. String selection = textArea.getSelectedText();
  131. if(selection == null)
  132. return;
  133. setRegister(register,selection);
  134. HistoryModel.getModel("clipboard").addItem(selection);
  135. textArea.setSelectedText("");
  136. }
  137. else
  138. textArea.getToolkit().beep();
  139. } //}}}
  140. //{{{ paste() method
  141. /**
  142. * Convinience method that pastes the contents of the specified
  143. * register into the text area.
  144. * @param textArea The text area
  145. * @param register The register
  146. * @since jEdit 2.7pre2
  147. */
  148. public static void paste(JEditTextArea textArea, char register)
  149. {
  150. if(!textArea.isEditable())
  151. {
  152. textArea.getToolkit().beep();
  153. return;
  154. }
  155. Register reg = getRegister(register);
  156. if(reg == null)
  157. {
  158. textArea.getToolkit().beep();
  159. return;
  160. }
  161. else
  162. {
  163. String selection = reg.toString();
  164. if(selection == null)
  165. {
  166. textArea.getToolkit().beep();
  167. return;
  168. }
  169. // preserve magic pos for easy insertion of the
  170. // same string at the start of multiple lines
  171. int magic = textArea.getMagicCaretPosition();
  172. textArea.setSelectedText(selection);
  173. if(textArea.getCaretPosition()
  174. != textArea.getLineEndOffset(textArea.getCaretLine()) - 1)
  175. {
  176. textArea.setMagicCaretPosition(magic);
  177. }
  178. else
  179. {
  180. // if user is pasting at end of line, chances are
  181. // they want the caret to go the the end of the
  182. // line again when they move it up or down
  183. }
  184. HistoryModel.getModel("clipboard").addItem(selection);
  185. }
  186. } //}}}
  187. //{{{ getRegister() method
  188. /**
  189. * Returns the specified register.
  190. * @param name The name
  191. */
  192. public static Register getRegister(char name)
  193. {
  194. if(registers == null || name >= registers.length)
  195. return null;
  196. else
  197. return registers[name];
  198. } //}}}
  199. //{{{ setRegister() method
  200. /**
  201. * Sets the specified register.
  202. * @param name The name
  203. * @param newRegister The new value
  204. */
  205. public static void setRegister(char name, Register newRegister)
  206. {
  207. if(name >= registers.length)
  208. {
  209. Register[] newRegisters = new Register[
  210. Math.min(1<<16,name * 2)];
  211. System.arraycopy(registers,0,newRegisters,0,
  212. registers.length);
  213. registers = newRegisters;
  214. }
  215. registers[name] = newRegister;
  216. } //}}}
  217. //{{{ setRegister() method
  218. /**
  219. * Sets the specified register.
  220. * @param name The name
  221. * @param value The new value
  222. */
  223. public static void setRegister(char name, String value)
  224. {
  225. if(name >= registers.length)
  226. {
  227. Register[] newRegisters = new Register[
  228. Math.min(1<<16,name * 2)];
  229. System.arraycopy(registers,0,newRegisters,0,
  230. registers.length);
  231. registers = newRegisters;
  232. registers[name] = new StringRegister(value);
  233. }
  234. else
  235. {
  236. Register register = registers[name];
  237. if(register != null)
  238. register.setValue(value);
  239. else
  240. registers[name] = new StringRegister(value);
  241. }
  242. } //}}}
  243. //{{{ clearRegister() method
  244. /**
  245. * Sets the value of the specified register to <code>null</code>.
  246. * @param name The register name
  247. */
  248. public static void clearRegister(char name)
  249. {
  250. if(name >= registers.length)
  251. return;
  252. Register register = registers[name];
  253. if(name == '$' || name == '%')
  254. register.setValue("");
  255. else
  256. registers[name] = null;
  257. } //}}}
  258. //{{{ getRegister() method
  259. /**
  260. * Returns an array of all available registers. Some of the elements
  261. * of this array might be null.
  262. */
  263. public static Register[] getRegisters()
  264. {
  265. return registers;
  266. } //}}}
  267. //{{{ Register interface
  268. /**
  269. * A register.
  270. */
  271. public interface Register
  272. {
  273. /**
  274. * Converts to a string.
  275. */
  276. String toString();
  277. /**
  278. * Sets the register contents.
  279. */
  280. void setValue(String value);
  281. } //}}}
  282. //{{{ ClipboardRegister class
  283. /**
  284. * A clipboard register. Register "$" should always be an
  285. * instance of this.
  286. */
  287. public static class ClipboardRegister implements Register
  288. {
  289. Clipboard clipboard;
  290. public ClipboardRegister(Clipboard clipboard)
  291. {
  292. this.clipboard = clipboard;
  293. }
  294. /**
  295. * Sets the clipboard contents.
  296. */
  297. public void setValue(String value)
  298. {
  299. StringSelection selection = new StringSelection(value);
  300. clipboard.setContents(selection,null);
  301. }
  302. /**
  303. * Returns the clipboard contents.
  304. */
  305. public String toString()
  306. {
  307. try
  308. {
  309. String selection = (String)(clipboard
  310. .getContents(this).getTransferData(
  311. DataFlavor.stringFlavor));
  312. boolean trailingEOL = (selection.endsWith("\n")
  313. || selection.endsWith(System.getProperty(
  314. "line.separator")));
  315. // Some Java versions return the clipboard
  316. // contents using the native line separator,
  317. // so have to convert it here
  318. BufferedReader in = new BufferedReader(
  319. new StringReader(selection));
  320. StringBuffer buf = new StringBuffer();
  321. String line;
  322. while((line = in.readLine()) != null)
  323. {
  324. buf.append(line);
  325. buf.append('\n');
  326. }
  327. // remove trailing \n
  328. if(!trailingEOL)
  329. buf.setLength(buf.length() - 1);
  330. return buf.toString();
  331. }
  332. catch(Exception e)
  333. {
  334. Log.log(Log.NOTICE,this,e);
  335. return null;
  336. }
  337. }
  338. } //}}}
  339. //{{{ StringRegister class
  340. /**
  341. * Register that stores a string.
  342. */
  343. public static class StringRegister implements Register
  344. {
  345. private String value;
  346. /**
  347. * Creates a new string register.
  348. * @param value The contents
  349. */
  350. public StringRegister(String value)
  351. {
  352. this.value = value;
  353. }
  354. /**
  355. * Sets the register contents.
  356. */
  357. public void setValue(String value)
  358. {
  359. this.value = value;
  360. }
  361. /**
  362. * Converts to a string.
  363. */
  364. public String toString()
  365. {
  366. return value;
  367. }
  368. /**
  369. * Called when this register is no longer available. This
  370. * implementation does nothing.
  371. */
  372. public void dispose() {}
  373. } //}}}
  374. //{{{ Private members
  375. private static Register[] registers;
  376. private Registers() {}
  377. static
  378. {
  379. registers = new Register[256];
  380. registers['$'] = new ClipboardRegister(Toolkit
  381. .getDefaultToolkit().getSystemClipboard());
  382. } //}}}
  383. }