Search in sources :

Example 56 with JPasswordField

use of javax.swing.JPasswordField in project jdk8u_jdk by JetBrains.

the class PasswordView method drawSelectedText.

/**
     * Renders the given range in the model as selected text.  This
     * is implemented to render the text in the color specified in
     * the hosting component.  It assumes the highlighter will render
     * the selected background.  Uses the result of getEchoChar() to
     * display the characters.
     *
     * @param g the graphics context
     * @param x the starting X coordinate >= 0
     * @param y the starting Y coordinate >= 0
     * @param p0 the starting offset in the model >= 0
     * @param p1 the ending offset in the model >= p0
     * @return the X location of the end of the range >= 0
     * @exception BadLocationException if p0 or p1 are out of range
     */
protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
    g.setColor(selected);
    Container c = getContainer();
    if (c instanceof JPasswordField) {
        JPasswordField f = (JPasswordField) c;
        if (!f.echoCharIsSet()) {
            return super.drawSelectedText(g, x, y, p0, p1);
        }
        char echoChar = f.getEchoChar();
        int n = p1 - p0;
        for (int i = 0; i < n; i++) {
            x = drawEchoCharacter(g, x, y, echoChar);
        }
    }
    return x;
}
Also used : JPasswordField(javax.swing.JPasswordField)

Example 57 with JPasswordField

use of javax.swing.JPasswordField in project jdk8u_jdk by JetBrains.

the class PasswordView method modelToView.

/**
     * Provides a mapping from the document model coordinate space
     * to the coordinate space of the view mapped to it.
     *
     * @param pos the position to convert &gt;= 0
     * @param a the allocated region to render into
     * @return the bounding box of the given position
     * @exception BadLocationException  if the given position does not
     *   represent a valid location in the associated document
     * @see View#modelToView
     */
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
    Container c = getContainer();
    if (c instanceof JPasswordField) {
        JPasswordField f = (JPasswordField) c;
        if (!f.echoCharIsSet()) {
            return super.modelToView(pos, a, b);
        }
        char echoChar = f.getEchoChar();
        FontMetrics m = f.getFontMetrics(f.getFont());
        Rectangle alloc = adjustAllocation(a).getBounds();
        int dx = (pos - getStartOffset()) * m.charWidth(echoChar);
        alloc.x += dx;
        alloc.width = 1;
        return alloc;
    }
    return null;
}
Also used : JPasswordField(javax.swing.JPasswordField)

Example 58 with JPasswordField

use of javax.swing.JPasswordField in project jdk8u_jdk by JetBrains.

the class PasswordView method drawUnselectedText.

/**
     * Renders the given range in the model as normal unselected
     * text.  This sets the foreground color and echos the characters
     * using the value returned by getEchoChar().
     *
     * @param g the graphics context
     * @param x the starting X coordinate &gt;= 0
     * @param y the starting Y coordinate &gt;= 0
     * @param p0 the starting offset in the model &gt;= 0
     * @param p1 the ending offset in the model &gt;= p0
     * @return the X location of the end of the range &gt;= 0
     * @exception BadLocationException if p0 or p1 are out of range
     */
protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
    Container c = getContainer();
    if (c instanceof JPasswordField) {
        JPasswordField f = (JPasswordField) c;
        if (!f.echoCharIsSet()) {
            return super.drawUnselectedText(g, x, y, p0, p1);
        }
        if (f.isEnabled()) {
            g.setColor(f.getForeground());
        } else {
            g.setColor(f.getDisabledTextColor());
        }
        char echoChar = f.getEchoChar();
        int n = p1 - p0;
        for (int i = 0; i < n; i++) {
            x = drawEchoCharacter(g, x, y, echoChar);
        }
    }
    return x;
}
Also used : JPasswordField(javax.swing.JPasswordField)

Example 59 with JPasswordField

use of javax.swing.JPasswordField in project jdk8u_jdk by JetBrains.

the class DialogCallbackHandler method handle.

