PageRenderTime 48ms CodeModel.GetById 20ms app.highlight 22ms RepoModel.GetById 2ms 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
 54import javax.swing.border.*;
 55
 56void makeGetSetDialog()
 57{
 58    title = "Make get and set methods from caret line text";
 59
 60    dialog = new JDialog(view, title, false);
 61    content = new JPanel(new BorderLayout());
 62    content.setBorder(new EmptyBorder(5, 10, 10, 10));
 63    content.setPreferredSize(new Dimension(480, 320));
 64    dialog.setContentPane(content);
 65
 66    // textPanel holds a getPanel and a setPanel; each of
 67    // the child panels holds a label and a scrolling text area
 68    textPanel = new JPanel(new GridLayout(2, 1, 0, 10));
 69    textPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
 70
 71    setPanel = new JPanel(new BorderLayout());
 72    setLabel = new JLabel("set() methods", SwingConstants.LEFT);
 73    setText = new JTextArea();
 74    setPanel.add(setLabel, "North");
 75    setPanel.add(new JScrollPane( setText), "Center");
 76    textPanel.add(setPanel);
 77
 78    getPanel = new JPanel(new BorderLayout());
 79    getLabel = new JLabel("get() methods", SwingConstants.LEFT);
 80    getText = new JTextArea();
 81    getPanel.add(getLabel, "North");
 82    getPanel.add(new JScrollPane( getText), "Center");
 83    textPanel.add(getPanel);
 84
 85    content.add(textPanel, "Center");
 86
 87
 88    buttonPanel = new JPanel(new GridLayout(5, 1, 0, 30));
 89    buttonPanel.setBorder( new EmptyBorder(22, 5, 5, 5));
 90
 91    makeSetButton = new JButton("Make Set");
 92    insertSetButton = new JButton("Insert Set");
 93    doneButton = new JButton("Done");
 94    insertGetButton = new JButton("Insert Get");
 95    makeGetButton = new JButton("Make Get");
 96
 97    buttonPanel.add(makeSetButton);
 98    buttonPanel.add(insertSetButton);
 99    buttonPanel.add(doneButton);
100    buttonPanel.add(makeGetButton);
101    buttonPanel.add(insertGetButton);
102
103    content.add(buttonPanel, "East");
104
105    // action listener for buttons
106
107    makeSetButton.addActionListener(this);
108    insertSetButton.addActionListener(this);
109    doneButton.addActionListener(this);
110    insertGetButton.addActionListener(this);
111    makeGetButton.addActionListener(this);
112
113
114    actionPerformed(e)
115    {
116        cmd = e.getActionCommand();
117        if(cmd.indexOf("Done") != -1)
118        {
119            this.dialog.dispose();
120            return;
121        }
122        isGetCmd = (cmd.indexOf("Get") != -1);
123        if(cmd.indexOf("Insert") != -1)
124            doInsert(isGetCmd ? this.getText : this.setText);
125        else if(isGetCmd)
126            doMakeGet(this.getText);
127        else
128            doMakeSet(this.setText);
129    }
130
131
132    dialog.pack();
133    dialog.setLocationRelativeTo(view);
134    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
135    dialog.setVisible(true);
136}
137
138void doMakeSet(JTextArea setText)
139{
140    result = parseLine();
141    if(result.variable.length() == 0) return;
142    c = result.variable.substring(0,1);
143    c = c.toUpperCase();
144    sb = new StringBuffer();
145    if(USE_JAVA == true)
146        sb.append("public ");
147    else
148    {
149        sb.append("/* header:\nvoid set");
150        sb.append(c);
151        sb.append(result.variable.substring(1));
152        sb.append("(");
153        sb.append(result.type);
154        sb.append(" ");
155        sb.append(result.variable);
156        sb.append("Param");
157        sb.append(");\n*/\n");
158    }
159    sb.append("void set");
160    sb.append(c);
161    sb.append(result.variable.substring(1));
162    sb.append("(");
163    sb.append(result.type);
164    sb.append(" ");
165    sb.append(result.variable);
166    if(USE_JAVA == false)
167        sb.append("Param");
168    sb.append(") {\n\t");
169    if(USE_JAVA == true)
170        sb.append("this.");
171    sb.append(result.variable);
172    sb.append(" = ");
173    sb.append(result.variable);
174    if(USE_JAVA == false)
175        sb.append("Param");
176    sb.append(";\n}\n");
177    setText.append(sb.toString());
178}
179
180
181
182void doMakeGet(JTextArea getText)
183{
184    result = parseLine();
185    if(result.variable.length() == 0) return;
186    c = result.variable.substring(0,1);
187    c = c.toUpperCase();
188    sb = new StringBuffer();
189    if(USE_JAVA == true)
190        sb.append("public ");
191    else
192    {
193        sb.append("/* header:\n");
194        sb.append(result.type);
195        sb.append(" get");
196        sb.append(c);
197        sb.append(result.variable.substring(1));
198        sb.append("() const;\n*/\n");
199    }
200
201    sb.append(result.type);
202    sb.append(" get");
203    sb.append(c);
204    sb.append(result.variable.substring(1));
205    sb.append("()");
206    if(USE_JAVA == false)
207        sb.append(" const");
208    sb.append(" {\n\treturn ");
209    sb.append(result.variable);
210    sb.append(";\n}\n");
211    getText.append(sb.toString());
212}
213
214parseLine()
215{
216    // scripted object stores result of parsing
217    resultObject( t, v)
218    {
219        type = t;
220        variable = v;
221        return this;
222    }
223    line = mainTextArea.getLineText(mainTextArea.getCaretLine()).trim();
224    if(!(line == null || line.equals("")))
225    {
226        tokenizer = new StringTokenizer(line);
227        if(tokenizer.countTokens() >= 2)
228        {
229            // get the first non-modifier token if there is one
230            returnType = tokenizer.nextToken();
231            if(USE_JAVA)
232            {
233                while( modifiers.indexOf(returnType) != -1
234                        && tokenizer.hasMoreTokens())
235                        returnType = tokenizer.nextToken();
236            }
237
238            if(tokenizer.hasMoreTokens())
239            {
240                // a non-modifier token was found and
241                // there is also an instance variable.
242                instanceVar = tokenizer.nextToken();
243                // remove the ; if there is one
244                if(instanceVar.endsWith(";"))
245                instanceVar =
246                instanceVar.substring(0, instanceVar.length() - 1);
247                // if the code doesn't have a space between the instance
248                // variable and the possible '=';
249                // get the correct instance variable.
250                if(instanceVar.indexOf('=') != -1)
251                    instanceVar =
252                        instanceVar.substring(0, instanceVar.indexOf('='));
253                return resultObject( returnType, instanceVar);
254            }
255        }
256    }
257    Macros.message(mainView, "Nothing to parse");
258    return resultObject( "", "");
259}
260
261
262
263void doInsert(JTextArea insertText)
264{
265    insert = insertText.getText();
266    if(insert != null)
267        mainTextArea.setSelectedText(insert);
268}
269
270
271
272// main routine
273
274// setting USE_JAVA to false will cause the macro to be suitable
275// for reading and writing C++ source code
276USE_JAVA = buffer.getMode().getName().equals("java");
277modifiers = "public protected private static transient final //";
278if(USE_JAVA == false)
279    modifiers = "//";
280
281// external global variables imported by jEdit are
282// not visible in methods called by ActionListener
283mainTextArea = textArea;
284mainView = view;
285
286makeGetSetDialog();
287
288/*
289	Macro index data (in DocBook format)
290
291<listitem>
292    <para><filename>Make_Get_and_Set_Methods.bsh</filename></para>
293    <abstract><para>
294        Creates <function>getXXX()</function> or <function>setXXX()</function>
295        methods that can be pasted into the buffer text.
296    </para></abstract>
297    <para>
298        This macro presents a dialog that will <quote>grab</quote> the names
299        of instance variables from the caret line of the current buffer
300        and paste a corresponding <function>getXXX()</function> or
301        <function>setXXX()</function> method to one of two text areas in the
302        dialog.  The text can be edited in the dialog and then pasted into the
303        current buffer using the <guilabel>Insert...</guilabel> buttons.  If
304        the caret is set to a line containing something other than an instance
305        variable, the text grabbing routine is likely to generate nonsense.
306    </para>
307    <para>
308        As explained in the notes accompanying the source code, the macro
309        uses a global variable which can be set to configure the macro to work
310        with either Java or C++ code.  When set for use with C++ code,
311        the macro will also write (in commented text) definitions of
312        <function>getXXX()</function> or <function>setXXX()</function>
313        suitable for inclusion in a header file.
314    </para>
315</listitem>
316
317*/
318
319// end Make_Get_and_Set_Methods.bsh
320