Example usage for javax.swing JOptionPane OK_CANCEL_OPTION

List of usage examples for javax.swing JOptionPane OK_CANCEL_OPTION

Introduction

In this page you can find the example usage for javax.swing JOptionPane OK_CANCEL_OPTION.

Prototype

int OK_CANCEL_OPTION

To view the source code for javax.swing JOptionPane OK_CANCEL_OPTION.

Click Source Link

Document

Type used for showConfirmDialog.

Usage

From source file:org.omegat.gui.main.ProjectUICommands.java

/**
 * Does wikiread//  w  w w. j a  v a  2 s.  c  o  m
 */
public static void doWikiImport() {
    String remote_url = JOptionPane.showInputDialog(Core.getMainWindow().getApplicationFrame(),
            OStrings.getString("TF_WIKI_IMPORT_PROMPT"), OStrings.getString("TF_WIKI_IMPORT_TITLE"),
            JOptionPane.OK_CANCEL_OPTION);
    String projectsource = Core.getProject().getProjectProperties().getSourceRoot();
    if (remote_url == null || remote_url.trim().isEmpty()) {
        // [1762625] Only try to get MediaWiki page if a string has been entered
        return;
    }
    try {
        WikiGet.doWikiGet(remote_url, projectsource);
        projectReload();
    } catch (Exception ex) {
        Log.log(ex);
        Core.getMainWindow().displayErrorRB(ex, "TF_WIKI_IMPORT_FAILED");
    }
}

From source file:org.opendatakit.briefcase.ui.MainClearBriefcasePreferencesWindow.java

/**
 * Launch the application./*from w  ww .  j a  v a2s . c o m*/
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                // Set System L&F
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                Image myImage = Toolkit.getDefaultToolkit().getImage(
                        MainClearBriefcasePreferencesWindow.class.getClassLoader().getResource("odk_logo.png"));
                Object[] options = { "Purge", "Cancel" };
                int outcome = JOptionPane.showOptionDialog(null,
                        "Clear all Briefcase preferences.                                            ",
                        CLEAR_PREFERENCES_VERSION, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,
                        new ImageIcon(myImage), options, options[1]);
                if (outcome == 0) {
                    BriefcasePreferences.setBriefcaseDirectoryProperty(null);
                }
            } catch (Exception e) {
                log.error("failed to launch app", e);
            }
        }
    });
}

From source file:org.openmrs.test.BaseContextSensitiveTest.java

/**
 * Utility method for obtaining username and password through Swing interface for tests. Any
 * tests extending the org.openmrs.BaseTest class may simply invoke this method by name.
 * Username and password are returned in a two-member String array. If the user aborts, null is
 * returned. <b> <em>Do not call for non-interactive tests, since this method will try to
 * render an interactive dialog box for authentication!</em></b>
 * /* w  ww  .  j  av  a2  s  . c  o m*/
 * @param message string to display above username field
 * @return Two-member String array containing username and password, respectively, or
 *         <code>null</code> if user aborts dialog
 */
public static synchronized String[] askForUsernameAndPassword(String message) {

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {

    }

    if (message == null || "".equals(message))
        message = "Enter username/password to authenticate to OpenMRS...";

    JPanel panel = new JPanel(new GridBagLayout());
    JLabel usernameLabel = new JLabel("Username");
    usernameLabel.setFont(font);
    usernameField = new JTextField(20);
    usernameField.setFont(font);
    JLabel passwordLabel = new JLabel("Password");
    passwordLabel.setFont(font);
    JPasswordField passwordField = new JPasswordField(20);
    passwordField.setFont(font);
    panel.add(usernameLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 5, 0));
    panel.add(usernameField, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    panel.add(passwordLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 5, 0));
    panel.add(passwordField, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

    frame = new JFrame();
    Window window = new Window(frame);
    frame.setVisible(true);
    frame.setTitle("JUnit Test Credentials");

    // We use a TimerTask to force focus on username, but still use
    // JOptionPane for model dialog
    TimerTask later = new TimerTask() {

        @Override
        public void run() {
            if (frame != null) {
                // bring the dialog's window to the front
                frame.toFront();
                usernameField.grabFocus();
            }
        }
    };
    // try setting focus half a second from now
    new Timer().schedule(later, 500);

    // attention grabber for those people that aren't as observant
    TimerTask laterStill = new TimerTask() {

        @Override
        public void run() {
            if (frame != null) {
                frame.toFront(); // bring the dialog's window to the
                // front
                usernameField.grabFocus();
            }
        }
    };
    // if the user hasn't done anything in 10 seconds, tell them the window
    // is there
    new Timer().schedule(laterStill, 10000);

    // show the dialog box
    int response = JOptionPane.showConfirmDialog(window, panel, message, JOptionPane.OK_CANCEL_OPTION);

    // clear out the window so the timer doesn't screw up
    laterStill.cancel();
    frame.setVisible(false);
    window.setVisible(false);
    frame = null;

    // response of 2 is the cancel button, response of -1 is the little red
    // X in the top right
    return (response == 2 || response == -1 ? null
            : new String[] { usernameField.getText(), String.valueOf(passwordField.getPassword()) });
}

