www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/cghistorymanager.m

    function cghistorymanager(Action,varargin)
%CGHISTORYMANAGER
%
% GUI to manage Cal files for the current session.
%
% CGHISTORYMANAGER(action)

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.

if nargin==0
    Action = 'create';
end
switch lower(Action)
    case 'precreate'
        i_CreateFigure;
    case 'create'
        fh = i_gethandle;
        if isempty(fh)
            i_CreateFigure;
            fh = i_gethandle;
        end
        if nargin>1
            i_Refresh(varargin{:});
        end
        fh.showDialog();
        i_Finish;
    case 'itemselect'
        i_PlotTable;
    case 'finish'
        i_Finish;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%             CREATEFIGURE            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_CreateFigure
c = cgbrowser;

fh= xregdialog('closerequestfcn',[mfilename,'(''finish'')'],...
    'Tag','HistoryManager',...
    'name','History',...
    'pointer','watch');
fhWidth = 700; 
fhHeight = 500;
xregcenterfigure(fh,[fhWidth,fhHeight],c.Figure)
c.registersubfigure(fh,'none');
ud.FigureHandle = fh;

str = {'Reset','Add...','Remove','Edit...','Clear','Close'};
callbacks = {@i_Reset,@i_Add,@i_Remove,@i_Edit, @iClear,'set(gcbf, ''visible'', ''off'');'};

for i = 1:length(str)
    ud.Hand.But(i) = uicontrol('Style','push',...
        'Parent',fh,...
        'Callback',callbacks{i},...
        'String',str{i},...
        'Visible','off');
end
ud.Hand.But(end+1)=cghelpbutton(fh,'CGHISTORYVIEWER');
ud.Hand.ResetNormalizer = uicontrol('Style','checkbox',...
    'Parent',fh,...
    'String','Reset normalizer',...
    'Visible','off',...
    'Tag','ResetNormalizerCheckbox');

lh = mbcwidgets.List( 'Parent', fh,...
    'Editable', false, ...
    'Visible','off',...
    'SelectionChangedCallback',@i_ClickedCallback,...
    'SelectionMode', 'MultiRow',...
    'Tag', 'VersionTable');

cols = {'Version','Comment / Action','Date and Time'};
widths = [50 350 120];
lh.Peer.setColumnData(cols);
lh.Peer.setColumnWidths(widths);
    

ud.List = lh;
btnLyt = xreggridbaglayout(fh,...
    'dimension',[1,3],...
    'elements',{[],ud.Hand.But(6),ud.Hand.But(7)},...
    'gapy',10,...
    'colsizes',[-1 80 80]);

% create table.
ud.TableWrapper = mbcwidgets.Table2D(com.mathworks.toolbox.mbc.gui.peer.DifferenceTablePeer, ...
    'parent', fh, ...
    'visible', 'off');
ud.layout = xreggridbaglayout(fh,'packstatus','off',...
    'elements',{lh,[],[],[],[],[],ud.TableWrapper,btnLyt,...
    [],[],[],[],[],ud.Hand.ResetNormalizer,[],[],...
    ud.Hand.But(1),ud.Hand.But(2),ud.Hand.But(3),ud.Hand.But(4),ud.Hand.But(5),[],[],[]},...
    'dimension',[8,3],...
    'colsizes',[-1 20 80],...
    'gapx',7,'gapy',10,...
    'rowsizes',[25,25,25,25,25,25,-1,25],...
    'mergeblock',{[1 6],[1 1]},...
    'mergeblock',{[6 6],[2 3]},...
    'mergeblock',{[7 7],[1 3]},...
    'mergeblock',{[8 8],[1 3]},...
    'border',[10 10 10 10]);
fh.LayoutManager = ud.layout;
set(ud.layout, 'packstatus','on','visible','on');
set(fh,'UserData',ud,'Pointer','arrow');


% --------------------------------------------------------
function h = i_gethandle
% --------------------------------------------------------
h = findall(0,'tag','HistoryManager');
h = mbcgui.hgclassesutil.toHandle(h);

% --------------------------------------------------------
function ud = i_getdata
% --------------------------------------------------------
ud = get(i_gethandle,'UserData');


% --------------------------------------------------------
function i_setdata(ud)
% --------------------------------------------------------
set(i_gethandle,'UserData',ud);

% --------------------------------------------------------
function i_Finish
% --------------------------------------------------------
c = cgbrowser;
c.doDrawTree([],'update');
ShowNode(c);
ViewNode(c);

% --------------------------------------------------------
function i_ClickedCallback(~,~, varargin)
% --------------------------------------------------------
persistent doing_cb
if isempty(doing_cb) || (now - doing_cb ) > 5e-5
    % Prevent re-entering code whilst already in callback
    %  - this can cause real problems.
    doing_cb = now;
    cghistorymanager('itemselect');
    doing_cb = [];
end

