1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.tagcloud.cavallo;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Locale;
21
22 import org.codehaus.plexus.util.StringUtils;
23 import org.mcavallo.opencloud.Cloud;
24 import org.mcavallo.opencloud.Cloud.Case;
25 import org.mcavallo.opencloud.Tag;
26 import org.mcavallo.opencloud.filters.TagFilter;
27
28 import de.smartics.tagcloud.TagCloud;
29
30
31
32
33
34
35
36
37 public class CavalloCloudAdapter implements TagCloud
38 {
39
40
41
42
43
44
45
46
47
48 private final Cloud adaptedCloud;
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public CavalloCloudAdapter(final Locale locale, final int maxTagsToDisplay,
63 final double minWeight, final double maxWeight)
64 {
65 this.adaptedCloud = new Cloud(locale);
66 if (maxTagsToDisplay > 0)
67 {
68 this.adaptedCloud.setMaxTagsToDisplay(maxTagsToDisplay);
69 }
70 this.adaptedCloud.setMinWeight(minWeight);
71 this.adaptedCloud.setMaxWeight(maxWeight);
72 this.adaptedCloud.setTagCase(Case.PRESERVE_CASE);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 public void addTag(final String tag)
90 {
91 adaptedCloud.addTag(tag);
92 }
93
94
95
96
97
98
99 @Override
100 public String toHtml()
101 {
102 final StringBuilder buffer = new StringBuilder(1024);
103 buffer.append("<div class='tag-cloud'>\n");
104 for (final Tag tag : adaptedCloud.tags())
105 {
106 final String link = tag.getLink();
107 final boolean linkProvided = StringUtils.isNotBlank(link);
108 if (linkProvided)
109 {
110 buffer.append("<a href='").append(link).append('\'');
111 }
112 else
113 {
114 buffer.append("<span");
115 }
116
117 buffer.append(" style='font-size: ").append(Math.round(tag.getNormScore() * tag.getWeight()))
118 .append("px;'>").append(tag.getName());
119
120 if (linkProvided)
121 {
122 buffer.append("</a>\n");
123 }
124 else
125 {
126 buffer.append("</span>\n");
127 }
128 }
129 buffer.append("</div>");
130
131 return buffer.toString();
132 }
133
134
135
136
137
138
139 public void addOutputFilter(final TagFilter filter)
140 {
141 adaptedCloud.addOutputFilter(filter);
142 }
143
144
145
146
147
148
149 @Override
150 public void clear()
151 {
152 adaptedCloud.clear();
153 }
154
155
156
157
158
159
160 @Override
161 public List<de.smartics.tagcloud.Tag> getTags()
162 {
163 final List<Tag> adaptedTags = adaptedCloud.tags();
164 final List<de.smartics.tagcloud.Tag> tags =
165 new ArrayList<de.smartics.tagcloud.Tag>(adaptedTags.size());
166 for (final Tag cavalloTag : adaptedTags)
167 {
168 final de.smartics.tagcloud.Tag tag =
169 new de.smartics.tagcloud.Tag(cavalloTag.getName(),
170 cavalloTag.getScoreInt(), cavalloTag.getWeightInt());
171 tags.add(tag);
172 }
173 return tags;
174 }
175
176
177
178
179
180 }