X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgui%2FtextEditorComponent%2FPage.java;h=47c898fe6c9b42610ff14d3cdf27e64a0bd3ac42;hb=a3ff3683bd0a025061667b26b6fcf56fe20f0afc;hp=e58da8606f3c7bc54bbb9e5dab4075c7794bee81;hpb=b1e8d7bd8c9d0905e9fe3c46fc84a11779b95982;p=sixth-3d.git diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java index e58da86..47c898f 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java @@ -1,53 +1,82 @@ /* - * Sixth 3D engine. Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - * + * Sixth 3D engine. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.sixth.e3d.gui.textEditorComponent; import java.util.ArrayList; import java.util.List; +/** + * A page in a text editor. + */ public class Page { - public List lines = new ArrayList<>(); + /** + * The text lines. + */ + public List rows = new ArrayList<>(); public void ensureMaxTextLine(final int row) { - while (lines.size() <= row) - lines.add(new TextLine()); + while (rows.size() <= row) + rows.add(new TextLine()); } + /** + * Returns the character at the specified location. + * If the location is out of bounds, returns a space. + * + * @return The character at the specified location. + */ public char getChar(final int row, final int column) { - if (lines.size() <= row) + if (rows.size() <= row) return ' '; - return lines.get(row).getCharForLocation(column); + return rows.get(row).getCharForLocation(column); } + /** + * Returns the specified line. + * + * @param row The line number. + * @return The line. + */ public TextLine getLine(final int row) { ensureMaxTextLine(row); - return lines.get(row); + return rows.get(row); } + /** + * Returns the length of the specified line. + * + * @param row The line number. + * @return The length of the line. + */ public int getLineLength(final int row) { - if (lines.size() <= row) + if (rows.size() <= row) return 0; - return lines.get(row).getLength(); + return rows.get(row).getLength(); } + /** + * Returns the number of lines in the page. + * + * @return The number of lines in the page. + */ public int getLinesCount() { pack(); - return lines.size(); + return rows.size(); } + /** + * Returns the text of the page. + * + * @return The text of the page. + */ public String getText() { pack(); final StringBuilder result = new StringBuilder(); - for (final TextLine textLine : lines) { + for (final TextLine textLine : rows) { if (result.length() > 0) result.append("\n"); result.append(textLine.toString()); @@ -60,34 +89,48 @@ public class Page { } public void insertLine(final int row, final TextLine textLine) { - lines.add(row, textLine); + rows.add(row, textLine); } + /** + * Removes empty lines from the end of the page. + */ private void pack() { int newLength = 0; - for (int i = lines.size() - 1; i >= 0; i--) - if (!lines.get(i).isEmpty()) { + for (int i = rows.size() - 1; i >= 0; i--) + if (!rows.get(i).isEmpty()) { newLength = i + 1; break; } - if (newLength == lines.size()) + if (newLength == rows.size()) return; - lines = lines.subList(0, newLength); + rows = rows.subList(0, newLength); } + /** + * Removes the specified character from the page. + * + * @param row The line number. + * @param col The character number. + */ public void removeCharacter(final int row, final int col) { - if (lines.size() <= row) + if (rows.size() <= row) return; getLine(row).removeCharacter(col); } + /** + * Removes the specified line from the page. + * + * @param row The line number. + */ public void removeLine(final int row) { - if (lines.size() <= row) + if (rows.size() <= row) return; - lines.remove(row); + rows.remove(row); } }