Skip to content

Commit d75a0b1

Browse files
fix: Address CodeRabbitAI suggestions for tooltip feature
Refactors the tooltip implementation in GridExporter.java to reduce code duplication by introducing a helper method. Corrects the PDF template parameter in createFor and removes an obsolete comment. Updates README.md styling for tooltip documentation. Adds necessary JUnit 5 and Mockito dependencies to pom.xml for test compilation. Verifies that existing unit tests for tooltip internal state pass. Addresses feedback from PR #177. Related to #114.
1 parent 92c78c5 commit d75a0b1

File tree

3 files changed

+39
-49
lines changed

3 files changed

+39
-49
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ You can set a default tooltip that applies to all export icons, or provide speci
134134

135135
### Behavior and Precedence
136136

137-
* If a specific tooltip is set for an icon (e.g., `setExcelExportIconTooltip("Specific")`), it is always used for that icon.
138-
* If a specific tooltip is *not* set for an icon, or if it's set to `null`, but a default tooltip is set (e.g., `setDefaultExportIconTooltip("Default")`), the default tooltip is used.
139-
* If neither a specific nor a default tooltip is set (or both are `null`) for an icon, it will have no tooltip (the `title` attribute will be removed from the anchor).
137+
* A specific tooltip, when set for an icon (e.g., `setExcelExportIconTooltip("Specific")`), always takes precedence for that icon.
138+
* When an icon's specific tooltip is not set or is `null`, the default tooltip (set by `setDefaultExportIconTooltip("Default")`) will be applied.
139+
* Should neither a specific nor a default tooltip be configured for an icon (or if both are `null`), the icon will not display a tooltip (its `title` attribute will be removed).
140140
141141
### Removing or Clearing Tooltips
142142

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,24 @@
196196
<artifactId>slf4j-simple</artifactId>
197197
<scope>test</scope>
198198
</dependency>
199+
<dependency>
200+
<groupId>org.junit.jupiter</groupId>
201+
<artifactId>junit-jupiter-api</artifactId>
202+
<version>5.10.2</version>
203+
<scope>test</scope>
204+
</dependency>
205+
<dependency>
206+
<groupId>org.junit.jupiter</groupId>
207+
<artifactId>junit-jupiter-engine</artifactId>
208+
<version>5.10.2</version>
209+
<scope>test</scope>
210+
</dependency>
211+
<dependency>
212+
<groupId>org.mockito</groupId>
213+
<artifactId>mockito-core</artifactId>
214+
<version>5.11.0</version>
215+
<scope>test</scope>
216+
</dependency>
199217
<dependency>
200218
<groupId>com.vaadin</groupId>
201219
<artifactId>vaadin-testbench</artifactId>

