PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 0ms 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
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.*;
  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 4243 2002-06-06 05:57:19Z 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. paste(textArea,register,false);
  151. } //}}}
  152. //{{{ paste() method
  153. /**
  154. * Convinience method that pastes the contents of the specified
  155. * register into the text area.
  156. * @param textArea The text area
  157. * @param register The register
  158. * @param vertical Vertical (columnar) paste
  159. * @since jEdit 4.1pre1
  160. */
  161. public static void paste(JEditTextArea textArea, char register,
  162. boolean vertical)
  163. {
  164. if(!textArea.isEditable())
  165. {
  166. textArea.getToolkit().beep();
  167. return;
  168. }
  169. Register reg = getRegister(register);
  170. if(reg == null)
  171. {
  172. textArea.getToolkit().beep();
  173. return;
  174. }
  175. else
  176. {
  177. String selection = reg.toString();
  178. if(selection == null)
  179. {
  180. textArea.getToolkit().beep();
  181. return;
  182. }
  183. if(vertical && textArea.getSelectionCount() == 0)
  184. {
  185. int caret = textArea.getCaretPosition();
  186. int caretLine = textArea.getCaretLine();
  187. Selection.Rect rect = new Selection.Rect(
  188. caretLine,caret,caretLine,caret);
  189. textArea.setSelectedText(rect,selection);
  190. }
  191. else
  192. textArea.setSelectedText(selection);
  193. HistoryModel.getModel("clipboard").addItem(selection);
  194. }
  195. } //}}}
  196. //{{{ getRegister() method
  197. /**
  198. * Returns the specified register.
  199. * @param name The name
  200. */
  201. public static Register getRegister(char name)
  202. {
  203. if(registers == null || name >= registers.length)
  204. return null;
  205. else
  206. return registers[name];
  207. } //}}}
  208. //{{{ setRegister() method
  209. /**
  210. * Sets the specified register.
  211. * @param name The name
  212. * @param newRegister The new value
  213. */
  214. public static void setRegister(char name, Register newRegister)
  215. {
  216. if(name >= registers.length)
  217. {
  218. Register[] newRegisters = new Register[
  219. Math.min(1<<16,name * 2)];
  220. System.arraycopy(registers,0,newRegisters,0,
  221. registers.length);
  222. registers = newRegisters;
  223. }
  224. registers[name] = newRegister;
  225. } //}}}
  226. //{{{ setRegister() method
  227. /**
  228. * Sets the specified register.
  229. * @param name The name
  230. * @param value The new value
  231. */
  232. public static void setRegister(char name, String value)
  233. {
  234. if(name >= registers.length)
  235. {
  236. Register[] newRegisters = new Register[
  237. Math.min(1<<16,name * 2)];
  238. System.arraycopy(registers,0,newRegisters,0,
  239. registers.length);
  240. registers = newRegisters;
  241. registers[name] = new StringRegister(value);
  242. }
  243. else
  244. {
  245. Register register = registers[name];
  246. if(register != null)
  247. register.setValue(value);
  248. else
  249. registers[name] = new StringRegister(value);
  250. }
  251. } //}}}
  252. //{{{ clearRegister() method
  253. /**
  254. * Sets the value of the specified register to <code>null</code>.
  255. * @param name The register name
  256. */
  257. public static void clearRegister(char name)
  258. {
  259. if(name >= registers.length)
  260. return;
  261. Register register = registers[name];
  262. if(name == '$' || name == '%')
  263. register.setValue("");
  264. else
  265. registers[name] = null;
  266. } //}}}
  267. //{{{ getRegister() method
  268. /**
  269. * Returns an array of all available registers. Some of the elements
  270. * of this array might be null.
  271. */
  272. public static Register[] getRegisters()
  273. {
  274. return registers;
  275. } //}}}
  276. //{{{ Register interface
  277. /**
  278. * A register.
  279. */
  280. public interface Register
  281. {
  282. /**
  283. * Converts to a string.
  284. */
  285. String toString();
  286. /**
  287. * Sets the register contents.
  288. */
  289. void setValue(String value);
  290. } //}}}
  291. //{{{ ClipboardRegister class
  292. /**
  293. * A clipboard register. Register "$" should always be an
  294. * instance of this.
  295. */
  296. public static class ClipboardRegister implements Register
  297. {
  298. Clipboard clipboard;
  299. public ClipboardRegister(Clipboard clipboard)
  300. {
  301. this.clipboard = clipboard;
  302. }
  303. /**
  304. * Sets the clipboard contents.
  305. */
  306. public void setValue(String value)
  307. {
  308. StringSelection selection = new StringSelection(value);
  309. clipboard.setContents(selection,null);
  310. }
  311. /**
  312. * Returns the clipboard contents.
  313. */
  314. public String toString()
  315. {
  316. try
  317. {
  318. String selection = (String)(clipboard
  319. .getContents(this).getTransferData(
  320. DataFlavor.stringFlavor));
  321. boolean trailingEOL = (selection.endsWith("\n")
  322. || selection.endsWith(System.getProperty(
  323. "line.separator")));
  324. // Some Java versions return the clipboard
  325. // contents using the native line separator,
  326. // so have to convert it here
  327. BufferedReader in = new BufferedReader(
  328. new StringReader(selection));
  329. StringBuffer buf = new StringBuffer();
  330. String line;
  331. while((line = in.readLine()) != null)
  332. {
  333. buf.append(line);
  334. buf.append('\n');
  335. }
  336. // remove trailing \n
  337. if(!trailingEOL)
  338. buf.setLength(buf.length() - 1);
  339. return buf.toString();
  340. }
  341. catch(Exception e)
  342. {
  343. Log.log(Log.NOTICE,this,e);
  344. return null;
  345. }
  346. }
  347. } //}}}
  348. //{{{ StringRegister class
  349. /**
  350. * Register that stores a string.
  351. */
  352. public static class StringRegister implements Register
  353. {
  354. private String value;
  355. /**
  356. * Creates a new string register.
  357. * @param value The contents
  358. */
  359. public StringRegister(String value)
  360. {
  361. this.value = value;
  362. }
  363. /**
  364. * Sets the register contents.
  365. */
  366. public void setValue(String value)
  367. {
  368. this.value = value;
  369. }
  370. /**
  371. * Converts to a string.
  372. */
  373. public String toString()
  374. {
  375. return value;
  376. }
  377. /**
  378. * Called when this register is no longer available. This
  379. * implementation does nothing.
  380. */
  381. public void dispose() {}
  382. } //}}}
  383. //{{{ Private members
  384. private static Register[] registers;
  385. private Registers() {}
  386. static
  387. {
  388. registers = new Register[256];
  389. registers['$'] = new ClipboardRegister(Toolkit
  390. .getDefaultToolkit().getSystemClipboard());
  391. } //}}}
  392. }