KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bluej > editor > moe > GoToLineDialog


1 // Copyright (c) 2000, 2005 BlueJ Group, Deakin University
2
//
3
// This software is made available under the terms of the "MIT License"
4
// A copy of this license is included with this source distribution
5
// in "license.txt" and is also available at:
6
// http://www.opensource.org/licenses/mit-license.html
7
// Any queries should be directed to Michael Kolling mik@bluej.org
8

9 package bluej.editor.moe;
10
11 import java.awt.*;
12 import java.awt.event.*;
13
14 import javax.swing.*;
15 import javax.swing.text.*;
16
17 import bluej.*;
18 import bluej.utility.EscapeDialog;
19
20
21 /**
22  * Dialog for user to input a line number to traverse source file in editor
23  *
24  * @author Bruce Quig
25  */

26 public class GoToLineDialog extends EscapeDialog implements ActionListener
27 {
28     static final String JavaDoc goToLineTitle = Config.getString("editor.gotoline.title");
29     static final String JavaDoc goToLineLabel = Config.getString("editor.gotoline.label");
30     static final String JavaDoc notNumericMessage = Config.getString("editor.gotoline.notNumericMessage");
31     static final String JavaDoc notInRangeMessage = Config.getString("editor.gotoline.notInRangeMessage");
32     static final int INVALID_NUMBER = -1;
33     
34     // -------- INSTANCE VARIABLES --------
35
private JButton okButton;
36     private JButton cancelButton;
37     private JTextField lineNumberField;
38     private JLabel instructionLabel;
39     private JLabel messageLabel;
40     private int lineNumber = INVALID_NUMBER;
41     private int sizeOfClass;
42     
43     
44     /**
45      * Creates a new GoToLineDialog object.
46      */

47     public GoToLineDialog(Frame owner)
48     {
49         super(owner, goToLineTitle, true);
50         makeDialog();
51     }
52
53     /**
54      * Make the dialod visible.
55      * @param range the number of lines of source code in source file
56      */

57     public void showDialog(int range)
58     {
59         //getRootPane().setDefaultButton(okButton);
60
sizeOfClass = range;
61         instructionLabel.setText(goToLineLabel + " ( 1 - " + range + " )");
62         lineNumberField.requestFocus();
63         setVisible(true);
64     }
65
66     // === Actionlistener interface ===
67

68     /**
69      * A button was pressed. Find out which one and do the appropriate thing.
70      */

71     public void actionPerformed(ActionEvent evt)
72     {
73         Object JavaDoc src = evt.getSource();
74
75         if (src == okButton) {
76             doOK();
77         } else if (src == cancelButton) {
78             doCancel();
79         }
80     }
81     
82     /**
83     * Setup the dialog
84     */

85     private void makeDialog()
86     {
87         addWindowListener(new WindowAdapter() {
88             public void windowClosing(WindowEvent E)
89             {
90                 doCancel();
91             }
92         });
93        
94         
95         JPanel bodyPanel = new JPanel();
96         bodyPanel.setLayout(new BoxLayout(bodyPanel, BoxLayout.Y_AXIS));
97         bodyPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 20, 20));
98    
99         instructionLabel = new JLabel(goToLineLabel);
100         bodyPanel.add(instructionLabel);
101         bodyPanel.add(Box.createVerticalStrut(6));
102    
103         lineNumberField = new JTextField();
104         bodyPanel.add(lineNumberField);
105         bodyPanel.add(Box.createVerticalStrut(6));
106         
107         IntegerDocument integerDocument1 = new IntegerDocument();
108         lineNumberField.setDocument(integerDocument1);
109         
110         messageLabel = new JLabel(" ");
111         bodyPanel.add(messageLabel);
112         bodyPanel.add(Box.createVerticalStrut(6));
113         
114         // add buttons
115
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
116         buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
117
118         okButton = BlueJTheme.getOkButton();
119         okButton.addActionListener(this);
120
121         cancelButton = BlueJTheme.getCancelButton();
122         cancelButton.addActionListener(this);
123
124         buttonPanel.add(okButton);
125         buttonPanel.add(cancelButton);
126         getRootPane().setDefaultButton(okButton);
127         
128         bodyPanel.add(buttonPanel);
129         getContentPane().add(bodyPanel, BorderLayout.CENTER);
130         pack();
131       
132     }
133     
134     /**
135      * When ok button is selected
136      */

137     private void doOK()
138     {
139         lineNumber = validateInput();
140         clear();
141         setVisible(false);
142     }
143     
144     /**
145      * When cancel button is selected
146      */

147     private void doCancel()
148     {
149         lineNumber = INVALID_NUMBER;
150         setVisible(false);
151     }
152     /**
153     * Clear the line number text field
154     */

155     private void clear()
156     {
157         lineNumberField.setText("");
158     }
159     
160     /**
161     * Returns the lineNumber.
162     * @return the line number entered, returns -1 if input is invalid
163     */

164     public int getLineNumber()
165     {
166         return lineNumber;
167     }
168     
169     /**
170      * Convert input field contents to an int
171      * @return int the String input value as an int
172      * representing line number
173      */

174     private int validateInput()
175     {
176         int validatedNumber = -1;
177         try {
178             validatedNumber = Integer.parseInt(lineNumberField.getText());
179         } catch (NumberFormatException JavaDoc nfe) {
180             //shouldn't happen, verified at data model level
181
}
182         return validatedNumber;
183     }
184     
185     /**
186     * Inner class that provides the formatted (Integer only) data model
187     * for the line number text field
188     */

189     class IntegerDocument extends PlainDocument
190     {
191         /**
192         * Inserts into Document model. Checks for format and for range
193         */

194         public void insertString(int offset, String JavaDoc string, AttributeSet attributes)
195             throws BadLocationException
196         {
197             if (string == null) {
198                 return;
199             } else {
200                 String JavaDoc newValue;
201                 int length = getLength();
202                 if (length == 0) {
203                     newValue = string;
204                 } else {
205                     String JavaDoc currentContent = getText(0, length);
206                     StringBuffer JavaDoc currentBuffer = new StringBuffer JavaDoc(currentContent);
207                     currentBuffer.insert(offset, string);
208                     newValue = currentBuffer.toString();
209                 }
210                 try {
211                     int parsedNumber = checkInputIsInteger(newValue);
212                     if(checkInputRange(parsedNumber)) {
213                         super.insertString(offset, string, attributes);
214                         messageLabel.setText(" ");
215                     }
216                 } catch (NumberFormatException JavaDoc exception) {
217                     Toolkit.getDefaultToolkit().beep();
218                     messageLabel.setText(notNumericMessage);
219                 }
220             }
221         }
222       
223         /**
224         * Check that the String is an integer
225         */

226         private int checkInputIsInteger(String JavaDoc proposedValue)
227            throws NumberFormatException JavaDoc
228         {
229             int newValue = 0;
230             if (proposedValue.length() > 0) {
231                 newValue = Integer.parseInt(proposedValue);
232             }
233             return newValue;
234         }
235        
236         /**
237         * Check that the int parameter is within range, meaning that it is
238         * greater than 0 and not greater than the number of lines in the source file
239         */

240         private boolean checkInputRange(int parsedNumber)
241         {
242             return(parsedNumber > 0 && parsedNumber <= sizeOfClass);
243         }
244     }
245 } // end class GoToLineDialog
246
Popular Tags