📄 range.java
字号:
* @param styleIndex The index of the paragraph's style in the style sheet. * @param text The text to insert. * @return A newly inserted paragraph. */ protected Paragraph insertBefore(ParagraphProperties props, int styleIndex, String text) //throws UnsupportedEncodingException { initAll(); StyleSheet ss = _doc.getStyleSheet(); ParagraphProperties baseStyle = ss.getParagraphStyle(styleIndex); CharacterProperties baseChp = ss.getCharacterStyle(styleIndex); byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty(props, baseStyle); byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE]; LittleEndian.putShort(withIndex, (short)styleIndex); System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length); SprmBuffer buf = new SprmBuffer(withIndex); _doc.getParagraphTable().insert(_parStart, _start, buf); insertBefore(text, baseChp); return getParagraph(0); } /** * Inserts and empty paragraph into the end of this range. * * @param props The properties that the new paragraph will have. * @param styleIndex The index into the stylesheet for the new paragraph. * @return The newly inserted paragraph. */ public Paragraph insertAfter(ParagraphProperties props, int styleIndex) //throws UnsupportedEncodingException { return this.insertAfter(props, styleIndex, "\r"); } /** * Inserts a paragraph into the end of this range. The paragraph will * contain one character run that has the default properties for the * paragraph's style. * * It is necessary for the text to end with the character '\r' * * @param props The paragraph's properties. * @param styleIndex The index of the paragraph's style in the style sheet. * @param text The text to insert. * @return A newly inserted paragraph. */ protected Paragraph insertAfter(ParagraphProperties props, int styleIndex, String text) //throws UnsupportedEncodingException { initAll(); StyleSheet ss = _doc.getStyleSheet(); ParagraphProperties baseStyle = ss.getParagraphStyle(styleIndex); CharacterProperties baseChp = ss.getCharacterStyle(styleIndex); byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty(props, baseStyle); byte[] withIndex = new byte[grpprl.length + LittleEndian.SHORT_SIZE]; LittleEndian.putShort(withIndex, (short)styleIndex); System.arraycopy(grpprl, 0, withIndex, LittleEndian.SHORT_SIZE, grpprl.length); SprmBuffer buf = new SprmBuffer(withIndex); _doc.getParagraphTable().insert(_parEnd, _end, buf); _parEnd++; insertAfter(text, baseChp); return getParagraph(numParagraphs() - 1); } public void delete() { initAll(); int numSections = _sections.size(); int numRuns = _characters.size(); int numParagraphs = _paragraphs.size(); for (int x = _charStart; x < numRuns; x++) { CHPX chpx = (CHPX)_characters.get(x); chpx.adjustForDelete(_start, _end - _start); } for (int x = _parStart; x < numParagraphs; x++) { PAPX papx = (PAPX)_paragraphs.get(x); papx.adjustForDelete(_start, _end - _start); } for (int x = _sectionStart; x < numSections; x++) { SEPX sepx = (SEPX)_sections.get(x); sepx.adjustForDelete(_start, _end - _start); } } /** * Inserts a simple table into the beginning of this range. The number of * columns is determined by the TableProperties passed into this function. * * @param props The table properties for the table. * @param rows The number of rows. * @return The empty Table that is now part of the document. */ public Table insertBefore(TableProperties props, int rows) { ParagraphProperties parProps = new ParagraphProperties(); parProps.setFInTable((byte)1); parProps.setTableLevel((byte)1); int columns = props.getItcMac(); for (int x = 0; x < rows; x++) { Paragraph cell = this.insertBefore(parProps, StyleSheet.NIL_STYLE); cell.insertAfter(String.valueOf('\u0007')); for(int y = 1; y < columns; y++) { cell = cell.insertAfter(parProps, StyleSheet.NIL_STYLE); cell.insertAfter(String.valueOf('\u0007')); } cell = cell.insertAfter(parProps, StyleSheet.NIL_STYLE, String.valueOf('\u0007')); cell.setTableRowEnd(props); } return new Table(_start, _start + (rows * (columns + 1)), this, 1); } /** * Inserts a list into the beginning of this range. * * @param props The properties of the list entry. All list entries are * paragraphs. * @param listID The id of the list that contains the properties. * @param level The indentation level of the list. * @param styleIndex The base style's index in the stylesheet. * @return The empty ListEntry that is now part of the document. */ public ListEntry insertBefore(ParagraphProperties props, int listID, int level, int styleIndex) { ListTables lt = _doc.getListTables(); if (lt.getLevel(listID, level) == null) { throw new NoSuchElementException("The specified list and level do not exist"); } int ilfo = lt.getOverrideIndexFromListID(listID); props.setIlfo(ilfo); props.setIlvl((byte)level); return (ListEntry)insertBefore(props, styleIndex); } /** * Inserts a list into the beginning of this range. * * @param props The properties of the list entry. All list entries are * paragraphs. * @param listID The id of the list that contains the properties. * @param level The indentation level of the list. * @param styleIndex The base style's index in the stylesheet. * @return The empty ListEntry that is now part of the document. */ public ListEntry insertAfter(ParagraphProperties props, int listID, int level, int styleIndex) { ListTables lt = _doc.getListTables(); if (lt.getLevel(listID, level) == null) { throw new NoSuchElementException("The specified list and level do not exist"); } int ilfo = lt.getOverrideIndexFromListID(listID); props.setIlfo(ilfo); props.setIlvl((byte)level); return (ListEntry)insertAfter(props, styleIndex); } /** * Gets the character run at index. The index is relative to this range. * * @param index The index of the character run to get. * @return The character run at the specified index in this range. */ public CharacterRun getCharacterRun(int index) { initCharacterRuns(); CHPX chpx = (CHPX)_characters.get(index + _charStart); int[] point = findRange(_paragraphs, _parStart, Math.max(chpx.getStart(), _start), chpx.getEnd()); PAPX papx = (PAPX)_paragraphs.get(point[0]); short istd = papx.getIstd(); CharacterRun chp = new CharacterRun(chpx, _doc.getStyleSheet(), istd, this); return chp; } /** * Gets the section at index. The index is relative to this range. * * @param index The index of the section to get. * @return The section at the specified index in this range. */ public Section getSection(int index) { initSections(); SEPX sepx = (SEPX)_sections.get(index + _sectionStart); Section sep = new Section(sepx, this); return sep; } /** * Gets the paragraph at index. The index is relative to this range. * * @param index The index of the paragraph to get. * @return The paragraph at the specified index in this range. */ public Paragraph getParagraph(int index) { initParagraphs(); PAPX papx = (PAPX)_paragraphs.get(index + _parStart); ParagraphProperties props = papx.getParagraphProperties(_doc.getStyleSheet()); Paragraph pap = null; if (props.getIlfo() > 0) { pap = new ListEntry(papx, this, _doc.getListTables()); } else { pap = new Paragraph(papx, this); } return pap; } /** * This method is used to determine the type. Handy for switch statements * compared to the instanceof operator. * * @return A TYPE constant. */ public int type() { return TYPE_UNDEFINED; } /** * Gets the table that starts with paragraph. In a Word file, a table consists * of a group of paragraphs with certain flags set. * * @param paragraph The paragraph that is the first paragraph in the table. * @return The table that starts with paragraph */ public Table getTable(Paragraph paragraph) { if (!paragraph.isInTable()) { throw new IllegalArgumentException("This paragraph doesn't belong to a table"); } Range r = (Range)paragraph; if (r._parent.get() != this) { throw new IllegalArgumentException("This paragraph is not a child of this range"); } r.initAll(); int tableEnd = r._parEnd; if (r._parStart != 0 && getParagraph(r._parStart - 1).isInTable() && getParagraph(r._parStart - 1)._sectionEnd >= r._sectionStart) { throw new IllegalArgumentException("This paragraph is not the first one in the table"); } int limit = _paragraphs.size(); for (; tableEnd < limit; tableEnd++) { if (!getParagraph(tableEnd).isInTable()) { break; } } initAll(); if (tableEnd > _parEnd) { throw new ArrayIndexOutOfBoundsException("The table's bounds fall outside of this Range"); } return new Table(r._parStart, tableEnd, r._doc.getRange(), paragraph.getTableLevel()); } /** * loads all of the list indexes. */ protected void initAll() { initText(); initCharacterRuns(); initParagraphs(); initSections(); } /** * inits the paragraph list indexes. */ private void initParagraphs() { if (!_parRangeFound) { int[] point = findRange(_paragraphs, _parStart, _start, _end); _parStart = point[0]; _parEnd = point[1]; _parRangeFound = true; } } /** * inits the character run list indexes. */ private void initCharacterRuns() { if (!_charRangeFound) { int[] point = findRange(_characters, _charStart, _start, _end); _charStart = point[0]; _charEnd = point[1]; _charRangeFound = true; } } /** * inits the text piece list indexes. */ private void initText() { if (!_textRangeFound) { int[] point = findRange(_text, _textStart, _start, _end); _textStart = point[0]; _textEnd = point[1]; _textRangeFound = true; } } /** * inits the section list indexes. */ private void initSections() { if (!_sectionRangeFound) { int[] point = findRange(_sections, _sectionStart, _start, _end); _sectionStart = point[0]; _sectionEnd = point[1]; _sectionRangeFound = true; } } /** * Used to find the list indexes of a particular property. * * @param rpl A list of property nodes. * @param min A hint on where to start looking. * @param start The starting character offset. * @param end The ending character offset. * @return An int array of length 2. The first int is the start index and the * second int is the end index. */ private int[] findRange(List rpl, int min, int start, int end) { int x = min; PropertyNode node = (PropertyNode)rpl.get(x); while(node.getEnd() <= start && x < rpl.size()-1) { x++; node = (PropertyNode)rpl.get(x); } int y = x; node = (PropertyNode)rpl.get(y); while(node.getEnd() < end && y < rpl.size()-1) { y++; node = (PropertyNode)rpl.get(y); } return new int[]{x, y + 1}; } /** * resets the list indexes. */ private void reset() { _textRangeFound = false; _charRangeFound = false; _parRangeFound = false; _sectionRangeFound = false; } /** * adjust this range after an insert happens. * @param length the length to adjust for */ private void adjustForInsert(int length) { _end += length; reset(); Range parent = (Range)_parent.get(); if (parent != null) { parent.adjustForInsert(length); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -