|
26 | 26 | import processing.core.*; |
27 | 27 |
|
28 | 28 | import java.awt.*; |
| 29 | +import java.awt.event.ActionListener; |
| 30 | +import java.awt.event.KeyAdapter; |
| 31 | +import java.awt.event.KeyEvent; |
29 | 32 | import java.io.*; |
30 | 33 |
|
31 | 34 | import javax.swing.*; |
@@ -286,7 +289,8 @@ public void handleNewCode() { |
286 | 289 | } |
287 | 290 |
|
288 | 291 | renamingCode = false; |
289 | | - editor.status.edit("Name for new file:", ""); |
| 292 | + // editor.status.edit("Name for new file:", ""); |
| 293 | + promptForTabName("Name for new file:", ""); |
290 | 294 | } |
291 | 295 |
|
292 | 296 |
|
@@ -326,8 +330,99 @@ public void handleRenameCode() { |
326 | 330 | "New name for sketch:" : "New name for file:"; |
327 | 331 | String oldName = (current.isExtension(mode.getDefaultExtension())) ? |
328 | 332 | current.getPrettyName() : current.getFileName(); |
329 | | - editor.status.edit(prompt, oldName); |
| 333 | + // editor.status.edit(prompt, oldName); |
| 334 | + promptForTabName(prompt, oldName); |
330 | 335 | } |
| 336 | + |
| 337 | + /** |
| 338 | + * Displays a dialog for renaming or creating a new tab |
| 339 | + * @param prompt - msg to display |
| 340 | + * @param oldName |
| 341 | + */ |
| 342 | + protected void promptForTabName(String prompt, String oldName) { |
| 343 | + final JTextField txtTabName = new JTextField(); |
| 344 | + txtTabName.addKeyListener(new KeyAdapter() { |
| 345 | + // Forget ESC, the JDialog should handle it. |
| 346 | + |
| 347 | + // Use keyTyped to catch when the feller is actually |
| 348 | + // added to the text field. With keyTyped, as opposed to |
| 349 | + // keyPressed, the keyCode will be zero, even if it's |
| 350 | + // enter or backspace or whatever, so the keychar should |
| 351 | + // be used instead. Grr. |
| 352 | + public void keyTyped(KeyEvent event) { |
| 353 | + //System.out.println("got event " + event); |
| 354 | + char ch = event.getKeyChar(); |
| 355 | + if ((ch == '_') || (ch == '.') || // allow.pde and .java |
| 356 | + (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z'))) { |
| 357 | + // These events are allowed straight through. |
| 358 | + } else if (ch == ' ') { |
| 359 | + String t = txtTabName.getText(); |
| 360 | + int start = txtTabName.getSelectionStart(); |
| 361 | + int end = txtTabName.getSelectionEnd(); |
| 362 | + txtTabName.setText(t.substring(0, start) + "_" + t.substring(end)); |
| 363 | + txtTabName.setCaretPosition(start + 1); |
| 364 | + event.consume(); |
| 365 | + } else if ((ch >= '0') && (ch <= '9')) { |
| 366 | + // getCaretPosition == 0 means that it's the first char |
| 367 | + // and the field is empty. |
| 368 | + // getSelectionStart means that it *will be* the first |
| 369 | + // char, because the selection is about to be replaced |
| 370 | + // with whatever is typed. |
| 371 | + if ((txtTabName.getCaretPosition() == 0) |
| 372 | + || (txtTabName.getSelectionStart() == 0)) { |
| 373 | + // number not allowed as first digit |
| 374 | + //System.out.println("bad number bad"); |
| 375 | + event.consume(); |
| 376 | + } |
| 377 | + } else if (ch == KeyEvent.VK_ENTER) { |
| 378 | + // Slightly ugly hack that ensures OK button of the dialog |
| 379 | + // consumes the Enter key event. Since the text field is the |
| 380 | + // default component in the dialog(see below), OK button doesn't |
| 381 | + // consume Enter key event, by default. |
| 382 | + Container parent = txtTabName.getParent(); |
| 383 | + while (!(parent instanceof JOptionPane)) { |
| 384 | + parent = parent.getParent(); |
| 385 | + } |
| 386 | + JOptionPane pane = (JOptionPane) parent; |
| 387 | + final JPanel pnlBottom = (JPanel) pane.getComponent(pane |
| 388 | + .getComponentCount() - 1); |
| 389 | + for (int i = 0; i < pnlBottom.getComponents().length; i++) { |
| 390 | + Component component = pnlBottom.getComponents()[i]; |
| 391 | + if (component instanceof JButton) { |
| 392 | + final JButton okButton = ((JButton) component); |
| 393 | + if (okButton.getText().equalsIgnoreCase("OK")) { |
| 394 | + ActionListener[] actionListeners = okButton |
| 395 | + .getActionListeners(); |
| 396 | + if (actionListeners.length > 0) { |
| 397 | + actionListeners[0].actionPerformed(null); |
| 398 | + event.consume(); |
| 399 | + } |
| 400 | + } |
| 401 | + } |
| 402 | + } |
| 403 | + } |
| 404 | + else { |
| 405 | + event.consume(); |
| 406 | + } |
| 407 | + } |
| 408 | + }); |
| 409 | + |
| 410 | + int userReply = JOptionPane.showOptionDialog(editor, new Object[] { |
| 411 | + prompt, txtTabName }, |
| 412 | + "Tab Name", |
| 413 | + JOptionPane.OK_CANCEL_OPTION, |
| 414 | + JOptionPane.PLAIN_MESSAGE, |
| 415 | + null, new Object[] { |
| 416 | + Preferences.PROMPT_OK, |
| 417 | + Preferences.PROMPT_CANCEL }, |
| 418 | + txtTabName); |
| 419 | + |
| 420 | + if (userReply == JOptionPane.OK_OPTION) { |
| 421 | + String answer = txtTabName.getText(); |
| 422 | + nameCode(answer); |
| 423 | + } |
| 424 | + } |
| 425 | + |
331 | 426 |
|
332 | 427 |
|
333 | 428 | /** |
@@ -382,7 +477,7 @@ protected void nameCode(String newName) { |
382 | 477 | if (current == code[0]) { // If this is the main tab, disallow |
383 | 478 | Base.showWarning("Problem with rename", |
384 | 479 | "The first tab cannot be a ." + newExtension + " file.\n" + |
385 | | - "(It may be time for your to graduate to a\n" + |
| 480 | + "(It may be time for you to graduate to a\n" + |
386 | 481 | "\"real\" programming environment, hotshot.)"); |
387 | 482 | return; |
388 | 483 | } |
|
0 commit comments