/** * Returns the current vertical cursor position. * * @return new position */ int cursorY() { final int hh = h; h = Integer.MAX_VALUE; final Graphics g = getGraphics(); init(g, 0); while (more(g) && !text.edited()) next(); h = hh; return y - fontH; }
/** Calculates the text height. */ void calc() { w = getWidth() - (off >> 1); h = Integer.MAX_VALUE; final Graphics g = getGraphics(); init(g, 0); while (more(g)) next(); h = getHeight() + fontH; bar.height(y + off); }
@Override public Dimension getPreferredSize() { final Graphics g = getGraphics(); w = Integer.MAX_VALUE; h = Integer.MAX_VALUE; init(g, 0); int max = 0; while (more(g)) { if (text.curr() == 0x0A) max = Math.max(x, max); next(); } return new Dimension(Math.max(x, max) + fwidth[' '], y + fontH); }
/** * Selects the text at the specified position. * * @param pos current text position * @param p mouse position * @param finish states if selection is in progress */ void select(final int pos, final Point p, final boolean finish) { if (!finish) text.noMark(); p.y -= 3; final Graphics g = getGraphics(); init(g, pos); if (p.y > y - fontH) { int s = text.pos(); while (true) { // end of line if (p.x > x && p.y < y - fontH) { text.pos(s); break; } // end of text - skip last characters if (!more(g)) { while (text.more()) text.next(); break; } // beginning of line if (p.x <= x && p.y < y) break; // middle of line if (p.x > x && p.x <= x + wordW && p.y > y - fontH && p.y <= y) { while (text.more()) { final int ww = charW(g, text.curr()); if (p.x < x + ww) break; x += ww; text.next(); } break; } s = text.pos(); next(); } if (!finish) text.startMark(); else text.endMark(); text.setCaret(); } repaint(); }
/** * Finds the current keyword and returns the text position. * * @param forward backward browsing * @param same string is the same as last time * @return new position */ int find(final boolean forward, final boolean same) { if (keyword.isEmpty()) return 0; while (true) { final int hh = h; int lp = 0; int ly = 0; int cp = text.cursor(); h = Integer.MAX_VALUE; final Graphics g = getGraphics(); for (init(g, 0); more(g); next()) { if (!found()) continue; final int np = text.pos(); final int ny = y - fontH; if (np >= cp && (np > cp || !same || !forward)) { if (forward || lp != 0) { h = hh; text.setCaret(forward ? np : lp); return forward ? ny : ly; } cp = Integer.MAX_VALUE; } lp = np; ly = ny; } h = hh; if (cp == 0 || cp == Integer.MAX_VALUE) { text.setCaret(lp); return ly; } text.setCaret(0); } }
/** * Writes the current string to the graphics reference. * * @param g graphics reference */ private void write(final Graphics g) { if (high) { high = false; } else { color = isEnabled() ? syntax.getColor(text) : Color.gray; } final int ch = text.curr(); if (y > 0 && y < h) { if (ch == TokenBuilder.MARK) { color = GUIConstants.GREEN; high = true; } // mark error if (text.erroneous()) { g.setColor(GUIConstants.LRED); g.fillRect(x, y - fontH + 4, wordW, fontH); } // mark text int xx = x; if (text.markStart()) { final int p = text.pos(); while (text.more()) { final int cw = charW(g, text.curr()); if (text.inMark()) { g.setColor(GUIConstants.color(3)); g.fillRect(xx, y - fontH + 4, cw, fontH); } xx += cw; text.next(); } text.pos(p); } if (found()) { int cw = 0; for (int c = 0; c < keyword.length(); ++c) { cw += charW(g, keyword.charAt(c)); } g.setColor(GUIConstants.color(text.cursor() == text.pos() ? 5 : 2)); g.fillRect(x, y - fontH + 4, cw, fontH); } // don't write whitespaces if (ch > ' ') { g.setColor(color); String n = text.nextWord(); int ww = w - x; if (x + wordW > ww) { // shorten string if it cannot be completely shown (saves memory) int c = 0; for (final int nl = n.length(); c < nl && ww > 0; c++) { ww -= charW(g, n.charAt(c)); } n = n.substring(0, c); } g.drawString(n, x, y); } else if (ch <= TokenBuilder.MARK) { g.setFont(font); } // show cursor if (cursor && text.edited()) { xx = x; final int p = text.pos(); while (text.more()) { if (text.cursor() == text.pos()) { cursor(g, xx); break; } xx += charW(g, text.next()); } text.pos(p); } } next(); }