()
| 239 | } |
| 240 | |
| 241 | private void initialize() { |
| 242 | cancelButton = new JButton(I18n.PROMPT_CANCEL); |
| 243 | okButton = new JButton(I18n.PROMPT_OK); |
| 244 | |
| 245 | cancelButton.addActionListener(e -> { |
| 246 | if (mode == EDIT) { |
| 247 | unedit(); |
| 248 | } |
| 249 | }); |
| 250 | |
| 251 | okButton.addActionListener(e -> { |
| 252 | // answering to rename/new code question |
| 253 | if (mode == EDIT) { // this if() isn't (shouldn't be?) necessary |
| 254 | String answer = editField.getText(); |
| 255 | editor.getSketchController().nameCode(answer); |
| 256 | unedit(); |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | if (OSUtils.isMacOS()) { |
| 261 | cancelButton.setBackground(BGCOLOR[EDIT]); |
| 262 | okButton.setBackground(BGCOLOR[EDIT]); |
| 263 | } |
| 264 | setLayout(null); |
| 265 | |
| 266 | add(cancelButton); |
| 267 | add(okButton); |
| 268 | |
| 269 | cancelButton.setVisible(false); |
| 270 | okButton.setVisible(false); |
| 271 | |
| 272 | editField = new JTextField(); |
| 273 | |
| 274 | editField.addKeyListener(new KeyAdapter() { |
| 275 | |
| 276 | // Grab ESC with keyPressed, because it's not making it to keyTyped |
| 277 | public void keyPressed(KeyEvent event) { |
| 278 | if (event.getKeyChar() == KeyEvent.VK_ESCAPE) { |
| 279 | unedit(); |
| 280 | event.consume(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // use keyTyped to catch when the feller is actually |
| 285 | // added to the text field. with keyTyped, as opposed to |
| 286 | // keyPressed, the keyCode will be zero, even if it's |
| 287 | // enter or backspace or whatever, so the keychar should |
| 288 | // be used instead. grr. |
| 289 | public void keyTyped(KeyEvent event) { |
| 290 | int c = event.getKeyChar(); |
| 291 | |
| 292 | if (c == KeyEvent.VK_ENTER) { // accept the input |
| 293 | String answer = editField.getText(); |
| 294 | editor.getSketchController().nameCode(answer); |
| 295 | unedit(); |
| 296 | event.consume(); |
| 297 | |
| 298 | // easier to test the affirmative case than the negative |
no test coverage detected