classdef TSLabel < toolpack.component.component
  % Toolstrip label.
  %
  % Example:
  %   text = 'File';
  %   icon = toolpack.component.Icon.OPEN
  %
  %   lbl = toolpack.component.TSLabel;
  %   lbl = toolpack.component.TSLabel(icon);
  %   lbl = toolpack.component.TSLabel(text);
  %   lbl = toolpack.component.TSLabel(text, icon);
  %
  % See also toolpack.component.TSButton,

  % 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 = private)
    % Icon or [] for default Icon.empty
    Icon_
  end
  
  % ----------------------------------------------------------------------------
  events
    % Event sent upon mouse clicks.
    MouseClicked
  end
  
  % ----------------------------------------------------------------------------
  methods
    function this = TSLabel(text, icon)
      % Creates a toolstrip label.
      this = this@toolpack.component.component;
      cls = 'com.mathworks.toolstrip.components.TSLabel';
      if nargin == 0
        % TSLabel();
        this.Peer = javaObjectEDT(cls);
      elseif nargin == 1
        text = controllib.internal.util.hString2Char(text);
        if ischar(text)
          % TSLabel(text)
          this.Peer = javaObjectEDT(cls, text);
        else
          % TSLabel(icon)
          icon = text;
          this.Icon = icon;
          this.Peer = javaObjectEDT(cls, icon.Peer);
        end
      else
        % TSLabel(text, icon)
        this.Icon = icon;
        this.Peer = javaObjectEDT(cls, text, icon.Peer, ...
          javax.swing.JLabel.LEADING);
      end
      addMouseClickedListener(this, {@LocalMouseClicked, 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
  end
  
  methods (Access = private)
    function addMouseClickedListener(this, fcn)
      % Do not modify. Use as is to prevent Java memory leaks.
      L = addlistener(this.Peer, 'MouseClicked', fcn);
      this.Listeners = [this.Listeners; L];
    end
  end
end

% ------------------------------------------------------------------------------
function LocalMouseClicked(~,~,obj)
obj.notify('MouseClicked')
end
