001/* A password attribute.
002
003 Copyright (c) 2004-2013 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 @ProposedRating Green (eal@eecs.berkeley.edu)
028 @AcceptedRating Green (bilung@eecs.berkeley.edu)
029 */
030package ptolemy.actor.gui;
031
032import ptolemy.data.expr.StringParameter;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// PasswordAttribute
039
040/**
041 An attribute that represents a password. The value of this attribute is
042 a string that represents the password in an encrypted form. To access the
043 password in unencrypted form, call the getPassword() method. To set the
044 password in unencrypted form, call the setPassword() method.
045
046 FIXME: we need to support the persistence and encryption.
047
048 @author Edward Lee, Yang Zhao
049 @version $Id$
050 @since Ptolemy II 4.1
051 */
052public class PasswordAttribute extends StringParameter {
053    /** Construct a password attribute with the given container and name.
054     *  @param container The container.
055     *  @param name The name of this attribute.
056     *  @exception IllegalActionException If the entity cannot be contained
057     *   by the proposed container.
058     *  @exception NameDuplicationException If the container already has an
059     *   attribute with this name.
060     */
061    public PasswordAttribute(CompositeEntity container, String name)
062            throws NameDuplicationException, IllegalActionException {
063        super(container, name);
064    }
065
066    ///////////////////////////////////////////////////////////////////
067    ////                         public methods                    ////
068
069    /** Get the password contained by this attribute. If the password
070     *  hasn't been set, then open a dialog and wait for the user to
071     *  set the password.
072     *  @return A copy of the password.
073     *  @see #setPassword(char[])
074     */
075    public char[] getPassword() {
076        if (_password == null) {
077            //FIXME: this need to be done in the swing thread...
078            new EditParametersDialog(null, this);
079        }
080
081        // FindBugs: Return a copy instead of a mutable value.
082        char[] returnValue = new char[_password.length];
083        for (int i = 0; i < _password.length; i++) {
084            returnValue[i] = _password[i];
085        }
086        return returnValue;
087    }
088
089    /** Set the password contained by this attribute.
090     *  @param password The password.
091     *  @see #getPassword()
092     */
093    public void setPassword(char[] password) {
094        // FindBugs: Don't incorporate reference to a mutable object.
095        //char[] returnValue = new char[_password.length];
096        for (int i = 0; i < _password.length; i++) {
097            _password[i] = password[i];
098        }
099        _password = password;
100    }
101
102    ///////////////////////////////////////////////////////////////////
103    ////                        private members                    ////
104    private char[] _password = null;
105}