columninforecordsaggregate.java
来自「EXCEL read and write」· Java 代码 · 共 524 行 · 第 1/2 页
JAVA
524 行
/* ==================================================================== 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.==================================================================== */package org.apache.poi.hssf.record.aggregates;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import org.apache.poi.hssf.model.RecordStream;import org.apache.poi.hssf.record.ColumnInfoRecord;/** * @author Glen Stampoultzis */public final class ColumnInfoRecordsAggregate extends RecordAggregate { /** * List of {@link ColumnInfoRecord}s assumed to be in order */ private final List records; private static final class CIRComparator implements Comparator { public static final Comparator instance = new CIRComparator(); private CIRComparator() { // enforce singleton } public int compare(Object a, Object b) { return compareColInfos((ColumnInfoRecord)a, (ColumnInfoRecord)b); } public static int compareColInfos(ColumnInfoRecord a, ColumnInfoRecord b) { return a.getFirstColumn()-b.getFirstColumn(); } } /** * Creates an empty aggregate */ public ColumnInfoRecordsAggregate() { records = new ArrayList(); } public ColumnInfoRecordsAggregate(RecordStream rs) { this(); boolean isInOrder = true; ColumnInfoRecord cirPrev = null; while(rs.peekNextClass() == ColumnInfoRecord.class) { ColumnInfoRecord cir = (ColumnInfoRecord) rs.getNext(); records.add(cir); if (cirPrev != null && CIRComparator.compareColInfos(cirPrev, cir) > 0) { isInOrder = false; } cirPrev = cir; } if (records.size() < 1) { throw new RuntimeException("No column info records found"); } if (!isInOrder) { Collections.sort(records, CIRComparator.instance); } } /** * Performs a deep clone of the record */ public Object clone() { ColumnInfoRecordsAggregate rec = new ColumnInfoRecordsAggregate(); for (int k = 0; k < records.size(); k++) { ColumnInfoRecord ci = ( ColumnInfoRecord ) records.get(k); rec.records.add(ci.clone()); } return rec; } /** * Inserts a column into the aggregate (at the end of the list). */ public void insertColumn(ColumnInfoRecord col) { records.add(col); Collections.sort(records, CIRComparator.instance); } /** * Inserts a column into the aggregate (at the position specified by * <code>idx</code>. */ private void insertColumn(int idx, ColumnInfoRecord col) { records.add(idx, col); } /* package */ int getNumColumns() { return records.size(); } public void visitContainedRecords(RecordVisitor rv) { int nItems = records.size(); if (nItems < 1) { return; } ColumnInfoRecord cirPrev = null; for(int i=0; i<nItems; i++) { ColumnInfoRecord cir = (ColumnInfoRecord)records.get(i); rv.visitRecord(cir); if (cirPrev != null && CIRComparator.compareColInfos(cirPrev, cir) > 0) { // Excel probably wouldn't mind, but there is much logic in this class // that assumes the column info records are kept in order throw new RuntimeException("Column info records are out of order"); } cirPrev = cir; } } private int findStartOfColumnOutlineGroup(int pIdx) { // Find the start of the group. ColumnInfoRecord columnInfo = (ColumnInfoRecord) records.get(pIdx); int level = columnInfo.getOutlineLevel(); int idx = pIdx; while (idx != 0) { ColumnInfoRecord prevColumnInfo = (ColumnInfoRecord) records.get(idx - 1); if (!prevColumnInfo.isAdjacentBefore(columnInfo)) { break; } if (prevColumnInfo.getOutlineLevel() < level) { break; } idx--; columnInfo = prevColumnInfo; } return idx; } private int findEndOfColumnOutlineGroup(int colInfoIndex) { // Find the end of the group. ColumnInfoRecord columnInfo = (ColumnInfoRecord) records.get(colInfoIndex); int level = columnInfo.getOutlineLevel(); int idx = colInfoIndex; while (idx < records.size() - 1) { ColumnInfoRecord nextColumnInfo = (ColumnInfoRecord) records.get(idx + 1); if (!columnInfo.isAdjacentBefore(nextColumnInfo)) { break; } if (nextColumnInfo.getOutlineLevel() < level) { break; } idx++; columnInfo = nextColumnInfo; } return idx; } private ColumnInfoRecord getColInfo(int idx) { return (ColumnInfoRecord) records.get( idx ); } /** * 'Collapsed' state is stored in a single column col info record immediately after the outline group * @param idx * @return */ private boolean isColumnGroupCollapsed(int idx) { int endOfOutlineGroupIdx = findEndOfColumnOutlineGroup(idx); int nextColInfoIx = endOfOutlineGroupIdx+1; if (nextColInfoIx >= records.size()) { return false; } ColumnInfoRecord nextColInfo = getColInfo(nextColInfoIx); if (!getColInfo(endOfOutlineGroupIdx).isAdjacentBefore(nextColInfo)) { return false; } return nextColInfo.getCollapsed(); } private boolean isColumnGroupHiddenByParent(int idx) { // Look out outline details of end int endLevel = 0; boolean endHidden = false; int endOfOutlineGroupIdx = findEndOfColumnOutlineGroup( idx ); if (endOfOutlineGroupIdx < records.size()) { ColumnInfoRecord nextInfo = getColInfo(endOfOutlineGroupIdx + 1); if (getColInfo(endOfOutlineGroupIdx).isAdjacentBefore(nextInfo)) { endLevel = nextInfo.getOutlineLevel(); endHidden = nextInfo.getHidden(); } } // Look out outline details of start int startLevel = 0; boolean startHidden = false; int startOfOutlineGroupIdx = findStartOfColumnOutlineGroup( idx ); if (startOfOutlineGroupIdx > 0) { ColumnInfoRecord prevInfo = getColInfo(startOfOutlineGroupIdx - 1); if (prevInfo.isAdjacentBefore(getColInfo(startOfOutlineGroupIdx))) { startLevel = prevInfo.getOutlineLevel(); startHidden = prevInfo.getHidden(); } } if (endLevel > startLevel) { return endHidden; } return startHidden; } public void collapseColumn(int columnIndex) { int colInfoIx = findColInfoIdx(columnIndex, 0); if (colInfoIx == -1) { return; } // Find the start of the group. int groupStartColInfoIx = findStartOfColumnOutlineGroup(colInfoIx); ColumnInfoRecord columnInfo = getColInfo(groupStartColInfoIx); // Hide all the columns until the end of the group int lastColIx = setGroupHidden(groupStartColInfoIx, columnInfo.getOutlineLevel(), true); // Write collapse field setColumn(lastColIx + 1, null, null, null, null, Boolean.TRUE); } /** * Sets all adjacent columns of the same outline level to the specified hidden status. * @param pIdx the col info index of the start of the outline group * @return the column index of the last column in the outline group */ private int setGroupHidden(int pIdx, int level, boolean hidden) { int idx = pIdx; ColumnInfoRecord columnInfo = getColInfo(idx); while (idx < records.size()) { columnInfo.setHidden(hidden); if (idx + 1 < records.size()) { ColumnInfoRecord nextColumnInfo = getColInfo(idx + 1); if (!columnInfo.isAdjacentBefore(nextColumnInfo)) { break; } if (nextColumnInfo.getOutlineLevel() < level) { break; } columnInfo = nextColumnInfo; } idx++; } return columnInfo.getLastColumn(); } public void expandColumn(int columnIndex) { int idx = findColInfoIdx(columnIndex, 0);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?