function h = calendar

import com.mathworks.toolbox.timeseries.*;
import javax.swing.*;

h = tsguis.calendar;

%% Build HG components
d=30;
h.Figure = figure('Visible','on','Units','Characters','CloseRequestFcn', ...
    {@localClose h},'Position',[20.6 23.7692 48.0000   19.4615],...
    'Toolbar','none','Menubar','none','NumberTitle','off','HandleVisibility',...
    'callback','resizeFcn',{@localFigResize h});

%% Create table
tpos = hgconvertunits(h.Figure,[4 1.5385 40 15.3846],'Characters','Pixels',h.Figure);
table_h = tsUitablePeer(javax.swing.table.DefaultTableModel);
[h.Table,h.Handles.tablPnl] = javacomponent(table_h,tpos,h.Figure);
set(h.Handles.tablPnl,'Units','Characters')%,'Position',[4 6.5385 35.8 7.8462])
drawnow 
awtinvoke(h.Table.getTable,'setAutoResizeMode(I)',JTable.AUTO_RESIZE_ALL_COLUMNS);
awtinvoke(h.Table.getTable,'setSelectionMode(I)',ListSelectionModel.SINGLE_SELECTION);
awtinvoke(h.Table.getTableScrollPane.getColumnHeader,'setVisible(Z)',false);
awtinvoke(h.Table.getTableScrollPane.getRowHeader,'setVisible(Z)',false);
awtinvoke(h.Table.getTable,'setGridColor(Ljava/awt/Color;)',java.awt.Color.white);
drawnow
h.Table.Editable = false;

months = {'January','February','March','April','May','June','July', ...
    'August','September','October','November','December'};
h.Handles.monthCombo = uicontrol('Parent',h.Figure,'Units','Characters',...
    'Style','popupmenu','Callback', {@localDateChange h}, ...
    'String', months,'BackgroundColor',[1 1 1]);
h.Handles.yearCombo = uicontrol('Style','edit','Parent',h.Figure,...
    'Units','Characters',...
    'Callback',{@localDateChange h},...
    'Horizontalalignment','Left','Backgroundcolor',[1 1 1],'String','2004');
h.Handles.okBtn = uicontrol('Parent', h.Figure, 'Units','Characters',...
    'Style', 'pushbutton','Callback', {@localOK h}, 'String','OK');
h.Handles.cancelBtn = uicontrol('Parent',h.Figure,'Units','Characters',...
    'Style', 'pushbutton','Callback', @(es,ed) set(h,'Visible','off'), ...
    'String','Cancel');
h.Handles.applyBtn = uicontrol('Parent',h.Figure,'Units','Characters',...
    'Style', 'pushbutton','Callback', {@localApply h}, 'String','Apply');
LBLtime = uicontrol('Style','Text','Parent',h.Figure,'String','Time:',...
    'Units','Characters',...
    'HorizontalAlignment','Left','Position',[4 3.4615 10 1.5385]);
set(h.Figure,'Color',get(LBLtime,'Backgroundcolor'))
h.Handles.EDITTime = uicontrol('Style','Edit','Parent',h.Figure,'String','',...
    'Units','Characters',...
    'HorizontalAlignment','Left',...
    'Callback',{@localDateChange h});

%% Customize the calendar table
calendarRenderer = tsCalendar;
awtinvoke(h.Table.getTable,...
    'setDefaultRenderer(Ljava/lang/Class;Ljavax/swing/table/TableCellRenderer;)',...
    getClass(java.lang.Object),calendarRenderer);

%% Install default listeners
h.Listeners = [handle.listener(h,h.findprop('Datenum'),'PropertyPostSet',...
       @(es,ed) open(h)); ...
              handle.listener(h,h.findprop('Visible'),'PropertyPostSet',...
       @(es,ed) set(get(h,'Figure'),'Visible',get(h,'Visible')))];
       
localFigResize(h.Figure,[],h)

function localDateChange(eventSrc, eventData, h)

h.update
 
function localApply(eventSrc, eventData, h)

if iscell(h.Updatefcn)
    feval(h.Updatefcn{:});
elseif ~isempty(h.Updatefcn)
    feval(h.Updatefcn);
end

function localOK(eventSrc, eventData, h)

h.update
if iscell(h.Updatefcn)
    feval(h.Updatefcn{:});
elseif ~isempty(h.Updatefcn)
    feval(h.Updatefcn);
end
h.Visible = 'off';

function localClose(eventSrc, eventData, h)

h.Visible = 'off';


function localFigResize(es,ed,h)

fpos = get(es,'Position');

set(h.Handles.okBtn,'Position',[fpos(3)-38.4 1.5385 10 1.5385]);
set(h.Handles.cancelBtn,'Position',[fpos(3)-26.4 1.5385 10 1.5385]);
set(h.Handles.applyBtn,'Position',[fpos(3)-14.4 1.5385 10 1.5385]);
set(h.Handles.EDITTime,'Position',[11 3.8462 fpos(3)-15.4 1.5385]);
set(h.Handles.tablPnl,'Position',[4 6.5385 fpos(3)-9 fpos(4)-10.3077]);
set(h.Handles.monthCombo,'Position',[4 fpos(4)-2.7739 18 1.5385])
set(h.Handles.yearCombo,'Position',[24 fpos(4)-2.7739 19 1.5385])
