classdef TSRadioButton < toolpack.component.component
  % Toolstrip radio button.
  %
  % See also toolpack.component.TSCheckBox, toolpack.component.TSToggleButton,
  % toolpack.component.ButtonGroup.
  
  % Author(s): Bora Eryilmaz
  % Revised:
  % Copyright 2010-2011 The MathWorks, Inc.
  
  % ----------------------------------------------------------------------------
  properties (Dependent, Access = public)
    % Component's text (string, default = '')
    Text
  end
  
  properties (Dependent, GetAccess = public, SetAccess = private)
    % Button group (ButtonGroup, default = ButtonGroup.empty)
    Group
  end
  
  properties (Dependent, Access = public, SetObservable, AbortSet)
    % State of the component (logical, default = false)
    Selected
  end
  
  properties (Access = private)
    % Logical
    Selected_
    % ButtonGroup or [] for default value ButtonGroup.empty.
    Group_
  end
  
  % ----------------------------------------------------------------------------
  events
    % Event sent upon selection changes.
    ItemStateChanged
  end
  
  % ----------------------------------------------------------------------------
  methods
    function this = TSRadioButton(text, selected)
      % Creates a toolstrip button.
      %
      % Example:
      %   text = 'Apply';
      %   selected = true;
      %
      %   btn = toolpack.component.TSRadioButton
      %   btn = toolpack.component.TSRadioButton(text)
      %   btn = toolpack.component.TSRadioButton(text, selected)
      
      this = this@toolpack.component.component;
      cls = 'com.mathworks.toolstrip.components.TSRadioButton';
      if nargin == 0
        % TSRadioButton();
        this.Peer = javaObjectEDT(cls);
      elseif nargin == 1
        % TSRadioButton(text)
        this.Peer = javaObjectEDT(cls, text);
      else
        % TSRadioButton(text, selected)
        this.Peer = javaObjectEDT(cls, text, selected);
      end
      this.Selected = this.Peer.isSelected;
      addItemListener( this, {@LocalItemStateChanged, 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.Selected(this)
      % GET function for Selected property.
      value = this.Selected_;
    end
    
    function set.Selected(this, value)
      % SET function for Selected property.
      if ~islogical(value)
        ctrlMsgUtils.error('Controllib:toolpack:InvalidPropertyValue', 'Selected')
      end
      this.Selected_ = value;
      this.Peer.setSelected(value);
    end
    
    function value = get.Group(this)
      % GET function for Group property.
      value = this.Group_;
      if isempty(value)
        value = toolpack.component.ButtonGroup.empty; % default
      end
    end
    
    function set.Group(this, value)
      % SET function for Group property.
      if isempty(value)
        value = [];
      elseif ~isa(value, 'toolpack.component.ButtonGroup')
        ctrlMsgUtils.error('Controllib:toolpack:InvalidPropertyValue', 'Group')
      end
      this.Group_ = value;
    end
    
    function delete(this)
      % Deletes the component
      if ~isempty(this.Group)
        % Remove this radio button from the button group.
        this.Group.remove(this)
      end
    end
  end
  
  methods (Hidden)
    function utSetGroup(this, group)
      this.Group = group;
    end
  end
  
  methods (Access = private)
    function addItemListener(this, fcn)
      % Do not modify. Use as is to prevent Java memory leaks.
      L = addlistener(this.Peer, 'ItemStateChanged', fcn);
      this.Listeners = [this.Listeners; L];
    end
  end
end

% ------------------------------------------------------------------------------
function LocalItemStateChanged(~,~,obj)
obj.Selected = obj.Peer.isSelected;
obj.notify('ItemStateChanged')
end
