Skip to content

Commit bbff7c1

Browse files
committed
dialog for new tab and rename. Fixes processing#2431
1 parent ce5d72b commit bbff7c1

1 file changed

Lines changed: 98 additions & 3 deletions

File tree

app/src/processing/app/Sketch.java

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import processing.core.*;
2727

2828
import java.awt.*;
29+
import java.awt.event.ActionListener;
30+
import java.awt.event.KeyAdapter;
31+
import java.awt.event.KeyEvent;
2932
import java.io.*;
3033

3134
import javax.swing.*;
@@ -286,7 +289,8 @@ public void handleNewCode() {
286289
}
287290

288291
renamingCode = false;
289-
editor.status.edit("Name for new file:", "");
292+
// editor.status.edit("Name for new file:", "");
293+
promptForTabName("Name for new file:", "");
290294
}
291295

292296

@@ -326,8 +330,99 @@ public void handleRenameCode() {
326330
"New name for sketch:" : "New name for file:";
327331
String oldName = (current.isExtension(mode.getDefaultExtension())) ?
328332
current.getPrettyName() : current.getFileName();
329-
editor.status.edit(prompt, oldName);
333+
// editor.status.edit(prompt, oldName);
334+
promptForTabName(prompt, oldName);
330335
}
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+
331426

332427

333428
/**
@@ -382,7 +477,7 @@ protected void nameCode(String newName) {
382477
if (current == code[0]) { // If this is the main tab, disallow
383478
Base.showWarning("Problem with rename",
384479
"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" +
386481
"\"real\" programming environment, hotshot.)");
387482
return;
388483
}

0 commit comments

Comments
 (0)