classdef TSPanel < toolpack.component.component
  % Toolstrip panel.
  
  % Author(s): Bora Eryilmaz
  % Revised:
  % Copyright 2010-2011 The MathWorks, Inc.
  
  % ----------------------------------------------------------------------------
  properties (Access = private)
  end
  
  % ----------------------------------------------------------------------------
  % Public methods
  methods
    function this = TSPanel( columnSpecs, rowSpecs, varargin )
      % Creates a toolstrip panel.
      %
      % Example:
      %  btn1 = toolpack.component.TSButton('New', toolpack.component.Icon.NEW);
      %  btn2 = toolpack.component.TSButton('Save', toolpack.component.Icon.SAVE);
      %
      %  pnl = toolpack.component.TSPanel('p:grow,2dlu,p:grow', '2dlu,fill:p:grow,2dlu');
      %  pnl.add( btn1, 'xy(1,2)' );
      %  pnl.add( btn2, 'xy(3,2)' );
      %
      %  f = javaObjectEDT('javax.swing.JFrame', 'Test Frame');
      %  f.setContentPane(pnl.Peer); f.setVisible(true); f.pack
      
      this = this@toolpack.component.component;
      cls = 'com.mathworks.toolstrip.components.TSPanel';
      % cls = 'com.jgoodies.forms.debug.FormDebugPanel';
      layout = com.jgoodies.forms.layout.FormLayout( columnSpecs, rowSpecs );
      this.Peer = javaObjectEDT(cls, layout);
    end
    
    function add(this, component, constraints)
      clc = com.jgoodies.forms.layout.CellConstraints;
      clc.insets.bottom = 1;
      c = eval(['clc' '.' constraints]);
      this.Peer.add( component.Peer, c );
      add@toolpack.component.component(this, component);
    end
    
    function remove(this, component)
      this.Peer.remove(component.Peer)
      remove@toolpack.component.component(this, component)
      % Why need the following?
      this.Peer.validate;
      this.Peer.repaint;
    end
    
    function removeAll(this)
      this.Peer.removeAll;
      removeAll@toolpack.component.component(this)
      % Why need the following?
      this.Peer.validate;
      this.Peer.repaint;
    end
    
    function setBorder(this, varargin)
      % Adds a border around the panel
      %
      % obj.setBorder("empty", 5, 5, 0, 5)
      % obj.setBorder("etched")
      % obj.setBorder("titled", "My Panel")
      import javax.swing.*;
      type = validatestring(varargin{1}, ["empty", "etched", "titled"]);
      switch type
        case "empty"
          t = varargin{2}; l = varargin{3}; b = varargin{4};  r = varargin{5};
          border = BorderFactory.createEmptyBorder(t,l,b,r);
        case "etched"
          border = BorderFactory.createEtchedBorder();
        case "titled"
          title = varargin{2};
          border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder, title);
      end
      this.Peer.setBorder(border);
    end
  end
end
