KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > SelectLineRange


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
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import javax.swing.border.*;
28 import java.awt.*;
29 import java.awt.event.*;
30 import org.gjt.sp.jedit.textarea.*;
31 import org.gjt.sp.jedit.*;
32 //}}}
33

34 public 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         setVisible(true);
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 JavaDoc 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 JavaDoc 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
}
185
Popular Tags