Merge commit '7ce18848c8327967ce27b90abf2e280953530b5f'
[fanfix.git] / src / jexer / help / Link.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.help;
30
31 import java.util.HashSet;
32 import java.util.Set;
33 import java.util.ResourceBundle;
34
35 /**
36 * A Link is a section of text with a reference to a Topic.
37 */
38 public class Link {
39
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
43
44 /**
45 * The topic id that this link points to.
46 */
47 private String topic;
48
49 /**
50 * The text inside the link tag.
51 */
52 private String text;
53
54 /**
55 * The number of words in this link.
56 */
57 private int wordCount;
58
59 /**
60 * The word number (from the beginning of topic text) that corresponds to
61 * the first word of this link.
62 */
63 private int index;
64
65 // ------------------------------------------------------------------------
66 // Constructors -----------------------------------------------------------
67 // ------------------------------------------------------------------------
68
69 /**
70 * Public constructor.
71 *
72 * @param topic the topic to point to
73 * @param text the text inside the link tag
74 * @param index the word count index
75 */
76 public Link(final String topic, final String text, final int index) {
77 this.topic = topic;
78 this.text = text;
79 this.index = index;
80 this.wordCount = text.split("\\s+").length;
81 }
82
83 // ------------------------------------------------------------------------
84 // Link -------------------------------------------------------------------
85 // ------------------------------------------------------------------------
86
87 /**
88 * Get the topic.
89 *
90 * @return the topic
91 */
92 public String getTopic() {
93 return topic;
94 }
95
96 /**
97 * Get the link text.
98 *
99 * @return the text inside the link tag
100 */
101 public String getText() {
102 return text;
103 }
104
105 /**
106 * Get the word index for this link.
107 *
108 * @return the word number (from the beginning of topic text) that
109 * corresponds to the first word of this link
110 */
111 public int getIndex() {
112 return index;
113 }
114
115 /**
116 * Get the number of words in this link.
117 *
118 * @return the number of words in this link
119 */
120 public int getWordCount() {
121 return wordCount;
122 }
123
124 /**
125 * Generate a human-readable string for this widget.
126 *
127 * @return a human-readable string
128 */
129 @Override
130 public String toString() {
131 return String.format("%s(%8x) topic %s link text %s word # %d count %d",
132 getClass().getName(), hashCode(), topic, text, index, wordCount);
133 }
134
135 }