String defaultName = null;
do {
String name = LOCALE.get(
"TemplateDefaultNamePattern", Integer.toString(n++)
);
TemplateKey key = new TemplateKey(nsDefault, name);
try {
TemplateDatabase.getTemplateDocument(key);
}
catch (TemplateDatabase.TemplateException e) {
// No Template with this name has been defined.
defaultName = name;
}
} while (defaultName == null);
namespaceField.addItem(nsDefault);
namespaceField.setSelectedItem(nsDefault);
namespaceField.setEditable(true);
nameField.setPreferredSize(
new Dimension(160, namespaceField.getPreferredSize().height)
);
nameField.getEditor().selectAll();
nameField.addItem(defaultName);
nameField.setSelectedItem(defaultName);
nameField.setEditable(true);
nameField.setPreferredSize(
new Dimension(160, nameField.getPreferredSize().height)
);
nameField.getEditor().selectAll();
Box messageBox = Box.createVerticalBox();
messageBox.add(textGrid);
messageBox.add(defaultBox);
messageBox.add(Box.createVerticalStrut(8));
if (selector != null) {
messageBox.add(selector);
}
// We use our own OK button, instead of the one generated by
// JOptionPane, so we can set it to be the default button for the
// dialog.
JButton okButton = new JButton(LOCALE.get("TemplateSaveOption"));
JButton cancelButton = new JButton(LOCALE.get("TemplateCancelOption"));
JOptionPane pane = new JOptionPane(
messageBox,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
new Object[] { okButton, cancelButton },
null
);
// Set a minimum width for the dialog.
Dimension size = pane.getPreferredSize();
size = new Dimension(Math.max(size.width, 400), size.height);
pane.setPreferredSize(size);
// Use a custom dialog instead of the JOptionPane showXxxDialog()
// methods so that we can set the initial focus on the combo box
// instead of the OK button.
final JDialog dialog = new JDialog(parent);
dialog.setTitle(LOCALE.get("TemplateSaveDialogTitle"));
dialog.setContentPane(pane);
dialog.getRootPane().setDefaultButton(okButton);
// This causes the dialog to go away when the user presses "ESCAPE".
pane.registerKeyboardAction(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
},
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW
);
// This causes the dialog to go away when the user clicks "OK".
okButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// In case this happens while the combo box editor is
// active, commit the current text to the combo box model:
ComboBoxEditor editor = nameField.getEditor();
JTextField text = (JTextField) editor.getEditorComponent();
String name = text.getText();
nameField.setSelectedItem(name);
isOkSelected = true;
dialog.setVisible(false);
}
}
);
// This causes the dialog to go away when the user clicks "Cancel".
cancelButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
dialog.setVisible(false);
}
}
);
dialog.setModal(true);
dialog.setResizable(true);
dialog.pack();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
isDefaultSelected = defaultCheck.isSelected();
if (! isOkSelected) {
// The dialog was disposed, or the user clicked Cancel.
return null;
}
if (nameField.getEditor().getEditorComponent().isFocusOwner()) {
KeyboardFocusManager focus =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
focus.upFocusCycle();
}
return new TemplateKey(getNamespaceText(), getNameText());
}