1 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 26 public class GoToLineDialog extends EscapeDialog implements ActionListener 27 { 28 static final String goToLineTitle = Config.getString("editor.gotoline.title"); 29 static final String goToLineLabel = Config.getString("editor.gotoline.label"); 30 static final String notNumericMessage = Config.getString("editor.gotoline.notNumericMessage"); 31 static final String notInRangeMessage = Config.getString("editor.gotoline.notInRangeMessage"); 32 static final int INVALID_NUMBER = -1; 33 34 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 47 public GoToLineDialog(Frame owner) 48 { 49 super(owner, goToLineTitle, true); 50 makeDialog(); 51 } 52 53 57 public void showDialog(int range) 58 { 59 sizeOfClass = range; 61 instructionLabel.setText(goToLineLabel + " ( 1 - " + range + " )"); 62 lineNumberField.requestFocus(); 63 setVisible(true); 64 } 65 66 68 71 public void actionPerformed(ActionEvent evt) 72 { 73 Object src = evt.getSource(); 74 75 if (src == okButton) { 76 doOK(); 77 } else if (src == cancelButton) { 78 doCancel(); 79 } 80 } 81 82 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 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 137 private void doOK() 138 { 139 lineNumber = validateInput(); 140 clear(); 141 setVisible(false); 142 } 143 144 147 private void doCancel() 148 { 149 lineNumber = INVALID_NUMBER; 150 setVisible(false); 151 } 152 155 private void clear() 156 { 157 lineNumberField.setText(""); 158 } 159 160 164 public int getLineNumber() 165 { 166 return lineNumber; 167 } 168 169 174 private int validateInput() 175 { 176 int validatedNumber = -1; 177 try { 178 validatedNumber = Integer.parseInt(lineNumberField.getText()); 179 } catch (NumberFormatException nfe) { 180 } 182 return validatedNumber; 183 } 184 185 189 class IntegerDocument extends PlainDocument 190 { 191 194 public void insertString(int offset, String string, AttributeSet attributes) 195 throws BadLocationException 196 { 197 if (string == null) { 198 return; 199 } else { 200 String newValue; 201 int length = getLength(); 202 if (length == 0) { 203 newValue = string; 204 } else { 205 String currentContent = getText(0, length); 206 StringBuffer currentBuffer = new StringBuffer (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 exception) { 217 Toolkit.getDefaultToolkit().beep(); 218 messageLabel.setText(notNumericMessage); 219 } 220 } 221 } 222 223 226 private int checkInputIsInteger(String proposedValue) 227 throws NumberFormatException 228 { 229 int newValue = 0; 230 if (proposedValue.length() > 0) { 231 newValue = Integer.parseInt(proposedValue); 232 } 233 return newValue; 234 } 235 236 240 private boolean checkInputRange(int parsedNumber) 241 { 242 return(parsedNumber > 0 && parsedNumber <= sizeOfClass); 243 } 244 } 245 } | Popular Tags |