Esempio n. 1
0
 /// <summary>
 /// Appends the specified style parts to the current one. The parts can be instances of sub-classes like Border or CellXf or a Style instance. Only the altered properties of the specified style or style part that differs from a new / untouched style instance will be appended. This enables method chaining
 /// </summary>
 /// <param name="styleToAppend">The style to append or a sub-class of Style</param>
 /// <returns>Current style with appended style parts</returns>
 public Style Append(AbstractStyle styleToAppend)
 {
     if (styleToAppend.GetType() == typeof(Border))
     {
         CurrentBorder.CopyProperties((Border)styleToAppend, new Border());
     }
     else if (styleToAppend.GetType() == typeof(CellXf))
     {
         CurrentCellXf.CopyProperties((CellXf)styleToAppend, new CellXf());
     }
     else if (styleToAppend.GetType() == typeof(Fill))
     {
         CurrentFill.CopyProperties((Fill)styleToAppend, new Fill());
     }
     else if (styleToAppend.GetType() == typeof(Font))
     {
         CurrentFont.CopyProperties((Font)styleToAppend, new Font());
     }
     else if (styleToAppend.GetType() == typeof(NumberFormat))
     {
         CurrentNumberFormat.CopyProperties((NumberFormat)styleToAppend, new NumberFormat());
     }
     else if (styleToAppend.GetType() == typeof(Style))
     {
         CurrentBorder.CopyProperties(((Style)styleToAppend).CurrentBorder, new Border());
         CurrentCellXf.CopyProperties(((Style)styleToAppend).CurrentCellXf, new CellXf());
         CurrentFill.CopyProperties(((Style)styleToAppend).CurrentFill, new Fill());
         CurrentFont.CopyProperties(((Style)styleToAppend).CurrentFont, new Font());
         CurrentNumberFormat.CopyProperties(((Style)styleToAppend).CurrentNumberFormat, new NumberFormat());
     }
     return(this);
 }
Esempio n. 2
0
        /// <summary>
        /// Gets a style by its hash
        /// </summary>
        /// <param name="hash">Hash of the style</param>
        /// <returns>Determined style</returns>
        /// <exception cref="StyleException">Throws a StyleException if the style was not found in the style manager</exception>
        public Style GetStyleByHash(String hash)
        {
            AbstractStyle component = GetComponentByHash(ref styles, hash);

            if (component == null)
            {
                throw new StyleException("MissingReferenceException", "The style component with the hash '" + hash + "' was not found");
            }
            return((Style)component);
        }
Esempio n. 3
0
        /* ****************************** */

        /// <summary>
        /// Gets a numberFormat by its hash
        /// </summary>
        /// <param name="hash">Hash of the numberFormat</param>
        /// <returns>Determined numberFormat</returns>
        /// <exception cref="StyleException">Throws a StyleException if the numberFormat was not found in the style manager</exception>
        public NumberFormat GetNumberFormatByHash(String hash)
        {
            AbstractStyle component = GetComponentByHash(ref numberFormats, hash);

            if (component == null)
            {
                throw new StyleException("MissingReferenceException", "The style component with the hash '" + hash + "' was not found");
            }
            return((NumberFormat)component);
        }
Esempio n. 4
0
        /// <summary>
        /// Gets a border by its hash
        /// </summary>
        /// <param name="hash">Hash of the border</param>
        /// <returns>Determined border</returns>
        /// <exception cref="StyleException">Throws a StyleException if the border was not found in the style manager</exception>
        public Border GetBorderByHash(int hash)
        {
            AbstractStyle component = GetComponentByHash(ref borders, hash);

            if (component == null)
            {
                throw new StyleException("MissingReferenceException", "The style component with the hash '" + hash + "' was not found");
            }
            return((Border)component);
        }
Esempio n. 5
0
        /* ****************************** */

        /// <summary>
        /// Gets a cellXf by its hash
        /// </summary>
        /// <param name="hash">Hash of the cellXf</param>
        /// <returns>Determined cellXf</returns>
        /// <exception cref="StyleException">Throws a StyleException if the cellXf was not found in the style manager</exception>
        public CellXf GetCellXfByHash(int hash)
        {
            AbstractStyle component = GetComponentByHash(ref cellXfs, hash);

            if (component == null)
            {
                throw new StyleException("MissingReferenceException", "The style component with the hash '" + hash + "' was not found");
            }
            return((CellXf)component);
        }
