function configureFolderPanel(this, manager)
% CONFIGUREFOLDERPANEL 

% Author(s): Bora Eryilmaz
% Revised:
% Copyright 1986-2004 The MathWorks, Inc.
% $Revision: 1.1.6.3 $ $Date: 2004/11/18 23:44:13 $

% Get panel handle
Panel = this.Panel;

% Add listeners
L = [ handle.listener( handle(Panel), 'AncestorAdded', ...
                       @(x,y) LocalUpdatePanel(x) ); ...
      handle.listener( this.Node, 'ObjectChildAdded', ...
                       @(x,y) LocalListChanged(x,y) ); ...
      handle.listener( this.Node, 'ObjectChildRemoved', ...
                       @(x,y) LocalListChanged(x,y) ) ];
set(L, 'CallbackTarget', this);
this.Listeners = [ this.Listeners; L(:)];

% Get the handles
Handles = this.Handles;
buttons   = Panel.getButtons;
menuitems = Panel.getMenuItems;

% New button callback
h = handle( buttons(1), 'callbackproperties' );
h.ActionPerformedCallback = @(x,y) LocalNewButton(this,manager);
h = handle( menuitems(1), 'callbackproperties' );
h.ActionPerformedCallback = @(x,y) LocalNewButton(this,manager);
h = handle( menuitems(4), 'callbackproperties' ); % Scroll right-click new
h.ActionPerformedCallback = @(x,y) LocalNewButton(this,manager);

% Delete button callback
h = handle( buttons(2), 'callbackproperties' );
h.ActionPerformedCallback = @(x,y) LocalDeleteButton(this);
h = handle( menuitems(2), 'callbackproperties' );
h.ActionPerformedCallback = @(x,y) LocalDeleteButton(this);

% Edit button callback
h = handle( buttons(3), 'callbackproperties' );
h.ActionPerformedCallback = @(x,y) LocalEditButton(this,manager);
h = handle( menuitems(3), 'callbackproperties' );
h.ActionPerformedCallback = @(x,y) LocalEditButton(this,manager);

% Description field callback
h = handle( Panel.getDescriptionArea, 'callbackproperties' );
h.FocusLostCallback = @(x,y) LocalDescriptionUpdate(y,this);

% Table model callback
Handles.TableModel = Panel.getFolderTable.getModel;
h = handle( Handles.TableModel, 'callbackproperties' );
h.TableChangedCallback = @(x,y) LocalTableChanged(y,this);
Handles.TableModel = h;

% Store the handles
this.Handles = Handles;

% ---------------------------------------------------------------------------- %
% Initialize & update components when the panel is shown
function LocalUpdatePanel(this)
children = LocalGetAllowedChildren(this);
LocalTableUpdate(this, children);
model = this.Handles.TableModel;
model.tableRowsUpdated( 0, java.lang.Integer.MAX_VALUE );

this.Panel.getDescriptionArea.setText( this.Node.Description );

% ----------------------------------------------------------------------------- %
function LocalNewButton(this, manager)
node = this.Node.addNode;

% Expand the tree nodes so the user sees the new node
manager.Explorer.expandNode( this.Node.getTreeNodeInterface );
str = sprintf('- %s node has been added to %s.', node.Label, this.Node.Label );
manager.Explorer.postText( str );

% ---------------------------------------------------------------------------- %
function LocalDeleteButton(this)
rows     = this.Panel.getFolderTable.getSelectedRows + 1;
children = LocalGetAllowedChildren(this);

for ct = length(rows):-1:1
  this.Node.removeNode( children( rows(ct) ) );
end

% ---------------------------------------------------------------------------- %
function LocalEditButton(this, manager)
row      = this.Panel.getFolderTable.getSelectedRow + 1;
children = LocalGetAllowedChildren(this);

Leaf = children(row).getTreeNodeInterface;
manager.Explorer.setSelected( Leaf );

% ----------------------------------------------------------------------------- %
function LocalDescriptionUpdate(hData, this)
this.Node.Description = char( hData.getSource.getText );

% ----------------------------------------------------------------------------- %
% Handle JAVA -> UDD changes
function LocalTableChanged(hData, this)
row = hData.getFirstRow;
col = hData.getColumn;

% React only to fireTableCellUpdated(row, col);
if (col < 0) 
  return
end

children = LocalGetAllowedChildren(this);
model    = hData.getSource;

switch col
case 0
  children(row+1).Label = model.getValueAt(row, col);
  
  % Update the table row in case Label change hasn't been accepted.
  if ~strcmp(model.getValueAt(row,col), children(row+1).Label)
    LocalTableUpdate(this, children);
    model.tableRowsUpdated( row, row );
  end
case 1
  children(row+1).Description = model.getValueAt(row, col);
end

% ---------------------------------------------------------------------------- %
% Handle UDD -> JAVA changes
function LocalListChanged(this, hEvent)
child    = hEvent.Child;
children = LocalGetAllowedChildren(this);

if any( strcmp( class(child), this.ExcludeList ) )
  return
end

idx = find( children == child );

if strcmp( hEvent.Type, 'ObjectChildRemoved' )
  children(idx) = [];
  type = javax.swing.event.TableModelEvent.DELETE;
else
  type = javax.swing.event.TableModelEvent.INSERT;
end

LocalTableUpdate(this, children);
model = this.Handles.TableModel;
model.tableChanged( idx-1, idx-1, ...
                    javax.swing.event.TableModelEvent.ALL_COLUMNS, type );

% ----------------------------------------------------------------------------- %
% Updata JAVA table model data
function LocalTableUpdate(this, children)
model = this.Handles.TableModel;

if ~isempty( children )
  table = javaArray('java.lang.Object', length(children), 2);
  
  for ct = 1:length(children)
    current = children(ct);
    table(ct,1) = java.lang.String( current.Label );
    table(ct,2) = java.lang.String( current.Description );
  end
else
  table = {};
end
model.setData( table );

% ----------------------------------------------------------------------------- %
% Get all children that are not excluded
function children = LocalGetAllowedChildren(this)
children = this.Node.getChildren;

% Remove excluded children from bottom
for ct = length(children):-1:1
  cls = class( children(ct) );
  
  if any( strcmp( cls, this.ExcludeList ) )
    children(ct) = [];
  end
end
