function [cb, cb_api] = immagboxjava(sp_api)
%IMMAGBOXJAVA Create magnification combo box for scrollpanel.
%   This function is not documented and may be removed in a future release.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 1.1.8.3 $  $Date: 2004/12/18 07:36:37 $

%   [CB,CB_API] = IMMAGBOXJAVA(SP_API) creates the magnification combo box
%   CB that is wired to work with a scrollpanel having api SP_API. CB_API
%   allows other functions to alert CB that the magnification has changed.
  
  cb_api.setMag = @setMag;
  cb_api.setScrollpanel = @setScrollpanel;
  
  import com.mathworks.toolbox.images.MagnificationComboBox;
  cb = MagnificationComboBox;
  sp_api = [];
  
  if nargin == 0
    % if no scroll panel was specified we disable the mag combo box
    set(cb,'Enabled','off');
  else
    setScrollpanel(sp_api);  
  end

  %---------------------------
  function setScrollpanel(scrollpanel_api)
  % Hook so that the mag combo box can be attached to a scrollpanel after
  % creation.
    sp_api = scrollpanel_api;
    
    % have to call cbSetMag twice to force initialization of magnification
    % box.
    cbSetMag(sp_api.getMagnification()) % initialize mag
    cbSetMag(sp_api.getMagnification()) % initialize mag

    awtinvoke(cb,'setEnabled(Z)',true);
    set(cb,'ActionPerformedCallback',@updateMag);

    sp_api.addNewMagnificationCallback(@setMag);
    
  end
  
  %---------------------------
  function updateMag(varargin)

    index = cb.getSelectedIndex;
    
    if (index==0) % Fit-To-Window

        %workaround to geck 230808 (assertions from JIT)
        dummyVariable1 = sp_api;                                                 
        dummyVariable2 = sp_api.findFitMag; 
        %end of workaround
        
        newMag = sp_api.findFitMag();
        cbSetMag(newMag) 
        
    else
         % User either selected a percentage from the combo-box list,
         % zoomed some other way to set the magnification,
         % typed something, or just clicked so focus
         % went away from ComboBox.
         newMag = cb.findMagnification;

         validStringTyped = (newMag ~= cb.getMag);

         if (validStringTyped)
            cbSetMag(newMag)
         
         else
             cb.updateString;

         end
    end
    
    % Only call setMagnification if the magnification really changed.
    newMagRounded = round(newMag*100)/100;
    spMag = sp_api.getMagnification();
    spMagRounded = round(spMag*100)/100;
    isMagWithinRoundoffOfSP = newMagRounded==spMagRounded;
    
    if ~isMagWithinRoundoffOfSP
        sp_api.setMagnification(newMag);
    end
                
  end % updateMag
  
  %------------------------
  function cbSetMag(newMag)
    
    awtinvoke(cb,'setMag(D)',newMag);
  
  end
  
  %------------------------
  function cbUpdateString
  
    awtinvoke(cb,'updateString');
  
  end

  %----------------------
  function setMag(newMag)
  % Hook so this combo box can receive messages that it can act on in a thread
  % safe way.

    iptcheckinput(newMag,{'numeric'},...
                  {'real','scalar','nonempty','nonnan','finite',...
                   'positive','nonsparse'},'setMag','newMag',1)
    
    cbSetMag(newMag)
    
  end
    
end
