www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+widget/IconText.m

    classdef IconText < mbcgui.widget.BasicContainer
%ICONTEXT Component that displays an icon next to some text
%   mbcgui.widget.IconText is a component that displays a specified image
%   alongside some text.  The image can either be loaded from a file or be
%   one of a pre-defined set of useful icons.  Important features to note
%   are:
%
%   * If the image is specified as RGB data or loaded from a
%     .bmp file, it is automatically filtered to convert green pixels
%     (0x00FF00) and magenta pixels (0xFF00FF) to transparent pixels.
%   * Image files can be specified as absolute paths, the name of a file in
%     an MBC resource location (e.g. 'cgemptytradeoff.bmp') or a partially
%     qualified MBC resource location (e.g. 'mbcview\resource\cgemptytradeoff,bmp).
%
%   Image properties:
%      Icon           - Name of a pre-defined icon to load.
%      ImageFile      - Name of a custom image file to load.
%      String         - String to display alongside the image.  HTML
%                       strings are supported which enables multi-line 
%                       text.
%      Style          - Sets the layout configuration of the icon and
%                       text.
%      TooltipString  - String to display as a tooltip for the icon.
%
%   See also BasicContainer, Component, Image.

%   Copyright 2008-2011 The MathWorks, Inc.
    
properties(Dependent)
    %ICON Set a pre-defined icon
    %   Setting the Icon property updates the image to display one of the
    %   pre-defined icons - 'error', 'warning' or 'ok'.  The default value
    %   of this property is 'warning'.  If a different icon is specified by
    %   using the ImageFile property then the Icon property will be changed
    %   to 'custom'.
    %
    %   See also ImageFile.
    Icon;

    %IMAGEFILE Image file to display
    %   Setting the ImageFile property loads the specified image and
    %   displays it.  The CData property is simultaneously cleared since
    %   the image source cannot be from both CData and a file.
    %
    %   If the image file is a .bmp format, its pixels are filtered and
    %   green or magenta ones are converted to transparent pixels.  Any
    %   other formats are used as-is, including transparency information if
    %   it exists.
    %
    %   See also Icon.
    ImageFile;
end
properties(AbortSet)
    %STRING String to display next to the icon
    %   The String property contains the text that will be displayed
    %   alongside the icon.  Simple strings are always displayed as single
    %   lines that truncate, however multiple lines may be created by
    %   specifying the string as html - enclose the string between <html>
    %   and </html> tags.  Note that not all html entities are supported -
    %   for example character entities do not work.  You should carefully
    %   check that html text looks correct.
    String = ''

    %TOOLTIPSTRING Tooltip to display over the icon
    %   Setting the TooltipString will cause the specified string to be
    %   displayed when the use hovers their mouse over the icon.
    TooltipString = '';
    
    %STYLE Layout configuration for the icon and text
    %   The Style property controls how the icon and text are positioned.
    %   Three options are available:
    %
    %   Option      |  Description
    %   -------------------------------------------------------------------
    %   'line'      | The icon is placed at the left and the text to the right 
    %               | of it.  The icon and text are vertically centered.
    %   'paragraph' | The icon is placed at the top-left corner and the
    %               | text is to the  right of it.  This is intended for
    %               | messages that will take up multiple lines.
    %   'item'      | The icon is placed above the text.  The combination
    %               | of both items is centered horizontally and
    %               | vertically.  It is expected that this option will be
    %               | used with larger icons.
    Style = 'line';
end

properties(SetAccess=private, GetAccess=private)
    IconTypeStore = 'warning';
    ImageFileStore = '';
end

methods
    function obj = IconText(varargin)
    %ICONTEXT Construct a new IconText object
    %   H = mbcgui.widget.IconText(PROP, VAL, ...) constructs a new
    %   IconText object with its properties set to the values specified by
    %   the input list of property-value pairs.

    obj@mbcgui.widget.BasicContainer(varargin{:});

    P = com.mathworks.toolbox.mbc.gui.peer.MBCLabelPeer;
    obj.ContentHandle = mbcwidgets.javacomponent(P, ...
        'Parent', obj.Parent, ...
        'Enable', obj.Enable, ...
        'Visible', obj.Visible, ...
        'Position', obj.Position);
    P.setTooltipText(i_multiline(obj.TooltipString));

    % Set the icon, text and layout
    obj.selectStyle;
    obj.selectIcon;
    P.setText(obj.String);
    
    end


    function set.Icon(obj, ic)
        obj.IconTypeStore = ic;
        obj.selectIcon;
    end

    function ic = get.Icon(obj)
        ic = obj.IconTypeStore;
    end

    function set.ImageFile(obj, File)
        if obj.isConstructed
            obj.ContentHandle.Peer.setIconFromFile(File);
        end
        obj.ImageFileStore = File;
        obj.IconTypeStore = 'custom';
    end  

    function File = get.ImageFile(obj)
        File = obj.ImageFileStore;
    end

    function set.TooltipString(obj, tt)
        if obj.isConstructed
            obj.ContentHandle.Peer.setTooltipText(i_multiline(tt));
        end
        obj.TooltipString = tt;
    end

    function set.String(obj, str)
        if obj.isConstructed
            obj.ContentHandle.Peer.setText(str);
        end
        obj.String = str;
    end
    
    function set.Style(obj, style)
        obj.Style = style;
        obj.selectStyle;
    end
end    

    
methods(Access=private)
    function selectIcon(obj)
        if obj.isConstructed
            switch obj.Icon
                case 'warning'
                    file = 'warningsmall.bmp';
                case 'ok'
                    file = 'ticksmall.bmp';  
                case 'error'
                    file = 'errorsmall.bmp';
                case 'custom'
                    file = obj.ImageFile;
                case ''
                    file = '';
                otherwise
                    warning(message('mbc:mbcgui:widget:IconText:InvalidPropertyValue'));
                    file = '';
            end
            obj.ContentHandle.Peer.setIconFromFile(file);
        end
    end
    
    function selectStyle(obj)
        if obj.isConstructed
            switch obj.Style
                case 'line'
                    st = 'ICON_LINE';
                case 'paragraph'
                    st = 'ICON_PARA';
                case 'item'
                    st = 'ICON_ITEM';
                otherwise
                    warning(message('mbc:mbcgui:widget:IconText:InvalidPropertyValue1'));
                    st = 'ICON_LINE';
            end
            obj.ContentHandle.Peer.setLayoutStyle(st);
        end
    end
end
    
end


function tt = i_multiline(tt)
% Support multiline tooltips by converting them to html if they
% are not already
if ~strncmp(tt, '<html>', 6) && ~isempty(strfind(tt, sprintf('\n')))
    tt = ['<html>' regexprep(tt, '\n', '<br>') '</html>'];
end
end