% --------------------------------------------------------
function i_Reset(~,~)
% --------------------------------------------------------
ud = i_getdata;
index = ud.List.getSelectedRows;
if ~isempty(index)
    N = ud.List.getRowCount;
    MemIndex = (N+1)-index;
    resetNorm = get(ud.Hand.ResetNormalizer,'Value'); 
    [ud.Obj.info, ok] = history_reset(ud.Obj.info, MemIndex,resetNorm);
    if ok
        i_Refresh(ud.Obj);
    else
        % Warn user that the operation was not allowed
        h = errordlg(['Unable to reset to this point in the history.  ' ...
            'This may be because the size of the table has been locked']);
        waitfor(h);
    end
end


% --------------------------------------------------------
function i_Remove(~,~)
% --------------------------------------------------------
ud = i_getdata;
try
    index = ud.List.getSelectedRows;
    if ~isempty(index)
        N = ud.List.getRowCount;
        MemIndex = (N+1)-index;
        mem = ud.Obj.get('memory');
        mem(MemIndex) = [];
        ud.Obj.info = ud.Obj.set('memory',mem);
        i_Refresh(ud.Obj,index(1));
    end
end

% --------------------------------------------------------
function i_Add(~,~)
% --------------------------------------------------------
ud = i_getdata;
V = ud.Obj.get('values');
ud.Obj.info = ud.Obj.set('values',{V,''});
i_Refresh(ud.Obj);
OK = i_Edit;
if ~OK
    % Cancel
    mem = ud.Obj.get('memory');
    mem(end) = [];
    ud.Obj.info = ud.Obj.set('memory',mem);
    i_Refresh(ud.Obj)
end

% --------------------------------------------------------
function varargout = i_Edit(~,~)
% --------------------------------------------------------
OK = 1;
ud = i_getdata;
index = ud.List.getSelectedRows;
N = ud.List.getRowCount;

MemIndex = (N+1)-index;
mem = ud.Obj.get('memory');
defstr=mem(MemIndex).Information;
if isempty(defstr)
    defstr='';   % make sure empties go to a string
end
answer=inputdlg({'Comment / Action:'},['Edit History: Version ',num2str(MemIndex)],1,{defstr});
if isempty(answer)
    % cancel
    OK = 0;
elseif ~isequal(mem(MemIndex).Information,answer{1})
    mem(MemIndex).Information = answer{1};
    ud.Obj.info = ud.Obj.set('memory',mem);
    i_Refresh(ud.Obj,index);
end
if nargout == 1
    varargout{1} = OK;
end

function iClear(~,~)
% clear all history
ud = i_getdata;

ud.Obj.info = clearHistory(info(ud.Obj));
i_Refresh(ud.Obj);

% --------------------------------------------------------
function i_PlotTable
% --------------------------------------------------------
try
    ud = i_getdata;
    index = ud.List.getSelectedRows;
    % Enable the buttons - reset, add, remove, edit
    if ~isscalar(index)
        set(ud.Hand.But([1 4]),'Enable','off');
    else
        set(ud.Hand.But(4),'Enable','on');
        if index == 1
            set(ud.Hand.But([1 3]),'Enable','off');
        else
            set(ud.Hand.But([1 3]),'Enable','on');
        end
    end
    N = ud.List.getRowCount;
    index = (N+1)-index; % Reverse order of list box
    if isempty(index) || length(index) > 2
        % Clear the table
        ud.TableWrapper.Peer.clearTable;
    else
        if length(index)==2
            ud.TableWrapper.Peer.setShowDifferences(true);
        else
            ud.TableWrapper.Peer.setShowDifferences(false);
        end
        mtableview(ud.Obj.info,ud.TableWrapper,index);
    end
    i_setdata(ud);
catch ME
    disp('Error');
    disp(getReport(ME));
end

% --------------------------------------------------------
function i_Refresh(ptr,SelInd)
% --------------------------------------------------------
ud = i_getdata;
set(ud.FigureHandle,'Name',['History for ',ptr.getname]);

mem = ptr.get('memory');
L = length(mem);
if (nargin == 1) || (SelInd > L) || (SelInd <= 0)
    SelInd = 1;
end
Data = cell(L,3);
for i = 1:L
    index = (L+1-i);
    Data{i,1} = index;
    information = mem(index).Information;
    if ~ischar(information)
        information = '';
    end
    Data{i,2} = information;
    Data{i,3} = mem(index).Date;
end
ud.List.Peer.setData(Data);
ud.List.selectRows(SelInd);

switch ptr.class
    case 'cgnormaliser'
        set(ud.Hand.ResetNormalizer,'Enable','off','Value',false)
    case 'cglookupone'
        set(ud.Hand.ResetNormalizer,'Enable','off','Value',true)
    otherwise
        set(ud.Hand.ResetNormalizer,'Enable','on')
end


ud.Obj = ptr;
i_setdata(ud);
i_PlotTable;