/**
     * Handles the specified set of callbacks.
     *
     * @param callbacks the callbacks to handle
     * @throws UnsupportedCallbackException if the callback is not an
     * instance  of NameCallback or PasswordCallback
     */
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    /* Collect messages to display in the dialog */
    final List<Object> messages = new ArrayList<>(3);
    /* Collection actions to perform if the user clicks OK */
    final List<Action> okActions = new ArrayList<>(2);
    ConfirmationInfo confirmation = new ConfirmationInfo();
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof TextOutputCallback) {
            TextOutputCallback tc = (TextOutputCallback) callbacks[i];
            switch(tc.getMessageType()) {
                case TextOutputCallback.INFORMATION:
                    confirmation.messageType = JOptionPane.INFORMATION_MESSAGE;
                    break;
                case TextOutputCallback.WARNING:
                    confirmation.messageType = JOptionPane.WARNING_MESSAGE;
                    break;
                case TextOutputCallback.ERROR:
                    confirmation.messageType = JOptionPane.ERROR_MESSAGE;
                    break;
                default:
                    throw new UnsupportedCallbackException(callbacks[i], "Unrecognized message type");
            }
            messages.add(tc.getMessage());
        } else if (callbacks[i] instanceof NameCallback) {
            final NameCallback nc = (NameCallback) callbacks[i];
            JLabel prompt = new JLabel(nc.getPrompt());
            final JTextField name = new JTextField(JTextFieldLen);
            String defaultName = nc.getDefaultName();
            if (defaultName != null) {
                name.setText(defaultName);
            }
            /*
                 * Put the prompt and name in a horizontal box,
                 * and add that to the set of messages.
                 */
            Box namePanel = Box.createHorizontalBox();
            namePanel.add(prompt);
            namePanel.add(name);
            messages.add(namePanel);
            /* Store the name back into the callback if OK */
            okActions.add(new Action() {

                public void perform() {
                    nc.setName(name.getText());
                }
            });
        } else if (callbacks[i] instanceof PasswordCallback) {
            final PasswordCallback pc = (PasswordCallback) callbacks[i];
            JLabel prompt = new JLabel(pc.getPrompt());
            final JPasswordField password = new JPasswordField(JPasswordFieldLen);
            if (!pc.isEchoOn()) {
                password.setEchoChar('*');
            }
            Box passwordPanel = Box.createHorizontalBox();
            passwordPanel.add(prompt);
            passwordPanel.add(password);
            messages.add(passwordPanel);
            okActions.add(new Action() {

                public void perform() {
                    pc.setPassword(password.getPassword());
                }
            });
        } else if (callbacks[i] instanceof ConfirmationCallback) {
            ConfirmationCallback cc = (ConfirmationCallback) callbacks[i];
            confirmation.setCallback(cc);
            if (cc.getPrompt() != null) {
                messages.add(cc.getPrompt());
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
    /* Display the dialog */
    int result = JOptionPane.showOptionDialog(parentComponent, messages.toArray(), "Confirmation", /* title */
    confirmation.optionType, confirmation.messageType, null, /* icon */
    confirmation.options, /* options */
    confirmation.initialValue);
    /* Perform the OK actions */
    if (result == JOptionPane.OK_OPTION || result == JOptionPane.YES_OPTION) {
        Iterator<Action> iterator = okActions.iterator();
        while (iterator.hasNext()) {
            iterator.next().perform();
        }
    }
    confirmation.handleResult(result);
}
Also used : ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) Box(javax.swing.Box) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) JTextField(javax.swing.JTextField) NameCallback(javax.security.auth.callback.NameCallback) JPasswordField(javax.swing.JPasswordField) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 60 with JPasswordField

use of javax.swing.JPasswordField in project processdash by dtuma.

the class TaskScheduleDialog method importNewSharedSchedule.

