PageRenderTime 200ms CodeModel.GetById 48ms app.highlight 3ms RepoModel.GetById 143ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre14/macros/Text/Add_Prefix_and_Suffix.bsh

#
Unknown | 162 lines | 145 code | 17 blank | 0 comment | 0 complexity | 84b831fafc36e8fd58c87f209380aea1 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 4988 2004-03-08 04:29:12Z 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
 37// beginning of Add_suffix_and_prefix.bsh
 38
 39// import statements
 40import javax.swing.border.*;
 41
 42// main routine
 43void prefixSuffixDialog(View view)
 44{
 45	this.view = view;
 46
 47    // create dialog object and set its features
 48    title = "Add prefix and suffix to selected lines";
 49    dialog = new JDialog(view, title, false);
 50    content = new JPanel(new BorderLayout());
 51    content.setBorder(new EmptyBorder(12, 12, 12, 12));
 52    dialog.setContentPane(content);
 53
 54    // add to the dialog a panel containing the text fields for
 55    // entry of the prefix and suffix text
 56    fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
 57    prefixField = new HistoryTextField("macro.add-prefix");
 58    prefixLabel = new JLabel("Prefix to add:");
 59    suffixField = new HistoryTextField("macro.add-suffix");
 60    suffixLabel = new JLabel("Suffix to add:");
 61    fieldPanel.add(prefixLabel);
 62    fieldPanel.add(prefixField);
 63    fieldPanel.add(suffixLabel);
 64    fieldPanel.add(suffixField);
 65    content.add(fieldPanel, "Center");
 66
 67    // add a panel containing the buttons
 68    buttonPanel = new JPanel();
 69    buttonPanel.setLayout(new BoxLayout(buttonPanel,
 70        BoxLayout.X_AXIS));
 71    buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50));
 72    buttonPanel.add(Box.createGlue());
 73    ok = new JButton("OK");
 74    cancel = new JButton("Cancel");
 75    ok.setPreferredSize(cancel.getPreferredSize());
 76    dialog.getRootPane().setDefaultButton(ok);
 77    buttonPanel.add(ok);
 78    buttonPanel.add(Box.createHorizontalStrut(6));
 79    buttonPanel.add(cancel);
 80    buttonPanel.add(Box.createGlue());
 81    content.add(buttonPanel, "South");
 82
 83    // register this method as an ActionListener for
 84    // the buttons and text fields
 85    ok.addActionListener(this);
 86    cancel.addActionListener(this);
 87    prefixField.addActionListener(this);
 88    suffixField.addActionListener(this);
 89
 90    // locate the dialog in the center of the
 91    // editing pane and make it visible
 92    dialog.pack();
 93    dialog.setLocationRelativeTo(view);
 94    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 95    dialog.setVisible(true);
 96
 97    // this method will be called when a button is clicked
 98    // or when ENTER is pressed
 99    void actionPerformed(e)
100    {
101        if(e.getSource() != cancel)
102        {
103            processText();
104        }
105        dialog.dispose();
106    }
107
108    // this is where the work gets done to insert
109    // the prefix and suffix
110    void processText()
111    {
112        prefix = prefixField.getText();
113        suffix = suffixField.getText();
114        if(prefix.length() == 0 && suffix.length() == 0)
115            return;
116        prefixField.addCurrentToHistory();
117        suffixField.addCurrentToHistory();
118
119        // text manipulation begins here using calls
120        // to jEdit methods
121        buffer.beginCompoundEdit();
122        selectedLines = textArea.getSelectedLines();
123        for(i = 0; i < selectedLines.length; ++i)
124        {
125            offsetBOL = textArea.getLineStartOffset(selectedLines[i]);
126            textArea.setCaretPosition(offsetBOL);
127            textArea.goToStartOfWhiteSpace(false);
128            textArea.goToEndOfWhiteSpace(true);
129            text = textArea.getSelectedText();
130            if(text == null) text = "";
131            textArea.setSelectedText(prefix + text + suffix);
132        }
133        buffer.endCompoundEdit();
134    }
135}
136
137// this single line of code is the script's main routine
138// it calls the methods and exits
139if(buffer.isReadOnly())
140	Macros.error(view, "Buffer is read-only.");
141else
142	prefixSuffixDialog(view);
143
144/*
145	Macro index data (in DocBook format)
146
147<listitem>
148    <para><filename>Add_Prefix_and_Suffix.bsh</filename></para>
149    <abstract><para>
150        Adds user-supplied <quote>prefix</quote> and <quote>suffix</quote>
151        text to each line in a group of selected lines.
152    </para></abstract>
153    <para>
154        Text is added after leading whitespace and before trailing whitespace.
155        A dialog window receives input and <quote>remembers</quote> past entries.
156    </para>
157</listitem>
158
159*/
160
161// end Add_Prefix_and_Suffix.bsh
162