From source file:org.openuat.apps.IPSecConnectorAdmin.java

/**
 * dialog to set up the dongle configuration.
 *///  www.  jav  a  2 s .  co m
private static Configuration configureDialog(String[] ports, String[] sides, String[] types) {
    JTextField username = new JTextField();
    JComboBox cport = new JComboBox(ports);
    JComboBox csides = new JComboBox(sides);
    JComboBox ctypes = new JComboBox(types);
    int option = JOptionPane.showOptionDialog(null,
            new Object[] { "User Name:", username, "Choose your port", cport,
                    "On which side of your Device is the Dongle plugged into:", csides, " What type of Device:",
                    ctypes, },
            " Relate Dongle Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null,
            null);
    if ((option == JOptionPane.CLOSED_OPTION) || (option == JOptionPane.CANCEL_OPTION)) {
        System.exit(0);
    }
    Configuration config = new Configuration(cport.getSelectedItem() + "");
    config.setType(ctypes.getSelectedIndex());
    config.setSide(csides.getSelectedIndex());
    config.setUserName(username.getText());
    config.setDeviceType(Configuration.DEVICE_TYPE_DONGLE);
    //      logger.finer(config);
    return config;
}

From source file:org.orbisgis.mapeditor.map.MapEditor.java

/**
 * The use want to export the rendering into a file.
 *//*from  www.j  a  va2  s .c om*/
