Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Style/XmlAccess/ExcelFillXml.cs

Last change on this file was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 6.6 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Jan Källman                    Initial Release           2009-10-01
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Globalization;
35using System.Text;
36using System.Xml;
37namespace OfficeOpenXml.Style.XmlAccess
38{
39    /// <summary>
40    /// Xml access class for fills
41    /// </summary>
42    public class ExcelFillXml : StyleXmlHelper
43    {
44        internal ExcelFillXml(XmlNamespaceManager nameSpaceManager)
45            : base(nameSpaceManager)
46        {
47            _fillPatternType = ExcelFillStyle.None;
48            _backgroundColor = new ExcelColorXml(NameSpaceManager);
49            _patternColor = new ExcelColorXml(NameSpaceManager);
50        }
51        internal ExcelFillXml(XmlNamespaceManager nsm, XmlNode topNode):
52            base(nsm, topNode)
53        {
54            PatternType = GetPatternType(GetXmlNodeString(fillPatternTypePath));
55            _backgroundColor = new ExcelColorXml(nsm, topNode.SelectSingleNode(_backgroundColorPath, nsm));
56            _patternColor = new ExcelColorXml(nsm, topNode.SelectSingleNode(_patternColorPath, nsm));
57        }
58
59        private ExcelFillStyle GetPatternType(string patternType)
60        {
61            if (patternType == "") return ExcelFillStyle.None;
62            patternType = patternType.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture) + patternType.Substring(1, patternType.Length - 1);
63            try
64            {
65                return (ExcelFillStyle)Enum.Parse(typeof(ExcelFillStyle), patternType);
66            }
67            catch
68            {
69                return ExcelFillStyle.None;
70            }
71        }
72        internal override string Id
73        {
74            get
75            {
76                return PatternType + PatternColor.Id + BackgroundColor.Id;
77            }
78        }
79        #region Public Properties
80        const string fillPatternTypePath = "d:patternFill/@patternType";
81        protected ExcelFillStyle _fillPatternType;
82        /// <summary>
83        /// Cell fill pattern style
84        /// </summary>
85        public ExcelFillStyle PatternType
86        {
87            get
88            {
89                return _fillPatternType;
90            }
91            set
92            {
93                _fillPatternType=value;
94            }
95        }
96        protected ExcelColorXml _patternColor = null;
97        const string _patternColorPath = "d:patternFill/d:bgColor";
98        /// <summary>
99        /// Pattern color
100        /// </summary>
101        public ExcelColorXml PatternColor
102        {
103            get
104            {
105                return _patternColor;
106            }
107            internal set
108            {
109                _patternColor = value;
110            }
111        }
112        protected ExcelColorXml _backgroundColor = null;
113        const string _backgroundColorPath = "d:patternFill/d:fgColor";
114        /// <summary>
115        /// Cell background color
116        /// </summary>
117        public ExcelColorXml BackgroundColor
118        {
119            get
120            {
121                return _backgroundColor;
122            }
123            internal set
124            {
125                _backgroundColor=value;
126            }
127        }
128        #endregion
129
130
131        //internal Fill Copy()
132        //{
133        //    Fill newFill = new Fill(NameSpaceManager, TopNode.Clone());
134        //    return newFill;
135        //}
136
137        internal virtual ExcelFillXml Copy()
138        {
139            ExcelFillXml newFill = new ExcelFillXml(NameSpaceManager);
140            newFill.PatternType = _fillPatternType;
141            newFill.BackgroundColor = _backgroundColor.Copy();
142            newFill.PatternColor = _patternColor.Copy();
143            return newFill;
144        }
145
146        internal override XmlNode CreateXmlNode(XmlNode topNode)
147        {
148            TopNode = topNode;
149            SetXmlNodeString(fillPatternTypePath, SetPatternString(_fillPatternType));
150            if (PatternType != ExcelFillStyle.None)
151            {
152                XmlNode pattern = topNode.SelectSingleNode(fillPatternTypePath, NameSpaceManager);
153                if (BackgroundColor.Exists)
154                {
155                    CreateNode(_backgroundColorPath);
156                    BackgroundColor.CreateXmlNode(topNode.SelectSingleNode(_backgroundColorPath, NameSpaceManager));
157                    if (PatternColor.Exists)
158                    {
159                        CreateNode(_patternColorPath);
160                        //topNode.AppendChild(PatternColor.CreateXmlNode(topNode.SelectSingleNode(_patternColorPath, NameSpaceManager)));
161                        PatternColor.CreateXmlNode(topNode.SelectSingleNode(_patternColorPath, NameSpaceManager));
162                    }
163                }
164            }
165            return topNode;
166        }
167
168        private string SetPatternString(ExcelFillStyle pattern)
169        {
170            string newName = Enum.GetName(typeof(ExcelFillStyle), pattern);
171            return newName.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + newName.Substring(1, newName.Length - 1);
172        }
173    }
174}
Note: See TracBrowser for help on using the repository browser.