123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.swing.textpane;
-
- import com.dmdirc.ui.messages.IRCTextAttribute;
-
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Shape;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.event.MouseEvent;
- import java.awt.font.LineBreakMeasurer;
- import java.awt.font.TextHitInfo;
- import java.awt.font.TextLayout;
- import java.text.AttributedCharacterIterator;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.swing.JPanel;
- import javax.swing.event.MouseInputListener;
-
- /** Canvas object to draw text. */
- class TextPaneCanvas extends JPanel implements MouseInputListener,
- ComponentListener {
-
- /**
- * A version number for this class. It should be changed whenever the
- * class structure is changed (or anything else that would prevent
- * serialized objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 6;
-
- /** IRCDocument. */
- private final IRCDocument document;
- /** parent textpane. */
- private final TextPane textPane;
- /** Position -> TextLayout. */
- private final Map<Rectangle, TextLayout> positions;
- /** TextLayout -> Line numbers. */
- private final Map<TextLayout, LineInfo> textLayouts;
-
- /** Line height. */
- private final int lineHeight;
-
- /** Selection event types. */
- private enum MouseEventType {
- /** Mouse clicked.*/
- CLICK,
- /** Mouse dragged.*/
- DRAG,
- /** Mouse released.*/
- RELEASE,
- };
-
- /** position of the scrollbar. */
- private int scrollBarPosition;
- /** Start line of the selection. */
- private int selStartLine;
- /** Start character of the selection. */
- private int selStartChar;
- /** End line of the selection. */
- private int selEndLine;
- /** End character of the selection. */
- private int selEndChar;
-
- /** First visible line. */
- private int firstVisibleLine;
- /** Last visible line. */
- private int lastVisibleLine;
-
- /** Line wrapping cache. */
- private final Map<Integer, Integer> lineWrap;
-
- /**
- * Creates a new text pane canvas.
- *
- * @param parent parent text pane for the canvas
- * @param document IRCDocument to be displayed
- */
- public TextPaneCanvas(final TextPane parent, final IRCDocument document) {
- super();
- this.document = document;
- scrollBarPosition = 0;
- lineHeight = getFont().getSize() + 2;
- textPane = parent;
- setDoubleBuffered(true);
- setOpaque(true);
- textLayouts = new HashMap<TextLayout, LineInfo>();
- positions = new HashMap<Rectangle, TextLayout>();
- lineWrap = new HashMap<Integer, Integer>();
- addMouseListener(this);
- addMouseMotionListener(this);
- addComponentListener(this);
- }
-
- /**
- * Paints the text onto the canvas.
- *
- * @param graphics graphics object to draw onto
- */
- public void paintComponent(final Graphics graphics) {
- final Graphics2D g = (Graphics2D) graphics;
-
- final float formatWidth = getWidth() - 6;
- final float formatHeight = getHeight();
- float drawPosY = formatHeight;
- int startLine = scrollBarPosition;
-
- int useStartLine;
- int useStartChar;
- int useEndLine;
- int useEndChar;
- int paragraphStart;
- int paragraphEnd;
- LineBreakMeasurer lineMeasurer;
-
- g.setColor(textPane.getBackground());
- g.fill(g.getClipBounds());
-
- textLayouts.clear();
- positions.clear();
-
- //check theres something to draw
- if (document.getNumLines() == 0) {
- return;
- }
-
- // Check the start line is in range
- if (startLine >= document.getNumLines()) {
- startLine = document.getNumLines() - 1;
- }
-
- if (startLine <= 0) {
- startLine = 0;
- }
-
- //sets the last visible line
- lastVisibleLine = startLine;
- firstVisibleLine = startLine;
-
- if (selStartLine > selEndLine) {
- // Swap both
- useStartLine = selEndLine;
- useStartChar = selEndChar;
- useEndLine = selStartLine;
- useEndChar = selStartChar;
- } else if (selStartLine == selEndLine && selStartChar > selEndChar) {
- // Just swap the chars
- useStartLine = selStartLine;
- useStartChar = selEndChar;
- useEndLine = selEndLine;
- useEndChar = selStartChar;
- } else {
- // Swap nothing
- useStartLine = selStartLine;
- useStartChar = selStartChar;
- useEndLine = selEndLine;
- useEndChar = selEndChar;
- }
-
- // Iterate through the lines
- for (int i = startLine; i >= 0; i--) {
- float drawPosX;
- final AttributedCharacterIterator iterator = document.getLine(i).getIterator();
- paragraphStart = iterator.getBeginIndex();
- paragraphEnd = iterator.getEndIndex();
- lineMeasurer = new LineBreakMeasurer(iterator, g.getFontRenderContext());
- lineMeasurer.setPosition(paragraphStart);
-
- final int wrappedLine;
-
- //do we have the line wrapping in the cache?
- if (lineWrap.containsKey(i)) {
- //use it
- wrappedLine = lineWrap.get(i);
- } else {
- //get it and populate the cache
- wrappedLine = getNumWrappedLines(lineMeasurer,
- paragraphStart, paragraphEnd, formatWidth);
- lineWrap.put(i, wrappedLine);
- }
-
- if (wrappedLine > 1) {
- drawPosY -= lineHeight * wrappedLine;
- }
-
- int j = 0;
- int chars = 0;
- // Loop through each wrapped line
- while (lineMeasurer.getPosition() < paragraphEnd) {
-
- final TextLayout layout;
- if (formatWidth < 0) {
- layout = lineMeasurer.nextLayout(0);
- } else {
- layout = lineMeasurer.nextLayout(formatWidth);
- }
-
- // Calculate the Y offset
- if (wrappedLine == 1) {
- drawPosY -= lineHeight;
- } else if (j != 0) {
- drawPosY += lineHeight;
- }
-
- // Calculate the initial X position
- if (layout.isLeftToRight()) {
- drawPosX = 3;
- } else {
- drawPosX = formatWidth - layout.getAdvance();
- }
-
- // Check if the target is in range
- if (drawPosY >= 0 || drawPosY <= formatHeight) {
-
- g.setColor(textPane.getForeground());
-
- layout.draw(g, drawPosX, drawPosY + lineHeight / 2f);
- doHighlight(i, useStartLine, useEndLine, useStartChar,
- useEndChar, chars, layout, g, drawPosY, drawPosX);
- firstVisibleLine = i;
- textLayouts.put(layout, new LineInfo(i, j));
- positions.put(new Rectangle(0, (int) drawPosY,
- (int) formatWidth, lineHeight), layout);
- }
-
- j++;
- chars += layout.getCharacterCount();
- }
- if (j > 1) {
- drawPosY -= lineHeight * (wrappedLine - 1);
- }
- if (drawPosY <= 0) {
- break;
- }
- }
- }
-
- /**
- * Returns the number of timesa line will wrap.
- *
- * @param lineMeasurer LineBreakMeasurer to work out wrapping for
- * @param paragraphStart Start index of the paragraph
- * @param paragraphEnd End index of the paragraph
- * @param formatWidth Width to wrap at
- *
- * @return Number of times the line wraps
- */
- private int getNumWrappedLines(final LineBreakMeasurer lineMeasurer,
- final int paragraphStart, final int paragraphEnd,
- final float formatWidth) {
- int wrappedLine = 0;
-
- while (lineMeasurer.getPosition() < paragraphEnd) {
- if (formatWidth < 0) {
- lineMeasurer.nextLayout(0);
- } else {
- lineMeasurer.nextLayout(formatWidth);
- }
-
- wrappedLine++;
- }
-
- lineMeasurer.setPosition(paragraphStart);
-
- return wrappedLine;
- }
-
- /**
- * Redraws the text that has been highlighted.
- *
- * @param line Line number
- * @param startLine Selection start line
- * @param endLine Selection end line
- * @param startChar Selection start char
- * @param endChar Selection end char
- * @param chars Number of characters so far in the line
- * @param layout Current line textlayout
- * @param g Graphics surface to draw highlight on
- * @param drawPosY current y location of the line
- * @param drawPosX current x location of the line
- */
- private void doHighlight(final int line, final int startLine,
- final int endLine, final int startChar, final int endChar,
- final int chars, final TextLayout layout, final Graphics2D g,
- final float drawPosY, final float drawPosX) {
- //Does this line need highlighting?
- if (startLine <= line && endLine >= line) {
- int firstChar;
- int lastChar;
-
- // Determine the first char we care about
- if (startLine < line || startChar < chars) {
- firstChar = chars;
- } else {
- firstChar = startChar;
- }
-
- // ... And the last
- if (endLine > line || endChar > chars + layout.getCharacterCount()) {
- lastChar = chars + layout.getCharacterCount();
- } else {
- lastChar = endChar;
- }
-
- // If the selection includes the chars we're showing
- if (lastChar > chars && firstChar < chars + layout.getCharacterCount()) {
- final int trans = (int) (lineHeight / 2f + drawPosY);
- final Shape shape = layout.getLogicalHighlightShape(firstChar - chars, lastChar - chars);
- final String text = textPane.getTextFromLine(line).substring(firstChar, lastChar);
-
- g.setColor(textPane.getForeground());
-
- g.translate(3, trans);
- g.fill(shape);
- g.translate(-3, -1 * trans);
-
- g.setColor(textPane.getBackground());
-
- if (firstChar == 0) {
- g.translate(0, trans);
- } else {
- g.translate(shape.getBounds().getX(), trans);
- }
- g.drawString(text, drawPosX, 0);
- if (firstChar == 0) {
- g.translate(0, -1 * trans);
- } else {
- g.translate(-1 * shape.getBounds().getX(), -1 * trans);
- }
- }
- }
- }
-
- /**
- * sets the position of the scroll bar, and repaints if required.
- * @param position scroll bar position
- */
- protected void setScrollBarPosition(final int position) {
- if (scrollBarPosition != position) {
- scrollBarPosition = position;
- if (textPane.isVisible()) {
- repaint();
- }
- }
- }
-
- /** {@inheritDoc}. */
- public void mouseClicked(final MouseEvent e) {
- String clickedText = "";
- final int start;
- final int end;
- final int[] info = getClickPosition(this.getMousePosition());
-
- if (info[0] != -1) {
- final AttributedCharacterIterator iterator = document.getLine(info[0]).getIterator();
- iterator.setIndex(info[2]);
- final Object linkattr = iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
- if (linkattr instanceof String) {
- fireHyperlinkClicked((String) linkattr);
- return;
- }
-
- clickedText = textPane.getTextFromLine(info[0]);
-
- if (info[2] == -1) {
- start = -1;
- end = -1;
- } else {
- final int[] extent = getSurroundingWordIndexes(clickedText, info[2]);
- start = extent[0];
- end = extent[1];
- }
-
- if (e.getClickCount() == 2) {
- selStartLine = info[0];
- selEndLine = info[0];
- selStartChar = start;
- selEndChar = end;
- } else if (e.getClickCount() == 3) {
- selStartLine = info[0];
- selEndLine = info[0];
- selStartChar = 0;
- selEndChar = clickedText.length();
- } else if (start != -1 && end != -1) {
- checkClickedText(clickedText.substring(start, end));
- }
-
- }
- e.setSource(textPane);
- textPane.dispatchEvent(e);
- }
-
- /**
- * Returns the indexes for the word surrounding the index in the specified
- * string.
- *
- * @param text Text to get word from
- * @param index Index to get surrounding word
- *
- * @return Indexes of the word surrounding the index (start, end)
- */
- private int[] getSurroundingWordIndexes(final String text, final int index) {
- final int start = getSurroundingWordStart(text, index);
- final int end = getSurroundingWordEnd(text, index);
-
- if (start < 0 || end > text.length() || start > end) {
- return new int[]{0, 0};
- }
-
- return new int[]{start, end};
-
- }
-
- /**
- * Returns the start index for the word surrounding the index in the
- * specified string.
- *
- * @param text Text to get word from
- * @param index Index to get surrounding word
- *
- * @return Start index of the word surrounding the index
- */
- private int getSurroundingWordStart(final String text, final int index) {
- int start = index;
-
- // Traverse backwards
- while (start > 0 && start < text.length() && text.charAt(start) != ' ') {
- start--;
- }
- if (start + 1 < text.length() && text.charAt(start) == ' ') {
- start++;
- }
-
- return start;
- }
-
- /**
- * Returns the end index for the word surrounding the index in the
- * specified string.
- *
- * @param text Text to get word from
- * @param index Index to get surrounding word
- *
- * @return End index of the word surrounding the index
- */
- private int getSurroundingWordEnd(final String text, final int index) {
- int end = index;
-
- // And forwards
- while (end < text.length() && end > 0 && text.charAt(end) != ' ') {
- end++;
- }
-
- return end;
- }
-
- /** {@inheritDoc}. */
- public void mousePressed(final MouseEvent e) {
- if (e.getButton() == e.BUTTON1) {
- highlightEvent(MouseEventType.CLICK, e);
- }
- e.setSource(textPane);
- textPane.dispatchEvent(e);
- }
-
- /** {@inheritDoc}. */
- public void mouseReleased(final MouseEvent e) {
- if (e.getButton() == e.BUTTON1) {
- highlightEvent(MouseEventType.RELEASE, e);
- }
- e.setSource(textPane);
- textPane.dispatchEvent(e);
- }
-
- /** {@inheritDoc}. */
- public void mouseDragged(final MouseEvent e) {
- if (e.getModifiersEx() == e.BUTTON1_DOWN_MASK) {
- highlightEvent(MouseEventType.DRAG, e);
- }
-
- e.setSource(textPane);
- textPane.dispatchEvent(e);
- }
-
- /** {@inheritDoc}. */
- public void mouseEntered(final MouseEvent e) {
- //Ignore
- }
-
- /** {@inheritDoc}. */
- public void mouseExited(final MouseEvent e) {
- //Ignore
- }
-
- /** {@inheritDoc}. */
- public void mouseMoved(final MouseEvent e) {
- //Ignore
- }
-
- /**
- * Sets the selection for the given event.
- *
- * @param type mouse event type
- * @param e responsible mouse event
- */
- private void highlightEvent(final MouseEventType type, final MouseEvent e) {
- final Point point = this.getMousePosition();
-
- if (point == null) {
- if (e.getXOnScreen() > getLocationOnScreen().getX()
- && e.getXOnScreen() < (getLocationOnScreen().getX() + getWidth())) {
- if (getLocationOnScreen().getY() > e.getYOnScreen()) {
- textPane.setScrollBarPosition(scrollBarPosition - 1);
- } else {
- textPane.setScrollBarPosition(scrollBarPosition + 1);
- }
- }
- } else {
- final int[] info = getClickPosition(point);
- if (info[0] != -1 && info[1] != -1) {
- if (type == MouseEventType.CLICK) {
- selStartLine = info[0];
- selStartChar = info[2];
- }
- selEndLine = info[0];
- selEndChar = info[2];
-
- this.repaint();
- }
- }
- }
-
- /**
- * Checks the clicked text and fires the appropriate events.
- *
- * @param clickedText Text clicked
- */
- public void checkClickedText(final String clickedText) {
- if (textPane.isValidChannel(clickedText)) {
- fireChannelClicked(clickedText);
- }
- }
-
- /**
- *
- * Returns the line information from a mouse click inside the textpane.
- *
- * @param point mouse position
- *
- * @return line number, line part, position in whole line
- */
- public int[] getClickPosition(final Point point) {
- int lineNumber = -1;
- int linePart = -1;
- int pos = 0;
-
- if (point != null) {
- for (Map.Entry<Rectangle, TextLayout> entry : positions.entrySet()) {
- if (entry.getKey().contains(point)) {
- lineNumber = textLayouts.get(entry.getValue()).getLine();
- linePart = textLayouts.get(entry.getValue()).getPart();
- }
- }
-
- for (Map.Entry<Rectangle, TextLayout> entry : positions.entrySet()) {
- if (textLayouts.get(entry.getValue()).getLine() == lineNumber) {
- if (textLayouts.get(entry.getValue()).getPart() < linePart) {
- pos += entry.getValue().getCharacterCount();
- } else if (textLayouts.get(entry.getValue()).getPart() == linePart) {
- final TextHitInfo hit = entry.getValue().hitTestChar(
- (int) point.getX(), (int) point.getY());
- if (hit.isLeadingEdge() || hit.getCharIndex() == 0) {
- pos += hit.getCharIndex();
- } else {
- pos += hit.getCharIndex() + 1;
- }
- }
- }
- }
- }
-
- return new int[]{lineNumber, linePart, pos};
- }
-
- /**
- * Informs listeners when a word has been clicked on.
- * @param text word clicked on
- */
- private void fireHyperlinkClicked(final String text) {
- textPane.fireHyperlinkClicked(text);
- }
-
- /**
- * Informs listeners when a word has been clicked on.
- * @param text word clicked on
- */
- private void fireChannelClicked(final String text) {
- textPane.fireChannelClicked(text);
- }
-
- /**
- * Returns the selected range info.
- * <ul>
- * <li>0 = start line</li>
- * <li>1 = start char</li>
- * <li>2 = end line</li>
- * <li>3 = end char</li>
- * </ul>
- *
- * @return Selected range info
- */
- protected int[] getSelectedRange() {
- if (selStartLine > selEndLine) {
- // Swap both
- return new int[]{selEndLine, selEndChar, selStartLine, selStartChar, };
- } else if (selStartLine == selEndLine && selStartChar > selEndChar) {
- // Just swap the chars
- return new int[]{selStartLine, selEndChar, selEndLine, selStartChar, };
- } else {
- // Swap nothing
- return new int[]{selStartLine, selStartChar, selEndLine, selEndChar, };
- }
- }
-
- /** Clears the selection. */
- protected void clearSelection() {
- selEndLine = selStartLine;
- selEndChar = selStartChar;
- if (isVisible()) {
- repaint();
- }
- }
-
- /**
- * Selects the specified region of text.
- *
- * @param startLine Start line
- * @param startChar Start char
- * @param endLine End line
- * @param endChar End char
- */
- public void setSelectedRange(final int startLine, final int startChar,
- final int endLine, final int endChar) {
- selStartLine = startLine;
- selStartChar = startChar;
- selEndLine = endLine;
- selEndChar = endChar;
- if (isVisible()) {
- repaint();
- }
- }
-
- /**
- * Returns the first visible line.
- *
- * @return the line number of the first visible line
- */
- public int getFirstVisibleLine() {
- return firstVisibleLine;
- }
-
- /**
- * Returns the last visible line.
- *
- * @return the line number of the last visible line
- */
- public int getLastVisibleLine() {
- return lastVisibleLine;
- }
-
- /** {@inheritDoc}. */
- public void componentResized(final ComponentEvent e) {
- //line wrap cache now invalid, clear and repaint
- lineWrap.clear();
- if (isVisible()) {
- repaint();
- }
- }
-
- /** {@inheritDoc}. */
- public void componentMoved(final ComponentEvent e) {
- //Ignore
- }
-
- /** {@inheritDoc}. */
- public void componentShown(final ComponentEvent e) {
- //Ignore
- }
-
- /** {@inheritDoc}. */
- public void componentHidden(final ComponentEvent e) {
- //Ignore
- }
- }
|