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