/** * Brings up a sheet with a specified icon, where the initial choice is determined by the {@code * initialValue} parameter and the number of choices is determined by the {@code optionType} * parameter. * * <p>If {@code optionType} is {@code YES_NO_OPTION}, or {@code YES_NO_CANCEL_OPTION} and the * {@code options} parameter is {@code null}, then the options are supplied by the look and feel. * * <p>The {@code messageType} parameter is primarily used to supply a default icon from the look * and feel. * * @param parentComponent determines the {@code Frame} in which the dialog is displayed; if {@code * null}, or if the {@code parentComponent} has no {@code Frame}, the sheet is displayed as a * dialog. * @param message the {@code Object} to display * @param optionType an integer designating the options available on the dialog: {@code * YES_NO_OPTION}, or {@code 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: {@code JOptionPane.ERROR_MESSAGE}, * {@code JOptionPane.INFORMATION_MESSAGE}, {@code JOptionPane.WARNING_MESSAGE}, {@code * JOptionPane.QUESTION_MESSAGE}, or {@code JOptionPane.PLAIN_MESSAGE} * @param icon the icon to display in the dialog * @param options an array of objects indicating the possible choices the user can make; if the * objects are components, they are rendered properly; non-{@code String} objects are rendered * using their {@code toString} methods; if this parameter is {@code null}, the options are * determined by the Look and Feel * @param initialValue the object that represents the default selection for the dialog; only * meaningful if {@code options} is used; can be {@code null} * @param listener The listener for SheetEvents. */ public static void showOptionSheet( Component parentComponent, Object message, int optionType, int messageType, Icon icon, Object[] options, Object initialValue, SheetListener listener) { JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue); pane.setInitialValue(initialValue); pane.setComponentOrientation( ((parentComponent == null) ? JOptionPane.getRootFrame() : parentComponent) .getComponentOrientation()); int style = styleFromMessageType(messageType); JSheet sheet = createSheet(pane, parentComponent, style); pane.selectInitialValue(); sheet.addSheetListener(listener); sheet.show(); sheet.toFront(); }
/** * Prompts the user for input in a sheet where the initial selection, possible selections, and all * other options can be specified. The user will able to choose from {@code selectionValues}, * where {@code null} implies the user can input whatever they wish, usually by means of a {@code * JTextField}. {@code initialSelectionValue} is the initial value to prompt the user with. It is * up to the UI to decide how best to represent the {@code selectionValues}, but usually a {@code * JComboBox}, {@code JList}, or {@code JTextField} will be used. * * @param parentComponent the parent {@code Component} for the dialog * @param message the {@code Object} to display * @param messageType the type of message to be displayed: {@code JOptionPane.ERROR_MESSAGE}, * {@code JOptionPane.INFORMATION_MESSAGE}, {@code JOptionPane.WARNING_MESSAGE}, {@code * JOptionPane.QUESTION_MESSAGE}, or {@code JOptionPane.PLAIN_MESSAGE} * @param icon the {@code Icon} image to display * @param selectionValues an array of {@code Object}s that gives the possible selections * @param initialSelectionValue the value used to initialize the input field * @param listener The listener for SheetEvents. */ public static void showInputSheet( Component parentComponent, Object message, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue, SheetListener listener) { JOptionPane pane = new JOptionPane(message, messageType, JOptionPane.OK_CANCEL_OPTION, icon, null, null); pane.setWantsInput(true); pane.setSelectionValues(selectionValues); pane.setInitialSelectionValue(initialSelectionValue); pane.setComponentOrientation( ((parentComponent == null) ? JOptionPane.getRootFrame() : parentComponent) .getComponentOrientation()); int style = styleFromMessageType(messageType); JSheet sheet = createSheet(pane, parentComponent, style); pane.selectInitialValue(); /* sheet.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent evt) { sheet.dispose(); } });*/ sheet.addSheetListener(listener); sheet.show(); sheet.toFront(); }
/** * Displays an option pane as a sheet on its parent window. * * @param pane The option pane. * @param parentComponent The parent of the option pane. * @param listener The listener for SheetEvents. */ public static void showSheet( JOptionPane pane, Component parentComponent, SheetListener listener) { final JSheet sheet = createSheet(pane, parentComponent, styleFromMessageType(pane.getMessageType())); sheet.addSheetListener(listener); sheet.show(); }
/** * Displays a custom file chooser sheet with a custom approve button. * * @param chooser the chooser * @param parent the parent component of the dialog; can be {@code null} * @param approveButtonText the text of the {@code ApproveButton} * @param listener The listener for SheetEvents. */ public static void showSheet( final JFileChooser chooser, Component parent, String approveButtonText, SheetListener listener) { if (approveButtonText != null) { chooser.setApproveButtonText(approveButtonText); chooser.setDialogType(JFileChooser.CUSTOM_DIALOG); } // Begin Create Dialog Frame frame = parent instanceof Frame ? (Frame) parent : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); String title = chooser.getUI().getDialogTitle(chooser); chooser.getAccessibleContext().setAccessibleDescription(title); final JSheet sheet = new JSheet(frame); sheet.addSheetListener(listener); Container contentPane = sheet.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(chooser, BorderLayout.CENTER); // End Create Dialog final ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent evt) { int option; if (evt.getActionCommand().equals("ApproveSelection")) { option = JFileChooser.APPROVE_OPTION; } else { option = JFileChooser.CANCEL_OPTION; } sheet.hide(); sheet.fireOptionSelected(chooser, option); chooser.removeActionListener(this); } }; chooser.addActionListener(actionListener); sheet.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { sheet.fireOptionSelected(chooser, JFileChooser.CANCEL_OPTION); chooser.removeActionListener(actionListener); } }); chooser.rescanCurrentDirectory(); sheet.pack(); sheet.show(); sheet.toFront(); }
protected void fileOpened(final Project project, File file, Object value) { if (value == null) { project.setFile(file); project.setEnabled(true); getApplication().addRecentFile(file); } else { JSheet.showMessageSheet( project.getComponent(), "<html>" + UIManager.getString("OptionPane.css") + "<b>Couldn't open the file \"" + file + "\".</b><br>" + value, JOptionPane.ERROR_MESSAGE, new SheetListener() { public void optionSelected(SheetEvent evt) { project.clear(); project.setEnabled(true); } }); } }
private static JSheet createSheet(final JOptionPane pane, Component parentComponent, int style) { Window window = getWindowForComponent(parentComponent); final JSheet sheet; if (window instanceof Frame) { sheet = new JSheet((Frame) window); } else { sheet = new JSheet((Dialog) window); } JComponent contentPane = (JComponent) sheet.getContentPane(); contentPane.setLayout(new BorderLayout()); if (isNativeSheetSupported()) { contentPane.setBorder(new EmptyBorder(12, 0, 0, 0)); } contentPane.add(pane, BorderLayout.CENTER); sheet.setResizable(false); sheet.addWindowListener( new WindowAdapter() { private boolean gotFocus = false; @Override public void windowClosing(WindowEvent we) { pane.setValue(null); } @Override public void windowClosed(WindowEvent we) { if (pane.getValue() == JOptionPane.UNINITIALIZED_VALUE) { sheet.fireOptionSelected(pane); } } @Override public void windowGainedFocus(WindowEvent we) { // Once window gets focus, set initial focus if (!gotFocus) { // Ugly dirty hack: JOptionPane.selectInitialValue() is protected. // So we call directly into the UI. This may cause mayhem, // because we override the encapsulation. // pane.selectInitialValue(); OptionPaneUI ui = pane.getUI(); if (ui != null) { ui.selectInitialValue(pane); } gotFocus = true; } } }); sheet.addComponentListener( new ComponentAdapter() { @Override public void componentShown(ComponentEvent ce) { // reset value to ensure closing works properly pane.setValue(JOptionPane.UNINITIALIZED_VALUE); } }); pane.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { // Let the defaultCloseOperation handle the closing // if the user closed the window without selecting a button // (newValue = null in that case). Otherwise, close the sheet. if (sheet.isVisible() && event.getSource() == pane && (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) && event.getNewValue() != null && event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { sheet.setVisible(false); sheet.fireOptionSelected(pane); } } }); sheet.pack(); return sheet; }