Esempio n. 6
0
        /// <summary>
        /// Checks whether a style component in the style manager is used by a style
        /// </summary>
        /// <param name="component">Component to check</param>
        /// <returns>If true, the component is in use</returns>
        private bool IsUsedByStyle(AbstractStyle component)
        {
            Style  s;
            bool   match = false;
            string hash  = component.Hash;
            int    len   = styles.Count;

            for (int i = 0; i < len; i++)
            {
                s = (Style)styles[i];
                if (component.GetType() == typeof(Border))
                {
                    if (s.CurrentBorder.Hash == hash)
                    {
                        match = true; break;
                    }
                }
                else if (component.GetType() == typeof(CellXf))
                {
                    if (s.CurrentCellXf.Hash == hash)
                    {
                        match = true; break;
                    }
                }
                if (component.GetType() == typeof(Fill))
                {
                    if (s.CurrentFill.Hash == hash)
                    {
                        match = true; break;
                    }
                }
                if (component.GetType() == typeof(Font))
                {
                    if (s.CurrentFont.Hash == hash)
                    {
                        match = true; break;
                    }
                }
                if (component.GetType() == typeof(NumberFormat))
                {
                    if (s.CurrentNumberFormat.Hash == hash)
                    {
                        match = true; break;
                    }
                }
            }
            return(match);
        }
Esempio n. 7
0
        /// <summary>
        /// Adds a style component to the manager
        /// </summary>
        /// <param name="style">Component to add</param>
        /// <returns>Hash of the added or determined component</returns>
        private string AddStyleComponent(AbstractStyle style)
        {
            string hash = style.Hash;

            if (style.GetType() == typeof(Border))
            {
                if (GetComponentByHash(ref borders, hash) == null)
                {
                    borders.Add(style);
                }
                Reorganize(ref borders);
            }
            else if (style.GetType() == typeof(CellXf))
            {
                if (GetComponentByHash(ref cellXfs, hash) == null)
                {
                    cellXfs.Add(style);
                }
                Reorganize(ref cellXfs);
            }
            else if (style.GetType() == typeof(Fill))
            {
                if (GetComponentByHash(ref fills, hash) == null)
                {
                    fills.Add(style);
                }
                Reorganize(ref fills);
            }
            else if (style.GetType() == typeof(Font))
            {
                if (GetComponentByHash(ref fonts, hash) == null)
                {
                    fonts.Add(style);
                }
                Reorganize(ref fonts);
            }
            else if (style.GetType() == typeof(NumberFormat))
            {
                if (GetComponentByHash(ref numberFormats, hash) == null)
                {
                    numberFormats.Add(style);
                }
                Reorganize(ref numberFormats);
            }
            else if (style.GetType() == typeof(Style))
            {
                Style s = (Style)style;
                if (styleNames.Contains(s.Name))
                {
                    throw new StyleException("StyleAlreadyExistsException", "The style with the name '" + s.Name + "' already exists");
                }
                if (GetComponentByHash(ref styles, hash) == null)
                {
                    int?id;
                    if (s.InternalID.HasValue == false)
                    {
                        id           = int.MaxValue;
                        s.InternalID = id;
                    }
                    else
                    {
                        id = s.InternalID.Value;
                    }
                    string temp = AddStyleComponent(s.CurrentBorder, id);
                    s.CurrentBorder       = (Border)GetComponentByHash(ref borders, temp);
                    temp                  = AddStyleComponent(s.CurrentCellXf, id);
                    s.CurrentCellXf       = (CellXf)GetComponentByHash(ref cellXfs, temp);
                    temp                  = AddStyleComponent(s.CurrentFill, id);
                    s.CurrentFill         = (Fill)GetComponentByHash(ref fills, temp);
                    temp                  = AddStyleComponent(s.CurrentFont, id);
                    s.CurrentFont         = (Font)GetComponentByHash(ref fonts, temp);
                    temp                  = AddStyleComponent(s.CurrentNumberFormat, id);
                    s.CurrentNumberFormat = (NumberFormat)GetComponentByHash(ref numberFormats, temp);
                    styles.Add(s);
                }
                Reorganize(ref styles);
                hash = s.CalculateHash();
            }
            return(hash);
        }
Esempio n. 8
0
 /// <summary>
 /// Adds a style component to the manager with an ID
 /// </summary>
 /// <param name="style">Component to add</param>
 /// <param name="id">Id of the component</param>
 /// <returns>Hash of the added or determined component</returns>
 private string AddStyleComponent(AbstractStyle style, int?id)
 {
     style.InternalID = id;
     return(AddStyleComponent(style));
 }