package com.onaro.util.jfc; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.text.MessageFormat; import java.util.Collection; import java.util.List; import java.util.ResourceBundle; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; public class Dialogs { /** * The locale dependant resources used for prompting the user. */ protected ResourceBundle resources; /** * Constructs use this class to create a Messae box dialog etc.. * @param resources the locale dependant resources used for prompting the user. */ public Dialogs(ResourceBundle resources) { this.resources = resources; } /** * Show a message to the user having its content, title, and possibley icon * retrieved from the resources. * @param type message type * @param messageResource the prefix name for accessing the resources to fetch * the message, title and icon * @param arg arguments for Formatting the message * @see javax.swing.JOptionPane */ public void showMessage(Component parentComponent, int type, String messageResource, Object...arg) { String message = resources.getString(messageResource + ".message"); //$NON-NLS-1$ String title = resources.getString(messageResource + ".title"); //$NON-NLS-1$ if (arg!= null && arg.length>0){ message = MessageFormat.format(message, arg); } JOptionPane.showMessageDialog(SwingUtilities.getRootPane(parentComponent), message, title, type); } /** * Show a message with a scrollable list of values * @param parentComponent parent UI component * @param type message type * @param title message dialog title * @param topMessage message to display above the list (can be null) * @param bottomMessage message to display below the list (can be null) * @param listElements elements do display within the list */ public void showListMessage(Component parentComponent, int type, String title, String topMessage, String bottomMessage, List listElements) { showListMessage(parentComponent, type, title, topMessage, bottomMessage, listElements.toArray(new Object[listElements.size()])); } /** * Show a message with a scrollable list of values * @param parentComponent parent UI component * @param type message type * @param title message dialog title * @param topMessage message to display above the list (can be null) * @param bottomMessage message to display below the list (can be null) * @param listElements elements do display within the list */ public void showListMessage(Component parentComponent, int type, String title, String topMessage, String bottomMessage, Object...listElements) { JPanel contents = new JPanel(new BorderLayout(5, 5)); contents.add(new JLabel(topMessage), BorderLayout.NORTH); contents.add(new JLabel(bottomMessage), BorderLayout.SOUTH); JList list = new JList(listElements); contents.add(new JScrollPane(list), BorderLayout.CENTER); OptionPane dialog = new OptionPane(title, JOptionPane.DEFAULT_OPTION, type); dialog.prepare(parentComponent, contents, true); dialog.show(); } public Object showInputDialog(Component parentComponent, int type, String messageResource, Object...arg) { String message = resources.getString(messageResource + ".message"); //$NON-NLS-1$ String title = resources.getString(messageResource + ".title"); //$NON-NLS-1$ if (arg!= null && arg.length>0){ message = MessageFormat.format(message, arg); } return JOptionPane.showInputDialog(parentComponent, message, title, type); } /** * Asks the user to confirm an operation with a "yes/no" dialog. * The prompt's content, title, and possibley icon are retrieved from the * resources. * @param type message type * @param messageResource the prefix name for accessing the resources to fetch * the message, title and icon * @see javax.swing.JOptionPane */ public boolean getConfirmation(Component parentComponent, int type, String messageResource, Object...arg) { return getConfirmation(parentComponent, JOptionPane.YES_NO_OPTION, type, messageResource, arg) == JOptionPane.YES_OPTION; } /** * Asks the user to confirm an operation with a "yes/no" dialog. * The prompt's content, title, and possibley icon are retrieved from the * resources. * @param optionType option type * @param messageType message type * @param messageResource the prefix name for accessing the resources to fetch * the message, title and icon * @see javax.swing.JOptionPane */ public int getConfirmation(Component parentComponent, int optionType, int messageType, String messageResource, Object...arg) { String message = resources.getString(messageResource + ".message"); //$NON-NLS-1$ String title = resources.getString(messageResource + ".title"); //$NON-NLS-1$ if (arg!= null && arg.length>0){ message = MessageFormat.format(message, arg); } return JOptionPane.showConfirmDialog(SwingUtilities.getRootPane(parentComponent), message, title, optionType, messageType); } /** * Asks the user confirmation. * The prompt's content, title, and possibley icon are retrieved from the * resources. * @param optionType an integer designating the options available on the dialog: YES_NO_OPTION, or YES_NO_CANCEL_OPTION * @param messageType an integer designating the kind of message this is, primarily used to determine the icon from the pluggable Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE * @param messageResource the prefix name for accessing the resources to fetch * the message, title and icon * @param options an array of objects indicating the possible choices the user can make * @param initialValue the object that represents the default selection for the dialog; only meaningful if options is used; can be null * @see javax.swing.JOptionPane */ public int getConfirmation(Component parentComponent, int optionType, int messageType, String messageResource, Object[] arg, Object[] options, Object initialValue) { String message = resources.getString(messageResource + ".message"); //$NON-NLS-1$ String title = resources.getString(messageResource + ".title"); //$NON-NLS-1$ if (arg!= null && arg.length>0){ message = MessageFormat.format(message, arg); } return JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, null, options, initialValue); } /** * Asks the user to confirm an operation with a "yes/no" dialog. * The prompt's content, title, and possibly icon are retrieved from the * resources. * @param type message type * @param values the list of values to be displayed in the message * @param messageResource the prefix name for accessing the resources to fetch * the message, title and icon * @see javax.swing.JOptionPane */ public boolean getConfirmation(Component parentComponent, int type, String messageResource, Object[] arg, Collection values) { String message = resources.getString(messageResource + ".message"); //$NON-NLS-1$ String title = resources.getString(messageResource + ".title"); //$NON-NLS-1$ if (arg!= null && arg.length>0){ message = MessageFormat.format(message, arg); } JList list = new JList(values.toArray()); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JLabel(message), BorderLayout.NORTH); panel.add( new JScrollPane(list), BorderLayout.CENTER); list.setVisibleRowCount(8); int selectedOption = JOptionPane.showConfirmDialog(SwingUtilities.getRootPane(parentComponent), panel, title, JOptionPane.YES_NO_OPTION, type); return selectedOption == JOptionPane.YES_OPTION; } /** * opens a dialog (JOptionPane) and displays the given component in it. * The Dialog is has two buttons: OK and Cancel. * If the dialog is closed using the "OK" button, true if returned. Otherwise - false * @param messageResource the prefix name for accessing the resources to fetch * the message, title and icon * @param resizable determine if the dialog will be resizable or not * @see javax.swing.JOptionPane */ public boolean optionDialog(Component parentComponent, JComponent optionDlg, int messageType, String messageResource, boolean resizable) { String title = resources.getString(messageResource + ".title"); //$NON-NLS-1$ JOptionPane pane = new JOptionPane(optionDlg, messageType, JOptionPane.OK_CANCEL_OPTION); pane.setComponentOrientation(((parentComponent == null) ? JOptionPane.getRootFrame() : parentComponent).getComponentOrientation()); JDialog dialog = pane.createDialog(parentComponent != null ? SwingUtilities.getRootPane(parentComponent) : null, title); pane.selectInitialValue(); dialog.setResizable(resizable); dialog.setVisible(true); dialog.dispose(); Object selectedValue = pane.getValue(); if (selectedValue instanceof Integer) { return ((Integer) selectedValue).intValue() == JOptionPane.YES_OPTION; }else{ return false; } } public static void setEnabledRecursively(Component comp, boolean enable){ comp.setEnabled(enable); if(comp instanceof Container) { Component[] components = ((Container)comp).getComponents(); for(int i = 0; i < components.length; i++){ setEnabledRecursively(components[i],enable); } } } }