private String importNewSharedSchedule() {
    String urlStr = "http://";
    String passwordStr = "";
    String errorMessage = null;
    URL u = null;
    while (true) {
        // ask the user for the relevant information to locate the
        // schedule.
        JTextField url = new JTextField(urlStr, 40);
        url.addFocusListener(new FocusHighlighter(url));
        JTextField password = new JPasswordField(passwordStr, 10);
        password.addFocusListener(new FocusHighlighter(password));
        String urlLabel = resources.getString("Import_Schedule.URL_Label");
        String passwordLabel = resources.getString("Import_Schedule.Password_Label");
        Object message = new Object[] { errorMessage, resources.getString("Import_Schedule.Instructions"), newHBox(new JLabel("  " + urlLabel + " "), url), newHBox(new JLabel("  " + passwordLabel + " "), password) };
        if (JOptionPane.showConfirmDialog(frame, message, resources.getString("Import_Schedule.Dialog_Title"), JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)
            // if the user didn't hit the OK button, return null.
            return null;
        urlStr = url.getText();
        passwordStr = password.getText();
        if (urlStr == null || urlStr.trim().length() == 0) {
            errorMessage = resources.getString("Import_Schedule.URL_Missing");
            continue;
        }
        if (urlStr.indexOf("/ev+/") != -1) {
            errorMessage = resources.getString("Import_Schedule.Pub_URL");
            continue;
        }
        try {
            u = new URL(urlStr.trim() + XML_QUERY_SUFFIX);
        } catch (MalformedURLException mue) {
            errorMessage = resources.getString("Import_Schedule.URL_Invalid");
            continue;
        }
        break;
    }
    // fetch the specified schedule.
    if (passwordStr != null) {
        passwordStr = passwordStr.trim();
        if (passwordStr.length() == 0)
            passwordStr = null;
    }
    CachedObject importedSchedule = new CachedURLObject(dash.getCache(), EVTaskListCached.CACHED_OBJECT_TYPE, u, passwordStr == null ? null : "EV", passwordStr);
    // check to see if there was an error in fetching the schedule.
    errorMessage = importedSchedule.getErrorMessage();
    String remoteName = null;
    if (errorMessage == null) {
        // if we were able to successfully fetch the schedule, try
        // to interpret its contents as an XML schedule.
        remoteName = EVTaskListCached.getNameFromXML(importedSchedule.getString("UTF-8"));
        // record an error message.
        if (remoteName == null)
            errorMessage = resources.getString("Import_Schedule.Invalid_Schedule");
    }
    // if there was any error, ask the user if they want to continue.
    if (errorMessage != null) {
        errorMessage = CachedURLObject.translateMessage(resources, "Import_Schedule.", errorMessage);
        Object message = new Object[] { resources.getString("Import_Schedule.Error.Header"), "    " + errorMessage, resources.getString("Import_Schedule.Error.Footer") };
        if (JOptionPane.showConfirmDialog(frame, message, resources.getString("Import_Schedule.Error.Title"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE) != JOptionPane.YES_OPTION)
            return null;
    }
    // get a local name to use for this schedule.
    String localName = remoteName;
    String owner = (String) importedSchedule.getLocalAttr(CachedURLObject.OWNER_ATTR);
    if (owner != null)
        localName = resources.format("Import_Schedule.Default_Name_FMT", localName, owner);
    do {
        localName = (String) JOptionPane.showInputDialog(frame, resources.getStrings("Import_Schedule.Name_Dialog.Prompt"), resources.getString("Import_Schedule.Name_Dialog.Title"), JOptionPane.PLAIN_MESSAGE, null, null, localName);
        if (localName != null)
            localName = localName.replace('/', ' ').trim();
    } while (localName == null || localName.length() == 0);
    importedSchedule.setLocalAttr(EVTaskListCached.LOCAL_NAME_ATTR, localName);
    // return the name of the cached schedule object
    return EVTaskListCached.buildTaskListName(importedSchedule.getID(), localName);
}
Also used : MalformedURLException(java.net.MalformedURLException) CachedURLObject(net.sourceforge.processdash.net.cache.CachedURLObject) CachedObject(net.sourceforge.processdash.net.cache.CachedObject) JPasswordField(javax.swing.JPasswordField) JLabel(javax.swing.JLabel) CachedObject(net.sourceforge.processdash.net.cache.CachedObject) EventObject(java.util.EventObject) CachedURLObject(net.sourceforge.processdash.net.cache.CachedURLObject) JTextField(javax.swing.JTextField) URL(java.net.URL)

Aggregations

JPasswordField (javax.swing.JPasswordField)100 JLabel (javax.swing.JLabel)60 JPanel (javax.swing.JPanel)47 JTextField (javax.swing.JTextField)44 BorderLayout (java.awt.BorderLayout)30 JButton (javax.swing.JButton)24 JCheckBox (javax.swing.JCheckBox)23 GridBagLayout (java.awt.GridBagLayout)22 GridBagConstraints (java.awt.GridBagConstraints)19 ActionEvent (java.awt.event.ActionEvent)19 ActionListener (java.awt.event.ActionListener)19 Insets (java.awt.Insets)18 Dimension (java.awt.Dimension)15 WindowAdapter (java.awt.event.WindowAdapter)12 WindowEvent (java.awt.event.WindowEvent)12 EmptyBorder (javax.swing.border.EmptyBorder)12 EtchedBorder (javax.swing.border.EtchedBorder)12 AbstractAction (javax.swing.AbstractAction)11 CompoundBorder (javax.swing.border.CompoundBorder)11 ItemEvent (java.awt.event.ItemEvent)7