PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/TextTools/TextToolsBlockHandling.java

#
Java | 315 lines | 160 code | 14 blank | 141 comment | 37 complexity | 0e6c04c99053794e4ba317afa33e34fc 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. * TextToolsBlockHandling.java - a Java class for the jEdit text editor
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2002 Rudolf Widmann
  7. * Rudi.Widmann@web.de
  8. *
  9. * 1) inserts or fills a given string or number into a selection
  10. * 2) converts spaces to tabs
  11. * based on:
  12. * - TextUtilities.spacesToTabs
  13. * - MiscUtilities.createWhiteSpace
  14. *
  15. * Checked for jEdit 4.0 API
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version 2
  20. * of the License, or any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. */
  31. //{{{ Imports
  32. import java.util.*;
  33. import org.gjt.sp.jedit.BeanShell;
  34. import org.gjt.sp.jedit.buffer.JEditBuffer;
  35. import org.gjt.sp.jedit.EditPlugin;
  36. import org.gjt.sp.jedit.GUIUtilities;
  37. import org.gjt.sp.jedit.jEdit;
  38. import org.gjt.sp.jedit.MiscUtilities;
  39. import org.gjt.sp.jedit.TextUtilities;
  40. import org.gjt.sp.jedit.View;
  41. import org.gjt.sp.jedit.textarea.JEditTextArea;
  42. import org.gjt.sp.jedit.textarea.Selection;
  43. import org.gjt.sp.util.Log;
  44. import org.gjt.sp.util.StandardUtilities;
  45. import java.awt.event.KeyListener;
  46. import java.awt.event.KeyEvent;
  47. import javax.swing.JDialog;
  48. import javax.swing.JTextField;
  49. import javax.swing.border.*;
  50. //}}}
  51. public class TextToolsBlockHandling
  52. {
  53. //{{{ doBlockAction() method
  54. public static void doBlockAction(
  55. View view,
  56. int incrementValue,
  57. int insertValue,
  58. String insertText,
  59. boolean overwriteBlock,
  60. boolean increment,
  61. boolean leadingZeros)
  62. {
  63. BeanShellUtility bsu = new BeanShellUtility(view);
  64. JEditTextArea textArea = view.getTextArea();
  65. int startRange;
  66. int endRange;
  67. boolean trace=true;
  68. JEditBuffer buffer = view.getBuffer();
  69. /*********************************************************
  70. * evaluate selection
  71. *********************************************************/
  72. Selection[] sel = textArea.getSelection();
  73. int selCount = textArea.getSelectionCount();
  74. int selBegin;
  75. int selEnd;
  76. // check anything is selected
  77. if (selCount == 0 )
  78. {
  79. //Note: The TextToolsPlugin class now prevents
  80. //this case from happening.
  81. selBegin = bsu.getVisibleRow(textArea.getCaretPosition());
  82. // Log.log(Log.DEBUG, BeanShell.class,"selBegin = "+selBegin);
  83. selEnd = selBegin + insertText.length();
  84. textArea.goToBufferEnd(true);
  85. } else {
  86. // get range, maybe different per line ico multiple/ rect selections
  87. // for several reasons, we take the width of the first selected line, and apply it to all selections
  88. // selBegin = bsu.getVisibleRow(sel[0].getStart());
  89. // selEnd = bsu.getVisibleRow(sel[0].getEnd(buffer, sel[0].getStartLine()));
  90. //Get the correct start column and end column of the selection.
  91. //Note: The TextToolsPlugin class ensures
  92. //that the selection is rectangular.
  93. selBegin = ((Selection.Rect)sel[0]).getStartColumn(buffer);
  94. selEnd = ((Selection.Rect)sel[0]).getEndColumn(buffer);
  95. }
  96. /*********************************************************
  97. * check if only one line selected ==> select form cursor to eof
  98. *********************************************************/
  99. if (selCount == 1 && (sel[0].getStartLine() == sel[0].getEndLine()))
  100. {
  101. //Note: The TextToolsPlugin now prevents
  102. //this case from happening.
  103. if (trace)
  104. Log.log(Log.DEBUG, TextToolsBlockHandling.class,"only one selection line");
  105. textArea.setCaretPosition(sel[0].getStart());
  106. textArea.goToBufferEnd(true);
  107. }
  108. /*********************************************************
  109. * check insert text matches selection
  110. *********************************************************/
  111. int targetLen = selEnd - selBegin;
  112. if (!increment && overwriteBlock) // insert shall match selection exactly
  113. {
  114. if (insertText.length() < targetLen)
  115. insertText = insertText + StandardUtilities.createWhiteSpace(targetLen - insertText.length(), 0);
  116. else if (insertText.length() > targetLen)
  117. insertText = insertText.substring(0,targetLen);
  118. }
  119. /*********************************************************
  120. * prepare increment filling string
  121. *********************************************************/
  122. String nullsBlanksString=null;
  123. int currentIncValue=insertValue;
  124. if (increment)
  125. {
  126. if (leadingZeros)
  127. {
  128. StringBuilder nulBuf = new StringBuilder("0000000000");
  129. for (int s=10; s<targetLen;s+=10)
  130. nulBuf.append("0000000000");
  131. nullsBlanksString = nulBuf.toString();
  132. } else {
  133. nullsBlanksString = StandardUtilities.createWhiteSpace(targetLen, 0);
  134. }
  135. }
  136. /*********************************************************
  137. * begin text manipulations
  138. *********************************************************/
  139. int[] selLines = textArea.getSelectedLines();
  140. String sourceString = insertText;
  141. // Note: from now on, we don't need the selections
  142. textArea.selectNone(); // reset selection
  143. StringBuilder blankBuf = new StringBuilder(" ");
  144. buffer.beginCompoundEdit();
  145. ArrayList targetSelection = new ArrayList(); // collects Selection
  146. for (int i=0;i<selLines.length;i++)
  147. {
  148. /*********************************************************
  149. * handle tabs: replace with spaces if tabs exist
  150. *********************************************************/
  151. boolean isWithTabs = false;
  152. if (buffer.getLineText(selLines[i]).indexOf("\t") != -1)
  153. {
  154. // line contains tabs
  155. isWithTabs = true;
  156. bsu.selectLine(selLines[i]);
  157. textArea.tabsToSpaces();
  158. }
  159. /*********************************************************
  160. * calculate insert string
  161. *********************************************************/
  162. if (increment)
  163. {
  164. String valString = Integer.toString(currentIncValue);
  165. String pfxString;
  166. if (targetLen - valString.length() > 0)
  167. {
  168. pfxString = nullsBlanksString.substring(0,targetLen - valString.length());
  169. } else
  170. pfxString = "";
  171. if (currentIncValue < 0 && leadingZeros)
  172. {
  173. // minus dash should be at beginning of zeros
  174. sourceString = "-" + pfxString + valString.substring(1);
  175. } else {
  176. sourceString = pfxString + valString;
  177. }
  178. if (sourceString.length() > targetLen)
  179. sourceString = sourceString.substring(sourceString.length() - targetLen);
  180. }
  181. /*********************************************************
  182. * check if linelength too short: add missing blanks
  183. *********************************************************/
  184. int lineEndOffset = textArea.getLineEndOffset(selLines[i]);
  185. int insertPos = textArea.getLineStartOffset(selLines[i])+selBegin;
  186. int missingBlanks = insertPos - lineEndOffset+1; // +1'cause of <cr>
  187. String prefixString = "";
  188. if (missingBlanks > 0)
  189. {
  190. while (blankBuf.length() < missingBlanks) blankBuf.append(" ");
  191. prefixString = blankBuf.substring(0,missingBlanks);
  192. insertPos = insertPos - missingBlanks;
  193. // Log.log(Log.DEBUG, BeanShell.class,"*3 insertPos = "+insertPos);
  194. }
  195. /*********************************************************
  196. * finally, do modification
  197. *********************************************************/
  198. if (overwriteBlock && lineEndOffset > 0)
  199. {
  200. // dont remove chars that dont exist ==> calc remove len
  201. int removeDecrement = insertPos +
  202. prefixString.length() +
  203. sourceString.length() -
  204. (lineEndOffset-1);
  205. if (removeDecrement < 0)
  206. removeDecrement = 0;
  207. buffer.remove(insertPos, prefixString.length() + sourceString.length() - removeDecrement);
  208. }
  209. buffer.insert(insertPos,prefixString+sourceString);
  210. if (isWithTabs)
  211. {
  212. // change back to tabs
  213. bsu.selectLine(selLines[i]);
  214. // Note: as long as there are bugs in
  215. //textArea.spacesToTabs, a local patch is used
  216. textArea.spacesToTabs();
  217. }
  218. /*********************************************************
  219. * memorize selection
  220. *********************************************************/
  221. int newBeginSelection = insertPos+prefixString.length();
  222. if (newBeginSelection > buffer.getLength())
  223. newBeginSelection = buffer.getLength();
  224. int newEndSelection = insertPos+prefixString.length() + sourceString.length();
  225. if (newEndSelection > buffer.getLength())
  226. newEndSelection = buffer.getLength();
  227. targetSelection.add(new Selection.Range(newBeginSelection, newEndSelection));
  228. // textArea.addToSelection(new Selection.Rect(insertPos, insertPos+prefixString.length()+
  229. // sourceString.length()));
  230. if (increment)
  231. currentIncValue += incrementValue;
  232. }
  233. /*********************************************************
  234. * set selection to changed chunks
  235. *********************************************************/
  236. Selection[] newSel = new Selection[targetSelection.size()];
  237. newSel = (Selection[])targetSelection.toArray(newSel);
  238. textArea.setSelection(newSel);
  239. buffer.endCompoundEdit();
  240. } //}}}
  241. //{{{ getTempIntProperty() method
  242. String getTempIntProperty(String prop)
  243. {
  244. String ret = jEdit.getProperty(prop);
  245. if (ret == null)
  246. return "";
  247. else
  248. return ret;
  249. } //}}}
  250. // this single line of code is the script's main routine
  251. // it calls the methods and exits
  252. // displayInsertFillDialog();
  253. /*
  254. Macro index data (in DocBook format)
  255. * - inserts or fills a given <source> into a <target box>
  256. * - the source is either
  257. * * a string
  258. * * a number to be incremented
  259. * - the target depends on the selection
  260. * * no selection
  261. * < in a selected area
  262. * <
  263. * or all lines
  264. <listitem>
  265. <para><filename>TextToolsBlockHandling.java</filename></para>
  266. <abstract><para>
  267. Inserts or replaces, fills or increments, a given string or number, into a selection block.
  268. Dialog driven
  269. </para>
  270. </abstract>
  271. <para>
  272. Inserts or replaces, fills or increments, a given <quote>text</quote> into a <quote>target box</quote>
  273. </para>
  274. <para>When invoked, a dialog pops-up, with the fields:
  275. * <quote>Text to be inserted</quote>: text you want to insert
  276. * <quote>Increment</quote>: int value serving as increment
  277. * <quote>Overwrite checkbox</quote>: checked: Overwrite selection, not checked: insert
  278. * <quote>Leading Zeros checkbox</quote>: checked: Fill unused digits with nulls , not checked: fill with blanks
  279. </para>
  280. <para>
  281. <quote>text</quote> is either:
  282. * a string
  283. * a number to be incremented
  284. </para>
  285. <para>
  286. the <quote>target box</quote> depends on the selection:
  287. * no selection: a box starting at the caret position till EOF, width of <quote>insert text</quote>
  288. * selection in one line: a box starting at the caret position till EOF, width as selected
  289. * rect selection: box as selected, width
  290. </para>
  291. <para>
  292. If the insertion doesnot match the target box, it is filled with blanks, or cut.
  293. </para>
  294. <para>
  295. Multiple selection is supported, but the width is according the the first selection
  296. </para>
  297. </listitem>
  298. */
  299. }