zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
 
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) Under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You Under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed Under the License is distributed on an "AS Is" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations Under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.HSSF.Record
{
    using System;
    using System.Collections;
    using System.Text;
    using HH.WMS.Utils.NPOI.Util;
    using System.Collections.Generic;
 
 
    /**
     * Record that Contains the functionality page _breaks (horizontal and vertical)
     * 
     * The other two classes just specifically Set the SIDS for record creation.
     * 
     * REFERENCE:  Microsoft Excel SDK page 322 and 420
     * 
     * @see HorizontalPageBreakRecord
     * @see VerticalPageBreakRecord
     * @author Danny Mui (dmui at apache dot org)
     */
    public class PageBreakRecord : StandardRecord
    {
        private const bool IS_EMPTY_RECORD_WRITTEN = false;
        private static int[] EMPTY_INT_ARRAY = { };
 
        public short sid;
        // fix warning CS0169 "never used": private short numBreaks;
        private IList<Break> _breaks;
        private Hashtable _breakMap;
 
        /**
         * Since both records store 2byte integers (short), no point in 
         * differentiating it in the records.
         * 
         * The subs (rows or columns, don't seem to be able to Set but excel Sets
         * them automatically)
         */
        public class Break
        {
 
            public static int ENCODED_SIZE = 6;
            public int main;
            public int subFrom;
            public int subTo;
 
            public Break(RecordInputStream in1)
            {
                main = in1.ReadUShort() - 1;
                subFrom = in1.ReadUShort();
                subTo = in1.ReadUShort();
            }
 
            public Break(int main, int subFrom, int subTo)
            {
                this.main = main;
                this.subFrom = subFrom;
                this.subTo = subTo;
            }
 
            public void Serialize(ILittleEndianOutput out1)
            {
                out1.WriteShort(main + 1);
                out1.WriteShort(subFrom);
                out1.WriteShort(subTo);
            }
        }
 
        public PageBreakRecord()
        {
            _breaks = new List<Break>();
            _breakMap = new Hashtable();
        }
 
        public PageBreakRecord(RecordInputStream in1)
        {
            int nBreaks = in1.ReadShort();
            _breaks = new List<Break>(nBreaks + 2);
            _breakMap = new Hashtable();
 
            for (int k = 0; k < nBreaks; k++)
            {
                Break br = new Break(in1);
                _breaks.Add(br);
                _breakMap[br.main] = br;
            }
        }
 
        public override short Sid
        {
            get { return sid; }
        }
 
        //public override int Serialize(int offset, byte[] data)
        //{
        //    int nBreaks = _breaks.Count;
        //    if (!IS_EMPTY_RECORD_WRITTEN && nBreaks < 1)
        //    {
        //        return 0;
        //    }
        //    int dataSize = DataSize;
        //    LittleEndian.PutUShort(data, offset + 0, Sid);
        //    LittleEndian.PutUShort(data, offset + 2, dataSize);
        //    LittleEndian.PutUShort(data, offset + 4, nBreaks);
        //    int pos = 6;
        //    for (int i = 0; i < nBreaks; i++)
        //    {
        //        Break br = (Break)_breaks[i];
        //        pos += br.Serialize(offset + pos, data);
        //    }
 
        //    return 4 + dataSize;
        //}
        public override void Serialize(ILittleEndianOutput out1)
        {
            int nBreaks = _breaks.Count;
            out1.WriteShort(nBreaks);
            for (int i = 0; i < nBreaks; i++)
            {
                _breaks[i].Serialize(out1);
            }
        }
        protected override int DataSize
        {
            get
            {
                return 2 + _breaks.Count * Break.ENCODED_SIZE;
            }
        }
 
        public IEnumerator<Break> GetBreaksEnumerator()
        {
            //if (_breaks == null)
            //    return new ArrayList().GetEnumerator();
            //else
            return _breaks.GetEnumerator();
        }
 
        public override String ToString()
        {
            StringBuilder retval = new StringBuilder();
 
            //if (Sid != HORIZONTAL_SID && Sid != VERTICAL_SID)
            //    return "[INVALIDPAGEBREAK]\n     .Sid =" + Sid + "[INVALIDPAGEBREAK]";
 
            String label;
            String mainLabel;
            String subLabel;
 
            if (Sid == HorizontalPageBreakRecord.sid)
            {
                label = "HORIZONTALPAGEBREAK";
                mainLabel = "row";
                subLabel = "col";
            }
            else
            {
                label = "VERTICALPAGEBREAK";
                mainLabel = "column";
                subLabel = "row";
            }
 
            retval.Append("[" + label + "]").Append("\n");
            retval.Append("     .Sid        =").Append(Sid).Append("\n");
            retval.Append("     .num_breaks =").Append(NumBreaks).Append("\n");
            IEnumerator iterator = GetBreaksEnumerator();
            for (int k = 0; k < NumBreaks; k++)
            {
                Break region = (Break)iterator.Current;
 
                retval.Append("     .").Append(mainLabel).Append(" (zero-based) =").Append(region.main).Append("\n");
                retval.Append("     .").Append(subLabel).Append("From    =").Append(region.subFrom).Append("\n");
                retval.Append("     .").Append(subLabel).Append("To      =").Append(region.subTo).Append("\n");
            }
 
            retval.Append("[" + label + "]").Append("\n");
            return retval.ToString();
        }
 
        /**
         * Adds the page break at the specified parameters
         * @param main Depending on sid, will determine row or column to put page break (zero-based)
         * @param subFrom No user-interface to Set (defaults to minumum, 0)
         * @param subTo No user-interface to Set
         */
        public void AddBreak(int main, int subFrom, int subTo)
        {
            //if (_breaks == null)
            //{
            //    _breaks = new ArrayList(NumBreaks + 10);
            //    _breakMap = new Hashtable();
            //}
            int key = (int)main;
            Break region = (Break)_breakMap[key];
            if (region != null)
            {
                region.main = main;
                region.subFrom = subFrom;
                region.subTo = subTo;
            }
            else
            {
                region = new Break(main, subFrom, subTo);
                _breaks.Add(region);
            }
            _breakMap[key] = region;
        }
 
        /**
         * Removes the break indicated by the parameter
         * @param main (zero-based)
         */
        public void RemoveBreak(int main)
        {
            int rowKey = main;
            Break region = (Break)_breakMap[rowKey];
            _breaks.Remove(region);
            _breakMap.Remove(rowKey);
        }
 
        public override int RecordSize
        {
            get {
                int nBreaks = _breaks.Count;
                if (!IS_EMPTY_RECORD_WRITTEN && nBreaks < 1)
                {
                    return 0;
                }
                return 4 + DataSize;
            }
        }
        public int NumBreaks
        {
            get
            {
                return _breaks.Count;
            }
        }
        public bool IsEmpty
        {
            get
            {
                return _breaks.Count==0;
            }
        }
        /**
         * Retrieves the region at the row/column indicated
         * @param main FIXME: Document this!
         * @return The Break or null if no break exists at the row/col specified.
         */
        public Break GetBreak(int main)
        {
            //if (_breakMap == null)
            //    return null;
            //int rowKey = (int)main;
            return (Break)_breakMap[main];
        }
        public int[] GetBreaks()
        {
            int count = NumBreaks;
            if (count < 1)
            {
                return EMPTY_INT_ARRAY;
            }
            int[] result = new int[count];
            for (int i = 0; i < count; i++)
            {
                Break breakItem = _breaks[i];
                result[i] = breakItem.main;
            }
            return result;
        }
    }
}