Skip to content

Method: getRenderableString(Object)

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.thesefoolishthings.examples.dci.swing.swing;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import java.awt.Component;
32: import javax.swing.DefaultListCellRenderer;
33: import javax.swing.JList;
34: import it.tidalwave.util.AsException;
35: import it.tidalwave.util.AsExtensions;
36: import it.tidalwave.role.HtmlRenderable;
37: import lombok.RequiredArgsConstructor;
38: import lombok.experimental.ExtensionMethod;
39: import static it.tidalwave.role.HtmlRenderable._HtmlRenderable_;
40:
41: /***********************************************************************************************************************
42: *
43: * A specialization of {@link javax.swing.DefaultListCellRenderer} that uses {@link HtmlRenderable} for rendering items.
44: *
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: @RequiredArgsConstructor
49: @ExtensionMethod(AsExtensions.class)
50: public class HtmlRenderableListCellRenderer extends DefaultListCellRenderer
51: {
52: @Override @Nonnull
53: public Component getListCellRendererComponent (@Nonnull final JList list,
54: @Nonnull final Object value,
55: @Nonnegative final int index,
56: final boolean isSelected,
57: final boolean cellHasFocus)
58: {
59: return super.getListCellRendererComponent(list, getRenderableString(value), index, isSelected, cellHasFocus);
60: }
61:
62: @Nonnull
63: private static String getRenderableString (@Nonnull final Object value)
64: {
65: try
66: {
67: final var builder = new StringBuilder("<html>");
68: value.as(_HtmlRenderable_).renderTo(builder);
69: return builder.append("</html>").toString();
70: }
71: catch (AsException e)
72: {
73: return value.toString();
74: }
75: }
76: }