PageRenderTime 67ms CodeModel.GetById 57ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/SelectLineRange.java

#
Java | 180 lines | 120 code | 28 blank | 32 comment | 8 complexity | 7aa11e198a8de0b38818e9310e5e6f26 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		textArea.setSelection(new Selection.Range(
107			buffer.getLineStartOffset(startLine),
108			buffer.getLineEndOffset(endLine) - 1));
109		textArea.moveCaretPosition(buffer.getLineEndOffset(endLine) - 1);
110
111		dispose();
112	} //}}}
113
114	//{{{ cancel() method
115	public void cancel()
116	{
117		dispose();
118	} //}}}
119
120	//{{{ actionPerformed() method
121	public void actionPerformed(ActionEvent evt)
122	{
123		Object source = evt.getSource();
124		if(source == ok)
125			ok();
126		else if(source == cancel)
127			cancel();
128	} //}}}
129
130	//{{{ Private members
131
132	//{{{ Instance variables
133	private View view;
134	private JTextField startField;
135	private JTextField endField;
136	private JButton ok;
137	private JButton cancel;
138	//}}}
139
140	//{{{ createFieldPanel() method
141	private JPanel createFieldPanel()
142	{
143		GridBagLayout layout = new GridBagLayout();
144		JPanel panel = new JPanel(layout);
145
146		GridBagConstraints cons = new GridBagConstraints();
147		cons.insets = new Insets(0,0,6,12);
148		cons.gridwidth = cons.gridheight = 1;
149		cons.gridx = cons.gridy = 0;
150		cons.fill = GridBagConstraints.BOTH;
151		JLabel label = new JLabel(jEdit.getProperty("selectlinerange.start"),
152			SwingConstants.RIGHT);
153		layout.setConstraints(label,cons);
154		panel.add(label);
155
156		startField = new JTextField(10);
157		cons.gridx = 1;
158		cons.weightx = 1.0f;
159		layout.setConstraints(startField,cons);
160		panel.add(startField);
161
162		label = new JLabel(jEdit.getProperty("selectlinerange.end"),
163			SwingConstants.RIGHT);
164		cons.gridx = 0;
165		cons.weightx = 0.0f;
166		cons.gridy = 1;
167		layout.setConstraints(label,cons);
168		panel.add(label);
169
170		endField = new JTextField(10);
171		cons.gridx = 1;
172		cons.weightx = 1.0f;
173		layout.setConstraints(endField,cons);
174		panel.add(endField);
175
176		return panel;
177	} //}}}
178
179	//}}}
180}