/jEdit/branches/lp_clone_test/macros/Java/Make_Get_and_Set_Methods.bsh

# · Unknown · 337 lines · 282 code · 55 blank · 0 comment · 0 complexity · 0eb90586f342f0394095863e2f1e80ad MD5 · raw file

  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. Modifications by Dale Anson, Dec 2008:
  17. 1. Allows variable declarations to have an initial assignment, like
  18. <code>
  19. public int foo = 1;
  20. public int bar = 2;
  21. </code>
  22. 2. Allows multiple variables on same line, like
  23. <code>
  24. public int foo, bar;
  25. </code>
  26. 3. Use line separator as set in buffer properties rather than always using \n.
  27. This program is free software; you can redistribute it and/or
  28. modify it under the terms of the GNU General Public License
  29. as published by the Free Software Foundation; either version 2
  30. of the License, or any later version.
  31. This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. GNU General Public License for more details.
  35. */
  36. boolean JAVA_MODE = buffer.getMode().getName().equals( "java" );
  37. // use line separator from current buffer
  38. String LS = buffer.getStringProperty( "lineSeparator" );
  39. if (LS == null) {
  40. // otherwise, use default line separator
  41. LS = jEdit.getProperty("buffer.lineSeparator");
  42. }
  43. boolean createGetMethods = true;
  44. boolean createSetMethods = true;
  45. void setCaret( int selectionStart, int selectionEnd ) {
  46. textArea.setCaretPosition( selectionStart );
  47. textArea.moveCaretPosition( selectionEnd );
  48. }
  49. String getClassName() {
  50. int selectionStart = textArea.getSelectionStart();
  51. int selectionEnd = textArea.getSelectionEnd();
  52. String text = textArea.getText();
  53. int index = text.lastIndexOf( "class", selectionStart );
  54. if ( index != -1 ) {
  55. textArea.setCaretPosition( index );
  56. int lineNumber = textArea.getCaretLine();
  57. int lineEnd = textArea.getLineEndOffset( lineNumber );
  58. String lineText = text.substring( index, lineEnd );
  59. StringTokenizer tokenizer = new StringTokenizer( lineText );
  60. tokenizer.nextToken(); //eat "class"
  61. if ( tokenizer.hasMoreTokens() ) {
  62. setCaret( selectionStart, selectionEnd );
  63. return tokenizer.nextToken();
  64. }
  65. }
  66. setCaret( selectionStart, selectionEnd );
  67. String fileClassName = buffer.getName();
  68. int index = fileClassName.lastIndexOf( '.' );
  69. if ( index != -1 ) {
  70. fileClassName = fileClassName.substring( 0, index );
  71. if ( fileClassName.toLowerCase().indexOf( "untitled" ) == -1 ) {
  72. return fileClassName;
  73. }
  74. }
  75. return "";
  76. }
  77. String createJavaGetMethod( String type, String variableName ) {
  78. String uppperVariable = Character.toUpperCase( variableName.charAt( 0 ) ) + variableName.substring( 1, variableName.length() );
  79. String result =
  80. "\t/**" + LS +
  81. "\t * Returns the value of " + variableName + "." + LS +
  82. "\t */" + LS +
  83. "\tpublic " + type + " get" + uppperVariable + "() {" + LS +
  84. "\t\treturn " + variableName + ";" + LS +
  85. "\t}" + LS;
  86. return result;
  87. }
  88. String createJavaSetMethod( String type, String variableName ) {
  89. String uppperVariable = Character.toUpperCase( variableName.charAt( 0 ) ) + variableName.substring( 1, variableName.length() );
  90. String result =
  91. "\t/**" + LS +
  92. "\t * Sets the value of " + variableName + "." + LS +
  93. "\t * @param " + variableName + " The value to assign " + variableName + "." + LS +
  94. "\t */" + LS +
  95. "\tpublic void set" + uppperVariable + "(" + type + " " + variableName + ") {" + LS +
  96. "\t\tthis." + variableName + " = " + variableName + ";" + LS +
  97. "\t}" + LS;
  98. return result;
  99. }
  100. String createCppGetMethod( String className, String type, String variableName ) {
  101. String scopeIndicator = "";
  102. if ( className != null && className.compareTo( "" ) != 0 ) {
  103. scopeIndicator = className + "::";
  104. }
  105. if (type == null) {
  106. type = "";
  107. }
  108. System.out.println("7 " + className + ", " + type + ", " + variableName);
  109. String uppperVariable = Character.toUpperCase( variableName.charAt( 0 ) ) + variableName.substring( 1, variableName.length() );
  110. System.out.println("8");
  111. String result =
  112. "/*" + LS +
  113. "function: get" + uppperVariable + "()" + LS +
  114. "Returns the value of " + variableName + "." + LS +
  115. "*/" + LS +
  116. type + (type.length() > 0 ? " " : "") + scopeIndicator + "get" + uppperVariable + "()" + "" + LS +
  117. "{" + "" + LS +
  118. " return " + variableName + ";" + "" + LS +
  119. "}" + LS;
  120. return result;
  121. }
  122. String createCppSetMethod( String className, String type, String variableName ) {
  123. String scopeIndicator = "";
  124. if ( className != null && className.compareTo( "" ) != 0 ) {
  125. scopeIndicator = className + "::";
  126. }
  127. String uppperVariable = Character.toUpperCase( variableName.charAt( 0 ) ) + variableName.substring( 1, variableName.length() );
  128. String setVariable = variableName + "Value";
  129. String result =
  130. "/*" + LS +
  131. "function: set" + uppperVariable + "()" + LS +
  132. "Sets the value of " + variableName + "." + LS +
  133. "Input: " + setVariable + " The value to assign " + variableName + "." + LS +
  134. "*/" + LS +
  135. "void " + scopeIndicator + "set" + uppperVariable + "(const " + type + "& " + setVariable + ")" + LS +
  136. "{" + "" + LS +
  137. " " + variableName + " = " + setVariable + ";" + "" + LS +
  138. "}" + LS;
  139. return result;
  140. }
  141. void parseSelection() {
  142. int selectionStart = textArea.getSelectionStart();
  143. int selectionEnd = textArea.getSelectionEnd();
  144. textArea.setCaretPosition( selectionStart );
  145. int startLine = textArea.getCaretLine();
  146. textArea.setCaretPosition( selectionEnd );
  147. int endLine = textArea.getCaretLine();
  148. StringBuffer code = new StringBuffer();
  149. String className = getClassName();
  150. for ( int i = startLine; i <= endLine; i++ ) {
  151. // parse each line for variable declaration
  152. String lineText = textArea.getLineText( i );
  153. if ( lineText != null && lineText.length() > 0 ) {
  154. System.out.println("1");
  155. // remove leading and trailing whitespace
  156. lineText = lineText.trim();
  157. if ( lineText.length() == 0 ) {
  158. continue; // nothing to do with this line
  159. }
  160. // remove semi-colon
  161. if ( lineText.endsWith( ";" ) ) {
  162. lineText = lineText.substring( 0, lineText.length() - 1 );
  163. }
  164. System.out.println("2");
  165. // remove initial assignment if present
  166. if ( lineText.indexOf( "=" ) > 0 ) {
  167. lineText = lineText.substring( 0, lineText.indexOf( "=" ) );
  168. }
  169. System.out.println("3");
  170. lineText = lineText.trim();
  171. if ( lineText.length() == 0 ) {
  172. continue;
  173. }
  174. System.out.println("4");
  175. // list to hold variable names
  176. ArrayList variables = new ArrayList();
  177. // could have declaration like int x, y; so split them out into the
  178. // variables array
  179. if ( lineText.indexOf( "," ) > 0 ) {
  180. int index = lineText.indexOf( "," ); // just after first variable name
  181. String front = lineText.substring( 0, index ); // up to and including the first variable name
  182. String back = lineText.substring( index ); // remaining variable names
  183. lineText = front.substring( 0, front.lastIndexOf( " " ) ); // adjust remaining line text, this contains the type
  184. front = front.substring( front.lastIndexOf( " " ) ).trim(); // first variable name
  185. String[] backs = back.split( "," ); // remaining variable names
  186. variables.add( front ); // add first variable name to the list
  187. for ( String back : backs ) { // add remaining variable names to the list
  188. String maybe = back.trim();
  189. if ( maybe.length() > 0 ) {
  190. variables.add( maybe );
  191. }
  192. }
  193. }
  194. else {
  195. // just one variable declared
  196. String var = lineText.substring( lineText.lastIndexOf( " " ) ).trim();
  197. variables.add( var );
  198. lineText = lineText.substring( 0, lineText.lastIndexOf( " " ) ).trim();
  199. }
  200. System.out.println("5");
  201. if ( lineText.trim().length() == 0 ) {
  202. continue; // no type declared for this variable
  203. }
  204. // get the variable type
  205. String type = "";
  206. if (lineText.lastIndexOf( " " ) > 0) {
  207. type = lineText.substring( lineText.lastIndexOf( " " ) );
  208. }
  209. type = type.trim();
  210. System.out.println("5.1");
  211. if ( variables.size() > 0 ) {
  212. code.append( LS );
  213. }
  214. // create the get and set methods for each variable
  215. for ( String variable : variables ) {
  216. if ( createGetMethods ) {
  217. String tmp = JAVA_MODE ? createJavaGetMethod( type, variable ) : createCppGetMethod( className, type, variable );
  218. if ( tmp != null && tmp.length() > 0 ) {
  219. code.append( tmp ).append( LS );
  220. }
  221. }
  222. if ( createSetMethods && lineText.indexOf( "final " ) == -1 && lineText.indexOf( "const " ) == -1 ) {
  223. String tmp = JAVA_MODE ? createJavaSetMethod( type, variable ) : createCppSetMethod( className, type, variable );
  224. if ( tmp != null && tmp.compareTo( "" ) != 0 ) {
  225. code.append( LS ).append( tmp ).append( LS );
  226. }
  227. }
  228. }
  229. }
  230. }
  231. // move to the end of the selected text
  232. textArea.setCaretPosition( selectionEnd );
  233. // insert get/set methods
  234. textArea.setSelectedText( code.toString() );
  235. // select the inserted code and indent it
  236. textArea.setCaretPosition( selectionEnd );
  237. textArea.moveCaretPosition( selectionEnd + code.length(), true );
  238. textArea.indentSelectedLines();
  239. }
  240. void displayPrompt() {
  241. String DONE = "Generate Code";
  242. String CANCEL = "Cancel";
  243. JCheckBox getCheckbox = new JCheckBox( "Create Get Methods", true );
  244. JCheckBox setCheckbox = new JCheckBox( "Create Set Methods", true );
  245. JPanel checkBoxPanel = new JPanel(new BorderLayout());
  246. checkBoxPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
  247. checkBoxPanel.add( getCheckbox, BorderLayout.NORTH );
  248. checkBoxPanel.add( setCheckbox, BorderLayout.SOUTH );
  249. JButton createButton = new JButton( DONE );
  250. JButton cancelButton = new JButton( CANCEL );
  251. JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 0));
  252. buttonPanel.setBorder(BorderFactory.createEmptyBorder(11, 11, 11, 11));
  253. buttonPanel.add( createButton, BorderLayout.WEST );
  254. buttonPanel.add( cancelButton, BorderLayout.EAST );
  255. JPanel mainPanel = new JPanel();
  256. mainPanel.setLayout( new BorderLayout() );
  257. mainPanel.add( checkBoxPanel, BorderLayout.NORTH );
  258. mainPanel.add( buttonPanel, BorderLayout.SOUTH );
  259. String title = "Create Get and Set Methods";
  260. JDialog dialog = new JDialog( view, title, false );
  261. dialog.setContentPane( mainPanel );
  262. actionPerformed( ActionEvent e ) {
  263. if ( e.getSource() == createButton ) {
  264. createGetMethods = getCheckbox.isSelected();
  265. createSetMethods = setCheckbox.isSelected();
  266. parseSelection();
  267. }
  268. this.dialog.dispose();
  269. return ;
  270. }
  271. createButton.addActionListener( this );
  272. cancelButton.addActionListener( this );
  273. dialog.pack();
  274. dialog.setLocationRelativeTo( view );
  275. dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
  276. dialog.setVisible( true );
  277. createButton.requestFocus();
  278. }
  279. if ( buffer.isReadOnly() )
  280. Macros.error( view, "Buffer is read-only." );
  281. else
  282. displayPrompt();