PageRenderTime 48ms CodeModel.GetById 43ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/macros/Text/Add_Prefix_and_Suffix.bsh

#
Unknown | 160 lines | 144 code | 16 blank | 0 comment | 0 complexity | 037511f8397c5330b4563837a3f6a2d7 MD5 | raw file
  1/*
  2 * Add_Prefix_and_Suffix.bsh - a BeanShell macro script for the
  3 * jEdit text editor - obtains and processes input for prefix and
  4 * suffix text to be inserted in selected lines in the current
  5 * editing buffer
  6 * Copyright (C) 2001 John Gellene
  7 * jgellene@nyc.rr.com
  8 * http://community.jedit.org
  9 *
 10 * This program is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU General Public License
 12 * as published by the Free Software Foundation; either version 2
 13 * of the License, or any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 23 *
 24 * $Id: Add_Prefix_and_Suffix.bsh 4068 2002-03-06 05:10:27Z spestov $
 25 *
 26 * Notes on use:
 27 *
 28 * If no text is selected, the macro will operate on the current line.
 29 *
 30 * The caret position is part of the selected text; if the caret is at
 31 * the beginning of a line, the macro will operate on that line.
 32 *
 33 * The use of HistoryTextField objects allows the macro to 'remember'
 34 * past entries for the prefix and suffix.
 35 *
 36 * Checked for jEdit 4.0 API
 37 *
 38 */
 39
 40// beginning of Add_suffix_and_prefix.bsh
 41
 42// import statements
 43import javax.swing.border.*;
 44
 45// main routine
 46void prefixSuffixDialog()
 47{
 48    // create dialog object and set its features
 49    title = "Add prefix and suffix to selected lines";
 50    dialog = new JDialog(view, title, false);
 51    content = new JPanel(new BorderLayout());
 52    content.setBorder(new EmptyBorder(12, 12, 12, 12));
 53    dialog.setContentPane(content);
 54
 55    // add to the dialog a panel containing the text fields for
 56    // entry of the prefix and suffix text
 57    fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
 58    prefixField = new HistoryTextField("macro.add-prefix");
 59    prefixLabel = new JLabel("Prefix to add:");
 60    suffixField = new HistoryTextField("macro.add-suffix");
 61    suffixLabel = new JLabel("Suffix to add:");
 62    fieldPanel.add(prefixLabel);
 63    fieldPanel.add(prefixField);
 64    fieldPanel.add(suffixLabel);
 65    fieldPanel.add(suffixField);
 66    content.add(fieldPanel, "Center");
 67
 68    // add a panel containing the buttons
 69    buttonPanel = new JPanel();
 70    buttonPanel.setLayout(new BoxLayout(buttonPanel,
 71        BoxLayout.X_AXIS));
 72    buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50));
 73    buttonPanel.add(Box.createGlue());
 74    ok = new JButton("OK");
 75    cancel = new JButton("Cancel");
 76    ok.setPreferredSize(cancel.getPreferredSize());
 77    dialog.getRootPane().setDefaultButton(ok);
 78    buttonPanel.add(ok);
 79    buttonPanel.add(Box.createHorizontalStrut(6));
 80    buttonPanel.add(cancel);
 81    buttonPanel.add(Box.createGlue());
 82    content.add(buttonPanel, "South");
 83
 84    // register this method as an ActionListener for
 85    // the buttons and text fields
 86    ok.addActionListener(this);
 87    cancel.addActionListener(this);
 88    prefixField.addActionListener(this);
 89    suffixField.addActionListener(this);
 90
 91    // locate the dialog in the center of the
 92    // editing pane and make it visible
 93    dialog.pack();
 94    dialog.setLocationRelativeTo(view);
 95    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 96    dialog.setVisible(true);
 97
 98    // this method will be called when a button is clicked
 99    // or when ENTER is pressed
100    void actionPerformed(e)
101    {
102        if(e.getSource() != cancel)
103        {
104            processText();
105        }
106        dialog.dispose();
107    }
108
109    // this is where the work gets done to insert
110    // the prefix and suffix
111    void processText()
112    {
113        prefix = prefixField.getText();
114        suffix = suffixField.getText();
115        if(prefix.length() == 0 && suffix.length() == 0)
116            return;
117        prefixField.addCurrentToHistory();
118        suffixField.addCurrentToHistory();
119
120        // text manipulation begins here using calls
121        // to jEdit methods
122        buffer.beginCompoundEdit();
123        selectedLines = textArea.getSelectedLines();
124        for(i = 0; i < selectedLines.length; ++i)
125        {
126            offsetBOL = textArea.getLineStartOffset(selectedLines[i]);
127            textArea.setCaretPosition(offsetBOL);
128            textArea.goToStartOfWhiteSpace(false);
129            textArea.goToEndOfWhiteSpace(true);
130            text = textArea.getSelectedText();
131            if(text == null) text = "";
132            textArea.setSelectedText(prefix + text + suffix);
133        }
134        buffer.endCompoundEdit();
135    }
136}
137
138// this single line of code is the script's main routine
139// it calls the methods and exits
140prefixSuffixDialog();
141
142/*
143	Macro index data (in DocBook format)
144
145<listitem>
146    <para><filename>Add_Prefix_and_Suffix.bsh</filename></para>
147    <abstract><para>
148        Adds user-supplied <quote>prefix</quote> and <quote>suffix</quote>
149        text to each line in a group of selected lines.
150    </para></abstract>
151    <para>
152        Text is added after leading whitespace and before trailing whitespace.
153        A dialog window receives input and <quote>remembers</quote> past entries.
154    </para>
155</listitem>
156
157*/
158
159// end Add_Prefix_and_Suffix.bsh
160