function chartable(h)

import javax.swing.*;

%% Method which builds/populates the char table on the node panel

if isempty(h.Handles) || isempty(h.Handles.PNLAnnoation) || ...
        ~ishandle(h.Handles.PNLAnnoation)
    return % No panel
end

%% Assemble the axes table data by traversing each characteristic
tableData = {false, 'Mean', '', '';...
             false, 'Mean +/- 1 std', '', '';...
             false, 'Median', '', '';...
             false, 'Median +/- 1 iqr', '', ''};

%% Populate the table - if necessary creating it
if ~isfield(h.Handles,'annoationTable') || ...
        isempty(h.Handles.annoationTable)
    headings = {'Show (y/n?)','Annotations','Start Time','End Time'};
    [h.Handles.annoationTable, h.Handles.PNLannotationTable] = ... 
        uitable(ancestor(h.Handles.PNLAnnoation,'figure'),tableData,headings);
    set(h.Handles.PNLannotationTable,'Parent',h.Handles.PNLAnnoation)
    % Turn off the default uitable context menus
    mouseListeners = h.Handles.annoationTable.getTable.getMouseListeners;
    for k=1:length(mouseListeners)
        if isa(mouseListeners(k), 'com.mathworks.widgets.spreadsheet.SpreadsheetTable$TableMouseEventListener')
            h.Handles.annoationTable.getTable.removeMouseListener(mouseListeners(k));
        end
    end
    % Check box editor for 1st col
    h.Handles.annoationTable.setCheckBoxEditor(1);
    % DataChange listener
    set(h.Handles.annoationTable,'DataChangedCallback',{@localDataChange ...
        h.Handles.annoationTable h})
else
    tstableSetData(h.Handles.annoationTable,tableData)
end

%% De-activate row header
set(h.Handles.annoationTable.TableScrollPane.getRowHeader,'Visible','off');
%% Pack the table
awtinvoke(h.Handles.annoationTable.getTable,'setAutoResizeMode',true)


function localDataChange(eventSrc, eventData, table, h)

%% View must exist
if isempty(h.Plot) || ~ishandle(h.Plot)
    return
end

%% Find the data which was changed
selRow = eventData.getEvent.getFirstRow+1;
col = eventData.getEvent.getColumn+1;

%% Get current table data
tableData = cell(table.Data);
       
switch col
    case 1 % Visibility change
        %% Set the visibility of the selected chars      

        %% Find the char
        charlist = localGetChars(h, selRow);

        %% Checked = visible
        if tableData{selRow,1}
            set(charlist,'Visible','on');
            set(h.Plot.AxesGrid.findMenu(charlist(1).Identifier),'Checked','on')
        else
            set(charlist,'Visible','off');
            set(h.Plot.AxesGrid.findMenu(charlist(1).Identifier),'Checked','off')
        end

    case 3 % Start time change
        %% Find the char
        charlist = localGetChars(h,selRow);
            
        %% Set the start time of each char in the list
        starttime = str2num(tableData{selRow,3});      
        if ~isempty(starttime) % Valid start time, reset the chars
            for k=1:length(charlist)  
                charlist(k).Data.Starttime = starttime;
            end
        else % Invlid start time - revert table to the val of the char
            tableData{startRow,3} = charlist(1).Data.Starttime;
        end      
    case 4 % End time change
         %% Find the char
        charlist = localGetChars(h,selRow);
            
        %% Set the end time of each char in the list
        endtime = str2num(tableData{selRow,4});      
        if ~isempty(endtime) % Valid start time, reset the chars
            for k=1:length(charlist)  
                charlist(k).Data.Endtime = endtime;
            end
        else % Invlid start time - revert table to the val of the char
            tableData{selRow,4} = charlist(1).Data.Endtime;
        end                   
end

h.Plot.AxesGrid.send('DataChanged');
tstableSetData(table,tableData)
%drawnow

function charlist = localGetChars(h,startRow)

%% Creates a list of all the characteristics of the type defined in the
%% table row "startRow" for all the attached waves

%% Define string constants 
charList = {'Mean','Control','????','????'};
dataList = {'tsguis.tsMeanData','tsguis.tsMeanBandData','',''};
viewList = {'tsguis.tsMeanView','tsguis.tsMeanBandView','',''};

%% Find the selected chars. If necesssary creating them 
charlist = [];
if ~isempty(h.Plot)
    for k=1:length(h.Plot.Waves)
        % Find the position of the char in the char array for this wave 
        ind = find(strcmp(charList{startRow},...
            get(h.Plot.Waves(k).Characteristics,{'Identifier'})));
        if ~isempty(ind) % Add the found char to the list
            charlist = [charlist; h.Plot.Waves(k).Characteristics(ind)];
        else % Create a new char and add it to the list
            h.Plot.Waves(k).addchar(charList{startRow},dataList{startRow}, ...
                viewList{startRow}); 
            charlist = [charlist; h.Plot.Waves(k).Characteristics(end)]; 
        end
    end
end            