PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/macros/Java/Make_Get_and_Set_Methods.bsh

#
Unknown | 320 lines | 281 code | 39 blank | 0 comment | 0 complexity | b2a15ae628cc27805b6ec9dfe8e92b2c 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. * Make_Get_and_Set_Methods.bsh - a BeanShell macro for
  3. * the jEdit text editor - facilitates the creation of
  4. * get() and set() methods from instance variables
  5. * Copyright (C) 2001 John Gellene
  6. * jgellene@nyc.rr.com
  7. * http://community.jedit.org
  8. *
  9. * based on code contributed to the jEdit Macro Guide project
  10. * by Seppo Silaste
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. *
  26. * $Id: Make_Get_and_Set_Methods.bsh 3872 2001-11-06 17:28:22Z jgellene $
  27. *
  28. * Notes on use:
  29. *
  30. * This macro works by grabbing text on the caret line of the text area
  31. * and parsing it to get the first two tokens that are not contained
  32. * in the macro's global string variable 'modifiers'. It then constructs
  33. * a simple get() or set() method from the parsing results, depending
  34. * on which button you push. You will probably generate nonsense if you
  35. * parse a line that does not begin with an instance variable.
  36. *
  37. * The results can be edited in the two text area in the dialog. Pressing
  38. * either of the 'Insert' buttons will cause the contents of the corresponding
  39. * text area to be pasted into the current buffer.
  40. *
  41. * The method parseLine() uses and returns a scripted object 'resultObject()'
  42. * to pass the results of the parsing operation. The absence of an explicit
  43. * return type in the proptotype of parseLine() is necessary to permit this.
  44. *
  45. * If the current buffer's edit mode is Java, the macro will parse Java
  46. * instance variables. Otherwise, the macro will parse C++ variables and
  47. * write C++ methods (with an equal potential for writing nonsense if an data
  48. * member is not being grabbed).
  49. *
  50. * Checked for jEdit 4.0 API
  51. *
  52. */
  53. import javax.swing.border.*;
  54. void makeGetSetDialog()
  55. {
  56. title = "Make get and set methods from caret line text";
  57. dialog = new JDialog(view, title, false);
  58. content = new JPanel(new BorderLayout());
  59. content.setBorder(new EmptyBorder(5, 10, 10, 10));
  60. content.setPreferredSize(new Dimension(480, 320));
  61. dialog.setContentPane(content);
  62. // textPanel holds a getPanel and a setPanel; each of
  63. // the child panels holds a label and a scrolling text area
  64. textPanel = new JPanel(new GridLayout(2, 1, 0, 10));
  65. textPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  66. setPanel = new JPanel(new BorderLayout());
  67. setLabel = new JLabel("set() methods", SwingConstants.LEFT);
  68. setText = new JTextArea();
  69. setPanel.add(setLabel, "North");
  70. setPanel.add(new JScrollPane( setText), "Center");
  71. textPanel.add(setPanel);
  72. getPanel = new JPanel(new BorderLayout());
  73. getLabel = new JLabel("get() methods", SwingConstants.LEFT);
  74. getText = new JTextArea();
  75. getPanel.add(getLabel, "North");
  76. getPanel.add(new JScrollPane( getText), "Center");
  77. textPanel.add(getPanel);
  78. content.add(textPanel, "Center");
  79. buttonPanel = new JPanel(new GridLayout(5, 1, 0, 30));
  80. buttonPanel.setBorder( new EmptyBorder(22, 5, 5, 5));
  81. makeSetButton = new JButton("Make Set");
  82. insertSetButton = new JButton("Insert Set");
  83. doneButton = new JButton("Done");
  84. insertGetButton = new JButton("Insert Get");
  85. makeGetButton = new JButton("Make Get");
  86. buttonPanel.add(makeSetButton);
  87. buttonPanel.add(insertSetButton);
  88. buttonPanel.add(doneButton);
  89. buttonPanel.add(makeGetButton);
  90. buttonPanel.add(insertGetButton);
  91. content.add(buttonPanel, "East");
  92. // action listener for buttons
  93. makeSetButton.addActionListener(this);
  94. insertSetButton.addActionListener(this);
  95. doneButton.addActionListener(this);
  96. insertGetButton.addActionListener(this);
  97. makeGetButton.addActionListener(this);
  98. actionPerformed(e)
  99. {
  100. cmd = e.getActionCommand();
  101. if(cmd.indexOf("Done") != -1)
  102. {
  103. this.dialog.dispose();
  104. return;
  105. }
  106. isGetCmd = (cmd.indexOf("Get") != -1);
  107. if(cmd.indexOf("Insert") != -1)
  108. doInsert(isGetCmd ? this.getText : this.setText);
  109. else if(isGetCmd)
  110. doMakeGet(this.getText);
  111. else
  112. doMakeSet(this.setText);
  113. }
  114. dialog.pack();
  115. dialog.setLocationRelativeTo(view);
  116. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  117. dialog.setVisible(true);
  118. }
  119. void doMakeSet(JTextArea setText)
  120. {
  121. result = parseLine();
  122. if(result.variable.length() == 0) return;
  123. c = result.variable.substring(0,1);
  124. c = c.toUpperCase();
  125. sb = new StringBuffer();
  126. if(USE_JAVA == true)
  127. sb.append("public ");
  128. else
  129. {
  130. sb.append("/* header:\nvoid set");
  131. sb.append(c);
  132. sb.append(result.variable.substring(1));
  133. sb.append("(");
  134. sb.append(result.type);
  135. sb.append(" ");
  136. sb.append(result.variable);
  137. sb.append("Param");
  138. sb.append(");\n*/\n");
  139. }
  140. sb.append("void set");
  141. sb.append(c);
  142. sb.append(result.variable.substring(1));
  143. sb.append("(");
  144. sb.append(result.type);
  145. sb.append(" ");
  146. sb.append(result.variable);
  147. if(USE_JAVA == false)
  148. sb.append("Param");
  149. sb.append(") {\n\t");
  150. if(USE_JAVA == true)
  151. sb.append("this.");
  152. sb.append(result.variable);
  153. sb.append(" = ");
  154. sb.append(result.variable);
  155. if(USE_JAVA == false)
  156. sb.append("Param");
  157. sb.append(";\n}\n");
  158. setText.append(sb.toString());
  159. }
  160. void doMakeGet(JTextArea getText)
  161. {
  162. result = parseLine();
  163. if(result.variable.length() == 0) return;
  164. c = result.variable.substring(0,1);
  165. c = c.toUpperCase();
  166. sb = new StringBuffer();
  167. if(USE_JAVA == true)
  168. sb.append("public ");
  169. else
  170. {
  171. sb.append("/* header:\n");
  172. sb.append(result.type);
  173. sb.append(" get");
  174. sb.append(c);
  175. sb.append(result.variable.substring(1));
  176. sb.append("() const;\n*/\n");
  177. }
  178. sb.append(result.type);
  179. sb.append(" get");
  180. sb.append(c);
  181. sb.append(result.variable.substring(1));
  182. sb.append("()");
  183. if(USE_JAVA == false)
  184. sb.append(" const");
  185. sb.append(" {\n\treturn ");
  186. sb.append(result.variable);
  187. sb.append(";\n}\n");
  188. getText.append(sb.toString());
  189. }
  190. parseLine()
  191. {
  192. // scripted object stores result of parsing
  193. resultObject( t, v)
  194. {
  195. type = t;
  196. variable = v;
  197. return this;
  198. }
  199. line = mainTextArea.getLineText(mainTextArea.getCaretLine()).trim();
  200. if(!(line == null || line.equals("")))
  201. {
  202. tokenizer = new StringTokenizer(line);
  203. if(tokenizer.countTokens() >= 2)
  204. {
  205. // get the first non-modifier token if there is one
  206. returnType = tokenizer.nextToken();
  207. if(USE_JAVA)
  208. {
  209. while( modifiers.indexOf(returnType) != -1
  210. && tokenizer.hasMoreTokens())
  211. returnType = tokenizer.nextToken();
  212. }
  213. if(tokenizer.hasMoreTokens())
  214. {
  215. // a non-modifier token was found and
  216. // there is also an instance variable.
  217. instanceVar = tokenizer.nextToken();
  218. // remove the ; if there is one
  219. if(instanceVar.endsWith(";"))
  220. instanceVar =
  221. instanceVar.substring(0, instanceVar.length() - 1);
  222. // if the code doesn't have a space between the instance
  223. // variable and the possible '=';
  224. // get the correct instance variable.
  225. if(instanceVar.indexOf('=') != -1)
  226. instanceVar =
  227. instanceVar.substring(0, instanceVar.indexOf('='));
  228. return resultObject( returnType, instanceVar);
  229. }
  230. }
  231. }
  232. Macros.message(mainView, "Nothing to parse");
  233. return resultObject( "", "");
  234. }
  235. void doInsert(JTextArea insertText)
  236. {
  237. insert = insertText.getText();
  238. if(insert != null)
  239. mainTextArea.setSelectedText(insert);
  240. }
  241. // main routine
  242. // setting USE_JAVA to false will cause the macro to be suitable
  243. // for reading and writing C++ source code
  244. USE_JAVA = buffer.getMode().getName().equals("java");
  245. modifiers = "public protected private static transient final //";
  246. if(USE_JAVA == false)
  247. modifiers = "//";
  248. // external global variables imported by jEdit are
  249. // not visible in methods called by ActionListener
  250. mainTextArea = textArea;
  251. mainView = view;
  252. makeGetSetDialog();
  253. /*
  254. Macro index data (in DocBook format)
  255. <listitem>
  256. <para><filename>Make_Get_and_Set_Methods.bsh</filename></para>
  257. <abstract><para>
  258. Creates <function>getXXX()</function> or <function>setXXX()</function>
  259. methods that can be pasted into the buffer text.
  260. </para></abstract>
  261. <para>
  262. This macro presents a dialog that will <quote>grab</quote> the names
  263. of instance variables from the caret line of the current buffer
  264. and paste a corresponding <function>getXXX()</function> or
  265. <function>setXXX()</function> method to one of two text areas in the
  266. dialog. The text can be edited in the dialog and then pasted into the
  267. current buffer using the <guilabel>Insert...</guilabel> buttons. If
  268. the caret is set to a line containing something other than an instance
  269. variable, the text grabbing routine is likely to generate nonsense.
  270. </para>
  271. <para>
  272. As explained in the notes accompanying the source code, the macro
  273. uses a global variable which can be set to configure the macro to work
  274. with either Java or C++ code. When set for use with C++ code,
  275. the macro will also write (in commented text) definitions of
  276. <function>getXXX()</function> or <function>setXXX()</function>
  277. suitable for inclusion in a header file.
  278. </para>
  279. </listitem>
  280. */
  281. // end Make_Get_and_Set_Methods.bsh