You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextPane.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.textpane;
  23. import com.dmdirc.FrameContainer;
  24. import java.awt.Point;
  25. import java.awt.Toolkit;
  26. import java.awt.datatransfer.StringSelection;
  27. import java.awt.event.AdjustmentEvent;
  28. import java.awt.event.AdjustmentListener;
  29. import java.awt.event.MouseEvent;
  30. import java.awt.event.MouseMotionAdapter;
  31. import java.awt.event.MouseMotionListener;
  32. import java.awt.event.MouseWheelEvent;
  33. import java.awt.event.MouseWheelListener;
  34. import javax.swing.BoundedRangeModel;
  35. import javax.swing.DefaultBoundedRangeModel;
  36. import javax.swing.JComponent;
  37. import javax.swing.JScrollBar;
  38. import net.miginfocom.swing.MigLayout;
  39. /**
  40. * Styled, scrollable text pane.
  41. */
  42. public final class TextPane extends JComponent implements AdjustmentListener,
  43. MouseWheelListener, IRCDocumentListener {
  44. /**
  45. * A version number for this class. It should be changed whenever the class
  46. * structure is changed (or anything else that would prevent serialized
  47. * objects being unserialized with the new class).
  48. */
  49. private static final long serialVersionUID = 5;
  50. /** Scrollbar model. */
  51. private final BoundedRangeModel scrollModel;
  52. /** Canvas object, used to draw text. */
  53. private final TextPaneCanvas canvas;
  54. /** IRCDocument. */
  55. private final IRCDocument document;
  56. /** Parent Frame. */
  57. private final FrameContainer frame;
  58. /**
  59. * Creates a new instance of TextPane.
  60. *
  61. * @param frame Parent Frame
  62. */
  63. public TextPane(final FrameContainer frame) {
  64. super();
  65. setUI(new TextPaneUI());
  66. this.frame = frame;
  67. document = new IRCDocument(frame.getConfigManager());
  68. frame.getConfigManager().addChangeListener("ui", "textPaneFontName",
  69. document);
  70. frame.getConfigManager().addChangeListener("ui", "textPaneFontSize",
  71. document);
  72. setLayout(new MigLayout("fill"));
  73. canvas = new TextPaneCanvas(this, document);
  74. add(canvas, "dock center");
  75. scrollModel = new DefaultBoundedRangeModel();
  76. scrollModel.setMaximum(document.getNumLines());
  77. scrollModel.setExtent(0);
  78. final JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL);
  79. scrollBar.setModel(scrollModel);
  80. add(scrollBar, "dock east");
  81. scrollBar.addAdjustmentListener(this);
  82. scrollBar.addAdjustmentListener(canvas);
  83. addMouseWheelListener(this);
  84. document.addIRCDocumentListener(this);
  85. setAutoscrolls(true);
  86. MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() {
  87. /** {@inheritDoc} */
  88. @Override
  89. public void mouseDragged(MouseEvent e) {
  90. if (e.getXOnScreen() > getLocationOnScreen().getX() && e.
  91. getXOnScreen() < (getLocationOnScreen().
  92. getX() + getWidth()) && e.getModifiersEx() ==
  93. MouseEvent.BUTTON1_DOWN_MASK) {
  94. if (getLocationOnScreen().getY() > e.getYOnScreen()) {
  95. scrollModel.setValue(scrollBar.getValue() - 1);
  96. } else if (getLocationOnScreen().getY() + getHeight() <
  97. e.getYOnScreen()) {
  98. scrollModel.setValue(scrollBar.getValue() + 1);
  99. }
  100. canvas.highlightEvent(MouseEventType.DRAG, e);
  101. }
  102. }
  103. };
  104. addMouseMotionListener(doScrollRectToVisible);
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void updateUI() {
  109. setUI(new TextPaneUI());
  110. }
  111. /**
  112. * Returns the last visible line in the textpane.
  113. *
  114. * @return Last visible line index
  115. */
  116. public int getLastVisibleLine() {
  117. return scrollModel.getValue();
  118. }
  119. /**
  120. * Sets the scrollbar's maximum position. If the current position is
  121. * within <code>linesAllowed</code> of the end of the document, the
  122. * scrollbar's current position is set to the end of the document.
  123. *
  124. * @param linesAllowed The number of lines allowed below the current position
  125. * @since 0.6
  126. */
  127. protected void setScrollBarMax(final int linesAllowed) {
  128. final int lines = document.getNumLines() - 1;
  129. final int currentLine = scrollModel.getValue();
  130. final int allowedDeviation = lines - linesAllowed;
  131. scrollModel.setMaximum(lines);
  132. boolean setToMax = currentLine == allowedDeviation;
  133. if (allowedDeviation == -1) {
  134. setToMax = true;
  135. }
  136. if (!scrollModel.getValueIsAdjusting() && setToMax) {
  137. scrollModel.setValue(lines);
  138. }
  139. canvas.recalc();
  140. }
  141. /**
  142. * Sets the new position for the scrollbar and the associated position
  143. * to render the text from.
  144. * @param position new position of the scrollbar
  145. */
  146. public void setScrollBarPosition(final int position) {
  147. scrollModel.setValue(position);
  148. }
  149. /**
  150. * {@inheritDoc}
  151. *
  152. * @param e Mouse wheel event
  153. */
  154. @Override
  155. public void adjustmentValueChanged(final AdjustmentEvent e) {
  156. scrollModel.setValue(e.getValue());
  157. }
  158. /**
  159. * {@inheritDoc}
  160. *
  161. * @param e Mouse wheel event
  162. */
  163. @Override
  164. public void mouseWheelMoved(final MouseWheelEvent e) {
  165. if (e.getWheelRotation() > 0) {
  166. scrollModel.setValue(scrollModel.getValue() + e.getScrollAmount());
  167. } else {
  168. scrollModel.setValue(scrollModel.getValue() - e.getScrollAmount());
  169. }
  170. }
  171. /**
  172. *
  173. * Returns the line information from a mouse click inside the textpane.
  174. *
  175. * @param point mouse position
  176. *
  177. * @return line number, line part, position in whole line
  178. */
  179. public LineInfo getClickPosition(final Point point) {
  180. return canvas.getClickPosition(point);
  181. }
  182. /**
  183. * Returns the selected text.
  184. *
  185. * <li>0 = start line</li>
  186. * <li>1 = start char</li>
  187. * <li>2 = end line</li>
  188. * <li>3 = end char</li>
  189. *
  190. * @return Selected text
  191. */
  192. public String getSelectedText() {
  193. final StringBuffer selectedText = new StringBuffer();
  194. final LinePosition selectedRange = canvas.getSelectedRange();
  195. if (selectedRange.getStartLine() == -1) {
  196. return null;
  197. }
  198. for (int i = selectedRange.getStartLine(); i <=
  199. selectedRange.getEndLine();
  200. i++) {
  201. if (i != selectedRange.getStartLine()) {
  202. selectedText.append('\n');
  203. }
  204. if (document.getNumLines() <= i) {
  205. return selectedText.toString();
  206. }
  207. final String line = document.getLine(i).getText();
  208. if (!line.isEmpty()) {
  209. if (selectedRange.getEndLine() == selectedRange.getStartLine()) {
  210. //loop through range
  211. if (selectedRange.getStartPos() != -1 && selectedRange.
  212. getEndPos() != -1) {
  213. selectedText.append(line.substring(
  214. selectedRange.getStartPos(),
  215. selectedRange.getEndPos()));
  216. }
  217. } else if (i == selectedRange.getStartLine()) {
  218. //loop from start of range to the end
  219. if (selectedRange.getStartPos() != -1) {
  220. selectedText.append(line.substring(
  221. selectedRange.getStartPos(), line.length()));
  222. }
  223. } else if (i == selectedRange.getEndLine()) {
  224. //loop from start to end of range
  225. if (selectedRange.getEndPos() != -1) {
  226. selectedText.append(line.substring(0, selectedRange.
  227. getEndPos()));
  228. }
  229. } else {
  230. //loop the whole line
  231. selectedText.append(line);
  232. }
  233. }
  234. }
  235. return selectedText.toString();
  236. }
  237. /**
  238. * Returns the selected range.
  239. *
  240. * @return selected range
  241. */
  242. public LinePosition getSelectedRange() {
  243. return canvas.getSelectedRange();
  244. }
  245. /**
  246. * Returns whether there is a selected range.
  247. *
  248. * @return true iif there is a selected range
  249. */
  250. public boolean hasSelectedRange() {
  251. final LinePosition selectedRange = canvas.getSelectedRange();
  252. return !(selectedRange.getStartLine() == selectedRange.getEndLine() &&
  253. selectedRange.getStartPos() == selectedRange.getEndPos());
  254. }
  255. /**
  256. * Selects the specified region of text.
  257. *
  258. * @param position Line position
  259. */
  260. public void setSelectedTexT(final LinePosition position) {
  261. canvas.setSelectedRange(position);
  262. }
  263. /**
  264. * Returns the type of text this click represents.
  265. *
  266. * @param lineInfo Line info of click.
  267. *
  268. * @return Click type for specified position
  269. */
  270. public ClickType getClickType(final LineInfo lineInfo) {
  271. return canvas.getClickType(lineInfo);
  272. }
  273. /**
  274. * Returns the surrouding word at the specified position.
  275. *
  276. * @param lineNumber Line number to get word from
  277. * @param index Position to get surrounding word
  278. *
  279. * @return Surrounding word
  280. */
  281. public String getWordAtIndex(final int lineNumber, final int index) {
  282. if (lineNumber == -1) {
  283. return "";
  284. }
  285. final int[] indexes =
  286. canvas.getSurroundingWordIndexes(document.getLine(lineNumber).
  287. getText(),
  288. index);
  289. return document.getLine(lineNumber).getText().substring(indexes[0],
  290. indexes[1]);
  291. }
  292. /**
  293. * Returns the atrriute value for the specified location.
  294. *
  295. * @param lineInfo Specified location
  296. *
  297. * @return Specified value
  298. */
  299. public Object getAttributeValueAtPoint(LineInfo lineInfo) {
  300. return canvas.getAttributeValueAtPoint(lineInfo);
  301. }
  302. /** Adds the selected text to the clipboard. */
  303. public void copy() {
  304. if (getSelectedText() != null && !getSelectedText().isEmpty()) {
  305. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
  306. new StringSelection(getSelectedText()), null);
  307. }
  308. }
  309. /** Clears the textpane. */
  310. public void clear() {
  311. document.clear();
  312. scrollModel.setValue(0);
  313. setScrollBarMax(1);
  314. canvas.recalc();
  315. }
  316. /** Clears the selection. */
  317. public void clearSelection() {
  318. canvas.clearSelection();
  319. }
  320. /**
  321. * Trims the document to the specified number of lines.
  322. *
  323. * @param numLines Number of lines to trim the document to
  324. */
  325. public void trim(final int numLines) {
  326. if (document.getNumLines() < numLines) {
  327. return;
  328. }
  329. final int trimmedLines = document.getNumLines() - numLines;
  330. final LinePosition selectedRange = getSelectedRange();
  331. selectedRange.setStartLine(selectedRange.getStartLine() - trimmedLines);
  332. selectedRange.setEndLine(selectedRange.getEndLine() - trimmedLines);
  333. if (selectedRange.getStartLine() < 0) {
  334. selectedRange.setStartLine(0);
  335. }
  336. if (selectedRange.getEndLine() < 0) {
  337. selectedRange.setEndLine(0);
  338. }
  339. setSelectedTexT(selectedRange);
  340. document.trim(numLines);
  341. }
  342. /** Scrolls one page up in the textpane. */
  343. public void pageDown() {
  344. scrollModel.setValue(scrollModel.getValue() + 10);
  345. }
  346. /** Scrolls one page down in the textpane. */
  347. public void pageUp() {
  348. scrollModel.setValue(scrollModel.getValue() - 10);
  349. }
  350. /** {@inheritDoc}. */
  351. @Override
  352. public void lineAdded(final int line, final int size) {
  353. setScrollBarMax(1);
  354. }
  355. /** {@inheritDoc}. */
  356. @Override
  357. public void trimmed(final int numLines) {
  358. setScrollBarMax(1);
  359. }
  360. /** {@inheritDoc}. */
  361. @Override
  362. public void cleared() {
  363. canvas.recalc();
  364. }
  365. /** {@inheritDoc}. */
  366. @Override
  367. public void linesAdded(int line, int length, int size) {
  368. setScrollBarMax(length);
  369. }
  370. /** {@inheritDoc}. */
  371. @Override
  372. public void repaintNeeded() {
  373. canvas.recalc();
  374. }
  375. /**
  376. * Retrieves this textpane's IRCDocument.
  377. *
  378. * @return This textpane's IRC document
  379. */
  380. public IRCDocument getDocument() {
  381. return document;
  382. }
  383. /**
  384. * Retrives the parent framecontainer for this textpane.
  385. *
  386. * @return Parent frame container
  387. */
  388. public FrameContainer getFrameContainer() {
  389. return frame;
  390. }
  391. }