PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/Java/Make_Get_and_Set_Methods.bsh

#
Unknown | 297 lines | 250 code | 47 blank | 0 comment | 0 complexity | 43a5d6637e031d1d2714691e3884bc64 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_Functions.bsh - a BeanShell macro for
  3. the jEdit text editor that creates simple get() and set()
  4. methods for the variables on selected lines.
  5. Copyright (C) 2004 Thomas Galvin - software@thomas-galvin.com
  6. based on Make_Get_and_Set_Methods.bsh by John Gellene
  7. This macro will work on multiple selected lines; for instance,
  8. selecting
  9. <code>
  10. public int foo;
  11. public int bar;
  12. </code>
  13. and running the macro will produce get and set functions for both
  14. variables, along with comments. This macro produces c-style
  15. functions, unless the buffer is in java mode.
  16. This program is free software; you can redistribute it and/or
  17. modify it under the terms of the GNU General Public License
  18. as published by the Free Software Foundation; either version 2
  19. of the License, or any later version.
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24. */
  25. boolean JAVA_MODE = buffer.getMode().getName().equals("java");
  26. boolean createGetMethods = true;
  27. boolean createSetMethods = true;
  28. void setCaret(int selectionStart, int selectionEnd)
  29. {
  30. textArea.setCaretPosition(selectionStart);
  31. textArea.moveCaretPosition(selectionEnd);
  32. }
  33. String getClassName()
  34. {
  35. int selectionStart = textArea.getSelectionStart();
  36. int selectionEnd = textArea.getSelectionEnd();
  37. String text = textArea.getText();
  38. int index = text.lastIndexOf("class", selectionStart);
  39. if(index != -1)
  40. {
  41. textArea.setCaretPosition(index);
  42. int lineNumber = textArea.getCaretLine();
  43. int lineEnd = textArea.getLineEndOffset(lineNumber);
  44. String lineText = text.substring(index, lineEnd);
  45. StringTokenizer tokenizer = new StringTokenizer(lineText);
  46. tokenizer.nextToken(); //eat "class"
  47. if(tokenizer.hasMoreTokens())
  48. {
  49. setCaret(selectionStart, selectionEnd);
  50. return tokenizer.nextToken();
  51. }
  52. }
  53. setCaret(selectionStart, selectionEnd);
  54. String fileClassName = buffer.getName();
  55. int index = fileClassName.lastIndexOf('.');
  56. if(index != -1)
  57. {
  58. fileClassName = fileClassName.substring(0, index);
  59. if(fileClassName.toLowerCase().indexOf("untitled") == -1)
  60. {
  61. return fileClassName;
  62. }
  63. }
  64. return "";
  65. }
  66. String createJavaGetMethod(String type, String variableName)
  67. {
  68. String uppperVariable = Character.toUpperCase(variableName.charAt(0)) + variableName.substring(1, variableName.length());
  69. String result =
  70. "\t/**\n" +
  71. "\t * Returns the value of " + variableName + ".\n" +
  72. "\t */\n" +
  73. "\tpublic " + type + " get" + uppperVariable + "()" + "\n" +
  74. "\t{" + "\n" +
  75. "\t\treturn " + variableName + ";" + "\n" +
  76. "\t}\n";
  77. return result;
  78. }
  79. String createJavaSetMethod(String type, String variableName)
  80. {
  81. String uppperVariable = Character.toUpperCase(variableName.charAt(0)) + variableName.substring(1, variableName.length());
  82. String result =
  83. "\t/**\n" +
  84. "\t * Sets the value of " + variableName + ".\n" +
  85. "\t * @param " + variableName + " The value to assign " + variableName + ".\n" +
  86. "\t */\n" +
  87. "\tpublic void set" + uppperVariable + "(" + type + " " + variableName + ")\n" +
  88. "\t{" + "\n" +
  89. "\t\tthis." + variableName + " = " + variableName + ";" + "\n" +
  90. "\t}\n";
  91. return result;
  92. }
  93. String createCppGetMethod(String className, String type, String variableName)
  94. {
  95. String scopeIndicator = "";
  96. if(className != null && className.compareTo("") != 0)
  97. {
  98. scopeIndicator = className + "::";
  99. }
  100. String uppperVariable = Character.toUpperCase(variableName.charAt(0)) + variableName.substring(1, variableName.length());
  101. String result =
  102. "/*\n" +
  103. "function: get" + uppperVariable + "()\n" +
  104. "Returns the value of " + variableName + ".\n" +
  105. "*/\n" +
  106. type + " " + scopeIndicator + "get" + uppperVariable + "()" + "\n" +
  107. "{" + "\n" +
  108. " return " + variableName + ";" + "\n" +
  109. "}\n";
  110. return result;
  111. }
  112. String createCppSetMethod(String className, String type, String variableName)
  113. {
  114. String scopeIndicator = "";
  115. if(className != null && className.compareTo("") != 0)
  116. {
  117. scopeIndicator = className + "::";
  118. }
  119. String uppperVariable = Character.toUpperCase(variableName.charAt(0)) + variableName.substring(1, variableName.length());
  120. String setVariable = variableName + "Value";
  121. String result =
  122. "/*\n" +
  123. "function: set" + uppperVariable + "()\n" +
  124. "Sets the value of " + variableName + ".\n" +
  125. "Input: " + setVariable + " The value to assign " + variableName + ".\n" +
  126. "*/\n" +
  127. "void " + scopeIndicator + "set" + uppperVariable + "(const " + type + "& " + setVariable + ")\n" +
  128. "{" + "\n" +
  129. " " + variableName + " = " + setVariable + ";" + "\n" +
  130. "}\n";
  131. return result;
  132. }
  133. void parseSelection()
  134. {
  135. int selectionStart = textArea.getSelectionStart();
  136. int selectionEnd = textArea.getSelectionEnd();
  137. textArea.setCaretPosition(selectionStart);
  138. int startLine = textArea.getCaretLine();
  139. textArea.setCaretPosition(selectionEnd);
  140. int endLine = textArea.getCaretLine();
  141. StringBuffer code = new StringBuffer();
  142. String className = getClassName();
  143. for(int i = startLine; i <= endLine; i++)
  144. {
  145. String lineText = textArea.getLineText(i);
  146. if( lineText != null && !lineText.equals("") )
  147. {
  148. lineText = lineText.trim();
  149. if( lineText.endsWith(";") )
  150. {
  151. lineText = lineText.substring( 0, lineText.length() -1 );
  152. }
  153. StringTokenizer tokenizer = new StringTokenizer(lineText);
  154. int tokenCount = tokenizer.countTokens();
  155. if(tokenCount >= 2)
  156. {
  157. int numGarbage = tokenCount - 2;
  158. for (int i = 0; i < numGarbage; i++)
  159. {
  160. tokenizer.nextToken();
  161. }
  162. String type = tokenizer.nextToken();
  163. String variable = tokenizer.nextToken();
  164. if(createGetMethods)
  165. {
  166. String tmp = "";
  167. if(JAVA_MODE)
  168. {
  169. tmp = createJavaGetMethod(type, variable);
  170. }
  171. else
  172. {
  173. tmp = createCppGetMethod(className, type, variable);
  174. }
  175. if(tmp != null && tmp.compareTo("") != 0)
  176. {
  177. code.append(tmp + "\n");
  178. }
  179. }
  180. if(createSetMethods && lineText.indexOf("final ") == -1 && lineText.indexOf("const ") == -1)
  181. {
  182. String tmp = "\n";
  183. if(JAVA_MODE)
  184. {
  185. tmp = createJavaSetMethod(type, variable);
  186. }
  187. else
  188. {
  189. tmp = createCppSetMethod(className, type, variable);
  190. }
  191. if(tmp != null && tmp.compareTo("") != 0)
  192. {
  193. code.append(tmp + "\n");
  194. }
  195. }
  196. }
  197. }
  198. }
  199. textArea.setCaretPosition(selectionEnd);
  200. textArea.setSelectedText(code.toString());
  201. textArea.setCaretPosition(selectionEnd);
  202. textArea.moveCaretPosition(selectionEnd + code.length(), true);
  203. textArea.indentSelectedLines();
  204. }
  205. void displayPrompt()
  206. {
  207. String DONE = "Generate Code";
  208. String CANCEL = " Cancel ";
  209. JCheckBox getCheckbox = new JCheckBox("Create Get Methods", true);
  210. JCheckBox setCheckbox = new JCheckBox("Create Set Methods", true);
  211. JPanel checkBoxPanel = new JPanel();
  212. checkBoxPanel.setLayout(new BorderLayout());
  213. checkBoxPanel.add(getCheckbox, BorderLayout.NORTH);
  214. checkBoxPanel.add(setCheckbox, BorderLayout.SOUTH);
  215. JButton createButton = new JButton(DONE);
  216. JButton cancelButton = new JButton(CANCEL);
  217. JPanel buttonPanel = new JPanel();
  218. buttonPanel.setLayout(new BorderLayout());
  219. buttonPanel.add(createButton, BorderLayout.WEST);
  220. buttonPanel.add(cancelButton, BorderLayout.EAST);
  221. JPanel mainPanel = new JPanel();
  222. mainPanel.setLayout(new BorderLayout());
  223. mainPanel.add(checkBoxPanel, BorderLayout.NORTH);
  224. mainPanel.add(buttonPanel, BorderLayout.SOUTH);
  225. String title = "Create Get and Set Methods";
  226. JDialog dialog = new JDialog(view, title, false);
  227. dialog.setContentPane(mainPanel);
  228. actionPerformed(ActionEvent e)
  229. {
  230. if(e.getSource() == createButton)
  231. {
  232. createGetMethods = getCheckbox.isSelected();
  233. createSetMethods = setCheckbox.isSelected();
  234. parseSelection();
  235. }
  236. this.dialog.dispose();
  237. return;
  238. }
  239. createButton.addActionListener(this);
  240. cancelButton.addActionListener(this);
  241. dialog.pack();
  242. dialog.setLocationRelativeTo(view);
  243. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  244. dialog.setVisible(true);
  245. createButton.requestFocus();
  246. }
  247. if( buffer.isReadOnly() )
  248. Macros.error( view, "Buffer is read-only." );
  249. else
  250. displayPrompt();