001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: /*
019: * HSSF Chart Title Format Record Type
020: */
021: package org.apache.poi.hssf.record;
022:
023: import org.apache.poi.util.LittleEndian;
024:
025: import java.util.ArrayList;
026:
027: /**
028: * Describes the formatting runs associated with a chart title.
029: */
030: public class ChartTitleFormatRecord extends Record {
031: public static final short sid = 0x1050;
032:
033: private int m_recs;
034:
035: private class CTFormat {
036: private short m_offset;
037: private short m_fontIndex;
038:
039: protected CTFormat(short offset, short fontIdx) {
040: m_offset = offset;
041: m_fontIndex = fontIdx;
042: }
043:
044: public short getOffset() {
045: return m_offset;
046: }
047:
048: public void setOffset(short newOff) {
049: m_offset = newOff;
050: }
051:
052: public short getFontIndex() {
053: return m_fontIndex;
054: }
055: }
056:
057: private ArrayList m_formats;
058:
059: public ChartTitleFormatRecord() {
060: super ();
061: }
062:
063: public ChartTitleFormatRecord(RecordInputStream in) {
064: super (in);
065: }
066:
067: protected void validateSid(short id) {
068: if (id != sid) {
069: throw new RecordFormatException(
070: "NOT A CHARTTITLEFORMAT RECORD");
071: }
072: }
073:
074: protected void fillFields(RecordInputStream in) {
075: m_recs = in.readUShort();
076: int idx;
077: CTFormat ctf;
078: if (m_formats == null) {
079: m_formats = new ArrayList(m_recs);
080: }
081: for (idx = 0; idx < m_recs; idx++) {
082: ctf = new CTFormat(in.readShort(), in.readShort());
083: m_formats.add(ctf);
084: }
085: }
086:
087: public int serialize(int offset, byte[] data) {
088: LittleEndian.putShort(data, 0 + offset, sid);
089: LittleEndian.putShort(data, 2 + offset,
090: (short) (getRecordSize() - 4));
091: int idx;
092: CTFormat ctf;
093: LittleEndian.putShort(data, 4 + offset, (short) m_formats
094: .size());
095: for (idx = 0; idx < m_formats.size(); idx++) {
096: ctf = (CTFormat) m_formats.get(idx);
097: LittleEndian.putShort(data, 6 + (idx * 4) + offset, ctf
098: .getOffset());
099: LittleEndian.putShort(data, 8 + (idx * 4) + offset, ctf
100: .getFontIndex());
101: }
102:
103: return getRecordSize();
104: }
105:
106: public int getRecordSize() {
107: return 4 + 2 + (4 * m_formats.size());
108: }
109:
110: public short getSid() {
111: return sid;
112: }
113:
114: public int getFormatCount() {
115: return m_formats.size();
116: }
117:
118: public void modifyFormatRun(short oldPos, short newLen) {
119: short shift = (short) 0;
120: for (int idx = 0; idx < m_formats.size(); idx++) {
121: CTFormat ctf = (CTFormat) m_formats.get(idx);
122: if (shift != 0) {
123: ctf.setOffset((short) (ctf.getOffset() + shift));
124: } else if ((oldPos == ctf.getOffset())
125: && (idx < (m_formats.size() - 1))) {
126: CTFormat nextCTF = (CTFormat) m_formats.get(idx + 1);
127: shift = (short) (newLen - (nextCTF.getOffset() - ctf
128: .getOffset()));
129: }
130: }
131: }
132:
133: public String toString() {
134: StringBuffer buffer = new StringBuffer();
135:
136: buffer.append("[CHARTTITLEFORMAT]\n");
137: buffer.append(" .format_runs = ").append(m_recs)
138: .append("\n");
139: int idx;
140: CTFormat ctf;
141: for (idx = 0; idx < m_formats.size(); idx++) {
142: ctf = (CTFormat) m_formats.get(idx);
143: buffer.append(" .char_offset= ").append(
144: ctf.getOffset());
145: buffer.append(",.fontidx= ").append(ctf.getFontIndex());
146: buffer.append("\n");
147: }
148: buffer.append("[/CHARTTITLEFORMAT]\n");
149: return buffer.toString();
150: }
151: }
|