|
| 1 | +/* |
| 2 | + * Copyright © 2025 XDEV Software (https://xdev.software) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package software.xdev.vaadin.chartjs; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +import com.vaadin.flow.component.Component; |
| 22 | +import com.vaadin.flow.component.dependency.CssImport; |
| 23 | +import com.vaadin.flow.component.dependency.JsModule; |
| 24 | +import com.vaadin.flow.component.html.Div; |
| 25 | +import com.vaadin.flow.component.icon.Icon; |
| 26 | +import com.vaadin.flow.component.icon.VaadinIcon; |
| 27 | +import com.vaadin.flow.component.shared.Tooltip; |
| 28 | +import com.vaadin.flow.theme.lumo.LumoUtility; |
| 29 | + |
| 30 | +import software.xdev.vaadin.chartjs.loading.DefaultLoadingErrorComponent; |
| 31 | +import software.xdev.vaadin.chartjs.loading.DefaultLoadingLoadComponent; |
| 32 | +import software.xdev.vaadin.chartjs.loading.LoadingErrorComponent; |
| 33 | +import software.xdev.vaadin.chartjs.resources.js.JSLibs; |
| 34 | +import software.xdev.vaadin.chartjs.resources.js.src.ChartFunc; |
| 35 | +import software.xdev.vaadin.chartjs.resources.js.src.ChartThemeManager; |
| 36 | + |
| 37 | + |
| 38 | +@JsModule(JSLibs.CHARTJS) |
| 39 | +@JsModule(ChartThemeManager.LOCATION) |
| 40 | +@JsModule(ChartFunc.LOCATION) |
| 41 | +@CssImport(ChartContainerStyles.LOCATION) |
| 42 | +public abstract class ChartContainer extends Div implements ChartCom |
| 43 | +{ |
| 44 | + protected Component loading = new DefaultLoadingLoadComponent(); |
| 45 | + protected Component error = new DefaultLoadingErrorComponent(); |
| 46 | + protected Div chartJSDiv = new Div(); |
| 47 | + |
| 48 | + protected Icon icoProblemsIndicator = VaadinIcon.WARNING.create(); |
| 49 | + |
| 50 | + protected ChartContainer() |
| 51 | + { |
| 52 | + this.loading.addClassName(ChartContainerStyles.LOADING_COMPONENT_CLASS); |
| 53 | + this.error.addClassName(ChartContainerStyles.ERROR_COMPONENT_CLASS); |
| 54 | + this.icoProblemsIndicator.addClassName(ChartContainerStyles.PROBLEM_INDICATOR_CLASS); |
| 55 | + |
| 56 | + this.chartJSDiv.addClassNames( |
| 57 | + ChartContainerStyles.DIV_CLASS, |
| 58 | + // Otherwise temporary scrollbars might be visible |
| 59 | + LumoUtility.Overflow.HIDDEN); |
| 60 | + this.chartJSDiv.setId(this.createChartJSDivId()); |
| 61 | + this.chartJSDiv.setHeightFull(); |
| 62 | + |
| 63 | + this.addClassName(ChartContainerStyles.ROOT_CLASS); |
| 64 | + this.add(this.chartJSDiv); |
| 65 | + this.setSizeFull(); |
| 66 | + } |
| 67 | + |
| 68 | + protected String createChartJSDivId() |
| 69 | + { |
| 70 | + return "chartjsdiv" + UUID.randomUUID(); |
| 71 | + } |
| 72 | + |
| 73 | + public String getChartJSDivId() |
| 74 | + { |
| 75 | + return this.chartJSDiv.getId().orElseThrow(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void showLoading() |
| 80 | + { |
| 81 | + this.clear(); |
| 82 | + this.add(this.loading); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void showFailed(final String errorMsg) |
| 87 | + { |
| 88 | + this.clear(); |
| 89 | + this.add(this.error); |
| 90 | + if(this.error instanceof final LoadingErrorComponent loadingErrorComp) |
| 91 | + { |
| 92 | + loadingErrorComp.setVisible(true, errorMsg); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void clear() |
| 98 | + { |
| 99 | + this.clear(false); |
| 100 | + } |
| 101 | + |
| 102 | + protected void clear(final boolean forDisplayingChart) |
| 103 | + { |
| 104 | + // Ignored on forDisplayingChart so that no "flickering" of the chart occurs |
| 105 | + if(!forDisplayingChart) |
| 106 | + { |
| 107 | + this.tryDestroyChart(); |
| 108 | + this.chartJSDiv.setVisible(false); |
| 109 | + this.chartJSDiv.removeAll(); |
| 110 | + } |
| 111 | + this.remove(this.getChildren() |
| 112 | + .filter(c -> !Objects.equals(c, this.chartJSDiv)) |
| 113 | + .toList()); |
| 114 | + } |
| 115 | + |
| 116 | + public void showProblemsIndicator(final String problemsText) |
| 117 | + { |
| 118 | + this.add(this.icoProblemsIndicator); |
| 119 | + Tooltip.forComponent(this.icoProblemsIndicator) |
| 120 | + .withText(problemsText); |
| 121 | + } |
| 122 | + |
| 123 | + protected void displayChart(final String payload) |
| 124 | + { |
| 125 | + Objects.requireNonNull(payload); |
| 126 | + |
| 127 | + this.clear(true); |
| 128 | + this.chartJSDiv.setVisible(true); |
| 129 | + |
| 130 | + final String thisid = this.getChartJSDivId(); |
| 131 | + this.executeJS(String.format(ChartFunc.BUILD_CHART, thisid, thisid + "Canvas", payload)); |
| 132 | + } |
| 133 | + |
| 134 | + protected void tryDestroyChart() |
| 135 | + { |
| 136 | + final String thisid = this.getChartJSDivId(); |
| 137 | + this.executeJS(String.format(ChartFunc.DESTROY_CHART, thisid + "Canvas")); |
| 138 | + } |
| 139 | + |
| 140 | + protected void executeJS(final String js) |
| 141 | + { |
| 142 | + this.getElement().executeJs(js); |
| 143 | + } |
| 144 | + |
| 145 | + // region Getter + Setter |
| 146 | + public Component getLoading() |
| 147 | + { |
| 148 | + return this.loading; |
| 149 | + } |
| 150 | + |
| 151 | + public void setLoading(final Component loading) |
| 152 | + { |
| 153 | + this.loading = loading; |
| 154 | + } |
| 155 | + |
| 156 | + public Component getError() |
| 157 | + { |
| 158 | + return this.error; |
| 159 | + } |
| 160 | + |
| 161 | + public void setError(final Component error) |
| 162 | + { |
| 163 | + this.error = error; |
| 164 | + } |
| 165 | + |
| 166 | + public Div getChartJSDiv() |
| 167 | + { |
| 168 | + return this.chartJSDiv; |
| 169 | + } |
| 170 | + |
| 171 | + public void setChartJSDiv(final Div chartJSDiv) |
| 172 | + { |
| 173 | + this.chartJSDiv = chartJSDiv; |
| 174 | + } |
| 175 | + |
| 176 | + public Icon getIcoProblemsIndicator() |
| 177 | + { |
| 178 | + return this.icoProblemsIndicator; |
| 179 | + } |
| 180 | + |
| 181 | + public void setIcoProblemsIndicator(final Icon icoProblemsIndicator) |
| 182 | + { |
| 183 | + this.icoProblemsIndicator = icoProblemsIndicator; |
| 184 | + } |
| 185 | + // endregion |
| 186 | +} |
0 commit comments