public void onExportMapRendering() {
    // Show Dialog to select image size
    final String WIDTH_T = "width";
    final String HEIGHT_T = "height";
    final String RATIO_CHECKBOX_T = "ratio checkbox";
    final String TRANSPARENT_BACKGROUND_T = "background";
    final String DPI_T = "dpi";
    final int textWidth = 8;
    final MultiInputPanel inputPanel = new MultiInputPanel(I18N.tr("Export parameters"));

    inputPanel.addInput(TRANSPARENT_BACKGROUND_T, "", "True",
            new CheckBoxChoice(true, "<html>" + I18N.tr("Transparent\nbackground") + "</html>"));

    inputPanel.addInput(DPI_T, I18N.tr("DPI"),
            String.valueOf((int) (MapImageWriter.MILLIMETERS_BY_INCH / MapImageWriter.DEFAULT_PIXEL_SIZE)),
            new TextBoxType(textWidth));

    TextBoxType tbWidth = new TextBoxType(textWidth);
    inputPanel.addInput(WIDTH_T, I18N.tr("Width (pixels)"), String.valueOf(mapControl.getImage().getWidth()),
            tbWidth);
    TextBoxType tbHeight = new TextBoxType(textWidth);
    inputPanel.addInput(HEIGHT_T, I18N.tr("Height (pixels)"), String.valueOf(mapControl.getImage().getHeight()),
            tbHeight);

    tbHeight.getComponent().addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            userChangedHeight = true;
        }

        @Override
        public void focusLost(FocusEvent e) {
            updateWidth();
        }

        private void updateWidth() {
            if (userChangedHeight) {
                if (inputPanel.getInput(RATIO_CHECKBOX_T).equals("true")) {
                    // Change image width to keep ratio
                    final String heightString = inputPanel.getInput(HEIGHT_T);
                    if (!heightString.isEmpty()) {
                        try {
                            final Envelope adjExtent = mapControl.getMapTransform().getAdjustedExtent();
                            final double ratio = adjExtent.getWidth() / adjExtent.getHeight();
                            final int height = Integer.parseInt(heightString);
                            final long newWidth = Math.round(height * ratio);
                            inputPanel.setValue(WIDTH_T, String.valueOf(newWidth));
                        } catch (NumberFormatException e) {
                        }
                    }
                }
            }
            userChangedWidth = false;
        }
    });

    tbWidth.getComponent().addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            userChangedWidth = true;
        }

        @Override
        public void focusLost(FocusEvent e) {
            updateHeight();
        }

        private void updateHeight() {
            if (userChangedWidth) {
                if (inputPanel.getInput(RATIO_CHECKBOX_T).equals("true")) {
                    // Change image height to keep ratio
                    final String widthString = inputPanel.getInput(WIDTH_T);
                    if (!widthString.isEmpty()) {
                        try {
                            final Envelope adjExtent = mapControl.getMapTransform().getAdjustedExtent();
                            final double ratio = adjExtent.getHeight() / adjExtent.getWidth();
                            final int width = Integer.parseInt(widthString);
                            final long newHeight = Math.round(width * ratio);
                            inputPanel.setValue(HEIGHT_T, String.valueOf(newHeight));
                        } catch (NumberFormatException e) {
                        }
                    }
                }
            }
            userChangedHeight = false;
        }
    });

    inputPanel.addInput(RATIO_CHECKBOX_T, "", new CheckBoxChoice(true, I18N.tr("Keep ratio")));

    inputPanel.addValidation(new MIPValidationInteger(WIDTH_T, I18N.tr("Width (pixels)")));
    inputPanel.addValidation(new MIPValidationInteger(HEIGHT_T, I18N.tr("Height (pixels)")));
    inputPanel.addValidation(new MIPValidationInteger(DPI_T, I18N.tr("DPI")));

    JButton refreshButton = new JButton(I18N.tr("Reset extent"));
    refreshButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            inputPanel.setValue(WIDTH_T, String.valueOf(mapControl.getImage().getWidth()));
            inputPanel.setValue(HEIGHT_T, String.valueOf(mapControl.getImage().getHeight()));
        }
    });

    // Show the dialog and get the user's choice.
    int userChoice = JOptionPane.showOptionDialog(this, inputPanel.getComponent(),
            I18N.tr("Export map as image"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
            MapEditorIcons.getIcon("map_catalog"),
            new Object[] { I18N.tr("OK"), I18N.tr("Cancel"), refreshButton }, null);

    // If the user clicked OK, then show the save image dialog.
    if (userChoice == JOptionPane.OK_OPTION) {
        MapImageWriter mapImageWriter = new MapImageWriter(mapContext.getLayerModel());
        mapImageWriter
                .setPixelSize(MapImageWriter.MILLIMETERS_BY_INCH / Double.valueOf(inputPanel.getInput(DPI_T)));
        // If the user want a background color, let him choose one
        if (!Boolean.valueOf(inputPanel.getInput(TRANSPARENT_BACKGROUND_T))) {
            ColorPicker colorPicker = new ColorPicker(Color.white);
            if (!UIFactory.showDialog(colorPicker, true, true)) {
                return;
            }
            mapImageWriter.setBackgroundColor(colorPicker.getColor());
        }
        // Save the picture in which location
        final SaveFilePanel outfilePanel = new SaveFilePanel("MapEditor.ExportInFile",
                I18N.tr("Save the map as image : " + mapContext.getTitle()));
        outfilePanel.addFilter("png", I18N.tr("Portable Network Graphics"));
        outfilePanel.addFilter("tiff", I18N.tr("Tagged Image File Format"));
        outfilePanel.addFilter("jpg", I18N.tr("Joint Photographic Experts Group"));
        outfilePanel.addFilter("pdf", I18N.tr("Portable Document Format"));
        outfilePanel.loadState(); // Load last use path
        // Show save into dialog
        if (UIFactory.showDialog(outfilePanel, true, true)) {
            File outFile = outfilePanel.getSelectedFile();
            String fileName = FilenameUtils.getExtension(outFile.getName());
            if (fileName.equalsIgnoreCase("png")) {
                mapImageWriter.setFormat(MapImageWriter.Format.PNG);
            } else if (fileName.equalsIgnoreCase("jpg")) {
                mapImageWriter.setFormat(MapImageWriter.Format.JPEG);
            } else if (fileName.equalsIgnoreCase("pdf")) {
                mapImageWriter.setFormat(MapImageWriter.Format.PDF);
            } else {
                mapImageWriter.setFormat(MapImageWriter.Format.TIFF);
            }
            mapImageWriter.setBoundingBox(mapContext.getBoundingBox());
            int width = Integer.valueOf(inputPanel.getInput(WIDTH_T));
            int height = Integer.valueOf(inputPanel.getInput(HEIGHT_T));

            mapImageWriter.setWidth(width);
            mapImageWriter.setHeight(height);
            execute(new ExportRenderingIntoFile(mapImageWriter, outFile));
        }
    }
}

