001: /*
002: * SelectLineRange.java - Selects a range of lines
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2000 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import javax.swing.border.*;
028: import java.awt.*;
029: import java.awt.event.*;
030: import org.gjt.sp.jedit.textarea.*;
031: import org.gjt.sp.jedit.*;
032:
033: //}}}
034:
035: public class SelectLineRange extends EnhancedDialog implements
036: ActionListener {
037: //{{{ SelectLineRange constructor
038: public SelectLineRange(View view) {
039: super (view, jEdit.getProperty("selectlinerange.title"), true);
040: this .view = view;
041:
042: JPanel content = new JPanel(new BorderLayout());
043: content.setBorder(new EmptyBorder(12, 12, 12, 0));
044: setContentPane(content);
045:
046: JLabel label = new JLabel(jEdit
047: .getProperty("selectlinerange.caption"));
048: label.setBorder(new EmptyBorder(0, 0, 6, 12));
049: content.add(BorderLayout.NORTH, label);
050:
051: JPanel panel = createFieldPanel();
052:
053: content.add(BorderLayout.CENTER, panel);
054:
055: panel = new JPanel();
056: panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
057: panel.setBorder(new EmptyBorder(6, 0, 0, 12));
058: panel.add(Box.createGlue());
059: panel.add(Box.createGlue());
060: ok = new JButton(jEdit.getProperty("common.ok"));
061: ok.addActionListener(this );
062: getRootPane().setDefaultButton(ok);
063: panel.add(ok);
064: panel.add(Box.createHorizontalStrut(6));
065: cancel = new JButton(jEdit.getProperty("common.cancel"));
066: cancel.addActionListener(this );
067: panel.add(cancel);
068: panel.add(Box.createGlue());
069:
070: content.add(panel, BorderLayout.SOUTH);
071:
072: GUIUtilities.requestFocus(this , startField);
073:
074: pack();
075: setLocationRelativeTo(view);
076: setVisible(true);
077: } //}}}
078:
079: //{{{ ok() method
080: public void ok() {
081: int startLine;
082: int endLine;
083:
084: try {
085: startLine = Integer.parseInt(startField.getText()) - 1;
086: endLine = Integer.parseInt(endField.getText()) - 1;
087: } catch (NumberFormatException nf) {
088: getToolkit().beep();
089: return;
090: }
091:
092: Buffer buffer = view.getBuffer();
093:
094: if (startLine < 0 || endLine >= buffer.getLineCount()
095: || startLine > endLine) {
096: getToolkit().beep();
097: return;
098: }
099:
100: JEditTextArea textArea = view.getTextArea();
101: Selection s = new Selection.Range(buffer
102: .getLineStartOffset(startLine), buffer
103: .getLineEndOffset(endLine) - 1);
104: if (textArea.isMultipleSelectionEnabled())
105: textArea.addToSelection(s);
106: else
107: textArea.setSelection(s);
108: textArea
109: .moveCaretPosition(buffer.getLineEndOffset(endLine) - 1);
110:
111: dispose();
112: } //}}}
113:
114: //{{{ cancel() method
115: public void cancel() {
116: dispose();
117: } //}}}
118:
119: //{{{ actionPerformed() method
120: public void actionPerformed(ActionEvent evt) {
121: Object source = evt.getSource();
122: if (source == ok)
123: ok();
124: else if (source == cancel)
125: cancel();
126: } //}}}
127:
128: //{{{ Private members
129:
130: //{{{ Instance variables
131: private View view;
132: private JTextField startField;
133: private JTextField endField;
134: private JButton ok;
135: private JButton cancel;
136:
137: //}}}
138:
139: //{{{ createFieldPanel() method
140: private JPanel createFieldPanel() {
141: GridBagLayout layout = new GridBagLayout();
142: JPanel panel = new JPanel(layout);
143:
144: GridBagConstraints cons = new GridBagConstraints();
145: cons.insets = new Insets(0, 0, 6, 12);
146: cons.gridwidth = cons.gridheight = 1;
147: cons.gridx = cons.gridy = 0;
148: cons.fill = GridBagConstraints.BOTH;
149: JLabel label = new JLabel(jEdit
150: .getProperty("selectlinerange.start"),
151: SwingConstants.RIGHT);
152: layout.setConstraints(label, cons);
153: panel.add(label);
154:
155: startField = new JTextField(10);
156: cons.gridx = 1;
157: cons.weightx = 1.0f;
158: layout.setConstraints(startField, cons);
159: panel.add(startField);
160:
161: label = new JLabel(jEdit.getProperty("selectlinerange.end"),
162: SwingConstants.RIGHT);
163: cons.gridx = 0;
164: cons.weightx = 0.0f;
165: cons.gridy = 1;
166: layout.setConstraints(label, cons);
167: panel.add(label);
168:
169: endField = new JTextField(10);
170: cons.gridx = 1;
171: cons.weightx = 1.0f;
172: layout.setConstraints(endField, cons);
173: panel.add(endField);
174:
175: return panel;
176: } //}}}
177:
178: //}}}
179: }
|