PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/SelectLineRange.java

#
Java | 184 lines | 124 code | 28 blank | 32 comment | 9 complexity | 87bef6d8452d6ba14b550743d2823572 MD5 | raw file
  1/*
  2 * SelectLineRange.java - Selects a range of lines
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import javax.swing.border.*;
 28import java.awt.*;
 29import java.awt.event.*;
 30import org.gjt.sp.jedit.textarea.*;
 31import org.gjt.sp.jedit.*;
 32//}}}
 33
 34public class SelectLineRange extends EnhancedDialog implements ActionListener
 35{
 36	//{{{ SelectLineRange constructor
 37	public SelectLineRange(View view)
 38	{
 39		super(view,jEdit.getProperty("selectlinerange.title"),true);
 40		this.view = view;
 41
 42		JPanel content = new JPanel(new BorderLayout());
 43		content.setBorder(new EmptyBorder(12,12,12,0));
 44		setContentPane(content);
 45
 46		JLabel label = new JLabel(jEdit.getProperty(
 47			"selectlinerange.caption"));
 48		label.setBorder(new EmptyBorder(0,0,6,12));
 49		content.add(BorderLayout.NORTH,label);
 50
 51		JPanel panel = createFieldPanel();
 52
 53		content.add(BorderLayout.CENTER,panel);
 54
 55		panel = new JPanel();
 56		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
 57		panel.setBorder(new EmptyBorder(6,0,0,12));
 58		panel.add(Box.createGlue());
 59		panel.add(Box.createGlue());
 60		ok = new JButton(jEdit.getProperty("common.ok"));
 61		ok.addActionListener(this);
 62		getRootPane().setDefaultButton(ok);
 63		panel.add(ok);
 64		panel.add(Box.createHorizontalStrut(6));
 65		cancel = new JButton(jEdit.getProperty("common.cancel"));
 66		cancel.addActionListener(this);
 67		panel.add(cancel);
 68		panel.add(Box.createGlue());
 69
 70		content.add(panel,BorderLayout.SOUTH);
 71
 72		GUIUtilities.requestFocus(this,startField);
 73
 74		pack();
 75		setLocationRelativeTo(view);
 76		show();
 77	} //}}}
 78
 79	//{{{ ok() method
 80	public void ok()
 81	{
 82		int startLine;
 83		int endLine;
 84
 85		try
 86		{
 87			startLine = Integer.parseInt(startField.getText()) - 1;
 88			endLine = Integer.parseInt(endField.getText()) - 1;
 89		}
 90		catch(NumberFormatException nf)
 91		{
 92			getToolkit().beep();
 93			return;
 94		}
 95
 96		Buffer buffer = view.getBuffer();
 97
 98		if(startLine < 0 || endLine >= buffer.getLineCount()
 99			|| startLine > endLine)
100		{
101			getToolkit().beep();
102			return;
103		}
104
105		JEditTextArea textArea = view.getTextArea();
106		Selection s = new Selection.Range(
107			buffer.getLineStartOffset(startLine),
108			buffer.getLineEndOffset(endLine) - 1);
109		if(textArea.isMultipleSelectionEnabled())
110			textArea.addToSelection(s);
111		else
112			textArea.setSelection(s);
113		textArea.moveCaretPosition(buffer.getLineEndOffset(endLine) - 1);
114
115		dispose();
116	} //}}}
117
118	//{{{ cancel() method
119	public void cancel()
120	{
121		dispose();
122	} //}}}
123
124	//{{{ actionPerformed() method
125	public void actionPerformed(ActionEvent evt)
126	{
127		Object source = evt.getSource();
128		if(source == ok)
129			ok();
130		else if(source == cancel)
131			cancel();
132	} //}}}
133
134	//{{{ Private members
135
136	//{{{ Instance variables
137	private View view;
138	private JTextField startField;
139	private JTextField endField;
140	private JButton ok;
141	private JButton cancel;
142	//}}}
143
144	//{{{ createFieldPanel() method
145	private JPanel createFieldPanel()
146	{
147		GridBagLayout layout = new GridBagLayout();
148		JPanel panel = new JPanel(layout);
149
150		GridBagConstraints cons = new GridBagConstraints();
151		cons.insets = new Insets(0,0,6,12);
152		cons.gridwidth = cons.gridheight = 1;
153		cons.gridx = cons.gridy = 0;
154		cons.fill = GridBagConstraints.BOTH;
155		JLabel label = new JLabel(jEdit.getProperty("selectlinerange.start"),
156			SwingConstants.RIGHT);
157		layout.setConstraints(label,cons);
158		panel.add(label);
159
160		startField = new JTextField(10);
161		cons.gridx = 1;
162		cons.weightx = 1.0f;
163		layout.setConstraints(startField,cons);
164		panel.add(startField);
165
166		label = new JLabel(jEdit.getProperty("selectlinerange.end"),
167			SwingConstants.RIGHT);
168		cons.gridx = 0;
169		cons.weightx = 0.0f;
170		cons.gridy = 1;
171		layout.setConstraints(label,cons);
172		panel.add(label);
173
174		endField = new JTextField(10);
175		cons.gridx = 1;
176		cons.weightx = 1.0f;
177		layout.setConstraints(endField,cons);
178		panel.add(endField);
179
180		return panel;
181	} //}}}
182
183	//}}}
184}