src/main/java/com/flowingcode/vaadin/addons/gridexporter/GridExporter.java

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,11 @@ public static <T> GridExporter<T> createFor(
166166
.setHref(exporter.getExcelStreamResource(excelCustomTemplate)
167167
.forComponent(excelLink));
168168
excelLink.getElement().setAttribute("download", true);
169-
String finalExcelTooltip = null;
170-
if (exporter.excelIconTooltip != null) {
171-
finalExcelTooltip = exporter.excelIconTooltip;
172-
} else if (exporter.defaultExportIconTooltip != null) {
173-
finalExcelTooltip = exporter.defaultExportIconTooltip;
174-
}
175-
if (finalExcelTooltip != null && !finalExcelTooltip.isEmpty()) {
176-
excelLink.setTitle(finalExcelTooltip);
177-
} else {
178-
excelLink.setTitle(null);
179-
}
169+
excelLink
170+
.setHref(exporter.getExcelStreamResource(excelCustomTemplate)
171+
.forComponent(excelLink));
172+
excelLink.getElement().setAttribute("download", true);
173+
exporter.applyTooltip(excelLink, exporter.excelIconTooltip);
180174
footerToolbar.add(
181175
new FooterToolbarItem(excelLink, FooterToolbarItemPosition.EXPORT_BUTTON));
182176
}
@@ -185,54 +179,24 @@ public static <T> GridExporter<T> createFor(
185179
docLink.setHref(
186180
exporter.getDocxStreamResource(docxCustomTemplate).forComponent(docLink));
187181
docLink.getElement().setAttribute("download", true);
188-
String finalDocxTooltip = null;
189-
if (exporter.docxIconTooltip != null) {
190-
finalDocxTooltip = exporter.docxIconTooltip;
191-
} else if (exporter.defaultExportIconTooltip != null) {
192-
finalDocxTooltip = exporter.defaultExportIconTooltip;
193-
}
194-
if (finalDocxTooltip != null && !finalDocxTooltip.isEmpty()) {
195-
docLink.setTitle(finalDocxTooltip);
196-
} else {
197-
docLink.setTitle(null);
198-
}
182+
exporter.applyTooltip(docLink, exporter.docxIconTooltip);
199183
footerToolbar
200184
.add(new FooterToolbarItem(docLink, FooterToolbarItemPosition.EXPORT_BUTTON));
201185
}
202186
if (exporter.isPdfExportEnabled()) {
203-
Anchor pdfLink = new Anchor("", FontAwesome.Regular.FILE_PDF.create()); // Renamed to pdfLink
187+
Anchor pdfLink = new Anchor("", FontAwesome.Regular.FILE_PDF.create());
204188
pdfLink.setHref(
205-
exporter.getPdfStreamResource(docxCustomTemplate).forComponent(pdfLink));
189+
exporter.getPdfStreamResource(null).forComponent(pdfLink));
206190
pdfLink.getElement().setAttribute("download", true);
207-
String finalPdfTooltip = null;
208-
if (exporter.pdfIconTooltip != null) {
209-
finalPdfTooltip = exporter.pdfIconTooltip;
210-
} else if (exporter.defaultExportIconTooltip != null) {
211-
finalPdfTooltip = exporter.defaultExportIconTooltip;
212-
}
213-
if (finalPdfTooltip != null && !finalPdfTooltip.isEmpty()) {
214-
pdfLink.setTitle(finalPdfTooltip);
215-
} else {
216-
pdfLink.setTitle(null);
217-
}
191+
exporter.applyTooltip(pdfLink, exporter.pdfIconTooltip);
218192
footerToolbar
219193
.add(new FooterToolbarItem(pdfLink, FooterToolbarItemPosition.EXPORT_BUTTON));
220194
}
221195
if (exporter.isCsvExportEnabled()) {
222196
Anchor csvLink = new Anchor("", FontAwesome.Regular.FILE_LINES.create());
223197
csvLink.setHref(exporter.getCsvStreamResource());
224198
csvLink.getElement().setAttribute("download", true);
225-
String finalCsvTooltip = null;
226-
if (exporter.csvIconTooltip != null) {
227-
finalCsvTooltip = exporter.csvIconTooltip;
228-
} else if (exporter.defaultExportIconTooltip != null) {
229-
finalCsvTooltip = exporter.defaultExportIconTooltip;
230-
}
231-
if (finalCsvTooltip != null && !finalCsvTooltip.isEmpty()) {
232-
csvLink.setTitle(finalCsvTooltip);
233-
} else {
234-
csvLink.setTitle(null);
235-
}
199+
exporter.applyTooltip(csvLink, exporter.csvIconTooltip);
236200
footerToolbar
237201
.add(new FooterToolbarItem(csvLink, FooterToolbarItemPosition.EXPORT_BUTTON));
238202
}
@@ -862,6 +826,14 @@ public void setCsvCharset(SerializableSupplier<Charset> charset) {
862826
csvCharset = charset;
863827
}
864828

829+
private void applyTooltip(Anchor link, String specificTooltipText) {
830+
String finalTooltip = specificTooltipText;
831+
if (finalTooltip == null) {
832+
finalTooltip = this.defaultExportIconTooltip;
833+
}
834+
link.setTitle(finalTooltip);
835+
}
836+
865837
/**
866838
* Sets the default tooltip text for all export icons.
867839
* This tooltip will be used for any export icon that does not have a specific tooltip set

0 commit comments

Comments
 (0)