Home > api-openbis-matlab > passwordEntryDialog.m

passwordEntryDialog

PURPOSE ^

PASSWORDENTRYDIALOG

SYNOPSIS ^

function [Password, UserName] = passwordEntryDialog(varargin)

DESCRIPTION ^

 PASSWORDENTRYDIALOG
 [Password, UserName] = passwordEntryDialog(varargin)

 Create a password entry dialog for entering a password that is visibly
 hidden.  Java must be enabled for this function to work properly.

 It has only been tested on the Windows platform in R2008a.  It should
 work in R2007a or later.

 The password box is created using the Java Swing component
 JPasswordField.

 Optional Input Arguments
 ------------------------

 'enterUserName'       DEFAULT: false
 Display the user name entry box.  The user name entered must be at least
 one character or an error dialog is displayed.

 'DefaultUserName'     DEFAULT: ''
 String value of user name to populate in User Name box upon creation.

 'ValidatePassword'    DEFAULT: false
 Display dialog box to reenter password for validation purposes.

 'CheckPasswordLength' DEFAULT: true
 Check the password length to ensure it meets the specified criteria.

 'PasswordLengthMin'   DEFAULT: 2
 Minimum password length allowed.

 'PasswordLengthMax'   DEFAULT: 8
 Maximum password length allowed.

 'WindowName'          DEFAULT: 'Login'
 Title of the password entry window.

 Examples
 --------

 Create a password dialog box with the default options.
 -----------------------------------------------------------------------
 [Password] = passwordEntryDialog;

 Create a user name and password entry dialog box without password
 verification.
 -----------------------------------------------------------------------
 [Password, UserName] = passwordEntryDialog('enterUserName', true)

 Create a user name and password entry dialog box without password
 verification.  Set the user name to 'jdoe' upon startup.
 -----------------------------------------------------------------------
 [Password, UserName] = passwordEntryDialog('enterUserName', true,...
      'DefaultUserName', 'jdoe')

 Create a password dialog box with password validation
 -----------------------------------------------------------------------
 [Password] = passwordEntryDialog('ValidatePassword', true);

 Create a user name and password entry dialog box with password
 verification.
 -----------------------------------------------------------------------
 [Password, UserName] = passwordEntryDialog('enterUserName', true,...
      'ValidatePassword', true)

 Check the length of the password to be between 5 and 8 characters
 -----------------------------------------------------------------------
 [Password, UserName] = passwordEntryDialog('CheckPasswordLength', true,...
      'PasswordLengthMin', 5,...
      'PasswordLengthMax', 8)

 -----------------------------------------------------------------------
 Copyright (C) 2007-2008, Jesse B. Lai

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [Password, UserName] = passwordEntryDialog(varargin)
0002 % PASSWORDENTRYDIALOG
0003 % [Password, UserName] = passwordEntryDialog(varargin)
0004 %
0005 % Create a password entry dialog for entering a password that is visibly
0006 % hidden.  Java must be enabled for this function to work properly.
0007 %
0008 % It has only been tested on the Windows platform in R2008a.  It should
0009 % work in R2007a or later.
0010 %
0011 % The password box is created using the Java Swing component
0012 % JPasswordField.
0013 %
0014 % Optional Input Arguments
0015 % ------------------------
0016 %
0017 % 'enterUserName'       DEFAULT: false
0018 % Display the user name entry box.  The user name entered must be at least
0019 % one character or an error dialog is displayed.
0020 %
0021 % 'DefaultUserName'     DEFAULT: ''
0022 % String value of user name to populate in User Name box upon creation.
0023 %
0024 % 'ValidatePassword'    DEFAULT: false
0025 % Display dialog box to reenter password for validation purposes.
0026 %
0027 % 'CheckPasswordLength' DEFAULT: true
0028 % Check the password length to ensure it meets the specified criteria.
0029 %
0030 % 'PasswordLengthMin'   DEFAULT: 2
0031 % Minimum password length allowed.
0032 %
0033 % 'PasswordLengthMax'   DEFAULT: 8
0034 % Maximum password length allowed.
0035 %
0036 % 'WindowName'          DEFAULT: 'Login'
0037 % Title of the password entry window.
0038 %
0039 % Examples
0040 % --------
0041 %
0042 % Create a password dialog box with the default options.
0043 % -----------------------------------------------------------------------
0044 % [Password] = passwordEntryDialog;
0045 %
0046 % Create a user name and password entry dialog box without password
0047 % verification.
0048 % -----------------------------------------------------------------------
0049 % [Password, UserName] = passwordEntryDialog('enterUserName', true)
0050 %
0051 % Create a user name and password entry dialog box without password
0052 % verification.  Set the user name to 'jdoe' upon startup.
0053 % -----------------------------------------------------------------------
0054 % [Password, UserName] = passwordEntryDialog('enterUserName', true,...
0055 %      'DefaultUserName', 'jdoe')
0056 %
0057 % Create a password dialog box with password validation
0058 % -----------------------------------------------------------------------
0059 % [Password] = passwordEntryDialog('ValidatePassword', true);
0060 %
0061 % Create a user name and password entry dialog box with password
0062 % verification.
0063 % -----------------------------------------------------------------------
0064 % [Password, UserName] = passwordEntryDialog('enterUserName', true,...
0065 %      'ValidatePassword', true)
0066 %
0067 % Check the length of the password to be between 5 and 8 characters
0068 % -----------------------------------------------------------------------
0069 % [Password, UserName] = passwordEntryDialog('CheckPasswordLength', true,...
0070 %      'PasswordLengthMin', 5,...
0071 %      'PasswordLengthMax', 8)
0072 %
0073 % -----------------------------------------------------------------------
0074 % Copyright (C) 2007-2008, Jesse B. Lai
0075 %
0076 % This program is free software: you can redistribute it and/or modify
0077 % it under the terms of the GNU Lesser General Public License as published
0078 % by the Free Software Foundation, either version 3 of the License, or
0079 % (at your option) any later version.
0080 %
0081 % This program is distributed in the hope that it will be useful,
0082 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0083 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0084 % GNU Lesser General Public License for more details.
0085 %
0086 % You should have received a copy of the GNU Lesser General Public License
0087 % along with this program.  If not, see <http://www.gnu.org/licenses/>
0088 
0089 %% History
0090 % The history of this program is outlined in this section.
0091 %
0092 % 20080616 - JBL - 0.0.2
0093 % Started 20080612
0094 %
0095 % Updated to remove requirement for uicomponent function written by Yair
0096 % Altman.  Now, the Java components are created manually and the
0097 % undocumented feature javacomponent is used.
0098 %
0099 % A focus issue was worked out by using the drawnow command in a couple of
0100 % places to allow the objects to be focused properly upon startup.
0101 %
0102 % 20080427 - JBL - 0.0.1
0103 % Started 20080425.
0104 %
0105 % Initial version.  Uses Java to create the password entry box.  An edit
0106 % box was initially attemped with the Java frame, but was occasionally
0107 % getting Java exceptions when trying to query the 'SelectionStart' and
0108 % 'SelectionEnd' properties.
0109 %
0110 % Basic options of entering user name and password with options for
0111 % password validation.
0112 %
0113 % ToDo: Maybe add valid string input argument to only allow certain
0114 % characters.
0115 
0116 %% Program Information
0117 
0118 % ProgramName = 'passwordEntryDialog';
0119 % ProgramVersion = '0.0.2';
0120 % svnRevision = '$Revision: 184 $';
0121 % svnRevision = getSVNRevision(svnRevision);
0122 % ProgramVersion = [ProgramVersion, '.' ,svnRevision];
0123 %
0124 % LastChangedDate = '$LastChangedDate: 2008-06-16 09:08:17 -0600 (Mon, 16 Jun 2008) $';
0125 % ProgramDate = getSVNDate(LastChangedDate);
0126 
0127 %% Check for Existance of Java
0128 if ~usejava('swing')
0129    error('passwordEntryDialog: Java is required for this program to run.');
0130 end
0131 
0132 %% Parse Input Arguments
0133 % Input arguments are parsed with the MATLAB inputParser class.
0134 
0135 % Create input parser object
0136 ProgramOptionsParser = inputParser;
0137 ProgramOptionsParser.KeepUnmatched = true;
0138 
0139 ProgramOptionsParser.addParamValue('enterUserName', false, @(x) islogical(x) || isnumeric(x));
0140 ProgramOptionsParser.addParamValue('DefaultUserName', '', @ischar);
0141 ProgramOptionsParser.addParamValue('ValidatePassword', false, @(x) islogical(x) || isnumeric(x));
0142 ProgramOptionsParser.addParamValue('CheckPasswordLength', true, @(x) islogical(x) || isnumeric(x));
0143 ProgramOptionsParser.addParamValue('PasswordLengthMin', 2, @isnumeric);
0144 ProgramOptionsParser.addParamValue('PasswordLengthMax', 8, @isnumeric);
0145 ProgramOptionsParser.addParamValue('WindowName', 'Login', @ischar);
0146 
0147 % Parse Input Arguments
0148 try
0149     ProgramOptionsParser.parse(varargin{:});
0150 catch Error
0151     ProgramOptionsParser.parse;
0152     if strcmpi(Error.identifier, 'MATLAB:InputParser:ArgumentFailedValidation')
0153         error(Error.identifier, Error.message);
0154     end;
0155 end;
0156 
0157 ProgramOptions = ProgramOptionsParser.Results;
0158 
0159 % Validate password length options
0160 if ProgramOptions.CheckPasswordLength
0161     if ProgramOptions.PasswordLengthMax < ProgramOptions.PasswordLengthMin
0162         error('MATLAB:InputParser:ArgumentFailedValidation', 'PasswordLengthMax must be greater than PasswordLengthMin');
0163     end;
0164 end;
0165 
0166 %% Determine GUI Size and Position
0167 % Center the GUI on the screen.
0168 
0169 set(0,'Units','pixels')
0170 Screen = get(0,'screensize');
0171 
0172 PositionGUI = [0 0 300 50];
0173 if ProgramOptions.enterUserName
0174     PositionGUI = PositionGUI + [0 0 0 50];
0175 end;
0176 if ProgramOptions.ValidatePassword
0177     PositionGUI = PositionGUI + [0 0 0 50];
0178     OffsetBottom = 43;
0179 else
0180     OffsetBottom = 0;
0181 end;
0182 
0183 PositionGUI = [Screen(3:4)/2-PositionGUI(3:4)/2 PositionGUI(3:4)];
0184 PositionLeft = 10;
0185 BoxWidth = 200;
0186 
0187 %% Create the GUI
0188 
0189 BackgroundColor = get(0,'DefaultUicontrolBackgroundcolor');
0190 handles.figure1 = figure('Menubar','none', ...
0191     'Units','Pixels', ...
0192     'Resize','off', ...
0193     'NumberTitle','off', ...
0194     'Name',ProgramOptions.WindowName, ...
0195     'Position',PositionGUI, ...
0196     'Color', BackgroundColor, ...
0197     'WindowStyle','modal');
0198 
0199 % Create Password Validation Entry Box
0200 if ProgramOptions.ValidatePassword
0201     handles.java_PasswordValidate = javax.swing.JPasswordField();
0202     handles.java_PasswordValidate.setFocusable(true);
0203     [handles.java_PasswordValidate, handles.edit_PasswordValidate] = javacomponent(handles.java_PasswordValidate, [], handles.figure1);
0204 
0205     set(handles.edit_PasswordValidate, ...
0206         'Parent', handles.figure1, ...
0207         'Tag', 'edit_PasswordValidate', ...
0208         'Units', 'Pixels', ...
0209         'Position',[PositionLeft 10 BoxWidth 23]);
0210 
0211     handles.text_LabelPasswordValidate = uicontrol('Parent',handles.figure1, ...
0212         'Tag', 'text_LabelPassword', ...
0213         'Style','Text', ...
0214         'Units','Pixels',...
0215         'Position',[PositionLeft 33 BoxWidth 16], ...
0216         'FontSize',10, ...
0217         'String','Reenter password:',...
0218         'HorizontalAlignment', 'Left');
0219 end;
0220 
0221 % Create Password Entry Box
0222 handles.java_Password = javax.swing.JPasswordField();
0223 [handles.java_Password, handles.edit_Password] = javacomponent(handles.java_Password, [PositionLeft 10+OffsetBottom BoxWidth 23], handles.figure1);
0224 handles.java_Password.setFocusable(true);
0225 
0226 set(handles.edit_Password, ...
0227     'Parent', handles.figure1, ...
0228     'Tag', 'edit_Password', ...
0229     'Units', 'Pixels', ...
0230     'Position',[PositionLeft 10+OffsetBottom BoxWidth 23]);
0231 drawnow;    % This drawnow is required to allow the focus to work
0232    
0233 handles.text_LabelPassword = uicontrol('Parent',handles.figure1, ...
0234     'Tag', 'text_LabelPassword', ...
0235     'Style','Text', ...
0236     'Units','Pixels',...
0237     'Position',[PositionLeft 33+OffsetBottom BoxWidth 16], ...
0238     'FontSize',10, ...
0239     'String','Key:',...
0240     'HorizontalAlignment', 'Left');
0241 
0242 % Create OK Pushbutton
0243 handles.pushbutton_OK = uicontrol('Parent',handles.figure1, ...
0244     'Tag', 'pushbutton_OK', ...
0245     'Style','Pushbutton', ...
0246     'Units','Pixels',...
0247     'Position',[PositionLeft+BoxWidth+5 10 30 23], ...
0248     'FontSize',10, ...
0249     'String','OK',...
0250     'HorizontalAlignment', 'Center');
0251 
0252 % Create Cancel Pushbutton
0253 handles.pushbutton_Cancel = uicontrol('Parent',handles.figure1, ...
0254     'Tag', 'pushbutton_Cancel', ...
0255     'Style','Pushbutton', ...
0256     'Units','Pixels',...
0257     'Position',[PositionLeft+BoxWidth+30+7 10 50 23], ...
0258     'FontSize',10, ...
0259     'String','Cancel',...
0260     'HorizontalAlignment', 'Center');
0261 
0262 % Create User Name Edit Box
0263 if ProgramOptions.enterUserName
0264     handles.java_UserName = javax.swing.JTextField();
0265     handles.java_UserName.setFocusable(true);
0266     [handles.java_UserName, handles.edit_UserName] = javacomponent(handles.java_UserName, [], handles.figure1);
0267 
0268     set(handles.edit_UserName, ...
0269         'Parent', handles.figure1, ...
0270         'Tag', 'edit_UserName', ...
0271         'Units', 'Pixels', ...
0272         'Position',[PositionLeft 53+OffsetBottom 200 23]);
0273     set(handles.java_UserName, 'Text', ProgramOptions.DefaultUserName);
0274     drawnow;    % This drawnow is required to allow the focus to work
0275 
0276     handles.text_LabelUserName = uicontrol('Parent',handles.figure1, ...
0277         'Tag', 'text_LabelUserName', ...
0278         'Style','Text', ...
0279         'Units','Pixels',...
0280         'Position',[PositionLeft 76+OffsetBottom 200 16], ...
0281         'FontSize',10, ...
0282         'String','User name:',...
0283         'HorizontalAlignment', 'Left');
0284 
0285     %uicontrol(handles.edit_UserName);
0286     %set(handles.figure1,'CurrentObject',handles.java_UserName)
0287     handles.java_UserName.requestFocus;     % Get focus
0288     drawnow;
0289 else
0290     handles.java_Password.requestFocus;     % Get focus
0291     drawnow;
0292 end;
0293 
0294 %% Setup Callbacks for Objects
0295 % Adds the callback functions for the objects in the GUI
0296 
0297 set(handles.pushbutton_OK,     'Callback', {@pushbutton_OK_Callback, handles, ProgramOptions}, 'KeyPressFcn', {@pushbutton_KeyPressFcn, handles, ProgramOptions});
0298 set(handles.pushbutton_Cancel, 'Callback', {@pushbutton_Cancel_Callback, handles, ProgramOptions}, 'KeyPressFcn', {@pushbutton_KeyPressFcn, handles, ProgramOptions});
0299 set(handles.java_Password, 'ActionPerformedCallback', {@pushbutton_OK_Callback, handles, ProgramOptions});
0300 
0301 if ProgramOptions.ValidatePassword
0302     if ProgramOptions.enterUserName
0303         ObjectNext = handles.java_UserName;
0304     else
0305         ObjectNext = handles.java_Password;
0306     end;
0307     set(handles.java_PasswordValidate, 'ActionPerformedCallback', {@pushbutton_OK_Callback, handles, ProgramOptions}, 'NextFocusableComponent', ObjectNext);
0308     set(handles.java_Password, 'NextFocusableComponent', handles.java_PasswordValidate);
0309 elseif ProgramOptions.enterUserName
0310     set(handles.java_Password, 'NextFocusableComponent', handles.java_UserName);
0311 end;
0312 
0313 if ProgramOptions.enterUserName
0314     set(handles.java_UserName, 'ActionPerformedCallback', {@pushbutton_OK_Callback, handles, ProgramOptions}, 'NextFocusableComponent', handles.java_Password);
0315 end;
0316 
0317 setappdata(handles.figure1, 'isCanceled', false);
0318 
0319 %% Wait for Completion
0320 
0321 % Wait for the user to complete entry.
0322 drawnow;
0323 uiwait(handles.figure1);
0324 
0325 %% Get current information
0326 % User either closed the window or pressed cancel or OK.
0327 
0328 isCanceled = ~ishandle(handles.figure1) || getappdata(handles.figure1, 'isCanceled');
0329 if isCanceled
0330     if ishandle(handles.figure1)
0331         delete(handles.figure1);
0332     end;
0333     Password = -1;
0334     UserName = '';
0335     return;
0336 end;
0337 
0338 Password = handles.java_Password.Password';
0339 if ProgramOptions.enterUserName
0340     UserName = strtrim(get(handles.java_UserName, 'Text'));
0341 else
0342     UserName = '';
0343 end;
0344 delete(handles.figure1);
0345 
0346 %% DEFINE FUNCTIONS
0347 % The subfunctions required by this program are in the following section.
0348 
0349 function pushbutton_KeyPressFcn(hObject, eventdata, handles, ProgramOptions)
0350 
0351 switch eventdata.Key
0352     case 'return'
0353         Callback = get(hObject, 'Callback');
0354         feval(Callback{1}, hObject, '', Callback{2:end});
0355 end;
0356 
0357 function pushbutton_OK_Callback(hObject, eventdata, handles, ProgramOptions)
0358 if ProgramOptions.enterUserName
0359     % Check if username is blank
0360     UserName = strtrim(get(handles.java_UserName, 'Text'));
0361     if isempty(UserName)
0362         strMessage = 'Username is blank';
0363         %disp(strMessage)
0364         hError = errordlg(strMessage, 'passwordEntryDialog');
0365         uiwait(hError);
0366         return;
0367     end;
0368 end;
0369 
0370 if ProgramOptions.CheckPasswordLength
0371     %Password = handles.edit_Password.Password';
0372     Password = handles.java_Password.Password';
0373     if length(Password) < ProgramOptions.PasswordLengthMin || length(Password) > ProgramOptions.PasswordLengthMax
0374         strMessage = sprintf('Password must be between %d and %d characters', ...
0375             ProgramOptions.PasswordLengthMin, ...
0376             ProgramOptions.PasswordLengthMax);
0377         %disp(strMessage);
0378         hError = errordlg(strMessage, 'passwordEntryDialog');
0379         uiwait(hError);
0380         if ProgramOptions.ValidatePassword
0381             set(handles.java_PasswordValidate,'Text', '');
0382         end;
0383         handles.java_Password.requestFocus
0384         return;
0385     end;
0386 end;
0387 
0388 if ProgramOptions.ValidatePassword
0389     % Check if passwords match
0390     if ~isequal(handles.java_Password.Password, handles.java_PasswordValidate.Password)
0391         strMessage = 'Passwords do not match.  Please try again';
0392         %disp(strMessage);
0393         hError=errordlg(strMessage, 'passwordEntryDialog','modal');
0394         uiwait(hError);
0395         set(handles.java_Password,'Text', '');
0396         set(handles.java_PasswordValidate,'Text', '');
0397 
0398         handles.java_Password.requestFocus
0399         return;
0400     end;
0401 end;
0402 uiresume(handles.figure1);
0403 
0404 function pushbutton_Cancel_Callback(hObject, eventdata, handles, ProgramOptions)
0405 setappdata(handles.figure1, 'isCanceled', true);
0406 uiresume(handles.figure1);

Generated on Tue 06-Jul-2021 16:01:18 by m2html © 2005