();
} else {
optionButtonsByValueMap.clear();
}
JButton buttons[] = null;
Object[] buttonFactory = getButtons();
if (buttonFactory != null) {
buttons = new JButton[buttonFactory.length];
for (int i = 0; i < buttonFactory.length; i++) {
ButtonFactory factory = (ButtonFactory)buttonFactory[i];
buttons[i] = factory.createButton();
buttons[i].addActionListener(new OptionButtonListener(factory.getValue(), optionPane, buttonFilter));
optionButtonsByValueMap.put(factory.getValue(), buttons[i]);
}
}
if (buttons != null) {
optionPane.setOptions(buttons);
optionPane.setInitialValue(buttons[0]);
}
}
private static class ButtonFactory {
private String text;
private int mnemonic;
private Icon icon;
private int value;
ButtonFactory(String text, int mnemonic, Icon icon, int value) {
this.text = text;
this.mnemonic = mnemonic;
this.icon = icon;
this.value = value;
}
JButton createButton() {
JButton button = new JButton(text);
button.setName(text);
if (icon != null) {
button.setIcon(icon);
}
if (mnemonic != 0) {
button.setMnemonic(mnemonic);
}
return button;
}
public int getValue() {
return value;
}
}
private int getMnemonic(String key, Locale l) {
String value = (String) UIManager.get(key, l);
if (value == null) {
return 0;
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException nfe) {
}
return 0;
}
protected Object[] getButtons() {
if (optionPane != null) {
Object[] suppliedOptions = optionPane.getOptions();
if (suppliedOptions == null) {
Object[] defaultOptions;
int type = optionPane.getOptionType();
Locale l = optionPane.getLocale();
if (type == JOptionPane.YES_NO_OPTION) {
defaultOptions = new ButtonFactory[2];
defaultOptions[0] = new ButtonFactory(UIManager.getString("OptionPane.yesButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.yesButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.yesIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.YES_OPTION);
defaultOptions[1] = new ButtonFactory(UIManager.getString("OptionPane.noButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.noButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.noIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.NO_OPTION);
} else if (type == JOptionPane.YES_NO_CANCEL_OPTION) {
defaultOptions = new ButtonFactory[3];
defaultOptions[0] = new ButtonFactory(UIManager.getString("OptionPane.yesButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.yesButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.yesIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.YES_OPTION);
defaultOptions[1] = new ButtonFactory(UIManager.getString("OptionPane.noButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.noButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.noIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.NO_OPTION);
defaultOptions[2] = new ButtonFactory(UIManager.getString("OptionPane.cancelButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.cancelButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.cancelIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.CANCEL_OPTION);
} else if (type == JOptionPane.OK_CANCEL_OPTION) {
defaultOptions = new ButtonFactory[2];
defaultOptions[0] = new ButtonFactory(UIManager.getString("OptionPane.okButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.okButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.okIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.OK_OPTION);
defaultOptions[1] = new ButtonFactory(UIManager.getString("OptionPane.cancelButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.cancelButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.cancelIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.CANCEL_OPTION);
} else {
defaultOptions = new ButtonFactory[1];
defaultOptions[0] = new ButtonFactory(UIManager.getString("OptionPane.okButtonText", l), //$NON-NLS-1$
getMnemonic("OptionPane.okButtonMnemonic", l), //$NON-NLS-1$
UIManager.getIcon("OptionPane.okIcon",optionPane.getLocale()), //$NON-NLS-1$
JOptionPane.OK_OPTION);
}
return defaultOptions;
}
return suppliedOptions;
}
return null;
}
private static class OptionButtonListener implements ActionListener {
int buttonValue;
JOptionPane optionPane;
OptionPane.OptionPaneButtonFilter buttonFilter;
public OptionButtonListener(int buttonValue, JOptionPane optionPane, OptionPane.OptionPaneButtonFilter buttonFilter) {
this.buttonValue = buttonValue;
this.optionPane = optionPane;
this.buttonFilter = buttonFilter;
}
public void actionPerformed(ActionEvent e) {
if (buttonFilter == null || buttonFilter.canSelectButton(buttonValue, optionPane)) {
optionPane.setValue(Integer.valueOf(buttonValue));
}
}
}
/**
* Shows a panel in a "OK" "Cancel" dialog.
*
* @param parent the component that invoked the dialog. the dialog will be positioned relatively to the parent.
* may be null
* @param component the component to display in the dialog
* @param title optional title for the dialog
* @return true if the OK was pressed. false otherwise
*/
public static boolean showDialog(Component parent, JComponent component, String title) {
return showDialog(parent, component, title, null);
}
public static boolean showDialog(Component parent, JComponent component, String title, boolean resizable) {
return showDialog(parent, component, title, null, resizable);
}
public static boolean showDialog(Component parent, JComponent component, String title, OptionPaneButtonEnabler enabler) {
return showDialog(parent, component, title, enabler, false);
}
public static boolean showDialog(Component parent, JComponent component, String title, OptionPaneButtonEnabler enabler, boolean resizable) {
if(parent == null) parent = MainDirector.getFrameDirector().getAppFrame();
OptionPane pane = new OptionPane(title,JOptionPane.OK_CANCEL_OPTION);
pane.prepare(parent, component, resizable);
//this will link between the button and the field.
if(enabler != null){
enabler.setButton(pane.getOptionButtonByValue(JOptionPane.OK_OPTION));
}
pane.show();
return pane.getSelectedOption() == JOptionPane.OK_OPTION;
}
public void propertyChange(PropertyChangeEvent evt) {
dialog.setVisible(false);
}
}