classdef TSDropDownButton < toolpack.component.TSButtonBehavior
  % Toolstrip drop-down button.
  %
  % See also toolpack.component.TSButton, toolpack.component.TSSplitButton.
  
  % Author(s): Bora Eryilmaz
  % Revised:
  % Copyright 2010-2015 The MathWorks, Inc.
  
  % ----------------------------------------------------------------------------
  properties (Dependent, Access = public)
    % Component's text (string, default = '')
    Text
    % Component's icon (Icon, default = Icon.empty)
    Icon
  end
  
  properties (Access = public)
    % Component's popup panel
    Popup
  end
  
  properties (Access = private)
    % Icon or [] for default Icon.empty
    Icon_
  end
  
  % ----------------------------------------------------------------------------
  events
    % Event sent upon popup button press.
    DropDownActionPerformed
  end
  
  % ----------------------------------------------------------------------------
  methods
    function this = TSDropDownButton(text, icon)
      % Creates a toolstrip drop-down button.
      %
      % Example:
      %   text = 'Plot Settings';
      %   icon = toolpack.component.Icon.SETTINGS;
      %
      %   btn = toolpack.component.TSDropDownButton
      %   btn = toolpack.component.TSDropDownButton(icon)
      %   btn = toolpack.component.TSDropDownButton(text)
      %   btn = toolpack.component.TSDropDownButton(text, icon)
      
      this = this@toolpack.component.TSButtonBehavior;
      cls = 'com.mathworks.toolstrip.components.TSDropDownButton';
      if nargin == 0
        % TSDropDownButton();
        this.Peer = javaObjectEDT(cls);
      elseif nargin == 1
        text = controllib.internal.util.hString2Char(text);
        if ischar(text)
          % TSDropDownButton(text)
          this.Peer = javaObjectEDT(cls, text);
        else
          % TSDropDownButton(icon)
          icon = text;
          this.Icon = icon;
          this.Peer = javaObjectEDT(cls, icon.Peer);
        end
      else
        % TSDropDownButton(text, icon)
        this.Icon = icon;
        this.Peer = javaObjectEDT(cls, text, icon.Peer);
      end
      % mnemonics is off by default
      this.Peer.setAutoMnemonicEnabled(false);
      this.Peer.setPopupListener(javaObjectEDT('com.mathworks.toolstrip.components.DelayedPopupListener'));
      addDropDownActionPerformedListener( this, ...
        {@LocalDropDownActionPerformed, this} )
    end
    
    function value = get.Text(this)
      % GET function for Text property.
      value = char(this.Peer.getText);
    end
    
    function set.Text(this, value)
      % SET function for Text property.
      value = controllib.internal.util.hString2Char(value);
      if isempty(value)
        value = '';
      elseif ~ischar(value)
        ctrlMsgUtils.error('Controllib:toolpack:StringArgumentNeeded')
      end
      this.Peer.setText(value);
    end
    
    function value = get.Icon(this)
      % GET function for Icon property.
      value = this.Icon_;
      if isempty(value)
        value = toolpack.component.Icon.empty; % default
      end
    end
    
    function set.Icon(this, value)
      % SET function for Icon property.
      if isempty(value)
        value = [];
        this.Peer.setIcon(value)
      elseif isa(value, 'toolpack.component.Icon')
        this.Peer.setIcon(value.Peer)
      else
        ctrlMsgUtils.error('Controllib:toolpack:InvalidPropertyValue', 'Icon')
      end
      this.Icon_ = value;
    end
    
    function set.Popup(this, value)
      % SET function for Popup property
      if ~isempty(value) && ~isa(value, 'toolpack.component.TSDropDownPopup')
          % TSTearOffPopup dialogs are not managed by PopupListener callbacks
          this.Peer.setPopupListener([]);
      end
      this.Popup = value;
    end
  end
  
  methods (Access = private)
    function addDropDownActionPerformedListener(this, fcn)
      % Do not modify. Use as is to prevent Java memory leaks.
      L = addlistener(this.Peer, 'ActionPerformed', fcn);
      this.Listeners = [this.Listeners; L];
    end
  end
end

% ------------------------------------------------------------------------------
function LocalDropDownActionPerformed(~,~,obj)
% Notification for popup button press.
obj.notify('DropDownActionPerformed')

% Show drop-down popup
popup = obj.Popup;
if ~isempty(popup)
    if isa(popup, 'toolpack.component.TSDropDownPopup')
        lsnr = obj.Peer.getPopupListener();
        lsnr.show(popup.Peer);
    else
        % Need to show TSTearOffPopup dialogs explicitly
        popup.show(obj)
    end
else
    % Show empty popup to manage selected state of the button
    lsnr = obj.Peer.getPopupListener();
    if ~isempty(lsnr)
        lsnr.show();
    end
end
end
