MCPcopy Index your code
hub / github.com/processing/processing / promptForTabName

Method promptForTabName

app/src/processing/app/Sketch.java:361–438  ·  view source on GitHub ↗

Displays a dialog for renaming or creating a new tab

(String prompt, String oldName)

Source from the content-addressed store, hash-verified

359 * Displays a dialog for renaming or creating a new tab
360 */
361 protected void promptForTabName(String prompt, String oldName) {
362 final JTextField field = new JTextField(oldName);
363
364 field.addKeyListener(new KeyAdapter() {
365 // Forget ESC, the JDialog should handle it.
366 // Use keyTyped to catch when the feller is actually added to the text
367 // field. With keyTyped, as opposed to keyPressed, the keyCode will be
368 // zero, even if it's enter or backspace or whatever, so the keychar
369 // should be used instead. Grr.
370 public void keyTyped(KeyEvent event) {
371 //System.out.println("got event " + event);
372 char ch = event.getKeyChar();
373 if ((ch == '_') || (ch == '.') || // allow.pde and .java
374 (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z'))) {
375 // These events are allowed straight through.
376 } else if (ch == ' ') {
377 String t = field.getText();
378 int start = field.getSelectionStart();
379 int end = field.getSelectionEnd();
380 field.setText(t.substring(0, start) + "_" + t.substring(end));
381 field.setCaretPosition(start + 1);
382 event.consume();
383 } else if ((ch >= '0') && (ch <= '9')) {
384 // getCaretPosition == 0 means that it's the first char
385 // and the field is empty.
386 // getSelectionStart means that it *will be* the first
387 // char, because the selection is about to be replaced
388 // with whatever is typed.
389 if (field.getCaretPosition() == 0 ||
390 field.getSelectionStart() == 0) {
391 // number not allowed as first digit
392 event.consume();
393 }
394 } else if (ch == KeyEvent.VK_ENTER) {
395 // Slightly ugly hack that ensures OK button of the dialog consumes
396 // the Enter key event. Since the text field is the default component
397 // in the dialog, OK doesn't consume Enter key event, by default.
398 Container parent = field.getParent();
399 while (!(parent instanceof JOptionPane)) {
400 parent = parent.getParent();
401 }
402 JOptionPane pane = (JOptionPane) parent;
403 final JPanel pnlBottom = (JPanel)
404 pane.getComponent(pane.getComponentCount() - 1);
405 for (int i = 0; i < pnlBottom.getComponents().length; i++) {
406 Component component = pnlBottom.getComponents()[i];
407 if (component instanceof JButton) {
408 final JButton okButton = (JButton) component;
409 if (okButton.getText().equalsIgnoreCase("OK")) {
410 ActionListener[] actionListeners =
411 okButton.getActionListeners();
412 if (actionListeners.length > 0) {
413 actionListeners[0].actionPerformed(null);
414 event.consume();
415 }
416 }
417 }
418 }

Callers 2

handleNewCodeMethod · 0.95
handleRenameCodeMethod · 0.95

Calls 4

textMethod · 0.95
getPromptMethod · 0.95
nameCodeMethod · 0.95
getTextMethod · 0.45

Tested by

no test coverage detected