From source file:org.orbisgis.view.map.MapEditor.java

/**
 * The use want to export the rendering into a file.
 *///  ww w  .  j  ava 2  s  . c om
public void onExportMapRendering() {
    // Show Dialog to select image size
    final String WIDTH_T = "width";
    final String HEIGHT_T = "height";
    final String RATIO_T = "ratio";
    final String TRANSPARENT_BACKGROUND_T = "background";
    final String DPI_T = "dpi";
    final int textWidth = 8;
    String[] RATIO = new String[] { "UPDATE_EXTENT", "FIX_WIDTH", "FIX_HEIGHT" };
    String[] RATIO_LABELS = new String[] { I18N.tr("Change extent"), I18N.tr("Update height to keep ratio"),
            I18N.tr("Update width to keep ratio") };
    MultiInputPanel inputPanel = new MultiInputPanel(I18N.tr("Export parameters"));

    inputPanel.addInput(WIDTH_T, I18N.tr("Width (pixels)"), String.valueOf(mapControl.getImage().getWidth()),
            new TextBoxType(textWidth));
    inputPanel.addInput(HEIGHT_T, I18N.tr("Height (pixels)"), String.valueOf(mapControl.getImage().getHeight()),
            new TextBoxType(textWidth));

    ComboBoxChoice comboBoxChoice = new ComboBoxChoice(RATIO, RATIO_LABELS);
    comboBoxChoice.setValue(RATIO[0]);
    inputPanel.addInput(RATIO_T, I18N.tr("Ratio"), comboBoxChoice);

    inputPanel.addInput(DPI_T, I18N.tr("DPI"),
            String.valueOf((int) (MapImageWriter.MILLIMETERS_BY_INCH / MapImageWriter.DEFAULT_PIXEL_SIZE)),
            new TextBoxType(textWidth));
    inputPanel.addInput(TRANSPARENT_BACKGROUND_T, "", "True",
            new CheckBoxChoice(true, "<html>" + I18N.tr("Transparent\nbackground") + "</html>"));

    inputPanel.addValidation(new MIPValidationInteger(WIDTH_T, I18N.tr("Width (pixels)")));
    inputPanel.addValidation(new MIPValidationInteger(HEIGHT_T, I18N.tr("Height (pixels)")));
    inputPanel.addValidation(new MIPValidationInteger(DPI_T, I18N.tr("DPI")));

    // Show the dialog and get the user's choice.
    int userChoice = JOptionPane.showConfirmDialog(this, inputPanel.getComponent(),
            I18N.tr("Export map as image"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
            OrbisGISIcon.getIcon("map_catalog"));

    // If the user clicked OK, then show the save image dialog.
    if (userChoice == JOptionPane.OK_OPTION) {
        MapImageWriter mapImageWriter = new MapImageWriter(mapContext.getLayerModel());
        mapImageWriter
                .setPixelSize(MapImageWriter.MILLIMETERS_BY_INCH / Double.valueOf(inputPanel.getInput(DPI_T)));
        // If the user want a background color, let him choose one
        if (!Boolean.valueOf(inputPanel.getInput(TRANSPARENT_BACKGROUND_T))) {
            ColorPicker colorPicker = new ColorPicker(Color.white);
            if (!UIFactory.showDialog(colorPicker, true, true)) {
                return;
            }
            mapImageWriter.setBackgroundColor(colorPicker.getColor());
        }
        // Save the picture in which location
        final SaveFilePanel outfilePanel = new SaveFilePanel("MapEditor.ExportInFile",
                I18N.tr("Save the map as image : " + mapContext.getTitle()));
        outfilePanel.setConfirmOverwrite(true);
        outfilePanel.addFilter("png", I18N.tr("Portable Network Graphics"));
        outfilePanel.addFilter("tiff", I18N.tr("Tagged Image File Format"));
        outfilePanel.loadState(); // Load last use path
        // Show save into dialog
        if (UIFactory.showDialog(outfilePanel, true, true)) {
            File outFile = outfilePanel.getSelectedFile();
            String fileName = FilenameUtils.getExtension(outFile.getName());
            if (fileName.equalsIgnoreCase("png")) {
                mapImageWriter.setFormat(MapImageWriter.Format.PNG);
            } else {
                mapImageWriter.setFormat(MapImageWriter.Format.TIFF);
            }
            mapImageWriter.setBoundingBox(mapContext.getBoundingBox());
            int width = Integer.valueOf(inputPanel.getInput(WIDTH_T));
            int height = Integer.valueOf(inputPanel.getInput(HEIGHT_T));
            Envelope adjExtent = mapControl.getMapTransform().getAdjustedExtent();
            if (comboBoxChoice.getValue().equals(RATIO[1])) {
                // Change image height to keep ratio
                height = (int) (width * (adjExtent.getHeight() / adjExtent.getWidth()));
            } else if (comboBoxChoice.getValue().equals(RATIO[2])) {
                width = (int) (height * (adjExtent.getWidth() / adjExtent.getHeight()));
            }
            mapImageWriter.setWidth(width);
            mapImageWriter.setHeight(height);
            ExportRenderingIntoFile renderingIntoFile = new ExportRenderingIntoFile(mapImageWriter, outFile);
            BackgroundManager bm = Services.getService(BackgroundManager.class);
            bm.nonBlockingBackgroundOperation(renderingIntoFile);
        }
    }
}

From source file:org.paxml.el.UtilFunctions.java

public static String ask(String question, boolean mask) {
    class DummyFrame extends JFrame {
        DummyFrame(String title) {
            super(title);
            setUndecorated(true);//w  w w  .ja  v  a  2 s  .  c o m
            setVisible(true);
            setLocationRelativeTo(null);
        }
    }
    JPasswordField pf = new JPasswordField();
    DummyFrame frame = new DummyFrame(question);
    int okCxl = JOptionPane.showConfirmDialog(frame, pf, question, JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE);
    frame.setVisible(false);
    if (okCxl == JOptionPane.OK_OPTION) {
        return new String(pf.getPassword());
    }
    return null;
}

From source file:org.paxml.security.SecretRepository.java

public static String askForPasswordInput(String question) {

    class DummyFrame extends JFrame {
        DummyFrame(String title) {
            super(title);
            setUndecorated(true);/*from w w  w. j  a v  a2 s  .co m*/
            setVisible(true);
            setLocationRelativeTo(null);
        }
    }
    JPasswordField pf = new JPasswordField();
    int okCxl = JOptionPane.showConfirmDialog(new DummyFrame(question), pf, question,
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

    if (okCxl == JOptionPane.OK_OPTION) {
        return new String(pf.getPassword());
    }
    return null;
}

From source file:org.pentaho.reporting.engine.classic.extensions.drilldown.devtools.DrillDownProfileEditor.java

public void create() {
    final int retval = JOptionPane.showOptionDialog(this, "Select link customizer type",
            "Select Link Customizer Type", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
            new Class[] { FormulaLinkCustomizer.class, PatternLinkCustomizer.class },
            FormulaLinkCustomizer.class);
    if (retval == 0) {
        drillDownProfiles.addElement(new DrillDownProfile(FormulaLinkCustomizer.class));
    } else if (retval == 1) {
        drillDownProfiles.addElement(new DrillDownProfile(PatternLinkCustomizer.class));
    }/*from w ww . j  a  v  a 2  s .co  m*/
}

From source file:org.pentaho.reporting.ui.datasources.kettle.EmbeddedKettleDataSourceDialog.java

@Override
protected boolean validateInputs(boolean onConfirm) {
    boolean valid = true;

    for (final KettleQueryEntry queryEntry : getQueryEntries()) {
        valid = queryEntry.isValidated();
        if (!valid) {
            break;
        }//from w ww  .  ja v a  2  s .c o m
    }

    if (valid == false && onConfirm == true) {
        int val = JOptionPane.showConfirmDialog(this,
                Messages.getString("EmbeddedKettleDataSourceDialog.QueryErrorWarning"),
                Messages.getString("EmbeddedKettleDataSourceDialog.QueryErrorTitle"),
                JOptionPane.OK_CANCEL_OPTION);
        valid = (val == JOptionPane.OK_OPTION);
    }

    return valid && super.validateInputs(onConfirm);
}